malloc.nim 2.6 KB

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