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
This commit is contained in:
Jacek Sieka
2016-06-06 23:00:00 +08:00
parent fe2b39f007
commit 8d7a45f205
6 changed files with 65 additions and 39 deletions

View File

@@ -29,9 +29,6 @@ proc c_strcmp(a, b: cstring): cint {.
type type
C_JmpBuf {.importc: "jmp_buf", header: "<setjmp.h>".} = object C_JmpBuf {.importc: "jmp_buf", header: "<setjmp.h>".} = object
# constants faked as variables:
when not declared(SIGINT):
when NoFakeVars:
when defined(windows): when defined(windows):
const const
SIGABRT = cint(22) SIGABRT = cint(22)
@@ -50,6 +47,7 @@ when not declared(SIGINT):
SIGTERM = cint(15) SIGTERM = cint(15)
SIGPIPE = cint(13) SIGPIPE = cint(13)
else: else:
when NoFakeVars:
{.error: "SIGABRT not ported to your platform".} {.error: "SIGABRT not ported to your platform".}
else: else:
var var
@@ -62,10 +60,7 @@ when not declared(SIGINT):
var SIGPIPE {.importc: "SIGPIPE", nodecl.}: cint var SIGPIPE {.importc: "SIGPIPE", nodecl.}: cint
when defined(macosx): when defined(macosx):
when NoFakeVars:
const SIGBUS = cint(10) const SIGBUS = cint(10)
else:
var SIGBUS {.importc: "SIGBUS", nodecl.}: cint
else: else:
template SIGBUS: expr = SIGSEGV template SIGBUS: expr = SIGSEGV

View File

@@ -52,11 +52,14 @@ when defined(posix):
# #
# c stuff: # c stuff:
when defined(linux) or defined(macosx):
const RTLD_NOW = cint(2)
else:
var var
RTLD_NOW {.importc: "RTLD_NOW", header: "<dlfcn.h>".}: int RTLD_NOW {.importc: "RTLD_NOW", header: "<dlfcn.h>".}: cint
proc dlclose(lib: LibHandle) {.importc, header: "<dlfcn.h>".} proc dlclose(lib: LibHandle) {.importc, header: "<dlfcn.h>".}
proc dlopen(path: cstring, mode: int): LibHandle {. proc dlopen(path: cstring, mode: cint): LibHandle {.
importc, header: "<dlfcn.h>".} importc, header: "<dlfcn.h>".}
proc dlsym(lib: LibHandle, name: cstring): ProcAddr {. proc dlsym(lib: LibHandle, name: cstring): ProcAddr {.
importc, header: "<dlfcn.h>".} importc, header: "<dlfcn.h>".}

View File

@@ -87,6 +87,8 @@ elif defined(posix):
const MAP_ANONYMOUS = 0x1000 const MAP_ANONYMOUS = 0x1000
elif defined(solaris): elif defined(solaris):
const MAP_ANONYMOUS = 0x100 const MAP_ANONYMOUS = 0x100
elif defined(linux):
const MAP_ANONYMOUS = 0x20
else: else:
var var
MAP_ANONYMOUS {.importc: "MAP_ANONYMOUS", header: "<sys/mman.h>".}: cint MAP_ANONYMOUS {.importc: "MAP_ANONYMOUS", header: "<sys/mman.h>".}: cint

View File

@@ -277,6 +277,22 @@ const
# should not be translated. # should not be translated.
when defined(posix) and not defined(nimscript): when defined(posix) and not defined(nimscript):
when defined(linux) and defined(amd64):
type
Mode {.importc: "mode_t", header: "<sys/types.h>".} = cint
# fillers ensure correct size & offsets
Stat {.importc: "struct stat",
header: "<sys/stat.h>", 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 =
## Test for a directory.
(m and 0o170000) == 0o40000
else:
type type
Mode {.importc: "mode_t", header: "<sys/types.h>".} = cint Mode {.importc: "mode_t", header: "<sys/types.h>".} = cint

View File

@@ -117,6 +117,11 @@ else:
schedh = "#define _GNU_SOURCE\n#include <sched.h>" schedh = "#define _GNU_SOURCE\n#include <sched.h>"
pthreadh = "#define _GNU_SOURCE\n#include <pthread.h>" pthreadh = "#define _GNU_SOURCE\n#include <pthread.h>"
when defined(linux):
type Time = clong
else:
type Time = int
type type
SysThread {.importc: "pthread_t", header: "<sys/types.h>", SysThread {.importc: "pthread_t", header: "<sys/types.h>",
final, pure.} = object final, pure.} = object
@@ -125,8 +130,8 @@ else:
Timespec {.importc: "struct timespec", Timespec {.importc: "struct timespec",
header: "<time.h>", final, pure.} = object header: "<time.h>", final, pure.} = object
tv_sec: int tv_sec: Time
tv_nsec: int tv_nsec: clong
{.deprecated: [TSysThread: SysThread, Tpthread_attr: PThreadAttr, {.deprecated: [TSysThread: SysThread, Tpthread_attr: PThreadAttr,
Ttimespec: Timespec].} Ttimespec: Timespec].}

View File

@@ -78,11 +78,16 @@ elif defined(posixRealtime):
else: else:
# fallback Posix implementation: # fallback Posix implementation:
when defined(linux):
type Time = clong
else:
type Time = int
type type
Timeval {.importc: "struct timeval", header: "<sys/select.h>", Timeval {.importc: "struct timeval", header: "<sys/select.h>",
final, pure.} = object ## struct timeval final, pure.} = object ## struct timeval
tv_sec: int ## Seconds. tv_sec: Time ## Seconds.
tv_usec: int ## Microseconds. tv_usec: clong ## Microseconds.
{.deprecated: [Ttimeval: Timeval].} {.deprecated: [Ttimeval: Timeval].}
proc posix_gettimeofday(tp: var Timeval, unused: pointer = nil) {. proc posix_gettimeofday(tp: var Timeval, unused: pointer = nil) {.
importc: "gettimeofday", header: "<sys/time.h>".} importc: "gettimeofday", header: "<sys/time.h>".}