diff options
author | Araq <rumpf_a@web.de> | 2019-04-08 17:09:27 +0200 |
---|---|---|
committer | Araq <rumpf_a@web.de> | 2019-04-08 17:09:27 +0200 |
commit | 6efa7ecf188c3658828875a8c2426f7db3c6f0ac (patch) | |
tree | 3b7409986f435f12a7dac2bd2cfe25b71e6d0c86 /lib | |
parent | 9565b844dd155fd976a2f043c530727355ac3c45 (diff) | |
download | Nim-6efa7ecf188c3658828875a8c2426f7db3c6f0ac.tar.gz |
respect -d:useMalloc everywhere; turn ansi_c and memory into proper Nim modules
Diffstat (limited to 'lib')
-rw-r--r-- | lib/core/allocators.nim | 21 | ||||
-rw-r--r-- | lib/system.nim | 5 | ||||
-rw-r--r-- | lib/system/ansi_c.nim | 110 | ||||
-rw-r--r-- | lib/system/memory.nim | 13 |
4 files changed, 83 insertions, 66 deletions
diff --git a/lib/core/allocators.nim b/lib/core/allocators.nim index 984e83690..c4302fc2c 100644 --- a/lib/core/allocators.nim +++ b/lib/core/allocators.nim @@ -27,18 +27,33 @@ var sharedAllocator: Allocator allocatorStorage {.threadvar.}: AllocatorObj +when defined(useMalloc) and not defined(nimscript): + import "system/ansi_c" + import "system/memory" + proc getLocalAllocator*(): Allocator = result = localAllocator if result == nil: result = addr allocatorStorage result.alloc = proc (a: Allocator; size: int; alignment: int = 8): pointer {.nimcall, raises: [].} = - result = system.alloc(size) + when defined(useMalloc) and not defined(nimscript): + result = c_malloc(size) + # XXX do we need this? + nimZeroMem(result, size) + else: + result = system.alloc(size) inc a.allocCount result.dealloc = proc (a: Allocator; p: pointer; size: int) {.nimcall, raises: [].} = - system.dealloc(p) + when defined(useMalloc) and not defined(nimscript): + c_free(p) + else: + system.dealloc(p) inc a.deallocCount result.realloc = proc (a: Allocator; p: pointer; oldSize, newSize: int): pointer {.nimcall, raises: [].} = - result = system.realloc(p, newSize) + when defined(useMalloc) and not defined(nimscript): + result = c_realloc(p, newSize) + else: + result = system.realloc(p, newSize) result.deallocAll = nil result.flags = {ThreadLocal} result.name = "nim_local" diff --git a/lib/system.nim b/lib/system.nim index f51767950..179dc175a 100644 --- a/lib/system.nim +++ b/lib/system.nim @@ -2969,7 +2969,8 @@ proc compiles*(x: untyped): bool {.magic: "Compiles", noSideEffect, compileTime. discard when not defined(js) and not defined(nimscript): - include "system/ansi_c" + import "system/ansi_c" + import "system/memory" when not defined(js): {.push stackTrace:off.} @@ -3468,8 +3469,6 @@ when not defined(JS): #and not defined(nimscript): {.pop.} when not defined(nimscript): - include "system/memory" - proc zeroMem(p: pointer, size: Natural) = nimZeroMem(p, size) when declared(memTrackerOp): diff --git a/lib/system/ansi_c.nim b/lib/system/ansi_c.nim index 05580ea95..9e36aafdc 100644 --- a/lib/system/ansi_c.nim +++ b/lib/system/ansi_c.nim @@ -7,108 +7,108 @@ # distribution, for details about the copyright. # -# This include file contains headers of Ansi C procs +# This module contains headers of Ansi C procs # and definitions of Ansi C types in Nim syntax # All symbols are prefixed with 'c_' to avoid ambiguities {.push hints:off, stack_trace: off, profiler: off.} -proc c_memchr(s: pointer, c: cint, n: csize): pointer {. +proc c_memchr*(s: pointer, c: cint, n: csize): pointer {. importc: "memchr", header: "<string.h>".} -proc c_memcmp(a, b: pointer, size: csize): cint {. +proc c_memcmp*(a, b: pointer, size: csize): cint {. importc: "memcmp", header: "<string.h>", noSideEffect.} -proc c_memcpy(a, b: pointer, size: csize): pointer {. +proc c_memcpy*(a, b: pointer, size: csize): pointer {. importc: "memcpy", header: "<string.h>", discardable.} -proc c_memmove(a, b: pointer, size: csize): pointer {. +proc c_memmove*(a, b: pointer, size: csize): pointer {. importc: "memmove", header: "<string.h>",discardable.} -proc c_memset(p: pointer, value: cint, size: csize): pointer {. +proc c_memset*(p: pointer, value: cint, size: csize): pointer {. importc: "memset", header: "<string.h>", discardable.} -proc c_strcmp(a, b: cstring): cint {. +proc c_strcmp*(a, b: cstring): cint {. importc: "strcmp", header: "<string.h>", noSideEffect.} -proc c_strlen(a: cstring): csize {. +proc c_strlen*(a: cstring): csize {. importc: "strlen", header: "<string.h>", noSideEffect.} -proc c_abort() {. +proc c_abort*() {. importc: "abort", header: "<stdlib.h>", noSideEffect.} when defined(linux) and defined(amd64): type - C_JmpBuf {.importc: "jmp_buf", header: "<setjmp.h>", bycopy.} = object + C_JmpBuf* {.importc: "jmp_buf", header: "<setjmp.h>", bycopy.} = object abi: array[200 div sizeof(clong), clong] else: type - C_JmpBuf {.importc: "jmp_buf", header: "<setjmp.h>".} = object + C_JmpBuf* {.importc: "jmp_buf", header: "<setjmp.h>".} = object when defined(windows): const - SIGABRT = cint(22) - SIGFPE = cint(8) - SIGILL = cint(4) - SIGINT = cint(2) - SIGSEGV = cint(11) + SIGABRT* = cint(22) + SIGFPE* = cint(8) + SIGILL* = cint(4) + SIGINT* = cint(2) + SIGSEGV* = cint(11) SIGTERM = cint(15) elif defined(macosx) or defined(linux) or defined(freebsd) or defined(openbsd) or defined(netbsd) or defined(solaris) or defined(dragonfly) or defined(nintendoswitch) or defined(genode) or defined(aix) or hostOS == "standalone": const - SIGABRT = cint(6) - SIGFPE = cint(8) - SIGILL = cint(4) - SIGINT = cint(2) - SIGSEGV = cint(11) - SIGTERM = cint(15) - SIGPIPE = cint(13) + SIGABRT* = cint(6) + SIGFPE* = cint(8) + SIGILL* = cint(4) + SIGINT* = cint(2) + SIGSEGV* = cint(11) + SIGTERM* = cint(15) + SIGPIPE* = cint(13) elif defined(haiku): const - SIGABRT = cint(6) - SIGFPE = cint(8) - SIGILL = cint(4) - SIGINT = cint(2) - SIGSEGV = cint(11) - SIGTERM = cint(15) - SIGPIPE = cint(7) + SIGABRT* = cint(6) + SIGFPE* = cint(8) + SIGILL* = cint(4) + SIGINT* = cint(2) + SIGSEGV* = cint(11) + SIGTERM* = cint(15) + SIGPIPE* = cint(7) else: when NoFakeVars: {.error: "SIGABRT not ported to your platform".} else: var - SIGINT {.importc: "SIGINT", nodecl.}: cint - SIGSEGV {.importc: "SIGSEGV", nodecl.}: cint - SIGABRT {.importc: "SIGABRT", nodecl.}: cint - SIGFPE {.importc: "SIGFPE", nodecl.}: cint - SIGILL {.importc: "SIGILL", nodecl.}: cint + SIGINT* {.importc: "SIGINT", nodecl.}: cint + SIGSEGV* {.importc: "SIGSEGV", nodecl.}: cint + SIGABRT* {.importc: "SIGABRT", nodecl.}: cint + SIGFPE* {.importc: "SIGFPE", nodecl.}: cint + SIGILL* {.importc: "SIGILL", nodecl.}: cint when defined(macosx) or defined(linux): - var SIGPIPE {.importc: "SIGPIPE", nodecl.}: cint + var SIGPIPE* {.importc: "SIGPIPE", nodecl.}: cint when defined(macosx): - const SIGBUS = cint(10) + const SIGBUS* = cint(10) elif defined(haiku): - const SIGBUS = cint(30) + const SIGBUS* = cint(30) else: - template SIGBUS: untyped = SIGSEGV + template SIGBUS*: untyped = SIGSEGV when defined(nimSigSetjmp) and not defined(nimStdSetjmp): - proc c_longjmp(jmpb: C_JmpBuf, retval: cint) {. + proc c_longjmp*(jmpb: C_JmpBuf, retval: cint) {. header: "<setjmp.h>", importc: "siglongjmp".} - template c_setjmp(jmpb: C_JmpBuf): cint = + template c_setjmp*(jmpb: C_JmpBuf): cint = proc c_sigsetjmp(jmpb: C_JmpBuf, savemask: cint): cint {. header: "<setjmp.h>", importc: "sigsetjmp".} c_sigsetjmp(jmpb, 0) elif defined(nimRawSetjmp) and not defined(nimStdSetjmp): - proc c_longjmp(jmpb: C_JmpBuf, retval: cint) {. + proc c_longjmp*(jmpb: C_JmpBuf, retval: cint) {. header: "<setjmp.h>", importc: "_longjmp".} - proc c_setjmp(jmpb: C_JmpBuf): cint {. + proc c_setjmp*(jmpb: C_JmpBuf): cint {. header: "<setjmp.h>", importc: "_setjmp".} else: - proc c_longjmp(jmpb: C_JmpBuf, retval: cint) {. + proc c_longjmp*(jmpb: C_JmpBuf, retval: cint) {. header: "<setjmp.h>", importc: "longjmp".} - proc c_setjmp(jmpb: C_JmpBuf): cint {. + proc c_setjmp*(jmpb: C_JmpBuf): cint {. header: "<setjmp.h>", importc: "setjmp".} type c_sighandler_t = proc (a: cint) {.noconv.} -proc c_signal(sign: cint, handler: proc (a: cint) {.noconv.}): c_sighandler_t {. +proc c_signal*(sign: cint, handler: proc (a: cint) {.noconv.}): c_sighandler_t {. importc: "signal", header: "<signal.h>", discardable.} type @@ -120,29 +120,29 @@ var cstderr* {.importc: "stderr", header: "<stdio.h>".}: CFilePtr cstdout* {.importc: "stdout", header: "<stdio.h>".}: CFilePtr -proc c_fprintf(f: CFilePtr, frmt: cstring): cint {. +proc c_fprintf*(f: CFilePtr, frmt: cstring): cint {. importc: "fprintf", header: "<stdio.h>", varargs, discardable.} -proc c_printf(frmt: cstring): cint {. +proc c_printf*(frmt: cstring): cint {. importc: "printf", header: "<stdio.h>", varargs, discardable.} -proc c_fputs(c: cstring, f: CFilePtr): cint {. +proc c_fputs*(c: cstring, f: CFilePtr): cint {. importc: "fputs", header: "<stdio.h>", discardable.} -proc c_sprintf(buf, frmt: cstring): cint {. +proc c_sprintf*(buf, frmt: cstring): cint {. importc: "sprintf", header: "<stdio.h>", varargs, noSideEffect.} # we use it only in a way that cannot lead to security issues -proc c_malloc(size: csize): pointer {. +proc c_malloc*(size: csize): pointer {. importc: "malloc", header: "<stdlib.h>".} -proc c_free(p: pointer) {. +proc c_free*(p: pointer) {. importc: "free", header: "<stdlib.h>".} -proc c_realloc(p: pointer, newsize: csize): pointer {. +proc c_realloc*(p: pointer, newsize: csize): pointer {. importc: "realloc", header: "<stdlib.h>".} -proc c_fwrite(buf: pointer, size, n: csize, f: CFilePtr): cint {. +proc c_fwrite*(buf: pointer, size, n: csize, f: CFilePtr): cint {. importc: "fwrite", header: "<stdio.h>".} -proc rawWrite(f: CFilePtr, s: cstring) {.compilerproc, nonreloadable, inline.} = +proc rawWrite*(f: CFilePtr, s: cstring) {.compilerproc, nonreloadable, inline.} = # we cannot throw an exception here! discard c_fwrite(s, 1, s.len, f) diff --git a/lib/system/memory.nim b/lib/system/memory.nim index 318dffa2d..25c803360 100644 --- a/lib/system/memory.nim +++ b/lib/system/memory.nim @@ -1,6 +1,9 @@ const useLibC = not defined(nimNoLibc) -proc nimCopyMem(dest, source: pointer, size: Natural) {.compilerproc, inline.} = +when useLibC: + import ansi_c + +proc nimCopyMem*(dest, source: pointer, size: Natural) {.compilerproc, inline.} = when useLibC: c_memcpy(dest, source, size) else: @@ -11,7 +14,7 @@ proc nimCopyMem(dest, source: pointer, size: Natural) {.compilerproc, inline.} = d[i] = s[i] inc i -proc nimSetMem(a: pointer, v: cint, size: Natural) {.nonReloadable, inline.} = +proc nimSetMem*(a: pointer, v: cint, size: Natural) {.nonReloadable, inline.} = when useLibC: c_memset(a, v, size) else: @@ -22,10 +25,10 @@ proc nimSetMem(a: pointer, v: cint, size: Natural) {.nonReloadable, inline.} = a[i] = v inc i -proc nimZeroMem(p: pointer, size: Natural) {.compilerproc, nonReloadable, inline.} = +proc nimZeroMem*(p: pointer, size: Natural) {.compilerproc, nonReloadable, inline.} = nimSetMem(p, 0, size) -proc nimCmpMem(a, b: pointer, size: Natural): cint {.compilerproc, inline.} = +proc nimCmpMem*(a, b: pointer, size: Natural): cint {.compilerproc, inline.} = when useLibC: c_memcmp(a, b, size) else: @@ -37,7 +40,7 @@ proc nimCmpMem(a, b: pointer, size: Natural): cint {.compilerproc, inline.} = if d != 0: return d inc i -proc nimCStrLen(a: cstring): csize {.compilerproc, nonReloadable, inline.} = +proc nimCStrLen*(a: cstring): csize {.compilerproc, nonReloadable, inline.} = when useLibC: c_strlen(a) else: |