From 8d7a45f2053698e24bfeb3831e6ea394eb974b58 Mon Sep 17 00:00:00 2001 From: Jacek Sieka Date: Mon, 6 Jun 2016 23:00:00 +0800 Subject: [PATCH] prefer consts to importing #defines from headers to be completed - better would be to have a libc wrapper that deals with all pesky C ABI details --- lib/system/ansi_c.nim | 45 ++++++++++++++++++----------------------- lib/system/dyncalls.nim | 9 ++++++--- lib/system/osalloc.nim | 2 ++ lib/system/sysio.nim | 30 ++++++++++++++++++++------- lib/system/threads.nim | 9 +++++++-- lib/system/timers.nim | 9 +++++++-- 6 files changed, 65 insertions(+), 39 deletions(-) diff --git a/lib/system/ansi_c.nim b/lib/system/ansi_c.nim index ff4d327138..2f3d560827 100644 --- a/lib/system/ansi_c.nim +++ b/lib/system/ansi_c.nim @@ -29,28 +29,26 @@ proc c_strcmp(a, b: cstring): cint {. type C_JmpBuf {.importc: "jmp_buf", header: "".} = object -# constants faked as variables: -when not declared(SIGINT): +when defined(windows): + const + SIGABRT = cint(22) + SIGFPE = cint(8) + SIGILL = cint(4) + SIGINT = cint(2) + SIGSEGV = cint(11) + SIGTERM = cint(15) +elif defined(macosx) or defined(linux): + const + SIGABRT = cint(6) + SIGFPE = cint(8) + SIGILL = cint(4) + SIGINT = cint(2) + SIGSEGV = cint(11) + SIGTERM = cint(15) + SIGPIPE = cint(13) +else: when NoFakeVars: - when defined(windows): - const - SIGABRT = cint(22) - SIGFPE = cint(8) - SIGILL = cint(4) - SIGINT = cint(2) - SIGSEGV = cint(11) - SIGTERM = cint(15) - elif defined(macosx) or defined(linux): - const - SIGABRT = cint(6) - SIGFPE = cint(8) - SIGILL = cint(4) - SIGINT = cint(2) - SIGSEGV = cint(11) - SIGTERM = cint(15) - SIGPIPE = cint(13) - else: - {.error: "SIGABRT not ported to your platform".} + {.error: "SIGABRT not ported to your platform".} else: var SIGINT {.importc: "SIGINT", nodecl.}: cint @@ -62,10 +60,7 @@ when not declared(SIGINT): var SIGPIPE {.importc: "SIGPIPE", nodecl.}: cint when defined(macosx): - when NoFakeVars: - const SIGBUS = cint(10) - else: - var SIGBUS {.importc: "SIGBUS", nodecl.}: cint + const SIGBUS = cint(10) else: template SIGBUS: expr = SIGSEGV diff --git a/lib/system/dyncalls.nim b/lib/system/dyncalls.nim index 61777e514e..0a994efac3 100644 --- a/lib/system/dyncalls.nim +++ b/lib/system/dyncalls.nim @@ -52,11 +52,14 @@ when defined(posix): # # c stuff: - var - RTLD_NOW {.importc: "RTLD_NOW", header: "".}: int + when defined(linux) or defined(macosx): + const RTLD_NOW = cint(2) + else: + var + RTLD_NOW {.importc: "RTLD_NOW", header: "".}: cint proc dlclose(lib: LibHandle) {.importc, header: "".} - proc dlopen(path: cstring, mode: int): LibHandle {. + proc dlopen(path: cstring, mode: cint): LibHandle {. importc, header: "".} proc dlsym(lib: LibHandle, name: cstring): ProcAddr {. importc, header: "".} diff --git a/lib/system/osalloc.nim b/lib/system/osalloc.nim index 8b83e194bf..62e36bfd61 100644 --- a/lib/system/osalloc.nim +++ b/lib/system/osalloc.nim @@ -87,6 +87,8 @@ elif defined(posix): const MAP_ANONYMOUS = 0x1000 elif defined(solaris): const MAP_ANONYMOUS = 0x100 + elif defined(linux): + const MAP_ANONYMOUS = 0x20 else: var MAP_ANONYMOUS {.importc: "MAP_ANONYMOUS", header: "".}: cint diff --git a/lib/system/sysio.nim b/lib/system/sysio.nim index 99d0420f9b..1f64c34bb2 100644 --- a/lib/system/sysio.nim +++ b/lib/system/sysio.nim @@ -277,15 +277,31 @@ const # should not be translated. when defined(posix) and not defined(nimscript): - type - Mode {.importc: "mode_t", header: "".} = cint + when defined(linux) and defined(amd64): + type + Mode {.importc: "mode_t", header: "".} = cint - Stat {.importc: "struct stat", - header: "", final, pure.} = object ## struct stat - st_mode: Mode ## Mode of file + # fillers ensure correct size & offsets + Stat {.importc: "struct stat", + header: "", final, pure.} = object ## struct stat + filler_1: array[24, char] + st_mode: Mode ## Mode of file + filler_2: array[144 - 24 - 4, char] - proc S_ISDIR(m: Mode): bool {.importc, header: "".} - ## Test for a directory. + proc S_ISDIR(m: Mode): bool = + ## Test for a directory. + (m and 0o170000) == 0o40000 + + else: + type + Mode {.importc: "mode_t", header: "".} = cint + + Stat {.importc: "struct stat", + header: "", final, pure.} = object ## struct stat + st_mode: Mode ## Mode of file + + proc S_ISDIR(m: Mode): bool {.importc, header: "".} + ## Test for a directory. proc c_fstat(a1: cint, a2: var Stat): cint {. importc: "fstat", header: "".} diff --git a/lib/system/threads.nim b/lib/system/threads.nim index 99927fbac2..8505202b59 100644 --- a/lib/system/threads.nim +++ b/lib/system/threads.nim @@ -117,6 +117,11 @@ else: schedh = "#define _GNU_SOURCE\n#include " pthreadh = "#define _GNU_SOURCE\n#include " + when defined(linux): + type Time = clong + else: + type Time = int + type SysThread {.importc: "pthread_t", header: "", final, pure.} = object @@ -125,8 +130,8 @@ else: Timespec {.importc: "struct timespec", header: "", final, pure.} = object - tv_sec: int - tv_nsec: int + tv_sec: Time + tv_nsec: clong {.deprecated: [TSysThread: SysThread, Tpthread_attr: PThreadAttr, Ttimespec: Timespec].} diff --git a/lib/system/timers.nim b/lib/system/timers.nim index 8aa4505c4d..129a7d0929 100644 --- a/lib/system/timers.nim +++ b/lib/system/timers.nim @@ -78,11 +78,16 @@ elif defined(posixRealtime): else: # fallback Posix implementation: + when defined(linux): + type Time = clong + else: + type Time = int + type Timeval {.importc: "struct timeval", header: "", final, pure.} = object ## struct timeval - tv_sec: int ## Seconds. - tv_usec: int ## Microseconds. + tv_sec: Time ## Seconds. + tv_usec: clong ## Microseconds. {.deprecated: [Ttimeval: Timeval].} proc posix_gettimeofday(tp: var Timeval, unused: pointer = nil) {. importc: "gettimeofday", header: "".}