weakrefs.nim 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. discard """
  2. output: "true"
  3. """
  4. import intsets
  5. type
  6. TMyObject = object
  7. id: int
  8. StrongObject = ref TMyObject
  9. WeakObject = object
  10. id: int
  11. data: ptr TMyObject
  12. var
  13. gid: int # for id generation
  14. valid = initIntSet()
  15. proc finalizer(x: StrongObject) =
  16. valid.excl(x.id)
  17. when defined(gcDestructors):
  18. proc `=destroy`(x: var TMyObject) =
  19. valid.excl(x.id)
  20. proc create: StrongObject =
  21. when defined(gcDestructors):
  22. new(result)
  23. else:
  24. new(result, finalizer)
  25. result.id = gid
  26. valid.incl(gid)
  27. inc gid
  28. proc register(s: StrongObject): WeakObject =
  29. result.data = cast[ptr TMyObject](s)
  30. result.id = s.id
  31. proc access(w: WeakObject): StrongObject =
  32. ## returns nil if the object doesn't exist anymore
  33. if valid.contains(w.id):
  34. result = cast[StrongObject](w.data)
  35. proc main =
  36. var s: seq[WeakObject]
  37. newSeq(s, 10_000)
  38. for i in 0 .. s.high:
  39. s[i] = register(create())
  40. # test that we have at least 80% unreachable weak objects by now:
  41. when defined(gcMarkAndSweep):
  42. GC_fullcollect()
  43. var unreachable = 0
  44. for i in 0 .. s.high:
  45. if access(s[i]) == nil: inc unreachable
  46. echo unreachable > 8_000
  47. main()