aboutsummaryrefslogtreecommitdiff
path: root/std
diff options
context:
space:
mode:
authorAndrew Kelley <superjoe30@gmail.com>2018-04-22 18:13:57 -0400
committerAndrew Kelley <superjoe30@gmail.com>2018-04-22 18:13:57 -0400
commit371a3ad4bd7b1fda56654f32b89c176a0197651f (patch)
tree9b402e02568e4f62a728a7cec1819363a89d6e9a /std
parent7af6ed3f20bbf0459ce6aed833c7e170ee6c927b (diff)
parent21767144fc1a8627a109e81a164c55171c279d82 (diff)
downloadzig-371a3ad4bd7b1fda56654f32b89c176a0197651f.tar.gz
zig-371a3ad4bd7b1fda56654f32b89c176a0197651f.zip
Merge branch 'tgschultz-std.os.time'
Diffstat (limited to 'std')
-rw-r--r--std/c/darwin.zig18
-rw-r--r--std/c/index.zig1
-rw-r--r--std/elf.zig611
-rw-r--r--std/fmt/index.zig3
-rw-r--r--std/os/darwin.zig12
-rw-r--r--std/os/epoch.zig26
-rw-r--r--std/os/index.zig49
-rw-r--r--std/os/linux/index.zig44
-rw-r--r--std/os/linux/test.zig1
-rw-r--r--std/os/linux/vdso.zig89
-rw-r--r--std/os/linux/x86_64.zig18
-rw-r--r--std/os/time.zig288
-rw-r--r--std/os/windows/index.zig11
-rw-r--r--std/special/bootstrap.zig27
14 files changed, 1140 insertions, 58 deletions
diff --git a/std/c/darwin.zig b/std/c/darwin.zig
index feb689cdc5..b958055ae8 100644
--- a/std/c/darwin.zig
+++ b/std/c/darwin.zig
@@ -3,10 +3,28 @@ pub extern "c" fn _NSGetExecutablePath(buf: &u8, bufsize: &u32) c_int;
pub extern "c" fn __getdirentries64(fd: c_int, buf_ptr: &u8, buf_len: usize, basep: &i64) usize;
+pub extern "c" fn mach_absolute_time() u64;
+pub extern "c" fn mach_timebase_info(tinfo: ?&mach_timebase_info_data) void;
+
pub use @import("../os/darwin_errno.zig");
pub const _errno = __error;
+pub const timeval = extern struct {
+ tv_sec: isize,
+ tv_usec: isize,
+};
+
+pub const timezone = extern struct {
+ tz_minuteswest: i32,
+ tz_dsttime: i32,
+};
+
+pub const mach_timebase_info_data = struct {
+ numer: u32,
+ denom: u32,
+};
+
/// Renamed to Stat to not conflict with the stat function.
pub const Stat = extern struct {
dev: i32,
diff --git a/std/c/index.zig b/std/c/index.zig
index 02321f1f34..cff86f4041 100644
--- a/std/c/index.zig
+++ b/std/c/index.zig
@@ -41,6 +41,7 @@ pub extern "c" fn dup2(old_fd: c_int, new_fd: c_int) c_int;
pub extern "c" fn readlink(noalias path: &const u8, noalias buf: &u8, bufsize: usize) isize;
pub extern "c" fn realpath(noalias file_name: &const u8, noalias resolved_name: &u8) ?&u8;
pub extern "c" fn sigprocmask(how: c_int, noalias set: &const sigset_t, noalias oset: ?&sigset_t) c_int;
+pub extern "c" fn gettimeofday(tv: ?&timeval, tz: ?&timezone) c_int;
pub extern "c" fn sigaction(sig: c_int, noalias act: &const Sigaction, noalias oact: ?&Sigaction) c_int;
pub extern "c" fn nanosleep(rqtp: &const timespec, rmtp: ?&timespec) c_int;
pub extern "c" fn setreuid(ruid: c_uint, euid: c_uint) c_int;
diff --git a/std/elf.zig b/std/elf.zig
index 7e20fa000f..1764829bc8 100644
--- a/std/elf.zig
+++ b/std/elf.zig
@@ -7,6 +7,246 @@ const mem = std.mem;
const debug = std.debug;
const InStream = std.stream.InStream;
+pub const AT_NULL = 0;
+pub const AT_IGNORE = 1;
+pub const AT_EXECFD = 2;
+pub const AT_PHDR = 3;
+pub const AT_PHENT = 4;
+pub const AT_PHNUM = 5;
+pub const AT_PAGESZ = 6;
+pub const AT_BASE = 7;
+pub const AT_FLAGS = 8;
+pub const AT_ENTRY = 9;
+pub const AT_NOTELF = 10;
+pub const AT_UID = 11;
+pub const AT_EUID = 12;
+pub const AT_GID = 13;
+pub const AT_EGID = 14;
+pub const AT_CLKTCK = 17;
+pub const AT_PLATFORM = 15;
+pub const AT_HWCAP = 16;
+pub const AT_FPUCW = 18;
+pub const AT_DCACHEBSIZE = 19;
+pub const AT_ICACHEBSIZE = 20;
+pub const AT_UCACHEBSIZE = 21;
+pub const AT_IGNOREPPC = 22;
+pub const AT_SECURE = 23;
+pub const AT_BASE_PLATFORM = 24;
+pub const AT_RANDOM = 25;
+pub const AT_HWCAP2 = 26;
+pub const AT_EXECFN = 31;
+pub const AT_SYSINFO = 32;
+pub const AT_SYSINFO_EHDR = 33;
+pub const AT_L1I_CACHESHAPE = 34;
+pub const AT_L1D_CACHESHAPE = 35;
+pub const AT_L2_CACHESHAPE = 36;
+pub const AT_L3_CACHESHAPE = 37;
+pub const AT_L1I_CACHESIZE = 40;
+pub const AT_L1I_CACHEGEOMETRY = 41;
+pub const AT_L1D_CACHESIZE = 42;
+pub const AT_L1D_CACHEGEOMETRY = 43;
+pub const AT_L2_CACHESIZE = 44;
+pub const AT_L2_CACHEGEOMETRY = 45;
+pub const AT_L3_CACHESIZE = 46;
+pub const AT_L3_CACHEGEOMETRY = 47;
+
+pub const DT_NULL = 0;
+pub const DT_NEEDED = 1;
+pub const DT_PLTRELSZ = 2;
+pub const DT_PLTGOT = 3;
+pub const DT_HASH = 4;
+pub const DT_STRTAB = 5;
+pub const DT_SYMTAB = 6;
+pub const DT_RELA = 7;
+pub const DT_RELASZ = 8;
+pub const DT_RELAENT = 9;
+pub const DT_STRSZ = 10;
+pub const DT_SYMENT = 11;
+pub const DT_INIT = 12;
+pub const DT_FINI = 13;
+pub const DT_SONAME = 14;
+pub const DT_RPATH = 15;
+pub const DT_SYMBOLIC = 16;
+pub const DT_REL = 17;
+pub const DT_RELSZ = 18;
+pub const DT_RELENT = 19;
+pub const DT_PLTREL = 20;
+pub const DT_DEBUG = 21;
+pub const DT_TEXTREL = 22;
+pub const DT_JMPREL = 23;
+pub const DT_BIND_NOW = 24;
+pub const DT_INIT_ARRAY = 25;
+pub const DT_FINI_ARRAY = 26;
+pub const DT_INIT_ARRAYSZ = 27;
+pub const DT_FINI_ARRAYSZ = 28;
+pub const DT_RUNPATH = 29;
+pub const DT_FLAGS = 30;
+pub const DT_ENCODING = 32;
+pub const DT_PREINIT_ARRAY = 32;
+pub const DT_PREINIT_ARRAYSZ = 33;
+pub const DT_SYMTAB_SHNDX = 34;
+pub const DT_NUM = 35;
+pub const DT_LOOS = 0x6000000d;
+pub const DT_HIOS = 0x6ffff000;
+pub const DT_LOPROC = 0x70000000;
+pub const DT_HIPROC = 0x7fffffff;
+pub const DT_PROCNUM = DT_MIPS_NUM;
+
+pub const DT_VALRNGLO = 0x6ffffd00;
+pub const DT_GNU_PRELINKED = 0x6ffffdf5;
+pub const DT_GNU_CONFLICTSZ = 0x6ffffdf6;
+pub const DT_GNU_LIBLISTSZ = 0x6ffffdf7;
+pub const DT_CHECKSUM = 0x6ffffdf8;
+pub const DT_PLTPADSZ = 0x6ffffdf9;
+pub const DT_MOVEENT = 0x6ffffdfa;
+pub const DT_MOVESZ = 0x6ffffdfb;
+pub const DT_FEATURE_1 = 0x6ffffdfc;
+pub const DT_POSFLAG_1 = 0x6ffffdfd;
+
+pub const DT_SYMINSZ = 0x6ffffdfe;
+pub const DT_SYMINENT = 0x6ffffdff;
+pub const DT_VALRNGHI = 0x6ffffdff;
+pub const DT_VALNUM = 12;
+
+pub const DT_ADDRRNGLO = 0x6ffffe00;
+pub const DT_GNU_HASH = 0x6ffffef5;
+pub const DT_TLSDESC_PLT = 0x6ffffef6;
+pub const DT_TLSDESC_GOT = 0x6ffffef7;
+pub const DT_GNU_CONFLICT = 0x6ffffef8;
+pub const DT_GNU_LIBLIST = 0x6ffffef9;
+pub const DT_CONFIG = 0x6ffffefa;
+pub const DT_DEPAUDIT = 0x6ffffefb;
+pub const DT_AUDIT = 0x6ffffefc;
+pub const DT_PLTPAD = 0x6ffffefd;
+pub const DT_MOVETAB = 0x6ffffefe;
+pub const DT_SYMINFO = 0x6ffffeff;
+pub const DT_ADDRRNGHI = 0x6ffffeff;
+pub const DT_ADDRNUM = 11;
+
+
+pub const DT_VERSYM = 0x6ffffff0;
+
+pub const DT_RELACOUNT = 0x6ffffff9;
+pub const DT_RELCOUNT = 0x6ffffffa;
+
+
+pub const DT_FLAGS_1 = 0x6ffffffb;
+pub const DT_VERDEF = 0x6ffffffc;
+
+pub const DT_VERDEFNUM = 0x6ffffffd;
+pub const DT_VERNEED = 0x6ffffffe;
+
+pub const DT_VERNEEDNUM = 0x6fffffff;
+pub const DT_VERSIONTAGNUM = 16;
+
+
+
+pub const DT_AUXILIARY = 0x7ffffffd;
+pub const DT_FILTER = 0x7fffffff;
+pub const DT_EXTRANUM = 3;
+
+
+pub const DT_SPARC_REGISTER = 0x70000001;
+pub const DT_SPARC_NUM = 2;
+
+pub const DT_MIPS_RLD_VERSION = 0x70000001;
+pub const DT_MIPS_TIME_STAMP = 0x70000002;
+pub const DT_MIPS_ICHECKSUM = 0x70000003;
+pub const DT_MIPS_IVERSION = 0x70000004;
+pub const DT_MIPS_FLAGS = 0x70000005;
+pub const DT_MIPS_BASE_ADDRESS = 0x70000006;
+pub const DT_MIPS_MSYM = 0x70000007;
+pub const DT_MIPS_CONFLICT = 0x70000008;
+pub const DT_MIPS_LIBLIST = 0x70000009;
+pub const DT_MIPS_LOCAL_GOTNO = 0x7000000a;
+pub const DT_MIPS_CONFLICTNO = 0x7000000b;
+pub const DT_MIPS_LIBLISTNO = 0x70000010;
+pub const DT_MIPS_SYMTABNO = 0x70000011;
+pub const DT_MIPS_UNREFEXTNO = 0x70000012;
+pub const DT_MIPS_GOTSYM = 0x70000013;
+pub const DT_MIPS_HIPAGENO = 0x70000014;
+pub const DT_MIPS_RLD_MAP = 0x70000016;
+pub const DT_MIPS_DELTA_CLASS = 0x70000017;
+pub const DT_MIPS_DELTA_CLASS_NO = 0x70000018;
+
+pub const DT_MIPS_DELTA_INSTANCE = 0x70000019;
+pub const DT_MIPS_DELTA_INSTANCE_NO = 0x7000001a;
+
+pub const DT_MIPS_DELTA_RELOC = 0x7000001b;
+pub const DT_MIPS_DELTA_RELOC_NO = 0x7000001c;
+
+pub const DT_MIPS_DELTA_SYM = 0x7000001d;
+
+pub const DT_MIPS_DELTA_SYM_NO = 0x7000001e;
+
+pub const DT_MIPS_DELTA_CLASSSYM = 0x70000020;
+
+pub const DT_MIPS_DELTA_CLASSSYM_NO = 0x70000021;
+
+pub const DT_MIPS_CXX_FLAGS = 0x70000022;
+pub const DT_MIPS_PIXIE_INIT = 0x70000023;
+pub const DT_MIPS_SYMBOL_LIB = 0x70000024;
+pub const DT_MIPS_LOCALPAGE_GOTIDX = 0x70000025;
+pub const DT_MIPS_LOCAL_GOTIDX = 0x70000026;
+pub const DT_MIPS_HIDDEN_GOTIDX = 0x70000027;
+pub const DT_MIPS_PROTECTED_GOTIDX = 0x70000028;
+pub const DT_MIPS_OPTIONS = 0x70000029;
+pub const DT_MIPS_INTERFACE = 0x7000002a;
+pub const DT_MIPS_DYNSTR_ALIGN = 0x7000002b;
+pub const DT_MIPS_INTERFACE_SIZE = 0x7000002c;
+pub const DT_MIPS_RLD_TEXT_RESOLVE_ADDR = 0x7000002d;
+
+pub const DT_MIPS_PERF_SUFFIX = 0x7000002e;
+
+pub const DT_MIPS_COMPACT_SIZE = 0x7000002f;
+pub const DT_MIPS_GP_VALUE = 0x70000030;
+pub const DT_MIPS_AUX_DYNAMIC = 0x70000031;
+
+pub const DT_MIPS_PLTGOT = 0x70000032;
+
+pub const DT_MIPS_RWPLT = 0x70000034;
+pub const DT_MIPS_RLD_MAP_REL = 0x70000035;
+pub const DT_MIPS_NUM = 0x36;
+
+pub const DT_ALPHA_PLTRO = (DT_LOPROC + 0);
+pub const DT_ALPHA_NUM = 1;
+
+pub const DT_PPC_GOT = (DT_LOPROC + 0);
+pub const DT_PPC_OPT = (DT_LOPROC + 1);
+pub const DT_PPC_NUM = 2;
+
+pub const DT_PPC64_GLINK = (DT_LOPROC + 0);
+pub const DT_PPC64_OPD = (DT_LOPROC + 1);
+pub const DT_PPC64_OPDSZ = (DT_LOPROC + 2);
+pub const DT_PPC64_OPT = (DT_LOPROC + 3);
+pub const DT_PPC64_NUM = 4;
+
+pub const DT_IA_64_PLT_RESERVE = (DT_LOPROC + 0);
+pub const DT_IA_64_NUM = 1;
+
+pub const DT_NIOS2_GP = 0x70000002;
+
+pub const PT_NULL = 0;
+pub const PT_LOAD = 1;
+pub const PT_DYNAMIC = 2;
+pub const PT_INTERP = 3;
+pub const PT_NOTE = 4;
+pub const PT_SHLIB = 5;
+pub const PT_PHDR = 6;
+pub const PT_TLS = 7;
+pub const PT_NUM = 8;
+pub const PT_LOOS = 0x60000000;
+pub const PT_GNU_EH_FRAME = 0x6474e550;
+pub const PT_GNU_STACK = 0x6474e551;
+pub const PT_GNU_RELRO = 0x6474e552;
+pub const PT_LOSUNW = 0x6ffffffa;
+pub const PT_SUNWBSS = 0x6ffffffa;
+pub const PT_SUNWSTACK = 0x6ffffffb;
+pub const PT_HISUNW = 0x6fffffff;
+pub const PT_HIOS = 0x6fffffff;
+pub const PT_LOPROC = 0x70000000;
+pub const PT_HIPROC = 0x7fffffff;
+
pub const SHT_NULL = 0;
pub const SHT_PROGBITS = 1;
pub const SHT_SYMTAB = 2;
@@ -31,6 +271,45 @@ pub const SHT_HIPROC = 0x7fffffff;
pub const SHT_LOUSER = 0x80000000;
pub const SHT_HIUSER = 0xffffffff;
+pub const STB_LOCAL = 0;
+pub const STB_GLOBAL = 1;
+pub const STB_WEAK = 2;
+pub const STB_NUM = 3;
+pub const STB_LOOS = 10;
+pub const STB_GNU_UNIQUE = 10;
+pub const STB_HIOS = 12;
+pub const STB_LOPROC = 13;
+pub const STB_HIPROC = 15;
+
+pub const STB_MIPS_SPLIT_COMMON = 13;
+
+pub const STT_NOTYPE = 0;
+pub const STT_OBJECT = 1;
+pub const STT_FUNC = 2;
+pub const STT_SECTION = 3;
+pub const STT_FILE = 4;
+pub const STT_COMMON = 5;
+pub const STT_TLS = 6;
+pub const STT_NUM = 7;
+pub const STT_LOOS = 10;
+pub const STT_GNU_IFUNC = 10;
+pub const STT_HIOS = 12;
+pub const STT_LOPROC = 13;
+pub const STT_HIPROC = 15;
+
+pub const STT_SPARC_REGISTER = 13;
+
+pub const STT_PARISC_MILLICODE = 13;
+
+pub const STT_HP_OPAQUE = (STT_LOOS + 0x1);
+pub const STT_HP_STUB = (STT_LOOS + 0x2);
+
+pub const STT_ARM_TFUNC = STT_LOPROC;
+pub const STT_ARM_16BIT = STT_HIPROC;
+
+pub const VER_FLG_BASE = 0x1;
+pub const VER_FLG_WEAK = 0x2;
+
pub const FileType = enum {
Relocatable,
Executable,
@@ -266,3 +545,335 @@ pub const Elf = struct {
try elf.in_file.seekTo(elf_section.offset);
}
};
+
+pub const EI_NIDENT = 16;
+pub const Elf32_Half = u16;
+pub const Elf64_Half = u16;
+pub const Elf32_Word = u32;
+pub const Elf32_Sword = i32;
+pub const Elf64_Word = u32;
+pub const Elf64_Sword = i32;
+pub const Elf32_Xword = u64;
+pub const Elf32_Sxword = i64;
+pub const Elf64_Xword = u64;
+pub const Elf64_Sxword = i64;
+pub const Elf32_Addr = u32;
+pub const Elf64_Addr = u64;
+pub const Elf32_Off = u32;
+pub const Elf64_Off = u64;
+pub const Elf32_Section = u16;
+pub const Elf64_Section = u16;
+pub const Elf32_Versym = Elf32_Half;
+pub const Elf64_Versym = Elf64_Half;
+pub const Elf32_Ehdr = extern struct {
+ e_ident: [EI_NIDENT]u8,
+ e_type: Elf32_Half,
+ e_machine: Elf32_Half,
+ e_version: Elf32_Word,
+ e_entry: Elf32_Addr,
+ e_phoff: Elf32_Off,
+ e_shoff: Elf32_Off,
+ e_flags: Elf32_Word,
+ e_ehsize: Elf32_Half,
+ e_phentsize: Elf32_Half,
+ e_phnum: Elf32_Half,
+ e_shentsize: Elf32_Half,
+ e_shnum: Elf32_Half,
+ e_shstrndx: Elf32_Half,
+};
+pub const Elf64_Ehdr = extern struct {
+ e_ident: [EI_NIDENT]u8,
+ e_type: Elf64_Half,
+ e_machine: Elf64_Half,
+ e_version: Elf64_Word,
+ e_entry: Elf64_Addr,
+ e_phoff: Elf64_Off,
+ e_shoff: Elf64_Off,
+ e_flags: Elf64_Word,
+ e_ehsize: Elf64_Half,
+ e_phentsize: Elf64_Half,
+ e_phnum: Elf64_Half,
+ e_shentsize: Elf64_Half,
+ e_shnum: Elf64_Half,
+ e_shstrndx: Elf64_Half,
+};
+pub const Elf32_Shdr = extern struct {
+ sh_name: Elf32_Word,
+ sh_type: Elf32_Word,
+ sh_flags: Elf32_Word,
+ sh_addr: Elf32_Addr,
+ sh_offset: Elf32_Off,
+ sh_size: Elf32_Word,
+ sh_link: Elf32_Word,
+ sh_info: Elf32_Word,
+ sh_addralign: Elf32_Word,
+ sh_entsize: Elf32_Word,
+};
+pub const Elf64_Shdr = extern struct {
+ sh_name: Elf64_Word,
+ sh_type: Elf64_Word,
+ sh_flags: Elf64_Xword,
+ sh_addr: Elf64_Addr,
+ sh_offset: Elf64_Off,
+ sh_size: Elf64_Xword,
+ sh_link: Elf64_Word,
+ sh_info: Elf64_Word,
+ sh_addralign: Elf64_Xword,
+ sh_entsize: Elf64_Xword,
+};
+pub const Elf32_Chdr = extern struct {
+ ch_type: Elf32_Word,
+ ch_size: Elf32_Word,
+ ch_addralign: Elf32_Word,
+};
+pub const Elf64_Chdr = extern struct {
+ ch_type: Elf64_Word,
+ ch_reserved: Elf64_Word,
+ ch_size: Elf64_Xword,
+ ch_addralign: Elf64_Xword,
+};
+pub const Elf32_Sym = extern struct {
+ st_name: Elf32_Word,
+ st_value: Elf32_Addr,
+ st_size: Elf32_Word,
+ st_info: u8,
+ st_other: u8,
+ st_shndx: Elf32_Section,
+};
+pub const Elf64_Sym = extern struct {
+ st_name: Elf64_Word,
+ st_info: u8,
+ st_other: u8,
+ st_shndx: Elf64_Section,
+ st_value: Elf64_Addr,
+ st_size: Elf64_Xword,
+};
+pub const Elf32_Syminfo = extern struct {
+ si_boundto: Elf32_Half,
+ si_flags: Elf32_Half,
+};
+pub const Elf64_Syminfo = extern struct {
+ si_boundto: Elf64_Half,
+ si_flags: Elf64_Half,
+};
+pub const Elf32_Rel = extern struct {
+ r_offset: Elf32_Addr,
+ r_info: Elf32_Word,
+};
+pub const Elf64_Rel = extern struct {
+ r_offset: Elf64_Addr,
+ r_info: Elf64_Xword,
+};
+pub const Elf32_Rela = extern struct {
+ r_offset: Elf32_Addr,
+ r_info: Elf32_Word,
+ r_addend: Elf32_Sword,
+};
+pub const Elf64_Rela = extern struct {
+ r_offset: Elf64_Addr,
+ r_info: Elf64_Xword,
+ r_addend: Elf64_Sxword,
+};
+pub const Elf32_Phdr = extern struct {
+ p_type: Elf32_Word,
+ p_offset: Elf32_Off,
+ p_vaddr: Elf32_Addr,
+ p_paddr: Elf32_Addr,
+ p_filesz: Elf32_Word,
+ p_memsz: Elf32_Word,
+ p_flags: Elf32_Word,
+ p_align: Elf32_Word,
+};
+pub const Elf64_Phdr = extern struct {
+ p_type: Elf64_Word,
+ p_flags: Elf64_Word,
+ p_offset: Elf64_Off,
+ p_vaddr: Elf64_Addr,
+ p_paddr: Elf64_Addr,
+ p_filesz: Elf64_Xword,
+ p_memsz: Elf64_Xword,
+ p_align: Elf64_Xword,
+};
+pub const Elf32_Dyn = extern struct {
+ d_tag: Elf32_Sword,
+ d_un: extern union {
+ d_val: Elf32_Word,
+ d_ptr: Elf32_Addr,
+ },
+};
+pub const Elf64_Dyn = extern struct {
+ d_tag: Elf64_Sxword,
+ d_un: extern union {
+ d_val: Elf64_Xword,
+ d_ptr: Elf64_Addr,
+ },
+};
+pub const Elf32_Verdef = extern struct {
+ vd_version: Elf32_Half,
+ vd_flags: Elf32_Half,
+ vd_ndx: Elf32_Half,
+ vd_cnt: Elf32_Half,
+ vd_hash: Elf32_Word,
+ vd_aux: Elf32_Word,
+ vd_next: Elf32_Word,
+};
+pub const Elf64_Verdef = extern struct {
+ vd_version: Elf64_Half,
+ vd_flags: Elf64_Half,
+ vd_ndx: Elf64_Half,
+ vd_cnt: Elf64_Half,
+ vd_hash: Elf64_Word,
+ vd_aux: Elf64_Word,
+ vd_next: Elf64_Word,
+};
+pub const Elf32_Verdaux = extern struct {
+ vda_name: Elf32_Word,
+ vda_next: Elf32_Word,
+};
+pub const Elf64_Verdaux = extern struct {
+ vda_name: Elf64_Word,
+ vda_next: Elf64_Word,
+};
+pub const Elf32_Verneed = extern struct {
+ vn_version: Elf32_Half,
+ vn_cnt: Elf32_Half,
+ vn_file: Elf32_Word,
+ vn_aux: Elf32_Word,
+ vn_next: Elf32_Word,
+};
+pub const Elf64_Verneed = extern struct {
+ vn_version: Elf64_Half,
+ vn_cnt: Elf64_Half,
+ vn_file: Elf64_Word,
+ vn_aux: Elf64_Word,
+ vn_next: Elf64_Word,
+};
+pub const Elf32_Vernaux = extern struct {
+ vna_hash: Elf32_Word,
+ vna_flags: Elf32_Half,
+ vna_other: Elf32_Half,
+ vna_name: Elf32_Word,
+ vna_next: Elf32_Word,
+};
+pub const Elf64_Vernaux = extern struct {
+ vna_hash: Elf64_Word,
+ vna_flags: Elf64_Half,
+ vna_other: Elf64_Half,
+ vna_name: Elf64_Word,
+ vna_next: Elf64_Word,
+};
+pub const Elf32_auxv_t = extern struct {
+ a_type: u32,
+ a_un: extern union {
+ a_val: u32,
+ },
+};
+pub const Elf64_auxv_t = extern struct {
+ a_type: u64,
+ a_un: extern union {
+ a_val: u64,
+ },
+};
+pub const Elf32_Nhdr = extern struct {
+ n_namesz: Elf32_Word,
+ n_descsz: Elf32_Word,
+ n_type: Elf32_Word,
+};
+pub const Elf64_Nhdr = extern struct {
+ n_namesz: Elf64_Word,
+ n_descsz: Elf64_Word,
+ n_type: Elf64_Word,
+};
+pub const Elf32_Move = extern struct {
+ m_value: Elf32_Xword,
+ m_info: Elf32_Word,
+ m_poffset: Elf32_Word,
+ m_repeat: Elf32_Half,
+ m_stride: Elf32_Half,
+};
+pub const Elf64_Move = extern struct {
+ m_value: Elf64_Xword,
+ m_info: Elf64_Xword,
+ m_poffset: Elf64_Xword,
+ m_repeat: Elf64_Half,
+ m_stride: Elf64_Half,
+};
+pub const Elf32_gptab = extern union {
+ gt_header: extern struct {
+ gt_current_g_value: Elf32_Word,
+ gt_unused: Elf32_Word,
+ },
+ gt_entry: extern struct {
+ gt_g_value: Elf32_Word,
+ gt_bytes: Elf32_Word,
+ },
+};
+pub const Elf32_RegInfo = extern struct {
+ ri_gprmask: Elf32_Word,
+ ri_cprmask: [4]Elf32_Word,
+ ri_gp_value: Elf32_Sword,
+};
+pub const Elf_Options = extern struct {
+ kind: u8,
+ size: u8,
+ @"section": Elf32_Section,
+ info: Elf32_Word,
+};
+pub const Elf_Options_Hw = extern struct {
+ hwp_flags1: Elf32_Word,
+ hwp_flags2: Elf32_Word,
+};
+pub const Elf32_Lib = extern struct {
+ l_name: Elf32_Word,
+ l_time_stamp: Elf32_Word,
+ l_checksum: Elf32_Word,
+ l_version: Elf32_Word,
+ l_flags: Elf32_Word,
+};
+pub const Elf64_Lib = extern struct {
+ l_name: Elf64_Word,
+ l_time_stamp: Elf64_Word,
+ l_checksum: Elf64_Word,
+ l_version: Elf64_Word,
+ l_flags: Elf64_Word,
+};
+pub const Elf32_Conflict = Elf32_Addr;
+pub const Elf_MIPS_ABIFlags_v0 = extern struct {
+ version: Elf32_Half,
+ isa_level: u8,
+ isa_rev: u8,
+ gpr_size: u8,
+ cpr1_size: u8,
+ cpr2_size: u8,
+ fp_abi: u8,
+ isa_ext: Elf32_Word,
+ ases: Elf32_Word,
+ flags1: Elf32_Word,
+ flags2: Elf32_Word,
+};
+
+pub const Ehdr = switch(@sizeOf(usize)) {
+ 4 => Elf32_Ehdr,
+ 8 => Elf64_Ehdr,
+ else => @compileError("expected pointer size of 32 or 64"),
+};
+pub const Phdr = switch(@sizeOf(usize)) {
+ 4 => Elf32_Phdr,
+ 8 => Elf64_Phdr,
+ else => @compileError("expected pointer size of 32 or 64"),
+};
+pub const Sym = switch(@sizeOf(usize)) {
+ 4 => Elf32_Sym,
+ 8 => Elf64_Sym,
+ else => @compileError("expected pointer size of 32 or 64"),
+};
+pub const Verdef = switch(@sizeOf(usize)) {
+ 4 => Elf32_Verdef,
+ 8 => Elf64_Verdef,
+ else => @compileError("expected pointer size of 32 or 64"),
+};
+pub const Verdaux = switch(@sizeOf(usize)) {
+ 4 => Elf32_Verdaux,
+ 8 => Elf64_Verdaux,
+ else => @compileError("expected pointer size of 32 or 64"),
+};
diff --git a/std/fmt/index.zig b/std/fmt/index.zig
index 4be5c6f827..7bb9829117 100644
--- a/std/fmt/index.zig
+++ b/std/fmt/index.zig
@@ -86,7 +86,8 @@ pub fn format(context: var, comptime Errors: type, output: fn(@typeOf(context),
},
's' => {
state = State.Buf;
- },'.' => {
+ },
+ '.' => {
state = State.Float;
},
else => @compileError("Unknown format character: " ++ []u8{c}),
diff --git a/std/os/darwin.zig b/std/os/darwin.zig
index 42b9917210..44418649ab 100644
--- a/std/os/darwin.zig
+++ b/std/os/darwin.zig
@@ -260,6 +260,10 @@ pub fn readlink(noalias path: &const u8, noalias buf_ptr: &u8, buf_len: usize) u
return errnoWrap(c.readlink(path, buf_ptr, buf_len));
}
+pub fn gettimeofday(tv: ?&timeval, tz: ?&timezone) usize {
+ return errnoWrap(c.gettimeofday(tv, tz));
+}
+
pub fn nanosleep(req: &const timespec, rem: ?&timespec) usize {
return errnoWrap(c.nanosleep(req, rem));
}
@@ -330,3 +334,11 @@ pub fn sigaddset(set: &sigset_t, signo: u5) void {
fn errnoWrap(value: isize) usize {
return @bitCast(usize, if (value == -1) -isize(*c._errno()) else value);
}
+
+
+pub const timezone = c.timezone;
+pub const timeval = c.timeval;
+pub const mach_timebase_info_data = c.mach_timebase_info_data;
+
+pub const mach_absolute_time = c.mach_absolute_time;
+pub const mach_timebase_info = c.mach_timebase_info; \ No newline at end of file
diff --git a/std/os/epoch.zig b/std/os/epoch.zig
new file mode 100644
index 0000000000..e1256c1374
--- /dev/null
+++ b/std/os/epoch.zig
@@ -0,0 +1,26 @@
+/// Epoch reference times in terms of their difference from
+/// posix epoch in seconds.
+pub const posix = 0; //Jan 01, 1970 AD
+pub const dos = 315532800; //Jan 01, 1980 AD
+pub const ios = 978307200; //Jan 01, 2001 AD
+pub const openvms = -3506716800; //Nov 17, 1858 AD
+pub const zos = -2208988800; //Jan 01, 1900 AD
+pub const windows = -11644473600; //Jan 01, 1601 AD
+pub const amiga = 252460800; //Jan 01, 1978 AD
+pub const pickos = -63244800; //Dec 31, 1967 AD
+pub const gps = 315964800; //Jan 06, 1980 AD
+pub const clr = -62135769600; //Jan 01, 0001 AD
+
+pub const unix = posix;
+pub const android = posix;
+pub const os2 = dos;
+pub const bios = dos;
+pub const vfat = dos;
+pub const ntfs = windows;
+pub const ntp = zos;
+pub const jbase = pickos;
+pub const aros = amiga;
+pub const morphos = amiga;
+pub const brew = gps;
+pub const atsc = gps;
+pub const go = clr; \ No newline at end of file
diff --git a/std/os/index.zig b/std/os/index.zig
index dbdb8c90cd..0639490725 100644
--- a/std/os/index.zig
+++ b/std/os/index.zig
@@ -9,11 +9,10 @@ test "std.os" {
_ = @import("darwin.zig");
_ = @import("darwin_errno.zig");
_ = @import("get_user_id.zig");
- _ = @import("linux/errno.zig");
_ = @import("linux/index.zig");
- _ = @import("linux/x86_64.zig");
_ = @import("path.zig");
_ = @import("test.zig");
+ _ = @import("time.zig");
_ = @import("windows/index.zig");
}
@@ -32,6 +31,7 @@ pub const net = @import("net.zig");
pub const ChildProcess = @import("child_process.zig").ChildProcess;
pub const path = @import("path.zig");
pub const File = @import("file.zig").File;
+pub const time = @import("time.zig");
pub const FileMode = switch (builtin.os) {
Os.windows => void,
@@ -478,6 +478,7 @@ fn posixExecveErrnoToErr(err: usize) PosixExecveError {
};
}
+pub var linux_aux_raw = []usize{0} ** 38;
pub var posix_environ_raw: []&u8 = undefined;
/// Caller must free result when done.
@@ -1379,50 +1380,6 @@ pub fn readLink(allocator: &Allocator, pathname: []const u8) ![]u8 {
}
}
-pub fn sleep(seconds: usize, nanoseconds: usize) void {
- switch(builtin.os) {
- Os.linux, Os.macosx, Os.ios => {
- posixSleep(u63(seconds), u63(nanoseconds));
- },
- Os.windows => {
- const milliseconds = seconds * 1000 + nanoseconds / 1000000;
- windows.Sleep(windows.DWORD(milliseconds));
- },
- else => @compileError("Unsupported OS"),
- }
-}
-
-const u63 = @IntType(false, 63);
-pub fn posixSleep(seconds: u63, nanoseconds: u63) void {
- var req = posix.timespec {
- .tv_sec = seconds,
- .tv_nsec = nanoseconds,
- };
- var rem: posix.timespec = undefined;
- while (true) {
- const ret_val = posix.nanosleep(&req, &rem);
- const err = posix.getErrno(ret_val);
- if (err == 0) return;
- switch (err) {
- posix.EFAULT => unreachable,
- posix.EINVAL => {
- // Sometimes Darwin returns EINVAL for no reason.
- // We treat it as a spurious wakeup.
- return;
- },
- posix.EINTR => {
- req = rem;
- continue;
- },
- else => return,
- }
- }
-}
-
-test "os.sleep" {
- sleep(0, 1);
-}
-
pub fn posix_setuid(uid: u32) !void {
const err = posix.getErrno(posix.setuid(uid));
if (err == 0) return;
diff --git a/std/os/linux/index.zig b/std/os/linux/index.zig
index 6eb2d74bb7..dcd9532d1d 100644
--- a/std/os/linux/index.zig
+++ b/std/os/linux/index.zig
@@ -1,6 +1,7 @@
const std = @import("../../index.zig");
const assert = std.debug.assert;
const builtin = @import("builtin");
+const vdso = @import("vdso.zig");
pub use switch (builtin.arch) {
builtin.Arch.x86_64 => @import("x86_64.zig"),
builtin.Arch.i386 => @import("i386.zig"),
@@ -805,6 +806,45 @@ pub fn waitpid(pid: i32, status: &i32, options: i32) usize {
return syscall4(SYS_wait4, @bitCast(usize, isize(pid)), @ptrToInt(status), @bitCast(usize, isize(options)), 0);
}
+pub fn clock_gettime(clk_id: i32, tp: &timespec) usize {
+ if (VDSO_CGT_SYM.len != 0) {
+ const f = @atomicLoad(@typeOf(init_vdso_clock_gettime), &vdso_clock_gettime, builtin.AtomicOrder.Unordered);
+ if (@ptrToInt(f) != 0) {
+ const rc = f(clk_id, tp);
+ switch (rc) {
+ 0, @bitCast(usize, isize(-EINVAL)) => return rc,
+ else => {},
+ }
+ }
+ }
+ return syscall2(SYS_clock_gettime, @bitCast(usize, isize(clk_id)), @ptrToInt(tp));
+}
+var vdso_clock_gettime = init_vdso_clock_gettime;
+extern fn init_vdso_clock_gettime(clk: i32, ts: &timespec) usize {
+ const addr = vdso.lookup(VDSO_CGT_VER, VDSO_CGT_SYM);
+ var f = @intToPtr(@typeOf(init_vdso_clock_gettime), addr);
+ _ = @cmpxchgStrong(@typeOf(init_vdso_clock_gettime), &vdso_clock_gettime, init_vdso_clock_gettime, f,
+ builtin.AtomicOrder.Monotonic, builtin.AtomicOrder.Monotonic);
+ if (@ptrToInt(f) == 0) return @bitCast(usize, isize(-ENOSYS));
+ return f(clk, ts);
+}
+
+pub fn clock_getres(clk_id: i32, tp: &timespec) usize {
+ return syscall2(SYS_clock_getres, @bitCast(usize, isize(clk_id)), @ptrToInt(tp));
+}
+
+pub fn clock_settime(clk_id: i32, tp: &const timespec) usize {
+ return syscall2(SYS_clock_settime, @bitCast(usize, isize(clk_id)), @ptrToInt(tp));
+}
+
+pub fn gettimeofday(tv: &timeval, tz: &timezone) usize {
+ return syscall2(SYS_gettimeofday, @ptrToInt(tv), @ptrToInt(tz));
+}
+
+pub fn settimeofday(tv: &const timeval, tz: &const timezone) usize {
+ return syscall2(SYS_settimeofday, @ptrToInt(tv), @ptrToInt(tz));
+}
+
pub fn nanosleep(req: &const timespec, rem: ?&timespec) usize {
return syscall2(SYS_nanosleep, @ptrToInt(req), @ptrToInt(rem));
}
@@ -1289,9 +1329,7 @@ pub fn capset(hdrp: &cap_user_header_t, datap: &const cap_user_data_t) usize {
return syscall2(SYS_capset, @ptrToInt(hdrp), @ptrToInt(datap));
}
-test "import linux test" {
- // TODO lazy analysis should prevent this test from being compiled on windows, but
- // it is still compiled on windows
+test "import" {
if (builtin.os == builtin.Os.linux) {
_ = @import("test.zig");
}
diff --git a/std/os/linux/test.zig b/std/os/linux/test.zig
index e427fd5d59..18a6e5f19f 100644
--- a/std/os/linux/test.zig
+++ b/std/os/linux/test.zig
@@ -1,4 +1,5 @@
const std = @import("../../index.zig");
+const builtin = @import("builtin");
const linux = std.os.linux;
const assert = std.debug.assert;
diff --git a/std/os/linux/vdso.zig b/std/os/linux/vdso.zig
new file mode 100644
index 0000000000..f4fb513af9
--- /dev/null
+++ b/std/os/linux/vdso.zig
@@ -0,0 +1,89 @@
+const std = @import("../../index.zig");
+const elf = std.elf;
+const linux = std.os.linux;
+const cstr = std.cstr;
+const mem = std.mem;
+
+pub fn lookup(vername: []const u8, name: []const u8) usize {
+ const vdso_addr = std.os.linux_aux_raw[std.elf.AT_SYSINFO_EHDR];
+ if (vdso_addr == 0) return 0;
+
+ const eh = @intToPtr(&elf.Ehdr, vdso_addr);
+ var ph_addr: usize = vdso_addr + eh.e_phoff;
+ const ph = @intToPtr(&elf.Phdr, ph_addr);
+
+ var maybe_dynv: ?&usize = null;
+ var base: usize = @maxValue(usize);
+ {
+ var i: usize = 0;
+ while (i < eh.e_phnum) : ({i += 1; ph_addr += eh.e_phentsize;}) {
+ const this_ph = @intToPtr(&elf.Phdr, ph_addr);
+ switch (this_ph.p_type) {
+ elf.PT_LOAD => base = vdso_addr + this_ph.p_offset - this_ph.p_vaddr,
+ elf.PT_DYNAMIC => maybe_dynv = @intToPtr(&usize, vdso_addr + this_ph.p_offset),
+ else => {},
+ }
+ }
+ }
+ const dynv = maybe_dynv ?? return 0;
+ if (base == @maxValue(usize)) return 0;
+
+ var maybe_strings: ?&u8 = null;
+ var maybe_syms: ?&elf.Sym = null;
+ var maybe_hashtab: ?&linux.Elf_Symndx = null;
+ var maybe_versym: ?&u16 = null;
+ var maybe_verdef: ?&elf.Verdef = null;
+
+ {
+ var i: usize = 0;
+ while (dynv[i] != 0) : (i += 2) {
+ const p = base + dynv[i + 1];
+ switch (dynv[i]) {
+ elf.DT_STRTAB => maybe_strings = @intToPtr(&u8, p),
+ elf.DT_SYMTAB => maybe_syms = @intToPtr(&elf.Sym, p),
+ elf.DT_HASH => maybe_hashtab = @intToPtr(&linux.Elf_Symndx, p),
+ elf.DT_VERSYM => maybe_versym = @intToPtr(&u16, p),
+ elf.DT_VERDEF => maybe_verdef = @intToPtr(&elf.Verdef, p),
+ else => {},
+ }
+ }
+ }
+
+ const strings = maybe_strings ?? return 0;
+ const syms = maybe_syms ?? return 0;
+ const hashtab = maybe_hashtab ?? return 0;
+ if (maybe_verdef == null) maybe_versym = null;
+
+
+ const OK_TYPES = (1<<elf.STT_NOTYPE | 1<<elf.STT_OBJECT | 1<<elf.STT_FUNC | 1<<elf.STT_COMMON);
+ const OK_BINDS = (1<<elf.STB_GLOBAL | 1<<elf.STB_WEAK | 1<<elf.STB_GNU_UNIQUE);
+
+ var i: usize = 0;
+ while (i < hashtab[1]) : (i += 1) {
+ if (0==(u32(1)<<u5(syms[i].st_info&0xf) & OK_TYPES)) continue;
+ if (0==(u32(1)<<u5(syms[i].st_info>>4) & OK_BINDS)) continue;
+ if (0==syms[i].st_shndx) continue;
+ if (!mem.eql(u8, name, cstr.toSliceConst(&strings[syms[i].st_name]))) continue;
+ if (maybe_versym) |versym| {
+ if (!checkver(??maybe_verdef, versym[i], vername, strings))
+ continue;
+ }
+ return base + syms[i].st_value;
+ }
+
+ return 0;
+}
+
+fn checkver(def_arg: &elf.Verdef, vsym_arg: i32, vername: []const u8, strings: &u8) bool {
+ var def = def_arg;
+ const vsym = @bitCast(u32, vsym_arg) & 0x7fff;
+ while (true) {
+ if (0==(def.vd_flags & elf.VER_FLG_BASE) and (def.vd_ndx & 0x7fff) == vsym)
+ break;
+ if (def.vd_next == 0)
+ return false;
+ def = @intToPtr(&elf.Verdef, @ptrToInt(def) + def.vd_next);
+ }
+ const aux = @intToPtr(&elf.Verdaux, @ptrToInt(def ) + def.vd_aux);
+ return mem.eql(u8, vername, cstr.toSliceConst(&strings[aux.vda_name]));
+}
diff --git a/std/os/linux/x86_64.zig b/std/os/linux/x86_64.zig
index d3d2c702fc..544b2365ce 100644
--- a/std/os/linux/x86_64.zig
+++ b/std/os/linux/x86_64.zig
@@ -371,6 +371,13 @@ pub const F_GETOWN_EX = 16;
pub const F_GETOWNER_UIDS = 17;
+
+pub const VDSO_USEFUL = true;
+pub const VDSO_CGT_SYM = "__vdso_clock_gettime";
+pub const VDSO_CGT_VER = "LINUX_2.6";
+pub const VDSO_GETCPU_SYM = "__vdso_getcpu";
+pub const VDSO_GETCPU_VER = "LINUX_2.6";
+
pub fn syscall0(number: usize) usize {
return asm volatile ("syscall"
: [ret] "={rax}" (-> usize)
@@ -492,6 +499,16 @@ pub const timespec = extern struct {
tv_nsec: isize,
};
+pub const timeval = extern struct {
+ tv_sec: isize,
+ tv_usec: isize,
+};
+
+pub const timezone = extern struct {
+ tz_minuteswest: i32,
+ tz_dsttime: i32,
+};
+
pub const dirent = extern struct {
d_ino: usize,
d_off: usize,
@@ -499,3 +516,4 @@ pub const dirent = extern struct {
d_name: u8, // field address is the address of first byte of name
};
+pub const Elf_Symndx = u32;
diff --git a/std/os/time.zig b/std/os/time.zig
new file mode 100644
index 0000000000..e9fbf9798c
--- /dev/null
+++ b/std/os/time.zig
@@ -0,0 +1,288 @@
+const std = @import("../index.zig");
+const builtin = @import("builtin");
+const Os = builtin.Os;
+const debug = std.debug;
+
+const windows = std.os.windows;
+const linux = std.os.linux;
+const darwin = std.os.darwin;
+const posix = std.os.posix;
+
+pub const epoch = @import("epoch.zig");
+
+/// Sleep for the specified duration
+pub fn sleep(seconds: usize, nanoseconds: usize) void {
+ switch (builtin.os) {
+ Os.linux, Os.macosx, Os.ios => {
+ posixSleep(u63(seconds), u63(nanoseconds));
+ },
+ Os.windows => {
+ const ns_per_ms = ns_per_s / ms_per_s;
+ const milliseconds = seconds * ms_per_s + nanoseconds / ns_per_ms;
+ windows.Sleep(windows.DWORD(milliseconds));
+ },
+ else => @compileError("Unsupported OS"),
+ }
+}
+
+const u63 = @IntType(false, 63);
+pub fn posixSleep(seconds: u63, nanoseconds: u63) void {
+ var req = posix.timespec {
+ .tv_sec = seconds,
+ .tv_nsec = nanoseconds,
+ };
+ var rem: posix.timespec = undefined;
+ while (true) {
+ const ret_val = posix.nanosleep(&req, &rem);
+ const err = posix.getErrno(ret_val);
+ if (err == 0) return;
+ switch (err) {
+ posix.EFAULT => unreachable,
+ posix.EINVAL => {
+ // Sometimes Darwin returns EINVAL for no reason.
+ // We treat it as a spurious wakeup.
+ return;
+ },
+ posix.EINTR => {
+ req = rem;
+ continue;
+ },
+ else => return,
+ }
+ }
+}
+
+/// Get the posix timestamp, UTC, in seconds
+pub fn timestamp() u64 {
+ return @divFloor(milliTimestamp(), ms_per_s);
+}
+
+/// Get the posix timestamp, UTC, in milliseconds
+pub const milliTimestamp = switch (builtin.os) {
+ Os.windows => milliTimestampWindows,
+ Os.linux => milliTimestampPosix,
+ Os.macosx, Os.ios => milliTimestampDarwin,
+ else => @compileError("Unsupported OS"),
+};
+
+fn milliTimestampWindows() u64 {
+ //FileTime has a granularity of 100 nanoseconds
+ // and uses the NTFS/Windows epoch
+ var ft: i64 = undefined;
+ windows.GetSystemTimeAsFileTime(&ft);
+ const hns_per_ms = (ns_per_s / 100) / ms_per_s;
+ const epoch_adj = epoch.windows * ms_per_s;
+ return u64(@divFloor(ft, hns_per_ms) + epoch_adj);
+}
+
+fn milliTimestampDarwin() u64 {
+ //Sources suggest MacOS 10.12 has support for
+ // posix clock_gettime.
+ var tv: darwin.timeval = undefined;
+ var err = darwin.gettimeofday(&tv, null);
+ debug.assert(err == 0);
+ const sec_ms = u64(tv.tv_sec) * ms_per_s;
+ const usec_ms = @divFloor(u64(tv.tv_usec), us_per_s / ms_per_s);
+ return u64(sec_ms) + u64(usec_ms);
+}
+
+fn milliTimestampPosix() u64 {
+ //From what I can tell there's no reason clock_gettime
+ // should ever fail for us with CLOCK_REALTIME,
+ // seccomp aside.
+ var ts: posix.timespec = undefined;
+ const err = posix.clock_gettime(posix.CLOCK_REALTIME, &ts);
+ debug.assert(err == 0);
+ const sec_ms = u64(ts.tv_sec) * ms_per_s;
+ const nsec_ms = @divFloor(u64(ts.tv_nsec), ns_per_s / ms_per_s);
+ return sec_ms + nsec_ms;
+}
+
+/// Divisions of a second
+pub const ns_per_s = 1000000000;
+pub const us_per_s = 1000000;
+pub const ms_per_s = 1000;
+pub const cs_per_s = 100;
+
+/// Common time divisions
+pub const s_per_min = 60;
+pub const s_per_hour = s_per_min * 60;
+pub const s_per_day = s_per_hour * 24;
+pub const s_per_week = s_per_day * 7;
+
+
+/// A monotonic high-performance timer.
+/// Timer.start() must be called to initialize the struct, which captures
+/// the counter frequency on windows and darwin, records the resolution,
+/// and gives the user an oportunity to check for the existnece of
+/// monotonic clocks without forcing them to check for error on each read.
+/// .resolution is in nanoseconds on all platforms but .start_time's meaning
+/// depends on the OS. On Windows and Darwin it is a hardware counter
+/// value that requires calculation to convert to a meaninful unit.
+pub const Timer = struct {
+
+ //if we used resolution's value when performing the
+ // performance counter calc on windows/darwin, it would
+ // be less precise
+ frequency: switch (builtin.os) {
+ Os.windows => u64,
+ Os.macosx, Os.ios => darwin.mach_timebase_info_data,
+ else => void,
+ },
+ resolution: u64,
+ start_time: u64,
+
+
+ //At some point we may change our minds on RAW, but for now we're
+ // sticking with posix standard MONOTONIC. For more information, see:
+ // https://github.com/zig-lang/zig/pull/933
+ //
+ //const monotonic_clock_id = switch(builtin.os) {
+ // Os.linux => linux.CLOCK_MONOTONIC_RAW,
+ // else => posix.CLOCK_MONOTONIC,
+ //};
+ const monotonic_clock_id = posix.CLOCK_MONOTONIC;
+
+
+ /// Initialize the timer structure.
+ //This gives us an oportunity to grab the counter frequency in windows.
+ //On Windows: QueryPerformanceCounter will succeed on anything >= XP/2000.
+ //On Posix: CLOCK_MONOTONIC will only fail if the monotonic counter is not
+ // supported, or if the timespec pointer is out of bounds, which should be
+ // impossible here barring cosmic rays or other such occurances of
+ // incredibly bad luck.
+ //On Darwin: This cannot fail, as far as I am able to tell.
+ const TimerError = error{TimerUnsupported, Unexpected};
+ pub fn start() TimerError!Timer {
+ var self: Timer = undefined;
+
+ switch (builtin.os) {
+ Os.windows => {
+ var freq: i64 = undefined;
+ var err = windows.QueryPerformanceFrequency(&freq);
+ if (err == windows.FALSE) return error.TimerUnsupported;
+ self.frequency = u64(freq);
+ self.resolution = @divFloor(ns_per_s, self.frequency);
+
+ var start_time: i64 = undefined;
+ err = windows.QueryPerformanceCounter(&start_time);
+ debug.assert(err != windows.FALSE);
+ self.start_time = u64(start_time);
+ },
+ Os.linux => {
+ //On Linux, seccomp can do arbitrary things to our ability to call
+ // syscalls, including return any errno value it wants and
+ // inconsistently throwing errors. Since we can't account for
+ // abuses of seccomp in a reasonable way, we'll assume that if
+ // seccomp is going to block us it will at least do so consistently
+ var ts: posix.timespec = undefined;
+ var result = posix.clock_getres(monotonic_clock_id, &ts);
+ var errno = posix.getErrno(result);
+ switch (errno) {
+ 0 => {},
+ posix.EINVAL => return error.TimerUnsupported,
+ else => return std.os.unexpectedErrorPosix(errno),
+ }
+ self.resolution = u64(ts.tv_sec) * u64(ns_per_s) + u64(ts.tv_nsec);
+
+ result = posix.clock_gettime(monotonic_clock_id, &ts);
+ errno = posix.getErrno(result);
+ if (errno != 0) return std.os.unexpectedErrorPosix(errno);
+ self.start_time = u64(ts.tv_sec) * u64(ns_per_s) + u64(ts.tv_nsec);
+ },
+ Os.macosx, Os.ios => {
+ darwin.mach_timebase_info(&self.frequency);
+ self.resolution = @divFloor(self.frequency.numer, self.frequency.denom);
+ self.start_time = darwin.mach_absolute_time();
+ },
+ else => @compileError("Unsupported OS"),
+ }
+ return self;
+ }
+
+ /// Reads the timer value since start or the last reset in nanoseconds
+ pub fn read(self: &Timer) u64 {
+ var clock = clockNative() - self.start_time;
+ return switch (builtin.os) {
+ Os.windows => @divFloor(clock * ns_per_s, self.frequency),
+ Os.linux => clock,
+ Os.macosx, Os.ios => @divFloor(clock * self.frequency.numer, self.frequency.denom),
+ else => @compileError("Unsupported OS"),
+ };
+ }
+
+ /// Resets the timer value to 0/now.
+ pub fn reset(self: &Timer) void
+ {
+ self.start_time = clockNative();
+ }
+
+ /// Returns the current value of the timer in nanoseconds, then resets it
+ pub fn lap(self: &Timer) u64 {
+ var now = clockNative();
+ var lap_time = self.read();
+ self.start_time = now;
+ return lap_time;
+ }
+
+
+ const clockNative = switch (builtin.os) {
+ Os.windows => clockWindows,
+ Os.linux => clockLinux,
+ Os.macosx, Os.ios => clockDarwin,
+ else => @compileError("Unsupported OS"),
+ };
+
+ fn clockWindows() u64 {
+ var result: i64 = undefined;
+ var err = windows.QueryPerformanceCounter(&result);
+ debug.assert(err != windows.FALSE);
+ return u64(result);
+ }
+
+ fn clockDarwin() u64 {
+ return darwin.mach_absolute_time();
+ }
+
+ fn clockLinux() u64 {
+ var ts: posix.timespec = undefined;
+ var result = posix.clock_gettime(monotonic_clock_id, &ts);
+ debug.assert(posix.getErrno(result) == 0);
+ return u64(ts.tv_sec) * u64(ns_per_s) + u64(ts.tv_nsec);
+ }
+};
+
+
+
+
+
+test "os.time.sleep" {
+ sleep(0, 1);
+}
+
+test "os.time.timestamp" {
+ const ns_per_ms = (ns_per_s / ms_per_s);
+ const margin = 50;
+
+ const time_0 = milliTimestamp();
+ sleep(0, ns_per_ms);
+ const time_1 = milliTimestamp();
+ const interval = time_1 - time_0;
+ debug.assert(interval > 0 and interval < margin);
+}
+
+test "os.time.Timer" {
+ const ns_per_ms = (ns_per_s / ms_per_s);
+ const margin = ns_per_ms * 50;
+
+ var timer = try Timer.start();
+ sleep(0, 10 * ns_per_ms);
+ const time_0 = timer.read();
+ debug.assert(time_0 > 0 and time_0 < margin);
+
+ const time_1 = timer.lap();
+ debug.assert(time_1 > time_0);
+
+ timer.reset();
+ debug.assert(timer.read() < time_1);
+}
diff --git a/std/os/windows/index.zig b/std/os/windows/index.zig
index aa02c27f39..d6ef7205e8 100644
--- a/std/os/windows/index.zig
+++ b/std/os/windows/index.zig
@@ -61,6 +61,8 @@ pub extern "kernel32" stdcallcc fn GetFinalPathNameByHandleA(hFile: HANDLE, lpsz
pub extern "kernel32" stdcallcc fn GetProcessHeap() ?HANDLE;
+pub extern "kernel32" stdcallcc fn GetSystemTimeAsFileTime(?&FILETIME) void;
+
pub extern "kernel32" stdcallcc fn HeapCreate(flOptions: DWORD, dwInitialSize: SIZE_T, dwMaximumSize: SIZE_T) ?HANDLE;
pub extern "kernel32" stdcallcc fn HeapDestroy(hHeap: HANDLE) BOOL;
pub extern "kernel32" stdcallcc fn HeapReAlloc(hHeap: HANDLE, dwFlags: DWORD, lpMem: &c_void, dwBytes: SIZE_T) ?&c_void;
@@ -77,6 +79,10 @@ pub extern "kernel32" stdcallcc fn HeapFree(hHeap: HANDLE, dwFlags: DWORD, lpMem
pub extern "kernel32" stdcallcc fn MoveFileExA(lpExistingFileName: LPCSTR, lpNewFileName: LPCSTR,
dwFlags: DWORD) BOOL;
+
+pub extern "kernel32" stdcallcc fn QueryPerformanceCounter(lpPerformanceCount: &LARGE_INTEGER) BOOL;
+
+pub extern "kernel32" stdcallcc fn QueryPerformanceFrequency(lpFrequency: &LARGE_INTEGER) BOOL;
pub extern "kernel32" stdcallcc fn PathFileExists(pszPath: ?LPCTSTR) BOOL;
@@ -139,6 +145,7 @@ pub const UNICODE = false;
pub const WCHAR = u16;
pub const WORD = u16;
pub const LARGE_INTEGER = i64;
+pub const FILETIME = i64;
pub const TRUE = 1;
pub const FALSE = 0;
@@ -310,3 +317,7 @@ pub const FILE_END = 2;
pub const HEAP_CREATE_ENABLE_EXECUTE = 0x00040000;
pub const HEAP_GENERATE_EXCEPTIONS = 0x00000004;
pub const HEAP_NO_SERIALIZE = 0x00000001;
+
+test "import" {
+ _ = @import("util.zig");
+}
diff --git a/std/special/bootstrap.zig b/std/special/bootstrap.zig
index d2c22c13e1..1dc7e24869 100644
--- a/std/special/bootstrap.zig
+++ b/std/special/bootstrap.zig
@@ -48,22 +48,33 @@ extern fn WinMainCRTStartup() noreturn {
fn posixCallMainAndExit() noreturn {
const argc = *argc_ptr;
const argv = @ptrCast(&&u8, &argc_ptr[1]);
- const envp = @ptrCast(&?&u8, &argv[argc + 1]);
+ const envp_nullable = @ptrCast(&?&u8, &argv[argc + 1]);
+ var envp_count: usize = 0;
+ while (envp_nullable[envp_count]) |_| : (envp_count += 1) {}
+ const envp = @ptrCast(&&u8, envp_nullable)[0..envp_count];
+ if (builtin.os == builtin.Os.linux) {
+ const auxv = &@ptrCast(&usize, envp.ptr)[envp_count + 1];
+ var i: usize = 0;
+ while (auxv[i] != 0) : (i += 2) {
+ if (auxv[i] < std.os.linux_aux_raw.len) std.os.linux_aux_raw[auxv[i]] = auxv[i+1];
+ }
+ std.debug.assert(std.os.linux_aux_raw[std.elf.AT_PAGESZ] == std.os.page_size);
+ }
+
std.os.posix.exit(callMainWithArgs(argc, argv, envp));
}
-fn callMainWithArgs(argc: usize, argv: &&u8, envp: &?&u8) u8 {
+fn callMainWithArgs(argc: usize, argv: &&u8, envp: []&u8) u8 {
std.os.ArgIteratorPosix.raw = argv[0..argc];
-
- var env_count: usize = 0;
- while (envp[env_count] != null) : (env_count += 1) {}
- std.os.posix_environ_raw = @ptrCast(&&u8, envp)[0..env_count];
-
+ std.os.posix_environ_raw = envp;
return callMain();
}
extern fn main(c_argc: i32, c_argv: &&u8, c_envp: &?&u8) i32 {
- return callMainWithArgs(usize(c_argc), c_argv, c_envp);
+ var env_count: usize = 0;
+ while (c_envp[env_count] != null) : (env_count += 1) {}
+ const envp = @ptrCast(&&u8, c_envp)[0..env_count];
+ return callMainWithArgs(usize(c_argc), c_argv, envp);
}
fn callMain() u8 {