runtime_v2.nim 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #[
  2. In this new runtime we simplify the object layouts a bit: The runtime type
  3. information is only accessed for the objects that have it and it's always
  4. at offset 0 then. The ``ref`` object header is independent from the
  5. runtime type and only contains a reference count.
  6. Object subtyping is checked via the generated 'name'. This should have
  7. comparable overhead to the old pointer chasing approach but has the benefit
  8. that it works across DLL boundaries.
  9. The generated name is a concatenation of the object names in the hierarchy
  10. so that a subtype check becomes a substring check. For example::
  11. type
  12. ObjectA = object of RootObj
  13. ObjectB = object of ObjectA
  14. ObjectA's ``name`` is "|ObjectA|RootObj|".
  15. ObjectB's ``name`` is "|ObjectB|ObjectA|RootObj|".
  16. Now to check for ``x of ObjectB`` we need to check
  17. for ``x.typ.name.hasSubstring("|ObjectB|")``. In the actual implementation,
  18. however, we could also use a
  19. hash of ``package & "." & module & "." & name`` to save space.
  20. ]#
  21. type
  22. RefHeader = object
  23. rc: int # the object header is now a single RC field.
  24. # we could remove it in non-debug builds but this seems
  25. # unwise.
  26. template `+!`(p: pointer, s: int): pointer =
  27. cast[pointer](cast[int](p) +% s)
  28. template `-!`(p: pointer, s: int): pointer =
  29. cast[pointer](cast[int](p) -% s)
  30. template head(p: pointer): ptr RefHeader =
  31. cast[ptr RefHeader](cast[int](p) -% sizeof(RefHeader))
  32. var allocs*: int
  33. proc nimNewObj(size: int): pointer {.compilerRtl.} =
  34. let s = size + sizeof(RefHeader)
  35. when defined(nimscript):
  36. discard
  37. elif defined(useMalloc):
  38. var orig = c_malloc(s)
  39. nimZeroMem(orig, s)
  40. result = orig +! sizeof(RefHeader)
  41. else:
  42. result = alloc0(s) +! sizeof(RefHeader)
  43. when hasThreadSupport:
  44. atomicInc allocs
  45. else:
  46. inc allocs
  47. proc nimDecWeakRef(p: pointer) {.compilerRtl.} =
  48. when hasThreadSupport:
  49. atomicDec head(p).rc
  50. else:
  51. dec head(p).rc
  52. proc nimIncWeakRef(p: pointer) {.compilerRtl.} =
  53. when hasThreadSupport:
  54. atomicInc head(p).rc
  55. else:
  56. inc head(p).rc
  57. proc nimRawDispose(p: pointer) {.compilerRtl.} =
  58. when not defined(nimscript):
  59. when hasThreadSupport:
  60. let hasDanglingRefs = atomicLoadN(addr head(p).rc, ATOMIC_RELAXED) != 0
  61. else:
  62. let hasDanglingRefs = head(p).rc != 0
  63. if hasDanglingRefs:
  64. cstderr.rawWrite "[FATAL] dangling references exist\n"
  65. quit 1
  66. when defined(useMalloc):
  67. c_free(p -! sizeof(RefHeader))
  68. else:
  69. dealloc(p -! sizeof(RefHeader))
  70. if allocs > 0:
  71. when hasThreadSupport:
  72. discard atomicDec(allocs)
  73. else:
  74. dec allocs
  75. else:
  76. cstderr.rawWrite "[FATAL] unpaired dealloc\n"
  77. quit 1
  78. template dispose*[T](x: owned(ref T)) = nimRawDispose(cast[pointer](x))
  79. #proc dispose*(x: pointer) = nimRawDispose(x)
  80. proc nimDestroyAndDispose(p: pointer) {.compilerRtl.} =
  81. let d = cast[ptr PNimType](p)[].destructor
  82. if d != nil: cast[DestructorProc](d)(p)
  83. when false:
  84. cstderr.rawWrite cast[ptr PNimType](p)[].name
  85. cstderr.rawWrite "\n"
  86. if d == nil:
  87. cstderr.rawWrite "bah, nil\n"
  88. else:
  89. cstderr.rawWrite "has destructor!\n"
  90. nimRawDispose(p)
  91. proc isObj(obj: PNimType, subclass: cstring): bool {.compilerproc.} =
  92. proc strstr(s, sub: cstring): cstring {.header: "<string.h>", importc.}
  93. result = strstr(obj.name, subclass) != nil
  94. proc chckObj(obj: PNimType, subclass: cstring) {.compilerproc.} =
  95. # checks if obj is of type subclass:
  96. if not isObj(obj, subclass): sysFatal(ObjectConversionError, "invalid object conversion")