@@ -79,12 +79,9 @@ void msgpack_unpacker_static_destroy(void)
79
79
80
80
#define HEAD_BYTE_REQUIRED 0xc1
81
81
82
- static inline msgpack_unpacker_stack_t * _msgpack_unpacker_new_stack (void ) {
83
- msgpack_unpacker_stack_t * stack = ZALLOC (msgpack_unpacker_stack_t );
82
+ static inline void _msgpack_unpacker_stack_init (msgpack_unpacker_stack_t * stack ) {
84
83
stack -> capacity = MSGPACK_UNPACKER_STACK_CAPACITY ;
85
84
stack -> data = msgpack_rmem_alloc (& s_stack_rmem );
86
- /*memset(uk->stack, 0, MSGPACK_UNPACKER_STACK_CAPACITY);*/
87
- return stack ;
88
85
}
89
86
90
87
void _msgpack_unpacker_init (msgpack_unpacker_t * uk )
@@ -96,45 +93,40 @@ void _msgpack_unpacker_init(msgpack_unpacker_t* uk)
96
93
uk -> last_object = Qnil ;
97
94
uk -> reading_raw = Qnil ;
98
95
99
- uk -> stack = _msgpack_unpacker_new_stack ( );
96
+ _msgpack_unpacker_stack_init ( & uk -> stack );
100
97
}
101
98
102
99
static inline void _msgpack_unpacker_free_stack (msgpack_unpacker_stack_t * stack ) {
103
100
if (!msgpack_rmem_free (& s_stack_rmem , stack -> data )) {
104
101
rb_bug ("Failed to free an rmem pointer, memory leak?" );
105
102
}
106
- xfree (stack );
103
+ stack -> data = NULL ;
104
+ stack -> depth = 0 ;
107
105
}
108
106
109
107
void _msgpack_unpacker_destroy (msgpack_unpacker_t * uk )
110
108
{
111
- msgpack_unpacker_stack_t * stack ;
112
- while ((stack = uk -> stack )) {
113
- uk -> stack = stack -> parent ;
114
- _msgpack_unpacker_free_stack (stack );
115
- }
116
-
109
+ _msgpack_unpacker_free_stack (& uk -> stack );
117
110
msgpack_buffer_destroy (UNPACKER_BUFFER_ (uk ));
118
111
}
119
112
120
113
void msgpack_unpacker_mark_stack (msgpack_unpacker_stack_t * stack )
121
114
{
122
- while (stack ) {
115
+ if (stack -> data ) {
123
116
msgpack_unpacker_stack_entry_t * s = stack -> data ;
124
117
msgpack_unpacker_stack_entry_t * send = stack -> data + stack -> depth ;
125
118
for (; s < send ; s ++ ) {
126
119
rb_gc_mark (s -> object );
127
120
rb_gc_mark (s -> key );
128
121
}
129
- stack = stack -> parent ;
130
122
}
131
123
}
132
124
133
125
void msgpack_unpacker_mark (msgpack_unpacker_t * uk )
134
126
{
135
127
rb_gc_mark (uk -> last_object );
136
128
rb_gc_mark (uk -> reading_raw );
137
- msgpack_unpacker_mark_stack (uk -> stack );
129
+ msgpack_unpacker_mark_stack (& uk -> stack );
138
130
/* See MessagePack_Buffer_wrap */
139
131
/* msgpack_buffer_mark(UNPACKER_BUFFER_(uk)); */
140
132
rb_gc_mark (uk -> buffer_ref );
@@ -147,8 +139,8 @@ void _msgpack_unpacker_reset(msgpack_unpacker_t* uk)
147
139
148
140
uk -> head_byte = HEAD_BYTE_REQUIRED ;
149
141
150
- /*memset(uk->stack, 0, sizeof(msgpack_unpacker_t) * uk->stack-> depth);*/
151
- uk -> stack -> depth = 0 ;
142
+ /*memset(uk->stack, 0, sizeof(msgpack_unpacker_t) * uk->stack. depth);*/
143
+ uk -> stack . depth = 0 ;
152
144
uk -> last_object = Qnil ;
153
145
uk -> reading_raw = Qnil ;
154
146
uk -> reading_raw_remaining = 0 ;
@@ -232,35 +224,35 @@ static inline int object_complete_ext(msgpack_unpacker_t* uk, int ext_type, VALU
232
224
/* stack funcs */
233
225
static inline msgpack_unpacker_stack_entry_t * _msgpack_unpacker_stack_entry_top (msgpack_unpacker_t * uk )
234
226
{
235
- return & uk -> stack -> data [uk -> stack -> depth - 1 ];
227
+ return & uk -> stack . data [uk -> stack . depth - 1 ];
236
228
}
237
229
238
230
static inline int _msgpack_unpacker_stack_push (msgpack_unpacker_t * uk , enum stack_type_t type , size_t count , VALUE object )
239
231
{
240
232
reset_head_byte (uk );
241
233
242
- if (uk -> stack -> capacity - uk -> stack -> depth <= 0 ) {
234
+ if (uk -> stack . capacity - uk -> stack . depth <= 0 ) {
243
235
return PRIMITIVE_STACK_TOO_DEEP ;
244
236
}
245
237
246
- msgpack_unpacker_stack_entry_t * next = & uk -> stack -> data [uk -> stack -> depth ];
238
+ msgpack_unpacker_stack_entry_t * next = & uk -> stack . data [uk -> stack . depth ];
247
239
next -> count = count ;
248
240
next -> type = type ;
249
241
next -> object = object ;
250
242
next -> key = Qnil ;
251
243
252
- uk -> stack -> depth ++ ;
244
+ uk -> stack . depth ++ ;
253
245
return PRIMITIVE_CONTAINER_START ;
254
246
}
255
247
256
- static inline VALUE msgpack_unpacker_stack_pop (msgpack_unpacker_t * uk )
248
+ static inline size_t msgpack_unpacker_stack_pop (msgpack_unpacker_t * uk )
257
249
{
258
- return -- uk -> stack -> depth ;
250
+ return -- uk -> stack . depth ;
259
251
}
260
252
261
253
static inline bool msgpack_unpacker_stack_is_empty (msgpack_unpacker_t * uk )
262
254
{
263
- return uk -> stack -> depth == 0 ;
255
+ return uk -> stack . depth == 0 ;
264
256
}
265
257
266
258
#ifdef USE_CASE_RANGE
@@ -301,7 +293,7 @@ union msgpack_buffer_cast_block_t {
301
293
302
294
static inline bool is_reading_map_key (msgpack_unpacker_t * uk )
303
295
{
304
- if (uk -> stack -> depth > 0 ) {
296
+ if (uk -> stack . depth > 0 ) {
305
297
msgpack_unpacker_stack_entry_t * top = _msgpack_unpacker_stack_entry_top (uk );
306
298
if (top -> type == STACK_TYPE_MAP_KEY ) {
307
299
return true;
@@ -356,14 +348,10 @@ static inline int read_raw_body_begin(msgpack_unpacker_t* uk, int raw_type)
356
348
reset_head_byte (uk );
357
349
uk -> reading_raw_remaining = 0 ;
358
350
359
- msgpack_unpacker_stack_t * child_stack = _msgpack_unpacker_new_stack ();
360
- child_stack -> parent = uk -> stack ;
361
- uk -> stack = child_stack ;
362
-
351
+ _msgpack_unpacker_stack_push (uk , STACK_TYPE_RECURSIVE , 1 , Qnil );
363
352
int raised ;
364
353
obj = protected_proc_call (proc , 1 , & uk -> self , & raised );
365
- uk -> stack = child_stack -> parent ;
366
- _msgpack_unpacker_free_stack (child_stack );
354
+ msgpack_unpacker_stack_pop (uk );
367
355
368
356
if (raised ) {
369
357
uk -> last_object = rb_errinfo ();
@@ -796,6 +784,8 @@ int msgpack_unpacker_read(msgpack_unpacker_t* uk, size_t target_stack_depth)
796
784
}
797
785
top -> type = STACK_TYPE_MAP_KEY ;
798
786
break ;
787
+ case STACK_TYPE_RECURSIVE :
788
+ return PRIMITIVE_OBJECT_COMPLETE ;
799
789
}
800
790
size_t count = -- top -> count ;
801
791
0 commit comments