weakrefs.nim 1.1 KB

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