@@ -28,10 +28,10 @@ void msgpack_packer_ext_registry_static_init(void)
2828void msgpack_packer_ext_registry_static_destroy (void )
2929{ }
3030
31- void msgpack_packer_ext_registry_init (msgpack_packer_ext_registry_t * pkrg )
31+ void msgpack_packer_ext_registry_init (VALUE owner , msgpack_packer_ext_registry_t * pkrg )
3232{
33- pkrg -> hash = Qnil ;
34- pkrg -> cache = Qnil ;
33+ RB_OBJ_WRITE ( owner , & pkrg -> hash , Qnil ) ;
34+ RB_OBJ_WRITE ( owner , & pkrg -> cache , Qnil ) ;
3535}
3636
3737void msgpack_packer_ext_registry_mark (msgpack_packer_ext_registry_t * pkrg )
@@ -40,32 +40,46 @@ void msgpack_packer_ext_registry_mark(msgpack_packer_ext_registry_t* pkrg)
4040 rb_gc_mark (pkrg -> cache );
4141}
4242
43- void msgpack_packer_ext_registry_dup ( msgpack_packer_ext_registry_t * src ,
43+ void msgpack_packer_ext_registry_borrow ( VALUE owner , msgpack_packer_ext_registry_t * src ,
4444 msgpack_packer_ext_registry_t * dst )
4545{
46- if (RTEST (src -> hash ) && !rb_obj_frozen_p (src -> hash )) {
47- dst -> hash = rb_hash_dup (src -> hash );
48- dst -> cache = RTEST (src -> cache ) ? rb_hash_dup (src -> cache ) : Qnil ;
46+ if (RTEST (src -> hash )) {
47+ if (rb_obj_frozen_p (src -> hash )) {
48+ // If the type registry is frozen we can safely share it, and share the cache as well.
49+ RB_OBJ_WRITE (owner , & dst -> hash , src -> hash );
50+ RB_OBJ_WRITE (owner , & dst -> cache , src -> cache );
51+ } else {
52+ RB_OBJ_WRITE (owner , & dst -> hash , rb_hash_dup (src -> hash ));
53+ RB_OBJ_WRITE (owner , & dst -> cache , NIL_P (src -> cache ) ? Qnil : rb_hash_dup (src -> cache ));
54+ }
4955 } else {
50- // If the type registry is frozen we can safely share it, and share the cache as well.
51- dst -> hash = src -> hash ;
52- dst -> cache = src -> cache ;
56+ RB_OBJ_WRITE (owner , & dst -> hash , Qnil );
57+ RB_OBJ_WRITE (owner , & dst -> cache , Qnil );
5358 }
5459}
5560
56- VALUE msgpack_packer_ext_registry_put (msgpack_packer_ext_registry_t * pkrg ,
61+ void msgpack_packer_ext_registry_dup (VALUE owner , msgpack_packer_ext_registry_t * src ,
62+ msgpack_packer_ext_registry_t * dst )
63+ {
64+ RB_OBJ_WRITE (owner , & dst -> hash , NIL_P (src -> hash ) ? Qnil : rb_hash_dup (src -> hash ));
65+ RB_OBJ_WRITE (owner , & dst -> cache , NIL_P (src -> cache ) ? Qnil : rb_hash_dup (src -> cache ));
66+ }
67+
68+ void msgpack_packer_ext_registry_put (VALUE owner , msgpack_packer_ext_registry_t * pkrg ,
5769 VALUE ext_module , int ext_type , int flags , VALUE proc , VALUE arg )
5870{
59- if (! RTEST (pkrg -> hash )) {
60- pkrg -> hash = rb_hash_new ();
71+ if ( NIL_P (pkrg -> hash )) {
72+ RB_OBJ_WRITE ( owner , & pkrg -> hash , rb_hash_new () );
6173 }
6274
63- if (RTEST (pkrg -> cache )) {
75+ if (NIL_P (pkrg -> cache )) {
76+ RB_OBJ_WRITE (owner , & pkrg -> cache , rb_hash_new ());
77+ } else {
6478 /* clear lookup cache not to miss added type */
6579 rb_hash_clear (pkrg -> cache );
6680 }
6781
6882 // TODO: Ruby embeded array limit is 3, merging `proc` and `arg` would be good.
6983 VALUE entry = rb_ary_new3 (4 , INT2FIX (ext_type ), proc , arg , INT2FIX (flags ));
70- return rb_hash_aset (pkrg -> hash , ext_module , entry );
84+ rb_hash_aset (pkrg -> hash , ext_module , entry );
7185}
0 commit comments