oj 3.17.3 → 3.17.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (61) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +146 -0
  3. data/ext/oj/cache.c +72 -39
  4. data/ext/oj/cache.h +5 -0
  5. data/ext/oj/circarray.c +6 -2
  6. data/ext/oj/code.c +38 -28
  7. data/ext/oj/code.h +2 -0
  8. data/ext/oj/compat.c +63 -31
  9. data/ext/oj/custom.c +43 -8
  10. data/ext/oj/dump.c +120 -41
  11. data/ext/oj/dump.h +10 -0
  12. data/ext/oj/dump_compat.c +19 -15
  13. data/ext/oj/dump_object.c +38 -12
  14. data/ext/oj/dump_strict.c +2 -1
  15. data/ext/oj/extconf.rb +7 -0
  16. data/ext/oj/fast.c +132 -72
  17. data/ext/oj/intern.c +80 -77
  18. data/ext/oj/mimic_json.c +5 -29
  19. data/ext/oj/object.c +47 -17
  20. data/ext/oj/odd.c +6 -2
  21. data/ext/oj/oj.c +393 -44
  22. data/ext/oj/oj.h +10 -3
  23. data/ext/oj/parse.c +75 -7
  24. data/ext/oj/parse.h +23 -2
  25. data/ext/oj/parser.c +219 -71
  26. data/ext/oj/parser.h +1 -1
  27. data/ext/oj/rails.c +188 -45
  28. data/ext/oj/reader.c +22 -6
  29. data/ext/oj/reader.h +1 -0
  30. data/ext/oj/resolve.c +16 -6
  31. data/ext/oj/resolve.h +9 -0
  32. data/ext/oj/rxclass.c +16 -0
  33. data/ext/oj/rxclass.h +1 -0
  34. data/ext/oj/safe.c +62 -48
  35. data/ext/oj/safe.h +14 -2
  36. data/ext/oj/saj.c +60 -17
  37. data/ext/oj/saj2.c +44 -28
  38. data/ext/oj/simd.h +5 -3
  39. data/ext/oj/sparse.c +23 -6
  40. data/ext/oj/stream_writer.c +19 -1
  41. data/ext/oj/strict.c +42 -4
  42. data/ext/oj/string_writer.c +40 -10
  43. data/ext/oj/usual.c +17 -5
  44. data/ext/oj/util.c +27 -0
  45. data/ext/oj/util.h +2 -1
  46. data/ext/oj/val_stack.c +11 -22
  47. data/ext/oj/val_stack.h +70 -30
  48. data/ext/oj/wab.c +17 -26
  49. data/lib/oj/generic_object.rb +82 -0
  50. data/lib/oj/json.rb +1 -71
  51. data/lib/oj/mimic.rb +0 -1
  52. data/lib/oj/version.rb +1 -1
  53. data/pages/Compatibility.md +1 -1
  54. data/pages/Custom.md +2 -3
  55. data/pages/InstallOptions.md +8 -6
  56. data/pages/Modes.md +1 -1
  57. data/pages/Options.md +5 -6
  58. data/pages/Parser.md +2 -2
  59. data/pages/Rails.md +6 -0
  60. data/pages/Security.md +21 -7
  61. metadata +17 -16
data/ext/oj/strict.c CHANGED
@@ -47,7 +47,29 @@ VALUE oj_calc_hash_key(ParseInfo pi, Val parent) {
47
47
  return rkey;
48
48
  }
49
49
 
50
+ // With the default Hash class the pairs of the just closed hash sit on top
51
+ // of the pair buffer; build the Hash in one bulk insert with the final size
52
+ // known instead of one rb_hash_aset per pair (which converts the small
53
+ // ar_table to an st_table mid-insertion for hashes above 8 entries).
50
54
  static void hash_end(ParseInfo pi) {
55
+ if (Qnil == pi->options.hash_class) {
56
+ ValStack stack = &pi->stack;
57
+ Val hash = stack_peek(stack);
58
+ size_t cnt = hash->pcnt;
59
+ volatile VALUE h;
60
+
61
+ #if HAVE_RB_HASH_NEW_CAPA
62
+ h = rb_hash_new_capa((long)(cnt / 2));
63
+ #else
64
+ h = rb_hash_new();
65
+ #endif
66
+ if (0 < cnt) {
67
+ rb_hash_bulk_insert((long)cnt, stack->pairs + stack->pcnt - cnt, h);
68
+ stack->pcnt -= cnt;
69
+ hash->pcnt = 0;
70
+ }
71
+ hash->val = h;
72
+ }
51
73
  TRACE_PARSE_HASH_END(pi->options.trace, pi);
52
74
  }
53
75
 
@@ -84,13 +106,19 @@ static VALUE start_hash(ParseInfo pi) {
84
106
  return rb_class_new_instance(0, NULL, pi->options.hash_class);
85
107
  }
86
108
  TRACE_PARSE_IN(pi->options.trace, "start_hash", pi);
87
- return rb_hash_new();
109
+ // The Hash is built at hash_end from the buffered pairs.
110
+ return Qundef;
88
111
  }
89
112
 
90
113
  static void hash_set_cstr(ParseInfo pi, Val parent, const char *str, size_t len, const char *orig) {
91
114
  volatile VALUE rstr = oj_cstr_to_value(str, len, (size_t)pi->options.cache_str);
92
115
 
93
- rb_hash_aset(stack_peek(&pi->stack)->val, oj_calc_hash_key(pi, parent), rstr);
116
+ if (Qnil == pi->options.hash_class) {
117
+ stack_pair_push(&pi->stack, oj_calc_hash_key(pi, parent), rstr);
118
+ parent->pcnt += 2;
119
+ } else {
120
+ rb_hash_aset(stack_peek(&pi->stack)->val, oj_calc_hash_key(pi, parent), rstr);
121
+ }
94
122
  TRACE_PARSE_CALL(pi->options.trace, "set_string", pi, rstr);
95
123
  }
96
124
 
@@ -101,12 +129,22 @@ static void hash_set_num(ParseInfo pi, Val parent, NumInfo ni) {
101
129
  oj_set_error_at(pi, oj_parse_error_class, __FILE__, __LINE__, "not a number or other value");
102
130
  }
103
131
  v = oj_num_as_value(ni);
104
- rb_hash_aset(stack_peek(&pi->stack)->val, oj_calc_hash_key(pi, parent), v);
132
+ if (Qnil == pi->options.hash_class) {
133
+ stack_pair_push(&pi->stack, oj_calc_hash_key(pi, parent), v);
134
+ parent->pcnt += 2;
135
+ } else {
136
+ rb_hash_aset(stack_peek(&pi->stack)->val, oj_calc_hash_key(pi, parent), v);
137
+ }
105
138
  TRACE_PARSE_CALL(pi->options.trace, "set_number", pi, v);
106
139
  }
107
140
 
108
141
  static void hash_set_value(ParseInfo pi, Val parent, VALUE value) {
109
- rb_hash_aset(stack_peek(&pi->stack)->val, oj_calc_hash_key(pi, parent), value);
142
+ if (Qnil == pi->options.hash_class) {
143
+ stack_pair_push(&pi->stack, oj_calc_hash_key(pi, parent), value);
144
+ parent->pcnt += 2;
145
+ } else {
146
+ rb_hash_aset(stack_peek(&pi->stack)->val, oj_calc_hash_key(pi, parent), value);
147
+ }
110
148
  TRACE_PARSE_CALL(pi->options.trace, "set_value", pi, value);
111
149
  }
112
150
 
@@ -42,12 +42,18 @@ static void maybe_comma(StrWriter sw) {
42
42
 
43
43
  // Used by stream writer also.
44
44
  void oj_str_writer_init(StrWriter sw, int buf_size) {
45
- sw->opts = oj_default_options;
46
- sw->depth = 0;
47
- sw->types = OJ_R_ALLOC_N(char, 256);
48
- sw->types_end = sw->types + 256;
49
- *sw->types = '\0';
50
- sw->keyWritten = 0;
45
+ sw->opts = oj_default_options;
46
+ // Detach from the match_string regexps owned by the defaults before the
47
+ // options are parsed. A :match_string option then builds a chain of its own
48
+ // that is freed with the writer instead of being appended to the chain the
49
+ // defaults are using.
50
+ sw->opts.str_rx.head = NULL;
51
+ sw->opts.str_rx.tail = NULL;
52
+ sw->depth = 0;
53
+ sw->types = OJ_R_ALLOC_N(char, 256);
54
+ sw->types_end = sw->types + 256;
55
+ *sw->types = '\0';
56
+ sw->keyWritten = 0;
51
57
 
52
58
  if (0 == buf_size) {
53
59
  buf_size = 4096;
@@ -72,6 +78,8 @@ void oj_str_writer_init(StrWriter sw, int buf_size) {
72
78
  sw->out.argv = NULL;
73
79
  sw->out.ropts = NULL;
74
80
  sw->out.omit_nil = oj_default_options.dump_opts.omit_nil;
81
+ // oj_str_writer_init does not call oj_out_init, so clear this here too.
82
+ sw->out.key_filter_off = false;
75
83
  }
76
84
 
77
85
  void oj_str_writer_push_key(StrWriter sw, const char *key) {
@@ -161,6 +169,11 @@ void oj_str_writer_push_value(StrWriter sw, VALUE val, const char *key) {
161
169
  *out->cur++ = ':';
162
170
  }
163
171
  }
172
+ if (NULL != out->circ_cache) {
173
+ oj_cache8_delete(out->circ_cache);
174
+ out->circ_cache = NULL;
175
+ }
176
+ out->circ_cnt = 0;
164
177
  switch (out->opts->mode) {
165
178
  case StrictMode: oj_dump_strict_val(val, sw->depth, out); break;
166
179
  case NullMode: oj_dump_null_val(val, sw->depth, out); break;
@@ -170,6 +183,10 @@ void oj_str_writer_push_value(StrWriter sw, VALUE val, const char *key) {
170
183
  case CustomMode: oj_dump_custom_val(val, sw->depth, out, true); break;
171
184
  default: oj_dump_custom_val(val, sw->depth, out, true); break;
172
185
  }
186
+ if (NULL != out->circ_cache) {
187
+ oj_cache8_delete(out->circ_cache);
188
+ out->circ_cache = NULL;
189
+ }
173
190
  }
174
191
 
175
192
  void oj_str_writer_push_json(StrWriter sw, const char *json, const char *key) {
@@ -195,16 +212,17 @@ void oj_str_writer_push_json(StrWriter sw, const char *json, const char *key) {
195
212
 
196
213
  void oj_str_writer_pop(StrWriter sw) {
197
214
  long size;
198
- DumpType type = sw->types[sw->depth];
215
+ DumpType type;
199
216
 
200
217
  if (sw->keyWritten) {
201
218
  sw->keyWritten = 0;
202
219
  rb_raise(rb_eStandardError, "Can not pop after writing a key but no value.");
203
220
  }
204
- sw->depth--;
205
- if (0 > sw->depth) {
221
+ if (0 >= sw->depth) {
206
222
  rb_raise(rb_eStandardError, "Can not pop with no open array or object.");
207
223
  }
224
+ type = sw->types[sw->depth];
225
+ sw->depth--;
208
226
  size = sw->depth * sw->out.indent + 2;
209
227
  assure_size(&sw->out, size);
210
228
  fill_indent(&sw->out, sw->depth);
@@ -233,16 +251,27 @@ static void string_writer_free(void *ptr) {
233
251
  }
234
252
  sw = (StrWriter)ptr;
235
253
 
254
+ if (NULL != sw->out.circ_cache) {
255
+ oj_cache8_delete(sw->out.circ_cache);
256
+ sw->out.circ_cache = NULL;
257
+ }
236
258
  oj_out_free(&sw->out);
259
+ oj_options_release(&sw->opts);
237
260
 
238
261
  OJ_R_FREE(sw->types);
239
262
  OJ_R_FREE(ptr);
240
263
  }
241
264
 
265
+ static void string_writer_mark(void *ptr) {
266
+ if (NULL != ptr) {
267
+ oj_options_mark(&((StrWriter)ptr)->opts);
268
+ }
269
+ }
270
+
242
271
  static const rb_data_type_t oj_string_writer_type = {
243
272
  "Oj/string_writer",
244
273
  {
245
- NULL,
274
+ string_writer_mark,
246
275
  string_writer_free,
247
276
  NULL,
248
277
  },
@@ -279,6 +308,7 @@ static VALUE str_writer_new(int argc, VALUE *argv, VALUE self) {
279
308
  if (1 == argc) {
280
309
  oj_parse_options(argv[0], &sw->opts);
281
310
  }
311
+ oj_options_take_ownership(&sw->opts);
282
312
  sw->out.argc = argc - 1;
283
313
  sw->out.argv = argv + 1;
284
314
  sw->out.indent = sw->opts.indent;
data/ext/oj/usual.c CHANGED
@@ -49,7 +49,12 @@ static VALUE form_str(const char *str, size_t len) {
49
49
  }
50
50
 
51
51
  static VALUE form_sym(const char *str, size_t len) {
52
- return rb_str_intern(rb_utf8_str_new(str, len));
52
+ VALUE sym = rb_check_symbol_cstr(str, (long)len, oj_utf8_encoding);
53
+
54
+ if (Qnil == sym) {
55
+ sym = rb_str_intern(rb_utf8_str_new(str, len));
56
+ }
57
+ return sym;
53
58
  }
54
59
 
55
60
  static VALUE form_attr(const char *str, size_t len) {
@@ -80,6 +85,9 @@ static VALUE resolve_classname(VALUE mod, const char *classname, bool auto_defin
80
85
 
81
86
  if (rb_const_defined_at(mod, ci)) {
82
87
  clas = rb_const_get_at(mod, ci);
88
+ if (!RB_TYPE_P(clas, T_CLASS) && !RB_TYPE_P(clas, T_MODULE)) {
89
+ clas = Qundef;
90
+ }
83
91
  } else if (auto_define) {
84
92
  clas = rb_define_class_under(mod, classname, oj_bag_class);
85
93
  } else {
@@ -172,9 +180,9 @@ static VALUE str_key(ojParser p, Key kp) {
172
180
 
173
181
  static VALUE sym_key(ojParser p, Key kp) {
174
182
  if ((size_t)kp->len < sizeof(kp->buf)) {
175
- return rb_str_freeze(rb_str_intern(rb_utf8_str_new(kp->buf, kp->len)));
183
+ return form_sym(kp->buf, kp->len);
176
184
  }
177
- return rb_str_freeze(rb_str_intern(rb_utf8_str_new(kp->key, kp->len)));
185
+ return form_sym(kp->key, kp->len);
178
186
  }
179
187
 
180
188
  static ID get_attr_id(ojParser p, Key kp) {
@@ -809,7 +817,7 @@ static VALUE opt_class_cache_set(ojParser p, VALUE value) {
809
817
 
810
818
  if (Qtrue == value) {
811
819
  if (NULL == d->class_cache) {
812
- d->class_cache = cache_create(0, form_class_auto, MISS_AUTO == d->miss_class, false);
820
+ d->class_cache = cache_create(0, form_class_auto, true, false);
813
821
  }
814
822
  } else if (NULL != d->class_cache) {
815
823
  cache_free(d->class_cache);
@@ -831,6 +839,7 @@ static VALUE opt_create_id_set(ojParser p, VALUE value) {
831
839
  Usual d = (Usual)p->ctx;
832
840
 
833
841
  if (Qnil == value) {
842
+ OJ_R_FREE(d->create_id);
834
843
  d->create_id = NULL;
835
844
  d->create_id_len = 0;
836
845
  p->funcs[OBJECT_FUN].add_str = add_str_key;
@@ -850,12 +859,15 @@ static VALUE opt_create_id_set(ojParser p, VALUE value) {
850
859
  if (1 << (8 * sizeof(d->create_id_len)) <= len) {
851
860
  rb_raise(rb_eArgError, "The create_id values is limited to %d bytes.", 1 << (8 * sizeof(d->create_id_len)));
852
861
  }
853
- d->create_id_len = (uint8_t)len;
862
+ char *prev = d->create_id;
863
+
854
864
  d->create_id = str_dup(RSTRING_PTR(value), len);
865
+ d->create_id_len = (uint8_t)len;
855
866
  p->funcs[OBJECT_FUN].add_str = add_str_key_create;
856
867
  p->funcs[TOP_FUN].close_object = close_object_create;
857
868
  p->funcs[ARRAY_FUN].close_object = close_object_create;
858
869
  p->funcs[OBJECT_FUN].close_object = close_object_create;
870
+ OJ_R_FREE(prev);
859
871
  }
860
872
  return opt_create_id(p, value);
861
873
  }
data/ext/oj/util.c CHANGED
@@ -134,3 +134,30 @@ void sec_as_time(int64_t secs, TimeInfo ti) {
134
134
  secs = secs - (int64_t)ti->min * 60LL;
135
135
  ti->sec = (int)secs;
136
136
  }
137
+
138
+ // The inverse of sec_as_time(). The days are counted with the civil calendar
139
+ // algorithm from Howard Hinnant's date library so that no libc call is needed.
140
+ // timegm() is not available on Windows and the mktime() based replacement that
141
+ // was used there returned the wrong value in any timezone east of UTC.
142
+ int64_t time_as_sec(TimeInfo ti) {
143
+ int64_t year = (int64_t)ti->year;
144
+ int64_t mon = (int64_t)ti->mon;
145
+ int64_t era;
146
+ int64_t yoe; // year of the era, [0, 399]
147
+ int64_t doy; // day of the year, [0, 365]
148
+ int64_t doe; // day of the era, [0, 146096]
149
+ int64_t days; // days since 1970-01-01
150
+
151
+ // Start the year on March 1st so that a leap day falls on the last day of
152
+ // the year and the day of the year is a simple expression.
153
+ if (mon <= 2) {
154
+ year--;
155
+ }
156
+ era = (0 <= year ? year : year - 399) / 400;
157
+ yoe = year - era * 400;
158
+ doy = (153 * (mon + (2 < mon ? -3 : 9)) + 2) / 5 + (int64_t)ti->day - 1;
159
+ doe = yoe * 365 + yoe / 4 - yoe / 100 + doy;
160
+ days = era * 146097 + doe - 719468;
161
+
162
+ return days * SECS_PER_DAY + (int64_t)ti->hour * 3600LL + (int64_t)ti->min * 60LL + (int64_t)ti->sec;
163
+ }
data/ext/oj/util.h CHANGED
@@ -15,6 +15,7 @@ typedef struct _timeInfo {
15
15
  int year;
16
16
  }* TimeInfo;
17
17
 
18
- extern void sec_as_time(int64_t secs, TimeInfo ti);
18
+ extern void sec_as_time(int64_t secs, TimeInfo ti);
19
+ extern int64_t time_as_sec(TimeInfo ti);
19
20
 
20
21
  #endif /* OJ_UTIL_H */
data/ext/oj/val_stack.c CHANGED
@@ -3,8 +3,6 @@
3
3
 
4
4
  #include "val_stack.h"
5
5
 
6
- #include <string.h>
7
-
8
6
  #include "odd.h"
9
7
  #include "oj.h"
10
8
 
@@ -15,12 +13,13 @@ static void stack_mark(void *ptr) {
15
13
  if (NULL == ptr) {
16
14
  return;
17
15
  }
18
- #ifdef HAVE_PTHREAD_MUTEX_INIT
19
- pthread_mutex_lock(&stack->mutex);
20
- #else
21
- rb_mutex_lock(stack->mutex);
22
- rb_gc_mark(stack->mutex);
23
- #endif
16
+ {
17
+ size_t i;
18
+
19
+ for (i = 0; i < stack->pcnt; i++) {
20
+ rb_gc_mark(stack->pairs[i]);
21
+ }
22
+ }
24
23
  for (v = stack->head; v < stack->tail; v++) {
25
24
  if (Qnil != v->val && Qundef != v->val) {
26
25
  rb_gc_mark(v->val);
@@ -39,11 +38,6 @@ static void stack_mark(void *ptr) {
39
38
  }
40
39
  }
41
40
  }
42
- #ifdef HAVE_PTHREAD_MUTEX_INIT
43
- pthread_mutex_unlock(&stack->mutex);
44
- #else
45
- rb_mutex_unlock(stack->mutex);
46
- #endif
47
41
  }
48
42
 
49
43
  static const rb_data_type_t oj_stack_type = {
@@ -59,18 +53,12 @@ static const rb_data_type_t oj_stack_type = {
59
53
 
60
54
  VALUE
61
55
  oj_stack_init(ValStack stack) {
62
- #ifdef HAVE_PTHREAD_MUTEX_INIT
63
- int err;
64
-
65
- if (0 != (err = pthread_mutex_init(&stack->mutex, 0))) {
66
- rb_raise(rb_eException, "failed to initialize a mutex. %s", strerror(err));
67
- }
68
- #else
69
- stack->mutex = rb_mutex_new();
70
- #endif
71
56
  stack->head = stack->base;
72
57
  stack->end = stack->base + sizeof(stack->base) / sizeof(struct _val);
73
58
  stack->tail = stack->head;
59
+ stack->pairs = stack->pbase;
60
+ stack->pcnt = 0;
61
+ stack->pend = sizeof(stack->pbase) / sizeof(VALUE);
74
62
  stack->head->val = Qundef;
75
63
  stack->head->key = NULL;
76
64
  stack->head->key_val = Qundef;
@@ -79,6 +67,7 @@ oj_stack_init(ValStack stack) {
79
67
  stack->head->clas = Qundef;
80
68
  stack->head->klen = 0;
81
69
  stack->head->clen = 0;
70
+ stack->head->pcnt = 0;
82
71
  stack->head->next = NEXT_NONE;
83
72
 
84
73
  return TypedData_Wrap_Struct(oj_cstack_class, &oj_stack_type, stack);
data/ext/oj/val_stack.h CHANGED
@@ -9,9 +9,6 @@
9
9
  #include "mem.h"
10
10
  #include "odd.h"
11
11
  #include "ruby.h"
12
- #ifdef HAVE_PTHREAD_MUTEX_INIT
13
- #include <pthread.h>
14
- #endif
15
12
 
16
13
  #define STACK_INC 64
17
14
 
@@ -35,24 +32,29 @@ typedef struct _val {
35
32
  const char *classname;
36
33
  VALUE clas;
37
34
  OddArgs odd_args;
38
- uint16_t klen;
39
- uint16_t clen;
35
+ size_t klen;
36
+ size_t clen;
37
+ size_t pcnt; // VALUEs this open hash holds in the stack pair buffer
40
38
  char next; // ValNext
41
39
  char k1; // first original character in the key
42
40
  char kalloc;
43
41
  } *Val;
44
42
 
43
+ #define PAIR_BASE_CNT 64
44
+
45
45
  typedef struct _valStack {
46
46
  struct _val base[STACK_INC];
47
- Val head; // current stack
48
- Val end; // stack end
49
- Val tail; // pointer to one past last element name on stack
50
- #ifdef HAVE_PTHREAD_MUTEX_INIT
51
- pthread_mutex_t mutex;
52
- #else
53
- VALUE mutex;
54
- #endif
55
-
47
+ VALUE pbase[PAIR_BASE_CNT]; // initial buffer for pairs
48
+ // Flat key/value pair buffer for hash modes that build each Hash in one
49
+ // rb_hash_bulk_insert at the closing brace instead of one rb_hash_aset
50
+ // per pair. Nesting is strictly LIFO so each open hash's pairs occupy
51
+ // the top of the buffer; the per-hash count lives in its Val's clen.
52
+ VALUE *pairs;
53
+ size_t pcnt; // number of live VALUEs in pairs
54
+ size_t pend; // capacity of pairs in VALUEs
55
+ Val head; // current stack
56
+ Val end; // stack end
57
+ Val tail; // pointer to one past last element name on stack
56
58
  } *ValStack;
57
59
 
58
60
  extern VALUE oj_stack_init(ValStack stack);
@@ -62,39 +64,53 @@ inline static int stack_empty(ValStack stack) {
62
64
  }
63
65
 
64
66
  inline static void stack_cleanup(ValStack stack) {
67
+ Val v;
68
+
69
+ // end_hash() frees the args of a completed odd class. Anything still here
70
+ // belongs to a parse that was abandoned part way through.
71
+ for (v = stack->head; v < stack->tail; v++) {
72
+ if (NULL != v->odd_args) {
73
+ oj_odd_free(v->odd_args);
74
+ v->odd_args = NULL;
75
+ }
76
+ }
65
77
  if (stack->base != stack->head) {
66
78
  OJ_R_FREE(stack->head);
67
79
  stack->head = NULL;
68
80
  }
81
+ if (stack->pbase != stack->pairs) {
82
+ OJ_R_FREE(stack->pairs);
83
+ stack->pairs = NULL;
84
+ }
85
+ stack->pcnt = 0;
69
86
  }
70
87
 
71
88
  inline static void stack_push(ValStack stack, VALUE val, ValNext next) {
72
89
  if (stack->end <= stack->tail) {
73
- size_t len = stack->end - stack->head;
74
- size_t toff = stack->tail - stack->head;
75
- Val head = stack->head;
76
-
77
- // A realloc can trigger a GC so make sure it happens outside the lock
78
- // but lock before changing pointers.
90
+ size_t len = stack->end - stack->head;
91
+ size_t toff = stack->tail - stack->head;
92
+ Val head = stack->head;
93
+ const char *old = (const char *)stack->head;
94
+ size_t i;
95
+
96
+ // A realloc can trigger a GC that marks the stack, so change the
97
+ // pointers only after it returns.
79
98
  if (stack->base == stack->head) {
80
99
  head = OJ_R_ALLOC_N(struct _val, len + STACK_INC);
81
100
  memcpy(head, stack->base, sizeof(struct _val) * len);
82
101
  } else {
83
102
  OJ_R_REALLOC_N(head, struct _val, len + STACK_INC);
84
103
  }
85
- #ifdef HAVE_PTHREAD_MUTEX_INIT
86
- pthread_mutex_lock(&stack->mutex);
87
- #else
88
- rb_mutex_lock(stack->mutex);
89
- #endif
104
+ // A key short enough to be kept in the Val itself points into the
105
+ // block that just moved.
106
+ for (i = 0; i < toff; i++) {
107
+ if (old <= head[i].key && head[i].key < old + sizeof(struct _val) * len) {
108
+ head[i].key = head[i].karray;
109
+ }
110
+ }
90
111
  stack->head = head;
91
112
  stack->tail = stack->head + toff;
92
113
  stack->end = stack->head + len + STACK_INC;
93
- #ifdef HAVE_PTHREAD_MUTEX_INIT
94
- pthread_mutex_unlock(&stack->mutex);
95
- #else
96
- rb_mutex_unlock(stack->mutex);
97
- #endif
98
114
  }
99
115
  stack->tail->val = val;
100
116
  stack->tail->next = next;
@@ -105,6 +121,7 @@ inline static void stack_push(ValStack stack, VALUE val, ValNext next) {
105
121
  stack->tail->key_val = Qundef;
106
122
  stack->tail->clen = 0;
107
123
  stack->tail->klen = 0;
124
+ stack->tail->pcnt = 0;
108
125
  stack->tail->kalloc = 0;
109
126
  stack->tail++;
110
127
  }
@@ -146,6 +163,29 @@ inline static Val stack_pop(ValStack stack) {
146
163
  return 0;
147
164
  }
148
165
 
166
+ // Append one key/value pair to the pair buffer. The same GC discipline as
167
+ // stack_push: the allocation can trigger a GC that marks the pairs, so the
168
+ // buffer pointer only changes after it returns.
169
+ inline static void stack_pair_push(ValStack stack, VALUE key, VALUE value) {
170
+ if (stack->pend <= stack->pcnt + 2) {
171
+ size_t cnt = stack->pend * 2;
172
+ VALUE *pairs;
173
+
174
+ if (stack->pbase == stack->pairs) {
175
+ pairs = OJ_R_ALLOC_N(VALUE, cnt);
176
+ memcpy(pairs, stack->pairs, sizeof(VALUE) * stack->pcnt);
177
+ } else {
178
+ pairs = stack->pairs;
179
+ OJ_R_REALLOC_N(pairs, VALUE, cnt);
180
+ }
181
+ stack->pairs = pairs;
182
+ stack->pend = cnt;
183
+ }
184
+ stack->pairs[stack->pcnt] = key;
185
+ stack->pairs[stack->pcnt + 1] = value;
186
+ stack->pcnt += 2;
187
+ }
188
+
149
189
  extern const char *oj_stack_next_string(ValNext n);
150
190
 
151
191
  #endif /* OJ_VAL_STACK_H */
data/ext/oj/wab.c CHANGED
@@ -40,6 +40,7 @@ static VALUE resolve_wab_uuid_class(void) {
40
40
  volatile VALUE wab_module;
41
41
 
42
42
  wab_uuid_clas = Qnil;
43
+ rb_gc_register_address(&wab_uuid_clas);
43
44
  if (rb_const_defined_at(rb_cObject, rb_intern("WAB"))) {
44
45
  wab_module = rb_const_get_at(rb_cObject, rb_intern("WAB"));
45
46
  if (rb_const_defined_at(wab_module, rb_intern("UUID"))) {
@@ -53,6 +54,7 @@ static VALUE resolve_wab_uuid_class(void) {
53
54
  static VALUE resolve_uri_class(void) {
54
55
  if (Qundef == uri_clas) {
55
56
  uri_clas = Qnil;
57
+ rb_gc_register_address(&uri_clas);
56
58
  if (rb_const_defined_at(rb_cObject, rb_intern("URI"))) {
57
59
  uri_clas = rb_const_get_at(rb_cObject, rb_intern("URI"));
58
60
  }
@@ -65,6 +67,7 @@ static VALUE resolve_uri_http_class(void) {
65
67
  volatile VALUE uri_module;
66
68
 
67
69
  uri_http_clas = Qnil;
70
+ rb_gc_register_address(&uri_http_clas);
68
71
  if (rb_const_defined_at(rb_cObject, rb_intern("URI"))) {
69
72
  uri_module = rb_const_get_at(rb_cObject, rb_intern("URI"));
70
73
  if (rb_const_defined_at(uri_module, rb_intern("HTTP"))) {
@@ -375,44 +378,41 @@ static const char *read_num(const char *s, int len, int *vp) {
375
378
  }
376
379
 
377
380
  static VALUE time_parse(const char *s, int len) {
378
- struct tm tm;
379
- bool neg = false;
380
- long nsecs = 0;
381
- int i;
382
- time_t secs;
381
+ struct _timeInfo ti;
382
+ bool neg = false;
383
+ long nsecs = 0;
384
+ int i;
383
385
 
384
- memset(&tm, 0, sizeof(tm));
386
+ memset(&ti, 0, sizeof(ti));
385
387
  if ('-' == *s) {
386
388
  s++;
387
389
  neg = true;
388
390
  }
389
- if (NULL == (s = read_num(s, 4, &tm.tm_year))) {
391
+ if (NULL == (s = read_num(s, 4, &ti.year))) {
390
392
  return Qnil;
391
393
  }
392
394
  if (neg) {
393
- tm.tm_year = -tm.tm_year;
394
- neg = false;
395
+ ti.year = -ti.year;
396
+ neg = false;
395
397
  }
396
- tm.tm_year -= 1900;
397
398
  s++;
398
- if (NULL == (s = read_num(s, 2, &tm.tm_mon))) {
399
+ if (NULL == (s = read_num(s, 2, &ti.mon))) {
399
400
  return Qnil;
400
401
  }
401
- tm.tm_mon--;
402
402
  s++;
403
- if (NULL == (s = read_num(s, 2, &tm.tm_mday))) {
403
+ if (NULL == (s = read_num(s, 2, &ti.day))) {
404
404
  return Qnil;
405
405
  }
406
406
  s++;
407
- if (NULL == (s = read_num(s, 2, &tm.tm_hour))) {
407
+ if (NULL == (s = read_num(s, 2, &ti.hour))) {
408
408
  return Qnil;
409
409
  }
410
410
  s++;
411
- if (NULL == (s = read_num(s, 2, &tm.tm_min))) {
411
+ if (NULL == (s = read_num(s, 2, &ti.min))) {
412
412
  return Qnil;
413
413
  }
414
414
  s++;
415
- if (NULL == (s = read_num(s, 2, &tm.tm_sec))) {
415
+ if (NULL == (s = read_num(s, 2, &ti.sec))) {
416
416
  return Qnil;
417
417
  }
418
418
  s++;
@@ -424,16 +424,7 @@ static VALUE time_parse(const char *s, int len) {
424
424
  return Qnil;
425
425
  }
426
426
  }
427
- #if IS_WINDOWS
428
- secs = (time_t)mktime(&tm);
429
- memset(&tm, 0, sizeof(tm));
430
- tm.tm_year = 70;
431
- tm.tm_mday = 1;
432
- secs -= (time_t)mktime(&tm);
433
- #else
434
- secs = (time_t)timegm(&tm);
435
- #endif
436
- return rb_funcall(rb_time_nano_new(secs, nsecs), oj_utc_id, 0);
427
+ return rb_funcall(rb_time_nano_new((time_t)time_as_sec(&ti), nsecs), oj_utc_id, 0);
437
428
  }
438
429
 
439
430
  static VALUE protect_uri(VALUE rstr) {