malloc.nim 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. {.push stackTrace: off.}
  2. proc allocImpl(size: Natural): pointer =
  3. c_malloc(size.csize_t)
  4. proc alloc0Impl(size: Natural): pointer =
  5. c_calloc(size.csize_t, 1)
  6. proc reallocImpl(p: pointer, newSize: Natural): pointer =
  7. c_realloc(p, newSize.csize_t)
  8. proc realloc0Impl(p: pointer, oldsize, newSize: Natural): pointer =
  9. result = realloc(p, newSize.csize_t)
  10. if newSize > oldSize:
  11. zeroMem(cast[pointer](cast[int](result) + oldSize), newSize - oldSize)
  12. proc deallocImpl(p: pointer) =
  13. c_free(p)
  14. # The shared allocators map on the regular ones
  15. proc allocSharedImpl(size: Natural): pointer =
  16. allocImpl(size)
  17. proc allocShared0Impl(size: Natural): pointer =
  18. alloc0Impl(size)
  19. proc reallocSharedImpl(p: pointer, newSize: Natural): pointer =
  20. reallocImpl(p, newSize)
  21. proc reallocShared0Impl(p: pointer, oldsize, newSize: Natural): pointer =
  22. realloc0Impl(p, oldSize, newSize)
  23. proc deallocSharedImpl(p: pointer) = deallocImpl(p)
  24. # Empty stubs for the GC
  25. proc GC_disable() = discard
  26. proc GC_enable() = discard
  27. when not defined(gcOrc):
  28. proc GC_fullCollect() = discard
  29. proc GC_enableMarkAndSweep() = discard
  30. proc GC_disableMarkAndSweep() = discard
  31. proc GC_setStrategy(strategy: GC_Strategy) = discard
  32. proc getOccupiedMem(): int = discard
  33. proc getFreeMem(): int = discard
  34. proc getTotalMem(): int = discard
  35. proc nimGC_setStackBottom(theStackBottom: pointer) = discard
  36. proc initGC() = discard
  37. proc newObjNoInit(typ: PNimType, size: int): pointer =
  38. result = alloc(size)
  39. proc growObj(old: pointer, newsize: int): pointer =
  40. result = realloc(old, newsize)
  41. proc nimGCref(p: pointer) {.compilerproc, inline.} = discard
  42. proc nimGCunref(p: pointer) {.compilerproc, inline.} = discard
  43. when not defined(gcDestructors):
  44. proc unsureAsgnRef(dest: PPointer, src: pointer) {.compilerproc, inline.} =
  45. dest[] = src
  46. proc asgnRef(dest: PPointer, src: pointer) {.compilerproc, inline.} =
  47. dest[] = src
  48. proc asgnRefNoCycle(dest: PPointer, src: pointer) {.compilerproc, inline,
  49. deprecated: "old compiler compat".} = asgnRef(dest, src)
  50. type
  51. MemRegion = object
  52. proc alloc(r: var MemRegion, size: int): pointer =
  53. result = alloc(size)
  54. proc alloc0Impl(r: var MemRegion, size: int): pointer =
  55. result = alloc0Impl(size)
  56. proc dealloc(r: var MemRegion, p: pointer) = dealloc(p)
  57. proc deallocOsPages(r: var MemRegion) = discard
  58. proc deallocOsPages() = discard
  59. {.pop.}