gc_interface.nim 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. # ----------------- GC interface ---------------------------------------------
  2. const
  3. usesDestructors = defined(gcDestructors) or defined(gcHooks)
  4. when not usesDestructors:
  5. {.pragma: nodestroy.}
  6. when hasAlloc:
  7. type
  8. GC_Strategy* = enum ## The strategy the GC should use for the application.
  9. gcThroughput, ## optimize for throughput
  10. gcResponsiveness, ## optimize for responsiveness (default)
  11. gcOptimizeTime, ## optimize for speed
  12. gcOptimizeSpace ## optimize for memory footprint
  13. when hasAlloc and not defined(js) and not usesDestructors:
  14. proc GC_disable*() {.rtl, inl, benign, raises: [].}
  15. ## Disables the GC. If called `n` times, `n` calls to `GC_enable`
  16. ## are needed to reactivate the GC.
  17. ##
  18. ## Note that in most circumstances one should only disable
  19. ## the mark and sweep phase with
  20. ## `GC_disableMarkAndSweep <#GC_disableMarkAndSweep>`_.
  21. proc GC_enable*() {.rtl, inl, benign, raises: [].}
  22. ## Enables the GC again.
  23. proc GC_fullCollect*() {.rtl, benign.}
  24. ## Forces a full garbage collection pass.
  25. ## Ordinary code does not need to call this (and should not).
  26. proc GC_enableMarkAndSweep*() {.rtl, benign.}
  27. proc GC_disableMarkAndSweep*() {.rtl, benign.}
  28. ## The current implementation uses a reference counting garbage collector
  29. ## with a seldomly run mark and sweep phase to free cycles. The mark and
  30. ## sweep phase may take a long time and is not needed if the application
  31. ## does not create cycles. Thus the mark and sweep phase can be deactivated
  32. ## and activated separately from the rest of the GC.
  33. proc GC_getStatistics*(): string {.rtl, benign.}
  34. ## Returns an informative string about the GC's activity. This may be useful
  35. ## for tweaking.
  36. proc GC_ref*[T](x: ref T) {.magic: "GCref", benign.}
  37. proc GC_ref*[T](x: seq[T]) {.magic: "GCref", benign.}
  38. proc GC_ref*(x: string) {.magic: "GCref", benign.}
  39. ## Marks the object `x` as referenced, so that it will not be freed until
  40. ## it is unmarked via `GC_unref`.
  41. ## If called n-times for the same object `x`,
  42. ## n calls to `GC_unref` are needed to unmark `x`.
  43. proc GC_unref*[T](x: ref T) {.magic: "GCunref", benign.}
  44. proc GC_unref*[T](x: seq[T]) {.magic: "GCunref", benign.}
  45. proc GC_unref*(x: string) {.magic: "GCunref", benign.}
  46. ## See the documentation of `GC_ref <#GC_ref,string>`_.
  47. proc nimGC_setStackBottom*(theStackBottom: pointer) {.compilerRtl, noinline, benign, raises: [].}
  48. ## Expands operating GC stack range to `theStackBottom`. Does nothing
  49. ## if current stack bottom is already lower than `theStackBottom`.
  50. when hasAlloc and defined(js):
  51. template GC_disable* =
  52. {.warning: "GC_disable is a no-op in JavaScript".}
  53. template GC_enable* =
  54. {.warning: "GC_enable is a no-op in JavaScript".}
  55. template GC_fullCollect* =
  56. {.warning: "GC_fullCollect is a no-op in JavaScript".}
  57. template GC_setStrategy* =
  58. {.warning: "GC_setStrategy is a no-op in JavaScript".}
  59. template GC_enableMarkAndSweep* =
  60. {.warning: "GC_enableMarkAndSweep is a no-op in JavaScript".}
  61. template GC_disableMarkAndSweep* =
  62. {.warning: "GC_disableMarkAndSweep is a no-op in JavaScript".}
  63. template GC_ref*[T](x: ref T) =
  64. {.warning: "GC_ref is a no-op in JavaScript".}
  65. template GC_ref*[T](x: seq[T]) =
  66. {.warning: "GC_ref is a no-op in JavaScript".}
  67. template GC_ref*(x: string) =
  68. {.warning: "GC_ref is a no-op in JavaScript".}
  69. template GC_unref*[T](x: ref T) =
  70. {.warning: "GC_unref is a no-op in JavaScript".}
  71. template GC_unref*[T](x: seq[T]) =
  72. {.warning: "GC_unref is a no-op in JavaScript".}
  73. template GC_unref*(x: string) =
  74. {.warning: "GC_unref is a no-op in JavaScript".}
  75. template GC_getStatistics*(): string =
  76. {.warning: "GC_getStatistics is a no-op in JavaScript".}
  77. ""