weakrefs.nim 1016 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. proc create: StrongObject =
  18. new(result, finalizer)
  19. result.id = gid
  20. valid.incl(gid)
  21. inc gid
  22. proc register(s: StrongObject): WeakObject =
  23. result.data = cast[ptr TMyObject](s)
  24. result.id = s.id
  25. proc access(w: WeakObject): StrongObject =
  26. ## returns nil if the object doesn't exist anymore
  27. if valid.contains(w.id):
  28. result = cast[StrongObject](w.data)
  29. proc main =
  30. var s: seq[WeakObject]
  31. newSeq(s, 10_000)
  32. for i in 0 .. s.high:
  33. s[i] = register(create())
  34. # test that we have at least 80% unreachable weak objects by now:
  35. when defined(gcMarkAndSweep):
  36. GC_fullcollect()
  37. var unreachable = 0
  38. for i in 0 .. s.high:
  39. if access(s[i]) == nil: inc unreachable
  40. echo unreachable > 8_000
  41. main()