ref 1.0.2 → 1.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,4 +1,4 @@
1
- Copyright (c) 2011 Brian Durand
1
+ Copyright (c) 2013 Brian Durand
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
@@ -33,3 +33,8 @@ Ruby does come with the WeakRef class in the standard library. However, there ar
33
33
  2. YARV 1.9 - WeakRef is unsafe to use because the garbage collector can run in a different system thread than a thread allocating memory. This exposes a bug where a WeakRef may end up pointing to a completely different object than it originally referenced.
34
34
  3. Jruby and IronRuby - Jruby and IronRuby using the Ruby 1.8 libraries suffers from the same performance issue with the Delegator class. Furthermore, these VM's don't implement the method used to load an object from the heap using an object id and so cannot use a pure Ruby method to implement weak references.
35
35
  4. Rubinius - Rubinius implements WeakRef with a lighter weight version of delegation and works very well.
36
+ 5. MRI Ruby 2.0 has a good implementation of WeakRef.
37
+
38
+ = BasicObject
39
+
40
+ Not that weak references will not work with MRI/REE 1.8 or YARV 1.9. References will be created, but the objects will never be stored so the reference object will always treat the object as if it is always garbage collected. BasicObject does not implement the necessary methods to maintain the reference.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.2
1
+ 1.0.3
data/lib/ref.rb CHANGED
@@ -1,15 +1,9 @@
1
1
  module Ref
2
- autoload :AbstractReferenceValueMap, File.join(File.dirname(__FILE__), "ref", "abstract_reference_value_map.rb")
3
- autoload :AbstractReferenceKeyMap, File.join(File.dirname(__FILE__), "ref", "abstract_reference_key_map.rb")
4
- autoload :Mock, File.join(File.dirname(__FILE__), "ref", "mock.rb")
5
- autoload :Reference, File.join(File.dirname(__FILE__), "ref", "reference.rb")
6
- autoload :ReferenceQueue, File.join(File.dirname(__FILE__), "ref", "reference_queue.rb")
7
- autoload :SafeMonitor, File.join(File.dirname(__FILE__), "ref", "safe_monitor.rb")
8
- autoload :SoftKeyMap, File.join(File.dirname(__FILE__), "ref", "soft_key_map.rb")
9
- autoload :SoftValueMap, File.join(File.dirname(__FILE__), "ref", "soft_value_map.rb")
10
- autoload :StrongReference, File.join(File.dirname(__FILE__), "ref", "strong_reference.rb")
11
- autoload :WeakKeyMap, File.join(File.dirname(__FILE__), "ref", "weak_key_map.rb")
12
- autoload :WeakValueMap, File.join(File.dirname(__FILE__), "ref", "weak_value_map.rb")
2
+ require File.join(File.dirname(__FILE__), "ref", "abstract_reference_value_map.rb")
3
+ require File.join(File.dirname(__FILE__), "ref", "abstract_reference_key_map.rb")
4
+ require File.join(File.dirname(__FILE__), "ref", "reference.rb")
5
+ require File.join(File.dirname(__FILE__), "ref", "reference_queue.rb")
6
+ require File.join(File.dirname(__FILE__), "ref", "safe_monitor.rb")
13
7
 
14
8
  # Set the best implementation for weak references based on the runtime.
15
9
  if defined?(RUBY_PLATFORM) && RUBY_PLATFORM == 'java'
@@ -21,19 +15,31 @@ module Ref
21
15
  $LOAD_PATH.shift if $LOAD_PATH.first == File.dirname(__FILE__)
22
16
  end
23
17
  else
24
- autoload :SoftReference, File.join(File.dirname(__FILE__), "ref", "soft_reference.rb")
18
+ require File.join(File.dirname(__FILE__), "ref", "soft_reference.rb")
25
19
  if defined?(RUBY_ENGINE) && RUBY_ENGINE == 'ironruby'
26
20
  # IronRuby has it's own implementation of weak references.
27
- autoload :WeakReference, File.join(File.dirname(__FILE__), "ref", "weak_reference", "iron_ruby.rb")
21
+ require File.join(File.dirname(__FILE__), "ref", "weak_reference", "iron_ruby.rb")
28
22
  elsif defined?(RUBY_ENGINE) && RUBY_ENGINE == 'rbx'
29
23
  # If using Rubinius set the implementation to use WeakRef since it is very efficient and using finalizers is not.
30
- autoload :WeakReference, File.join(File.dirname(__FILE__), "ref", "weak_reference", "weak_ref.rb")
31
- elsif defined?(ObjectSpace._id2ref)
24
+ require File.join(File.dirname(__FILE__), "ref", "weak_reference", "weak_ref.rb")
25
+ elsif defined?(::ObjectSpace::WeakMap)
26
+ # Ruby 2.0 has a working implementation of weakref.rb backed by the new ObjectSpace::WeakMap
27
+ require File.join(File.dirname(__FILE__), "ref", "weak_reference", "weak_ref.rb")
28
+ elsif defined?(::ObjectSpace._id2ref)
32
29
  # If ObjectSpace can lookup objects from their object_id, then use the pure ruby implementation.
33
- autoload :WeakReference, File.join(File.dirname(__FILE__), "ref", "weak_reference", "pure_ruby.rb")
30
+ require File.join(File.dirname(__FILE__), "ref", "weak_reference", "pure_ruby.rb")
34
31
  else
35
32
  # Otherwise, wrap the standard library WeakRef class
36
- autoload :WeakReference, File.join(File.dirname(__FILE__), "ref", "weak_reference", "weak_ref.rb")
33
+ require File.join(File.dirname(__FILE__), "ref", "weak_reference", "weak_ref.rb")
37
34
  end
38
35
  end
36
+
37
+ require File.join(File.dirname(__FILE__), "ref", "soft_key_map.rb")
38
+ require File.join(File.dirname(__FILE__), "ref", "soft_value_map.rb")
39
+ require File.join(File.dirname(__FILE__), "ref", "strong_reference.rb")
40
+ require File.join(File.dirname(__FILE__), "ref", "weak_key_map.rb")
41
+ require File.join(File.dirname(__FILE__), "ref", "weak_value_map.rb")
42
+
43
+ # Used for testing
44
+ autoload :Mock, File.join(File.dirname(__FILE__), "ref", "mock.rb")
39
45
  end
@@ -1,8 +1,8 @@
1
1
  module Ref
2
2
  # This is a pure ruby implementation of a weak reference. It is much more
3
- # efficient than the bundled WeakRef implementation because it does not
3
+ # efficient than the WeakRef implementation bundled in MRI 1.8 and 1.9
4
4
  # subclass Delegator which is very heavy to instantiate and utilizes a
5
- # fair amount of memory under Ruby 1.8.
5
+ # because it does not fair amount of memory under Ruby 1.8.
6
6
  class WeakReference < Reference
7
7
 
8
8
  class ReferencePointer
@@ -26,12 +26,14 @@ module Ref
26
26
  private
27
27
  # Verify that the object is the same one originally set for the weak reference.
28
28
  def verify_backreferences(obj) #:nodoc:
29
+ return nil unless supports_backreference?(obj)
29
30
  backreferences = obj.instance_variable_get(:@__weak_backreferences__) if obj.instance_variable_defined?(:@__weak_backreferences__)
30
31
  backreferences && backreferences.include?(object_id)
31
32
  end
32
33
 
33
34
  # Add a backreference to the object.
34
35
  def add_backreference(obj) #:nodoc:
36
+ return unless supports_backreference?(obj)
35
37
  backreferences = obj.instance_variable_get(:@__weak_backreferences__) if obj.instance_variable_defined?(:@__weak_backreferences__)
36
38
  unless backreferences
37
39
  backreferences = []
@@ -42,12 +44,17 @@ module Ref
42
44
 
43
45
  # Remove backreferences from the object.
44
46
  def remove_backreference(obj) #:nodoc:
47
+ return unless supports_backreference?(obj)
45
48
  backreferences = obj.instance_variable_get(:@__weak_backreferences__) if obj.instance_variable_defined?(:@__weak_backreferences__)
46
49
  if backreferences
47
50
  backreferences.dup.delete(object_id)
48
51
  obj.send(:remove_instance_variable, :@__weak_backreferences__) if backreferences.empty?
49
52
  end
50
53
  end
54
+
55
+ def supports_backreference?(obj)
56
+ obj.respond_to?(:instance_variable_get) && obj.respond_to?(:instance_variable_defined?)
57
+ end
51
58
  end
52
59
 
53
60
  @@weak_references = {}
@@ -0,0 +1,856 @@
1
+ !RBIX
2
+ 12529030924842180271
3
+ x
4
+ M
5
+ 1
6
+ n
7
+ n
8
+ x
9
+ 10
10
+ __script__
11
+ i
12
+ 54
13
+ 5
14
+ 45
15
+ 0
16
+ 1
17
+ 7
18
+ 2
19
+ 64
20
+ 65
21
+ 49
22
+ 3
23
+ 0
24
+ 49
25
+ 4
26
+ 2
27
+ 47
28
+ 49
29
+ 5
30
+ 1
31
+ 15
32
+ 99
33
+ 7
34
+ 6
35
+ 45
36
+ 7
37
+ 8
38
+ 43
39
+ 9
40
+ 43
41
+ 10
42
+ 65
43
+ 49
44
+ 11
45
+ 3
46
+ 13
47
+ 99
48
+ 12
49
+ 7
50
+ 12
51
+ 12
52
+ 7
53
+ 13
54
+ 12
55
+ 65
56
+ 12
57
+ 49
58
+ 14
59
+ 4
60
+ 15
61
+ 49
62
+ 12
63
+ 0
64
+ 15
65
+ 2
66
+ 11
67
+ I
68
+ 6
69
+ I
70
+ 0
71
+ I
72
+ 0
73
+ I
74
+ 0
75
+ n
76
+ p
77
+ 15
78
+ x
79
+ 4
80
+ File
81
+ n
82
+ s
83
+ 14
84
+ ../test_helper
85
+ x
86
+ 11
87
+ active_path
88
+ x
89
+ 11
90
+ expand_path
91
+ x
92
+ 7
93
+ require
94
+ x
95
+ 8
96
+ TestMock
97
+ x
98
+ 4
99
+ Test
100
+ n
101
+ x
102
+ 4
103
+ Unit
104
+ x
105
+ 8
106
+ TestCase
107
+ x
108
+ 10
109
+ open_class
110
+ x
111
+ 14
112
+ __class_init__
113
+ M
114
+ 1
115
+ n
116
+ n
117
+ x
118
+ 8
119
+ TestMock
120
+ i
121
+ 30
122
+ 5
123
+ 66
124
+ 99
125
+ 7
126
+ 0
127
+ 7
128
+ 1
129
+ 65
130
+ 67
131
+ 49
132
+ 2
133
+ 0
134
+ 49
135
+ 3
136
+ 4
137
+ 15
138
+ 99
139
+ 7
140
+ 4
141
+ 7
142
+ 5
143
+ 65
144
+ 67
145
+ 49
146
+ 2
147
+ 0
148
+ 49
149
+ 3
150
+ 4
151
+ 11
152
+ I
153
+ 5
154
+ I
155
+ 0
156
+ I
157
+ 0
158
+ I
159
+ 0
160
+ n
161
+ p
162
+ 6
163
+ x
164
+ 21
165
+ test_gc_with_argument
166
+ M
167
+ 1
168
+ n
169
+ n
170
+ x
171
+ 21
172
+ test_gc_with_argument
173
+ i
174
+ 11
175
+ 45
176
+ 0
177
+ 1
178
+ 43
179
+ 2
180
+ 56
181
+ 3
182
+ 50
183
+ 4
184
+ 0
185
+ 11
186
+ I
187
+ 2
188
+ I
189
+ 0
190
+ I
191
+ 0
192
+ I
193
+ 0
194
+ n
195
+ p
196
+ 5
197
+ x
198
+ 3
199
+ Ref
200
+ n
201
+ x
202
+ 4
203
+ Mock
204
+ M
205
+ 1
206
+ p
207
+ 2
208
+ x
209
+ 9
210
+ for_block
211
+ t
212
+ n
213
+ x
214
+ 21
215
+ test_gc_with_argument
216
+ i
217
+ 155
218
+ 45
219
+ 0
220
+ 1
221
+ 13
222
+ 71
223
+ 2
224
+ 47
225
+ 9
226
+ 21
227
+ 47
228
+ 49
229
+ 3
230
+ 0
231
+ 13
232
+ 47
233
+ 49
234
+ 4
235
+ 0
236
+ 15
237
+ 8
238
+ 24
239
+ 49
240
+ 2
241
+ 0
242
+ 19
243
+ 0
244
+ 15
245
+ 45
246
+ 0
247
+ 5
248
+ 13
249
+ 71
250
+ 2
251
+ 47
252
+ 9
253
+ 48
254
+ 47
255
+ 49
256
+ 3
257
+ 0
258
+ 13
259
+ 47
260
+ 49
261
+ 4
262
+ 0
263
+ 15
264
+ 8
265
+ 51
266
+ 49
267
+ 2
268
+ 0
269
+ 19
270
+ 1
271
+ 15
272
+ 45
273
+ 6
274
+ 7
275
+ 43
276
+ 8
277
+ 13
278
+ 71
279
+ 2
280
+ 47
281
+ 9
282
+ 79
283
+ 47
284
+ 49
285
+ 3
286
+ 0
287
+ 13
288
+ 20
289
+ 0
290
+ 47
291
+ 49
292
+ 4
293
+ 1
294
+ 15
295
+ 8
296
+ 84
297
+ 20
298
+ 0
299
+ 49
300
+ 2
301
+ 1
302
+ 19
303
+ 2
304
+ 15
305
+ 45
306
+ 6
307
+ 9
308
+ 43
309
+ 8
310
+ 13
311
+ 71
312
+ 2
313
+ 47
314
+ 9
315
+ 112
316
+ 47
317
+ 49
318
+ 3
319
+ 0
320
+ 13
321
+ 20
322
+ 1
323
+ 47
324
+ 49
325
+ 4
326
+ 1
327
+ 15
328
+ 8
329
+ 117
330
+ 20
331
+ 1
332
+ 49
333
+ 2
334
+ 1
335
+ 19
336
+ 3
337
+ 15
338
+ 45
339
+ 6
340
+ 10
341
+ 43
342
+ 11
343
+ 20
344
+ 0
345
+ 49
346
+ 12
347
+ 1
348
+ 15
349
+ 5
350
+ 20
351
+ 2
352
+ 49
353
+ 13
354
+ 0
355
+ 47
356
+ 49
357
+ 14
358
+ 1
359
+ 15
360
+ 5
361
+ 20
362
+ 3
363
+ 49
364
+ 13
365
+ 0
366
+ 20
367
+ 1
368
+ 47
369
+ 49
370
+ 15
371
+ 2
372
+ 11
373
+ I
374
+ 8
375
+ I
376
+ 4
377
+ I
378
+ 0
379
+ I
380
+ 0
381
+ I
382
+ -2
383
+ p
384
+ 16
385
+ x
386
+ 6
387
+ Object
388
+ n
389
+ x
390
+ 3
391
+ new
392
+ x
393
+ 8
394
+ allocate
395
+ x
396
+ 10
397
+ initialize
398
+ n
399
+ x
400
+ 3
401
+ Ref
402
+ n
403
+ x
404
+ 13
405
+ WeakReference
406
+ n
407
+ n
408
+ x
409
+ 4
410
+ Mock
411
+ x
412
+ 2
413
+ gc
414
+ x
415
+ 6
416
+ object
417
+ x
418
+ 10
419
+ assert_nil
420
+ x
421
+ 12
422
+ assert_equal
423
+ p
424
+ 15
425
+ I
426
+ 0
427
+ I
428
+ 6
429
+ I
430
+ 1b
431
+ I
432
+ 7
433
+ I
434
+ 36
435
+ I
436
+ 9
437
+ I
438
+ 57
439
+ I
440
+ a
441
+ I
442
+ 78
443
+ I
444
+ c
445
+ I
446
+ 83
447
+ I
448
+ e
449
+ I
450
+ 8e
451
+ I
452
+ f
453
+ I
454
+ 9b
455
+ x
456
+ 49
457
+ /Users/bdurand/dev/projects/ref/test/mock_test.rb
458
+ p
459
+ 4
460
+ x
461
+ 5
462
+ obj_1
463
+ x
464
+ 5
465
+ obj_2
466
+ x
467
+ 5
468
+ ref_1
469
+ x
470
+ 5
471
+ ref_2
472
+ x
473
+ 3
474
+ use
475
+ p
476
+ 5
477
+ I
478
+ -1
479
+ I
480
+ 4
481
+ I
482
+ 0
483
+ I
484
+ 5
485
+ I
486
+ b
487
+ x
488
+ 49
489
+ /Users/bdurand/dev/projects/ref/test/mock_test.rb
490
+ p
491
+ 0
492
+ x
493
+ 17
494
+ method_visibility
495
+ x
496
+ 15
497
+ add_defn_method
498
+ x
499
+ 24
500
+ test_gc_with_no_argument
501
+ M
502
+ 1
503
+ n
504
+ n
505
+ x
506
+ 24
507
+ test_gc_with_no_argument
508
+ i
509
+ 11
510
+ 45
511
+ 0
512
+ 1
513
+ 43
514
+ 2
515
+ 56
516
+ 3
517
+ 50
518
+ 4
519
+ 0
520
+ 11
521
+ I
522
+ 2
523
+ I
524
+ 0
525
+ I
526
+ 0
527
+ I
528
+ 0
529
+ n
530
+ p
531
+ 5
532
+ x
533
+ 3
534
+ Ref
535
+ n
536
+ x
537
+ 4
538
+ Mock
539
+ M
540
+ 1
541
+ p
542
+ 2
543
+ x
544
+ 9
545
+ for_block
546
+ t
547
+ n
548
+ x
549
+ 24
550
+ test_gc_with_no_argument
551
+ i
552
+ 151
553
+ 45
554
+ 0
555
+ 1
556
+ 13
557
+ 71
558
+ 2
559
+ 47
560
+ 9
561
+ 21
562
+ 47
563
+ 49
564
+ 3
565
+ 0
566
+ 13
567
+ 47
568
+ 49
569
+ 4
570
+ 0
571
+ 15
572
+ 8
573
+ 24
574
+ 49
575
+ 2
576
+ 0
577
+ 19
578
+ 0
579
+ 15
580
+ 45
581
+ 0
582
+ 5
583
+ 13
584
+ 71
585
+ 2
586
+ 47
587
+ 9
588
+ 48
589
+ 47
590
+ 49
591
+ 3
592
+ 0
593
+ 13
594
+ 47
595
+ 49
596
+ 4
597
+ 0
598
+ 15
599
+ 8
600
+ 51
601
+ 49
602
+ 2
603
+ 0
604
+ 19
605
+ 1
606
+ 15
607
+ 45
608
+ 6
609
+ 7
610
+ 43
611
+ 8
612
+ 13
613
+ 71
614
+ 2
615
+ 47
616
+ 9
617
+ 79
618
+ 47
619
+ 49
620
+ 3
621
+ 0
622
+ 13
623
+ 20
624
+ 0
625
+ 47
626
+ 49
627
+ 4
628
+ 1
629
+ 15
630
+ 8
631
+ 84
632
+ 20
633
+ 0
634
+ 49
635
+ 2
636
+ 1
637
+ 19
638
+ 2
639
+ 15
640
+ 45
641
+ 6
642
+ 9
643
+ 43
644
+ 8
645
+ 13
646
+ 71
647
+ 2
648
+ 47
649
+ 9
650
+ 112
651
+ 47
652
+ 49
653
+ 3
654
+ 0
655
+ 13
656
+ 20
657
+ 1
658
+ 47
659
+ 49
660
+ 4
661
+ 1
662
+ 15
663
+ 8
664
+ 117
665
+ 20
666
+ 1
667
+ 49
668
+ 2
669
+ 1
670
+ 19
671
+ 3
672
+ 15
673
+ 45
674
+ 6
675
+ 10
676
+ 43
677
+ 11
678
+ 49
679
+ 12
680
+ 0
681
+ 15
682
+ 5
683
+ 20
684
+ 2
685
+ 49
686
+ 13
687
+ 0
688
+ 47
689
+ 49
690
+ 14
691
+ 1
692
+ 15
693
+ 5
694
+ 20
695
+ 3
696
+ 49
697
+ 13
698
+ 0
699
+ 47
700
+ 49
701
+ 14
702
+ 1
703
+ 11
704
+ I
705
+ 8
706
+ I
707
+ 4
708
+ I
709
+ 0
710
+ I
711
+ 0
712
+ I
713
+ -2
714
+ p
715
+ 15
716
+ x
717
+ 6
718
+ Object
719
+ n
720
+ x
721
+ 3
722
+ new
723
+ x
724
+ 8
725
+ allocate
726
+ x
727
+ 10
728
+ initialize
729
+ n
730
+ x
731
+ 3
732
+ Ref
733
+ n
734
+ x
735
+ 13
736
+ WeakReference
737
+ n
738
+ n
739
+ x
740
+ 4
741
+ Mock
742
+ x
743
+ 2
744
+ gc
745
+ x
746
+ 6
747
+ object
748
+ x
749
+ 10
750
+ assert_nil
751
+ p
752
+ 15
753
+ I
754
+ 0
755
+ I
756
+ 15
757
+ I
758
+ 1b
759
+ I
760
+ 16
761
+ I
762
+ 36
763
+ I
764
+ 18
765
+ I
766
+ 57
767
+ I
768
+ 19
769
+ I
770
+ 78
771
+ I
772
+ 1b
773
+ I
774
+ 81
775
+ I
776
+ 1d
777
+ I
778
+ 8c
779
+ I
780
+ 1e
781
+ I
782
+ 97
783
+ x
784
+ 49
785
+ /Users/bdurand/dev/projects/ref/test/mock_test.rb
786
+ p
787
+ 4
788
+ x
789
+ 5
790
+ obj_1
791
+ x
792
+ 5
793
+ obj_2
794
+ x
795
+ 5
796
+ ref_1
797
+ x
798
+ 5
799
+ ref_2
800
+ x
801
+ 3
802
+ use
803
+ p
804
+ 5
805
+ I
806
+ -1
807
+ I
808
+ 13
809
+ I
810
+ 0
811
+ I
812
+ 14
813
+ I
814
+ b
815
+ x
816
+ 49
817
+ /Users/bdurand/dev/projects/ref/test/mock_test.rb
818
+ p
819
+ 0
820
+ p
821
+ 5
822
+ I
823
+ 2
824
+ I
825
+ 4
826
+ I
827
+ 10
828
+ I
829
+ 13
830
+ I
831
+ 1e
832
+ x
833
+ 49
834
+ /Users/bdurand/dev/projects/ref/test/mock_test.rb
835
+ p
836
+ 0
837
+ x
838
+ 13
839
+ attach_method
840
+ p
841
+ 5
842
+ I
843
+ 0
844
+ I
845
+ 1
846
+ I
847
+ 13
848
+ I
849
+ 3
850
+ I
851
+ 36
852
+ x
853
+ 49
854
+ /Users/bdurand/dev/projects/ref/test/mock_test.rb
855
+ p
856
+ 0