oj 3.7.4 → 3.13.23

Sign up to get free protection for your applications and to get access to all the features.
Files changed (147) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +1360 -0
  3. data/README.md +31 -8
  4. data/RELEASE_NOTES.md +61 -0
  5. data/ext/oj/buf.h +53 -72
  6. data/ext/oj/cache.c +326 -0
  7. data/ext/oj/cache.h +21 -0
  8. data/ext/oj/cache8.c +61 -64
  9. data/ext/oj/cache8.h +12 -39
  10. data/ext/oj/circarray.c +37 -43
  11. data/ext/oj/circarray.h +16 -17
  12. data/ext/oj/code.c +165 -179
  13. data/ext/oj/code.h +27 -29
  14. data/ext/oj/compat.c +174 -194
  15. data/ext/oj/custom.c +790 -866
  16. data/ext/oj/debug.c +132 -0
  17. data/ext/oj/dump.c +848 -863
  18. data/ext/oj/dump.h +81 -67
  19. data/ext/oj/dump_compat.c +85 -123
  20. data/ext/oj/dump_leaf.c +100 -188
  21. data/ext/oj/dump_object.c +527 -656
  22. data/ext/oj/dump_strict.c +315 -338
  23. data/ext/oj/encode.h +7 -34
  24. data/ext/oj/encoder.c +43 -0
  25. data/ext/oj/err.c +40 -29
  26. data/ext/oj/err.h +48 -48
  27. data/ext/oj/extconf.rb +17 -4
  28. data/ext/oj/fast.c +1073 -1088
  29. data/ext/oj/intern.c +298 -0
  30. data/ext/oj/intern.h +26 -0
  31. data/ext/oj/mimic_json.c +469 -436
  32. data/ext/oj/object.c +532 -599
  33. data/ext/oj/odd.c +154 -138
  34. data/ext/oj/odd.h +37 -38
  35. data/ext/oj/oj.c +1333 -986
  36. data/ext/oj/oj.h +336 -316
  37. data/ext/oj/parse.c +1002 -846
  38. data/ext/oj/parse.h +92 -87
  39. data/ext/oj/parser.c +1587 -0
  40. data/ext/oj/parser.h +102 -0
  41. data/ext/oj/rails.c +888 -878
  42. data/ext/oj/rails.h +11 -14
  43. data/ext/oj/reader.c +141 -147
  44. data/ext/oj/reader.h +73 -89
  45. data/ext/oj/resolve.c +41 -62
  46. data/ext/oj/resolve.h +7 -9
  47. data/ext/oj/rxclass.c +71 -75
  48. data/ext/oj/rxclass.h +18 -19
  49. data/ext/oj/saj.c +443 -486
  50. data/ext/oj/saj2.c +596 -0
  51. data/ext/oj/saj2.h +23 -0
  52. data/ext/oj/scp.c +88 -113
  53. data/ext/oj/sparse.c +787 -709
  54. data/ext/oj/stream_writer.c +133 -159
  55. data/ext/oj/strict.c +127 -118
  56. data/ext/oj/string_writer.c +230 -249
  57. data/ext/oj/trace.c +34 -41
  58. data/ext/oj/trace.h +19 -19
  59. data/ext/oj/usual.c +1207 -0
  60. data/ext/oj/usual.h +68 -0
  61. data/ext/oj/util.c +136 -0
  62. data/ext/oj/util.h +20 -0
  63. data/ext/oj/val_stack.c +60 -68
  64. data/ext/oj/val_stack.h +91 -129
  65. data/ext/oj/validate.c +46 -0
  66. data/ext/oj/wab.c +342 -353
  67. data/lib/oj/bag.rb +1 -0
  68. data/lib/oj/easy_hash.rb +5 -4
  69. data/lib/oj/error.rb +1 -1
  70. data/lib/oj/json.rb +1 -1
  71. data/lib/oj/mimic.rb +48 -14
  72. data/lib/oj/saj.rb +20 -6
  73. data/lib/oj/state.rb +9 -8
  74. data/lib/oj/version.rb +2 -2
  75. data/lib/oj.rb +0 -8
  76. data/pages/Compatibility.md +1 -1
  77. data/pages/JsonGem.md +15 -0
  78. data/pages/Modes.md +53 -46
  79. data/pages/Options.md +78 -11
  80. data/pages/Parser.md +309 -0
  81. data/pages/Rails.md +73 -22
  82. data/pages/Security.md +1 -1
  83. data/test/activerecord/result_test.rb +7 -2
  84. data/test/activesupport5/abstract_unit.rb +45 -0
  85. data/test/activesupport5/decoding_test.rb +68 -60
  86. data/test/activesupport5/encoding_test.rb +111 -96
  87. data/test/activesupport5/encoding_test_cases.rb +33 -25
  88. data/test/activesupport5/test_helper.rb +43 -21
  89. data/test/activesupport5/time_zone_test_helpers.rb +18 -3
  90. data/test/activesupport6/abstract_unit.rb +44 -0
  91. data/test/activesupport6/decoding_test.rb +133 -0
  92. data/test/activesupport6/encoding_test.rb +507 -0
  93. data/test/activesupport6/encoding_test_cases.rb +98 -0
  94. data/test/activesupport6/test_common.rb +17 -0
  95. data/test/activesupport6/test_helper.rb +163 -0
  96. data/test/activesupport6/time_zone_test_helpers.rb +39 -0
  97. data/test/activesupport7/abstract_unit.rb +49 -0
  98. data/test/activesupport7/decoding_test.rb +125 -0
  99. data/test/activesupport7/encoding_test.rb +486 -0
  100. data/test/activesupport7/encoding_test_cases.rb +104 -0
  101. data/test/activesupport7/time_zone_test_helpers.rb +47 -0
  102. data/test/bar.rb +6 -12
  103. data/test/baz.rb +16 -0
  104. data/test/bug.rb +16 -0
  105. data/test/foo.rb +69 -75
  106. data/test/helper.rb +16 -0
  107. data/test/json_gem/json_common_interface_test.rb +8 -3
  108. data/test/json_gem/json_generator_test.rb +21 -8
  109. data/test/json_gem/json_parser_test.rb +8 -1
  110. data/test/json_gem/test_helper.rb +12 -0
  111. data/test/mem.rb +33 -0
  112. data/test/perf.rb +1 -1
  113. data/test/perf_dump.rb +50 -0
  114. data/test/perf_once.rb +58 -0
  115. data/test/perf_parser.rb +189 -0
  116. data/test/perf_scp.rb +11 -10
  117. data/test/perf_strict.rb +17 -23
  118. data/test/prec.rb +23 -0
  119. data/test/sample_json.rb +1 -1
  120. data/test/test_compat.rb +46 -10
  121. data/test/test_custom.rb +145 -7
  122. data/test/test_fast.rb +62 -2
  123. data/test/test_file.rb +23 -7
  124. data/test/test_gc.rb +11 -0
  125. data/test/test_generate.rb +21 -0
  126. data/test/test_hash.rb +11 -1
  127. data/test/test_integer_range.rb +1 -2
  128. data/test/test_object.rb +43 -12
  129. data/test/test_parser.rb +11 -0
  130. data/test/test_parser_debug.rb +27 -0
  131. data/test/test_parser_saj.rb +335 -0
  132. data/test/test_parser_usual.rb +217 -0
  133. data/test/test_rails.rb +35 -0
  134. data/test/test_saj.rb +1 -1
  135. data/test/test_scp.rb +3 -5
  136. data/test/test_strict.rb +26 -1
  137. data/test/test_various.rb +86 -65
  138. data/test/test_wab.rb +2 -0
  139. data/test/test_writer.rb +19 -2
  140. data/test/tests.rb +10 -1
  141. data/test/tests_mimic.rb +9 -0
  142. data/test/tests_mimic_addition.rb +9 -0
  143. data/test/zoo.rb +13 -0
  144. metadata +63 -110
  145. data/ext/oj/hash.c +0 -163
  146. data/ext/oj/hash.h +0 -46
  147. data/ext/oj/hash_test.c +0 -512
data/ext/oj/usual.h ADDED
@@ -0,0 +1,68 @@
1
+ // Copyright (c) 2022, Peter Ohler, All rights reserved.
2
+
3
+ #include <ruby.h>
4
+ #include <stdbool.h>
5
+ #include <stdint.h>
6
+
7
+ struct _cache;
8
+ struct _ojParser;
9
+
10
+ // Used to mark the start of each Hash, Array, or Object. The members point at
11
+ // positions of the start in the value stack and if not an Array into the key
12
+ // stack.
13
+ typedef struct _col {
14
+ long vi; // value stack index
15
+ long ki; // key stack index if an hash else -1 for an array
16
+ } * Col;
17
+
18
+ typedef union _key {
19
+ struct {
20
+ int16_t len;
21
+ char buf[30];
22
+ };
23
+ struct {
24
+ int16_t xlen; // should be the same as len
25
+ char * key;
26
+ };
27
+ } * Key;
28
+
29
+ #define MISS_AUTO 'A'
30
+ #define MISS_RAISE 'R'
31
+ #define MISS_IGNORE 'I'
32
+
33
+ typedef struct _usual {
34
+ VALUE *vhead;
35
+ VALUE *vtail;
36
+ VALUE *vend;
37
+
38
+ Col chead;
39
+ Col ctail;
40
+ Col cend;
41
+
42
+ Key khead;
43
+ Key ktail;
44
+ Key kend;
45
+
46
+ VALUE (*get_key)(ojParser p, Key kp);
47
+ struct _cache *key_cache; // same as str_cache or sym_cache
48
+ struct _cache *str_cache;
49
+ struct _cache *sym_cache;
50
+ struct _cache *class_cache;
51
+ struct _cache *attr_cache;
52
+
53
+ VALUE array_class;
54
+ VALUE hash_class;
55
+
56
+ char * create_id;
57
+ uint8_t create_id_len;
58
+ uint8_t cache_str;
59
+ uint8_t cache_xrate;
60
+ uint8_t miss_class;
61
+ bool cache_keys;
62
+ bool ignore_json_create;
63
+ } * Usual;
64
+
65
+ // Initialize the parser with the usual delegate. If the usual delegate is
66
+ // wrapped then this function is called first and then the parser functions
67
+ // can be replaced.
68
+ extern void oj_init_usual(struct _ojParser *p, Usual d);
data/ext/oj/util.c ADDED
@@ -0,0 +1,136 @@
1
+ // Copyright (c) 2019 Peter Ohler. All rights reserved.
2
+ // Licensed under the MIT License. See LICENSE file in the project root for license details.
3
+
4
+ #include "util.h"
5
+
6
+ #include <stdbool.h>
7
+ #include <stdint.h>
8
+ #include <stdio.h>
9
+ #include <string.h>
10
+ #include <time.h>
11
+
12
+ #define SECS_PER_DAY 86400LL
13
+ #define SECS_PER_YEAR 31536000LL
14
+ #define SECS_PER_LEAP 31622400LL
15
+ #define SECS_PER_QUAD_YEAR (SECS_PER_YEAR * 3 + SECS_PER_LEAP)
16
+ #define SECS_PER_CENT (SECS_PER_QUAD_YEAR * 24 + SECS_PER_YEAR * 4)
17
+ #define SECS_PER_LEAP_CENT (SECS_PER_CENT + SECS_PER_DAY)
18
+ #define SECS_PER_QUAD_CENT (SECS_PER_CENT * 4 + SECS_PER_DAY)
19
+
20
+ static int64_t eom_secs[] = {
21
+ 2678400, // January (31)
22
+ 5097600, // February (28) 2419200 2505600
23
+ 7776000, // March (31)
24
+ 10368000, // April (30 2592000
25
+ 13046400, // May (31)
26
+ 15638400, // June (30)
27
+ 18316800, // July (31)
28
+ 20995200, // August (31)
29
+ 23587200, // September (30)
30
+ 26265600, // October (31)
31
+ 28857600, // November (30)
32
+ 31536000, // December (31)
33
+ };
34
+
35
+ static int64_t eom_leap_secs[] = {
36
+ 2678400, // January (31)
37
+ 5184000, // February (28) 2419200 2505600
38
+ 7862400, // March (31)
39
+ 10454400, // April (30 2592000
40
+ 13132800, // May (31)
41
+ 15724800, // June (30)
42
+ 18403200, // July (31)
43
+ 21081600, // August (31)
44
+ 23673600, // September (30)
45
+ 26352000, // October (31)
46
+ 28944000, // November (30)
47
+ 31622400, // December (31)
48
+ };
49
+
50
+ void sec_as_time(int64_t secs, TimeInfo ti) {
51
+ int64_t qc = 0;
52
+ int64_t c = 0;
53
+ int64_t qy = 0;
54
+ int64_t y = 0;
55
+ bool leap = false;
56
+ int64_t *ms;
57
+ int m;
58
+ int shift = 0;
59
+
60
+ secs += 62167219200LL; // normalize to first day of the year 0
61
+ if (secs < 0) {
62
+ shift = -secs / SECS_PER_QUAD_CENT;
63
+ shift++;
64
+ secs += shift * SECS_PER_QUAD_CENT;
65
+ }
66
+ qc = secs / SECS_PER_QUAD_CENT;
67
+ secs = secs - qc * SECS_PER_QUAD_CENT;
68
+ if (secs < SECS_PER_LEAP) {
69
+ leap = true;
70
+ } else if (secs < SECS_PER_QUAD_YEAR) {
71
+ if (SECS_PER_LEAP <= secs) {
72
+ secs -= SECS_PER_LEAP;
73
+ y = secs / SECS_PER_YEAR;
74
+ secs = secs - y * SECS_PER_YEAR;
75
+ y++;
76
+ leap = false;
77
+ }
78
+ } else if (secs < SECS_PER_LEAP_CENT) { // first century in 400 years is a leap century (one
79
+ // extra day)
80
+ qy = secs / SECS_PER_QUAD_YEAR;
81
+ secs = secs - qy * SECS_PER_QUAD_YEAR;
82
+ if (secs < SECS_PER_LEAP) {
83
+ leap = true;
84
+ } else {
85
+ secs -= SECS_PER_LEAP;
86
+ y = secs / SECS_PER_YEAR;
87
+ secs = secs - y * SECS_PER_YEAR;
88
+ y++;
89
+ }
90
+ } else {
91
+ secs -= SECS_PER_LEAP_CENT;
92
+ c = secs / SECS_PER_CENT;
93
+ secs = secs - c * SECS_PER_CENT;
94
+ c++;
95
+ if (secs < SECS_PER_YEAR * 4) {
96
+ y = secs / SECS_PER_YEAR;
97
+ secs = secs - y * SECS_PER_YEAR;
98
+ } else {
99
+ secs -= SECS_PER_YEAR * 4;
100
+ qy = secs / SECS_PER_QUAD_YEAR;
101
+ secs = secs - qy * SECS_PER_QUAD_YEAR;
102
+ qy++;
103
+ if (secs < SECS_PER_LEAP) {
104
+ leap = true;
105
+ } else {
106
+ secs -= SECS_PER_LEAP;
107
+ y = secs / SECS_PER_YEAR;
108
+ secs = secs - y * SECS_PER_YEAR;
109
+ y++;
110
+ }
111
+ }
112
+ }
113
+ ti->year = (int)((qc - (int64_t)shift) * 400 + c * 100 + qy * 4 + y);
114
+ if (leap) {
115
+ ms = eom_leap_secs;
116
+ } else {
117
+ ms = eom_secs;
118
+ }
119
+ for (m = 1; m <= 12; m++, ms++) {
120
+ if (secs < *ms) {
121
+ if (1 < m) {
122
+ secs -= *(ms - 1);
123
+ }
124
+ ti->mon = m;
125
+ break;
126
+ }
127
+ }
128
+ ti->day = (int)(secs / 86400LL);
129
+ secs = secs - (int64_t)ti->day * 86400LL;
130
+ ti->day++;
131
+ ti->hour = (int)(secs / 3600LL);
132
+ secs = secs - (int64_t)ti->hour * 3600LL;
133
+ ti->min = (int)(secs / 60LL);
134
+ secs = secs - (int64_t)ti->min * 60LL;
135
+ ti->sec = (int)secs;
136
+ }
data/ext/oj/util.h ADDED
@@ -0,0 +1,20 @@
1
+ // Copyright (c) 2019 Peter Ohler. All rights reserved.
2
+ // Licensed under the MIT License. See LICENSE file in the project root for license details.
3
+
4
+ #ifndef OJ_UTIL_H
5
+ #define OJ_UTIL_H
6
+
7
+ #include <stdint.h>
8
+
9
+ typedef struct _timeInfo {
10
+ int sec;
11
+ int min;
12
+ int hour;
13
+ int day;
14
+ int mon;
15
+ int year;
16
+ } * TimeInfo;
17
+
18
+ extern void sec_as_time(int64_t secs, TimeInfo ti);
19
+
20
+ #endif /* OJ_UTIL_H */
data/ext/oj/val_stack.c CHANGED
@@ -1,59 +1,45 @@
1
- /* val_stack.c
2
- * Copyright (c) 2011, Peter Ohler
3
- * All rights reserved.
4
- *
5
- * Redistribution and use in source and binary forms, with or without
6
- * modification, are permitted provided that the following conditions are met:
7
- *
8
- * - Redistributions of source code must retain the above copyright notice, this
9
- * list of conditions and the following disclaimer.
10
- *
11
- * - Redistributions in binary form must reproduce the above copyright notice,
12
- * this list of conditions and the following disclaimer in the documentation
13
- * and/or other materials provided with the distribution.
14
- *
15
- * - Neither the name of Peter Ohler nor the names of its contributors may be
16
- * used to endorse or promote products derived from this software without
17
- * specific prior written permission.
18
- *
19
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
- */
1
+ // Copyright (c) 2011 Peter Ohler. All rights reserved.
2
+ // Licensed under the MIT License. See LICENSE file in the project root for license details.
30
3
 
31
- #include "oj.h"
32
4
  #include "val_stack.h"
33
5
 
34
- static void
35
- mark(void *ptr) {
36
- ValStack stack = (ValStack)ptr;
37
- Val v;
6
+ #include <string.h>
7
+
8
+ #include "odd.h"
9
+ #include "oj.h"
10
+
11
+ static void mark(void *ptr) {
12
+ ValStack stack = (ValStack)ptr;
13
+ Val v;
38
14
 
39
- if (0 == ptr) {
40
- return;
15
+ if (NULL == ptr) {
16
+ return;
41
17
  }
42
- #if HAVE_LIBPTHREAD
18
+ #ifdef HAVE_PTHREAD_MUTEX_INIT
43
19
  pthread_mutex_lock(&stack->mutex);
44
20
  #else
45
21
  rb_mutex_lock(stack->mutex);
46
22
  rb_gc_mark(stack->mutex);
47
23
  #endif
48
24
  for (v = stack->head; v < stack->tail; v++) {
49
- if (Qnil != v->val && Qundef != v->val) {
50
- rb_gc_mark(v->val);
51
- }
52
- if (Qnil != v->key_val && Qundef != v->key_val) {
53
- rb_gc_mark(v->key_val);
54
- }
25
+ if (Qnil != v->val && Qundef != v->val) {
26
+ rb_gc_mark(v->val);
27
+ }
28
+ if (Qnil != v->key_val && Qundef != v->key_val) {
29
+ rb_gc_mark(v->key_val);
30
+ }
31
+ if (NULL != v->odd_args) {
32
+ VALUE *a;
33
+ int i;
34
+
35
+ for (i = v->odd_args->odd->attr_cnt, a = v->odd_args->args; 0 < i; i--, a++) {
36
+ if (Qnil != *a) {
37
+ rb_gc_mark(*a);
38
+ }
39
+ }
40
+ }
55
41
  }
56
- #if HAVE_LIBPTHREAD
42
+ #ifdef HAVE_PTHREAD_MUTEX_INIT
57
43
  pthread_mutex_unlock(&stack->mutex);
58
44
  #else
59
45
  rb_mutex_unlock(stack->mutex);
@@ -62,37 +48,43 @@ mark(void *ptr) {
62
48
 
63
49
  VALUE
64
50
  oj_stack_init(ValStack stack) {
65
- #if HAVE_LIBPTHREAD
66
- pthread_mutex_init(&stack->mutex, 0);
51
+ #ifdef HAVE_PTHREAD_MUTEX_INIT
52
+ int err;
53
+
54
+ if (0 != (err = pthread_mutex_init(&stack->mutex, 0))) {
55
+ rb_raise(rb_eException, "failed to initialize a mutex. %s", strerror(err));
56
+ }
67
57
  #else
68
58
  stack->mutex = rb_mutex_new();
69
59
  #endif
70
- stack->head = stack->base;
71
- stack->end = stack->base + sizeof(stack->base) / sizeof(struct _Val);
72
- stack->tail = stack->head;
73
- stack->head->val = Qundef;
74
- stack->head->key = 0;
75
- stack->head->key_val = Qundef;
76
- stack->head->classname = 0;
77
- stack->head->klen = 0;
78
- stack->head->clen = 0;
79
- stack->head->next = NEXT_NONE;
60
+ stack->head = stack->base;
61
+ stack->end = stack->base + sizeof(stack->base) / sizeof(struct _val);
62
+ stack->tail = stack->head;
63
+ stack->head->val = Qundef;
64
+ stack->head->key = NULL;
65
+ stack->head->key_val = Qundef;
66
+ stack->head->classname = NULL;
67
+ stack->head->odd_args = NULL;
68
+ stack->head->clas = Qundef;
69
+ stack->head->klen = 0;
70
+ stack->head->clen = 0;
71
+ stack->head->next = NEXT_NONE;
72
+
80
73
  return Data_Wrap_Struct(oj_cstack_class, mark, 0, stack);
81
74
  }
82
75
 
83
- const char*
84
- oj_stack_next_string(ValNext n) {
76
+ const char *oj_stack_next_string(ValNext n) {
85
77
  switch (n) {
86
- case NEXT_ARRAY_NEW: return "array element or close";
87
- case NEXT_ARRAY_ELEMENT: return "array element";
88
- case NEXT_ARRAY_COMMA: return "comma";
89
- case NEXT_HASH_NEW: return "hash pair or close";
90
- case NEXT_HASH_KEY: return "hash key";
91
- case NEXT_HASH_COLON: return "colon";
92
- case NEXT_HASH_VALUE: return "hash value";
93
- case NEXT_HASH_COMMA: return "comma";
94
- case NEXT_NONE: break;
95
- default: break;
78
+ case NEXT_ARRAY_NEW: return "array element or close";
79
+ case NEXT_ARRAY_ELEMENT: return "array element";
80
+ case NEXT_ARRAY_COMMA: return "comma";
81
+ case NEXT_HASH_NEW: return "hash pair or close";
82
+ case NEXT_HASH_KEY: return "hash key";
83
+ case NEXT_HASH_COLON: return "colon";
84
+ case NEXT_HASH_VALUE: return "hash value";
85
+ case NEXT_HASH_COMMA: return "comma";
86
+ case NEXT_NONE: break;
87
+ default: break;
96
88
  }
97
89
  return "nothing";
98
90
  }
data/ext/oj/val_stack.h CHANGED
@@ -1,188 +1,150 @@
1
- /* val_stack.h
2
- * Copyright (c) 2011, Peter Ohler
3
- * All rights reserved.
4
- *
5
- * Redistribution and use in source and binary forms, with or without
6
- * modification, are permitted provided that the following conditions are met:
7
- *
8
- * - Redistributions of source code must retain the above copyright notice, this
9
- * list of conditions and the following disclaimer.
10
- *
11
- * - Redistributions in binary form must reproduce the above copyright notice,
12
- * this list of conditions and the following disclaimer in the documentation
13
- * and/or other materials provided with the distribution.
14
- *
15
- * - Neither the name of Peter Ohler nor the names of its contributors may be
16
- * used to endorse or promote products derived from this software without
17
- * specific prior written permission.
18
- *
19
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
- */
30
-
31
- #ifndef __OJ_VAL_STACK_H__
32
- #define __OJ_VAL_STACK_H__
1
+ // Copyright (c) 2011 Peter Ohler. All rights reserved.
2
+ // Licensed under the MIT License. See LICENSE file in the project root for license details.
3
+
4
+ #ifndef OJ_VAL_STACK_H
5
+ #define OJ_VAL_STACK_H
33
6
 
34
- #include "ruby.h"
35
- #include "odd.h"
36
7
  #include <stdint.h>
37
- #if HAVE_LIBPTHREAD
8
+
9
+ #include "odd.h"
10
+ #include "ruby.h"
11
+ #ifdef HAVE_PTHREAD_MUTEX_INIT
38
12
  #include <pthread.h>
39
13
  #endif
40
14
 
41
- #define STACK_INC 64
15
+ #define STACK_INC 64
42
16
 
43
17
  typedef enum {
44
- NEXT_NONE = 0,
45
- NEXT_ARRAY_NEW = 'a',
46
- NEXT_ARRAY_ELEMENT = 'e',
47
- NEXT_ARRAY_COMMA = ',',
48
- NEXT_HASH_NEW = 'h',
49
- NEXT_HASH_KEY = 'k',
50
- NEXT_HASH_COLON = ':',
51
- NEXT_HASH_VALUE = 'v',
52
- NEXT_HASH_COMMA = 'n',
18
+ NEXT_NONE = 0,
19
+ NEXT_ARRAY_NEW = 'a',
20
+ NEXT_ARRAY_ELEMENT = 'e',
21
+ NEXT_ARRAY_COMMA = ',',
22
+ NEXT_HASH_NEW = 'h',
23
+ NEXT_HASH_KEY = 'k',
24
+ NEXT_HASH_COLON = ':',
25
+ NEXT_HASH_VALUE = 'v',
26
+ NEXT_HASH_COMMA = 'n',
53
27
  } ValNext;
54
28
 
55
- typedef struct _Val {
56
- volatile VALUE val;
57
- const char *key;
58
- char karray[32];
59
- volatile VALUE key_val;
60
- union {
61
- struct {
62
- const char *classname;
63
- VALUE clas;
64
- };
65
- OddArgs odd_args;
66
- };
67
- uint16_t klen;
68
- uint16_t clen;
69
- char next; // ValNext
70
- char k1; // first original character in the key
71
- char kalloc;
72
- } *Val;
73
-
74
- typedef struct _ValStack {
75
- struct _Val base[STACK_INC];
76
- Val head; // current stack
77
- Val end; // stack end
78
- Val tail; // pointer to one past last element name on stack
79
- #if HAVE_LIBPTHREAD
80
- pthread_mutex_t mutex;
29
+ typedef struct _val {
30
+ volatile VALUE val;
31
+ const char * key;
32
+ char karray[32];
33
+ volatile VALUE key_val;
34
+ const char * classname;
35
+ VALUE clas;
36
+ OddArgs odd_args;
37
+ uint16_t klen;
38
+ uint16_t clen;
39
+ char next; // ValNext
40
+ char k1; // first original character in the key
41
+ char kalloc;
42
+ } * Val;
43
+
44
+ typedef struct _valStack {
45
+ struct _val base[STACK_INC];
46
+ Val head; // current stack
47
+ Val end; // stack end
48
+ Val tail; // pointer to one past last element name on stack
49
+ #ifdef HAVE_PTHREAD_MUTEX_INIT
50
+ pthread_mutex_t mutex;
81
51
  #else
82
- VALUE mutex;
52
+ VALUE mutex;
83
53
  #endif
84
54
 
85
- } *ValStack;
55
+ } * ValStack;
86
56
 
87
- extern VALUE oj_stack_init(ValStack stack);
57
+ extern VALUE oj_stack_init(ValStack stack);
88
58
 
89
- inline static int
90
- stack_empty(ValStack stack) {
59
+ inline static int stack_empty(ValStack stack) {
91
60
  return (stack->head == stack->tail);
92
61
  }
93
62
 
94
- inline static void
95
- stack_cleanup(ValStack stack) {
63
+ inline static void stack_cleanup(ValStack stack) {
96
64
  if (stack->base != stack->head) {
97
65
  xfree(stack->head);
98
- stack->head = NULL;
66
+ stack->head = NULL;
99
67
  }
100
68
  }
101
69
 
102
- inline static void
103
- stack_push(ValStack stack, VALUE val, ValNext next) {
70
+ inline static void stack_push(ValStack stack, VALUE val, ValNext next) {
104
71
  if (stack->end <= stack->tail) {
105
- size_t len = stack->end - stack->head;
106
- size_t toff = stack->tail - stack->head;
107
- Val head = stack->head;
108
-
109
- // A realloc can trigger a GC so make sure it happens outside the lock
110
- // but lock before changing pointers.
111
- if (stack->base == stack->head) {
112
- head = ALLOC_N(struct _Val, len + STACK_INC);
113
- memcpy(head, stack->base, sizeof(struct _Val) * len);
114
- } else {
115
- REALLOC_N(head, struct _Val, len + STACK_INC);
116
- }
117
- #if HAVE_LIBPTHREAD
118
- pthread_mutex_lock(&stack->mutex);
72
+ size_t len = stack->end - stack->head;
73
+ size_t toff = stack->tail - stack->head;
74
+ Val head = stack->head;
75
+
76
+ // A realloc can trigger a GC so make sure it happens outside the lock
77
+ // but lock before changing pointers.
78
+ if (stack->base == stack->head) {
79
+ head = ALLOC_N(struct _val, len + STACK_INC);
80
+ memcpy(head, stack->base, sizeof(struct _val) * len);
81
+ } else {
82
+ REALLOC_N(head, struct _val, len + STACK_INC);
83
+ }
84
+ #ifdef HAVE_PTHREAD_MUTEX_INIT
85
+ pthread_mutex_lock(&stack->mutex);
119
86
  #else
120
- rb_mutex_lock(stack->mutex);
87
+ rb_mutex_lock(stack->mutex);
121
88
  #endif
122
- stack->head = head;
123
- stack->tail = stack->head + toff;
124
- stack->end = stack->head + len + STACK_INC;
125
- #if HAVE_LIBPTHREAD
126
- pthread_mutex_unlock(&stack->mutex);
89
+ stack->head = head;
90
+ stack->tail = stack->head + toff;
91
+ stack->end = stack->head + len + STACK_INC;
92
+ #ifdef HAVE_PTHREAD_MUTEX_INIT
93
+ pthread_mutex_unlock(&stack->mutex);
127
94
  #else
128
- rb_mutex_unlock(stack->mutex);
95
+ rb_mutex_unlock(stack->mutex);
129
96
  #endif
130
97
  }
131
- stack->tail->val = val;
132
- stack->tail->next = next;
98
+ stack->tail->val = val;
99
+ stack->tail->next = next;
133
100
  stack->tail->classname = NULL;
134
- stack->tail->clas = Qundef;
135
- stack->tail->key = 0;
136
- stack->tail->key_val = Qundef;
137
- stack->tail->clen = 0;
138
- stack->tail->klen = 0;
139
- stack->tail->kalloc = 0;
101
+ stack->tail->clas = Qundef;
102
+ stack->tail->odd_args = NULL;
103
+ stack->tail->key = 0;
104
+ stack->tail->key_val = Qundef;
105
+ stack->tail->clen = 0;
106
+ stack->tail->klen = 0;
107
+ stack->tail->kalloc = 0;
140
108
  stack->tail++;
141
109
  }
142
110
 
143
- inline static size_t
144
- stack_size(ValStack stack) {
111
+ inline static size_t stack_size(ValStack stack) {
145
112
  return stack->tail - stack->head;
146
113
  }
147
114
 
148
- inline static Val
149
- stack_peek(ValStack stack) {
115
+ inline static Val stack_peek(ValStack stack) {
150
116
  if (stack->head < stack->tail) {
151
- return stack->tail - 1;
117
+ return stack->tail - 1;
152
118
  }
153
119
  return 0;
154
120
  }
155
121
 
156
- inline static Val
157
- stack_peek_up(ValStack stack) {
122
+ inline static Val stack_peek_up(ValStack stack) {
158
123
  if (stack->head < stack->tail - 1) {
159
- return stack->tail - 2;
124
+ return stack->tail - 2;
160
125
  }
161
126
  return 0;
162
127
  }
163
128
 
164
- inline static Val
165
- stack_prev(ValStack stack) {
129
+ inline static Val stack_prev(ValStack stack) {
166
130
  return stack->tail;
167
131
  }
168
132
 
169
- inline static VALUE
170
- stack_head_val(ValStack stack) {
133
+ inline static VALUE stack_head_val(ValStack stack) {
171
134
  if (Qundef != stack->head->val) {
172
- return stack->head->val;
135
+ return stack->head->val;
173
136
  }
174
137
  return Qnil;
175
138
  }
176
139
 
177
- inline static Val
178
- stack_pop(ValStack stack) {
140
+ inline static Val stack_pop(ValStack stack) {
179
141
  if (stack->head < stack->tail) {
180
- stack->tail--;
181
- return stack->tail;
142
+ stack->tail--;
143
+ return stack->tail;
182
144
  }
183
145
  return 0;
184
146
  }
185
147
 
186
- extern const char* oj_stack_next_string(ValNext n);
148
+ extern const char *oj_stack_next_string(ValNext n);
187
149
 
188
- #endif /* __OJ_VAL_STACK_H__ */
150
+ #endif /* OJ_VAL_STACK_H */