oj 2.18.5 → 3.16.11
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.
- checksums.yaml +5 -5
- data/CHANGELOG.md +1452 -0
- data/README.md +53 -221
- data/RELEASE_NOTES.md +61 -0
- data/ext/oj/buf.h +54 -72
- data/ext/oj/cache.c +329 -0
- data/ext/oj/cache.h +22 -0
- data/ext/oj/cache8.c +61 -63
- data/ext/oj/cache8.h +12 -39
- data/ext/oj/circarray.c +38 -67
- data/ext/oj/circarray.h +16 -42
- data/ext/oj/code.c +214 -0
- data/ext/oj/code.h +40 -0
- data/ext/oj/compat.c +194 -110
- data/ext/oj/custom.c +1074 -0
- data/ext/oj/debug.c +126 -0
- data/ext/oj/dump.c +1276 -2494
- data/ext/oj/dump.h +110 -0
- data/ext/oj/dump_compat.c +897 -0
- data/ext/oj/dump_leaf.c +162 -0
- data/ext/oj/dump_object.c +710 -0
- data/ext/oj/dump_strict.c +399 -0
- data/ext/oj/encode.h +7 -42
- data/ext/oj/encoder.c +43 -0
- data/ext/oj/err.c +28 -53
- data/ext/oj/err.h +49 -46
- data/ext/oj/extconf.rb +33 -32
- data/ext/oj/fast.c +1082 -1098
- data/ext/oj/intern.c +313 -0
- data/ext/oj/intern.h +22 -0
- data/ext/oj/mem.c +318 -0
- data/ext/oj/mem.h +53 -0
- data/ext/oj/mimic_json.c +919 -0
- data/ext/oj/object.c +545 -625
- data/ext/oj/odd.c +158 -168
- data/ext/oj/odd.h +32 -58
- data/ext/oj/oj.c +1727 -2080
- data/ext/oj/oj.h +334 -259
- data/ext/oj/parse.c +974 -753
- data/ext/oj/parse.h +97 -90
- data/ext/oj/parser.c +1600 -0
- data/ext/oj/parser.h +103 -0
- data/ext/oj/rails.c +1478 -0
- data/ext/oj/rails.h +18 -0
- data/ext/oj/reader.c +136 -163
- data/ext/oj/reader.h +76 -112
- data/ext/oj/resolve.c +45 -94
- data/ext/oj/resolve.h +7 -34
- data/ext/oj/rxclass.c +144 -0
- data/ext/oj/rxclass.h +26 -0
- data/ext/oj/saj.c +445 -511
- data/ext/oj/saj2.c +584 -0
- data/ext/oj/saj2.h +23 -0
- data/ext/oj/scp.c +82 -143
- data/ext/oj/simd.h +10 -0
- data/ext/oj/sparse.c +749 -644
- data/ext/oj/stream_writer.c +329 -0
- data/ext/oj/strict.c +114 -112
- data/ext/oj/string_writer.c +517 -0
- data/ext/oj/trace.c +72 -0
- data/ext/oj/trace.h +55 -0
- data/ext/oj/usual.c +1218 -0
- data/ext/oj/usual.h +69 -0
- data/ext/oj/util.c +136 -0
- data/ext/oj/util.h +20 -0
- data/ext/oj/val_stack.c +75 -72
- data/ext/oj/val_stack.h +94 -127
- data/ext/oj/validate.c +46 -0
- data/ext/oj/wab.c +586 -0
- data/lib/oj/active_support_helper.rb +1 -3
- data/lib/oj/bag.rb +8 -1
- data/lib/oj/easy_hash.rb +21 -13
- data/lib/oj/error.rb +10 -12
- data/lib/oj/json.rb +188 -0
- data/lib/oj/mimic.rb +165 -26
- data/lib/oj/saj.rb +20 -6
- data/lib/oj/schandler.rb +5 -4
- data/lib/oj/state.rb +135 -0
- data/lib/oj/version.rb +2 -3
- data/lib/oj.rb +3 -31
- data/pages/Advanced.md +22 -0
- data/pages/Compatibility.md +25 -0
- data/pages/Custom.md +23 -0
- data/pages/Encoding.md +65 -0
- data/pages/InstallOptions.md +20 -0
- data/pages/JsonGem.md +94 -0
- data/pages/Modes.md +161 -0
- data/pages/Options.md +337 -0
- data/pages/Parser.md +309 -0
- data/pages/Rails.md +167 -0
- data/pages/Security.md +20 -0
- data/pages/WAB.md +13 -0
- metadata +126 -163
- data/ext/oj/hash.c +0 -163
- data/ext/oj/hash.h +0 -46
- data/ext/oj/hash_test.c +0 -512
- data/test/_test_active.rb +0 -76
- data/test/_test_active_mimic.rb +0 -96
- data/test/_test_mimic_rails.rb +0 -126
- data/test/activesupport_datetime_test.rb +0 -23
- data/test/bug.rb +0 -51
- data/test/bug2.rb +0 -10
- data/test/bug3.rb +0 -46
- data/test/bug_fast.rb +0 -32
- data/test/bug_load.rb +0 -24
- data/test/crash.rb +0 -111
- data/test/curl/curl_oj.rb +0 -46
- data/test/curl/get_oj.rb +0 -24
- data/test/curl/just_curl.rb +0 -31
- data/test/curl/just_oj.rb +0 -51
- data/test/example.rb +0 -11
- data/test/files.rb +0 -29
- data/test/foo.rb +0 -24
- data/test/helper.rb +0 -27
- data/test/io.rb +0 -48
- data/test/isolated/shared.rb +0 -310
- data/test/isolated/test_mimic_after.rb +0 -13
- data/test/isolated/test_mimic_alone.rb +0 -12
- data/test/isolated/test_mimic_as_json.rb +0 -45
- data/test/isolated/test_mimic_before.rb +0 -13
- data/test/isolated/test_mimic_define.rb +0 -28
- data/test/isolated/test_mimic_rails_after.rb +0 -22
- data/test/isolated/test_mimic_rails_before.rb +0 -21
- data/test/isolated/test_mimic_rails_datetime.rb +0 -27
- data/test/isolated/test_mimic_redefine.rb +0 -15
- data/test/mod.rb +0 -16
- data/test/perf.rb +0 -107
- data/test/perf_compat.rb +0 -128
- data/test/perf_fast.rb +0 -164
- data/test/perf_file.rb +0 -64
- data/test/perf_object.rb +0 -138
- data/test/perf_saj.rb +0 -109
- data/test/perf_scp.rb +0 -151
- data/test/perf_simple.rb +0 -287
- data/test/perf_strict.rb +0 -128
- data/test/rails.rb +0 -50
- data/test/russian.rb +0 -18
- data/test/sample/change.rb +0 -14
- data/test/sample/dir.rb +0 -19
- data/test/sample/doc.rb +0 -36
- data/test/sample/file.rb +0 -48
- data/test/sample/group.rb +0 -16
- data/test/sample/hasprops.rb +0 -16
- data/test/sample/layer.rb +0 -12
- data/test/sample/line.rb +0 -20
- data/test/sample/oval.rb +0 -10
- data/test/sample/rect.rb +0 -10
- data/test/sample/shape.rb +0 -35
- data/test/sample/text.rb +0 -20
- data/test/sample.rb +0 -55
- data/test/sample_json.rb +0 -37
- data/test/struct.rb +0 -29
- data/test/test_compat.rb +0 -398
- data/test/test_debian.rb +0 -53
- data/test/test_fast.rb +0 -458
- data/test/test_file.rb +0 -245
- data/test/test_gc.rb +0 -49
- data/test/test_hash.rb +0 -29
- data/test/test_object.rb +0 -745
- data/test/test_saj.rb +0 -186
- data/test/test_scp.rb +0 -396
- data/test/test_serializer.rb +0 -59
- data/test/test_strict.rb +0 -254
- data/test/test_various.rb +0 -1383
- data/test/test_writer.rb +0 -308
- data/test/write_timebars.rb +0 -31
data/ext/oj/usual.h
ADDED
@@ -0,0 +1,69 @@
|
|
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)(struct _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
|
+
bool raise_on_empty;
|
64
|
+
} *Usual;
|
65
|
+
|
66
|
+
// Initialize the parser with the usual delegate. If the usual delegate is
|
67
|
+
// wrapped then this function is called first and then the parser functions
|
68
|
+
// can be replaced.
|
69
|
+
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,98 +1,101 @@
|
|
1
|
-
|
2
|
-
|
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
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
6
|
+
#include <string.h>
|
7
|
+
|
8
|
+
#include "odd.h"
|
9
|
+
#include "oj.h"
|
38
10
|
|
39
|
-
|
40
|
-
|
11
|
+
static void stack_mark(void *ptr) {
|
12
|
+
ValStack stack = (ValStack)ptr;
|
13
|
+
Val v;
|
14
|
+
|
15
|
+
if (NULL == ptr) {
|
16
|
+
return;
|
41
17
|
}
|
42
|
-
#
|
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
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
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
|
-
#
|
42
|
+
#ifdef HAVE_PTHREAD_MUTEX_INIT
|
57
43
|
pthread_mutex_unlock(&stack->mutex);
|
58
|
-
#
|
44
|
+
#else
|
59
45
|
rb_mutex_unlock(stack->mutex);
|
60
46
|
#endif
|
61
47
|
}
|
62
48
|
|
49
|
+
static const rb_data_type_t oj_stack_type = {
|
50
|
+
"Oj/stack",
|
51
|
+
{
|
52
|
+
stack_mark,
|
53
|
+
NULL,
|
54
|
+
NULL,
|
55
|
+
},
|
56
|
+
0,
|
57
|
+
0,
|
58
|
+
};
|
59
|
+
|
63
60
|
VALUE
|
64
61
|
oj_stack_init(ValStack stack) {
|
65
|
-
#
|
66
|
-
|
67
|
-
|
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
|
68
69
|
stack->mutex = rb_mutex_new();
|
69
70
|
#endif
|
70
|
-
stack->head
|
71
|
-
stack->end
|
72
|
-
stack->tail
|
73
|
-
stack->head->val
|
74
|
-
stack->head->key
|
75
|
-
stack->head->key_val
|
76
|
-
stack->head->classname =
|
77
|
-
stack->head->
|
78
|
-
stack->head->
|
79
|
-
stack->head->
|
80
|
-
|
71
|
+
stack->head = stack->base;
|
72
|
+
stack->end = stack->base + sizeof(stack->base) / sizeof(struct _val);
|
73
|
+
stack->tail = stack->head;
|
74
|
+
stack->head->val = Qundef;
|
75
|
+
stack->head->key = NULL;
|
76
|
+
stack->head->key_val = Qundef;
|
77
|
+
stack->head->classname = NULL;
|
78
|
+
stack->head->odd_args = NULL;
|
79
|
+
stack->head->clas = Qundef;
|
80
|
+
stack->head->klen = 0;
|
81
|
+
stack->head->clen = 0;
|
82
|
+
stack->head->next = NEXT_NONE;
|
83
|
+
|
84
|
+
return TypedData_Wrap_Struct(oj_cstack_class, &oj_stack_type, stack);
|
81
85
|
}
|
82
86
|
|
83
|
-
const char*
|
84
|
-
oj_stack_next_string(ValNext n) {
|
87
|
+
const char *oj_stack_next_string(ValNext n) {
|
85
88
|
switch (n) {
|
86
|
-
case NEXT_ARRAY_NEW:
|
87
|
-
case NEXT_ARRAY_ELEMENT:
|
88
|
-
case NEXT_ARRAY_COMMA:
|
89
|
-
case NEXT_HASH_NEW:
|
90
|
-
case NEXT_HASH_KEY:
|
91
|
-
case NEXT_HASH_COLON:
|
92
|
-
case NEXT_HASH_VALUE:
|
93
|
-
case NEXT_HASH_COMMA:
|
94
|
-
case NEXT_NONE:
|
95
|
-
default:
|
89
|
+
case NEXT_ARRAY_NEW: return "array element or close";
|
90
|
+
case NEXT_ARRAY_ELEMENT: return "array element";
|
91
|
+
case NEXT_ARRAY_COMMA: return "comma";
|
92
|
+
case NEXT_HASH_NEW: return "hash pair or close";
|
93
|
+
case NEXT_HASH_KEY: return "hash key";
|
94
|
+
case NEXT_HASH_COLON: return "colon";
|
95
|
+
case NEXT_HASH_VALUE: return "hash value";
|
96
|
+
case NEXT_HASH_COMMA: return "comma";
|
97
|
+
case NEXT_NONE: break;
|
98
|
+
default: break;
|
96
99
|
}
|
97
100
|
return "nothing";
|
98
101
|
}
|
data/ext/oj/val_stack.h
CHANGED
@@ -1,184 +1,151 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
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
|
-
|
8
|
+
|
9
|
+
#include "mem.h"
|
10
|
+
#include "odd.h"
|
11
|
+
#include "ruby.h"
|
12
|
+
#ifdef HAVE_PTHREAD_MUTEX_INIT
|
38
13
|
#include <pthread.h>
|
39
14
|
#endif
|
40
15
|
|
41
|
-
#define STACK_INC
|
16
|
+
#define STACK_INC 64
|
42
17
|
|
43
18
|
typedef enum {
|
44
|
-
NEXT_NONE
|
45
|
-
NEXT_ARRAY_NEW
|
46
|
-
NEXT_ARRAY_ELEMENT
|
47
|
-
NEXT_ARRAY_COMMA
|
48
|
-
NEXT_HASH_NEW
|
49
|
-
NEXT_HASH_KEY
|
50
|
-
NEXT_HASH_COLON
|
51
|
-
NEXT_HASH_VALUE
|
52
|
-
NEXT_HASH_COMMA
|
19
|
+
NEXT_NONE = 0,
|
20
|
+
NEXT_ARRAY_NEW = 'a',
|
21
|
+
NEXT_ARRAY_ELEMENT = 'e',
|
22
|
+
NEXT_ARRAY_COMMA = ',',
|
23
|
+
NEXT_HASH_NEW = 'h',
|
24
|
+
NEXT_HASH_KEY = 'k',
|
25
|
+
NEXT_HASH_COLON = ':',
|
26
|
+
NEXT_HASH_VALUE = 'v',
|
27
|
+
NEXT_HASH_COMMA = 'n',
|
53
28
|
} ValNext;
|
54
29
|
|
55
|
-
typedef struct
|
56
|
-
volatile VALUE
|
57
|
-
const char
|
58
|
-
char
|
59
|
-
volatile VALUE
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
uint16_t
|
65
|
-
|
66
|
-
char
|
67
|
-
char
|
68
|
-
char kalloc;
|
30
|
+
typedef struct _val {
|
31
|
+
volatile VALUE val;
|
32
|
+
const char *key;
|
33
|
+
char karray[32];
|
34
|
+
volatile VALUE key_val;
|
35
|
+
const char *classname;
|
36
|
+
VALUE clas;
|
37
|
+
OddArgs odd_args;
|
38
|
+
uint16_t klen;
|
39
|
+
uint16_t clen;
|
40
|
+
char next; // ValNext
|
41
|
+
char k1; // first original character in the key
|
42
|
+
char kalloc;
|
69
43
|
} *Val;
|
70
44
|
|
71
|
-
typedef struct
|
72
|
-
struct
|
73
|
-
Val
|
74
|
-
Val
|
75
|
-
Val
|
76
|
-
#
|
77
|
-
pthread_mutex_t
|
78
|
-
#
|
79
|
-
VALUE
|
45
|
+
typedef struct _valStack {
|
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;
|
80
54
|
#endif
|
81
55
|
|
82
56
|
} *ValStack;
|
83
57
|
|
84
|
-
extern VALUE
|
58
|
+
extern VALUE oj_stack_init(ValStack stack);
|
85
59
|
|
86
|
-
inline static int
|
87
|
-
stack_empty(ValStack stack) {
|
60
|
+
inline static int stack_empty(ValStack stack) {
|
88
61
|
return (stack->head == stack->tail);
|
89
62
|
}
|
90
63
|
|
91
|
-
inline static void
|
92
|
-
stack_cleanup(ValStack stack) {
|
64
|
+
inline static void stack_cleanup(ValStack stack) {
|
93
65
|
if (stack->base != stack->head) {
|
94
|
-
|
95
|
-
|
66
|
+
OJ_R_FREE(stack->head);
|
67
|
+
stack->head = NULL;
|
96
68
|
}
|
97
69
|
}
|
98
70
|
|
99
|
-
inline static void
|
100
|
-
stack_push(ValStack stack, VALUE val, ValNext next) {
|
71
|
+
inline static void stack_push(ValStack stack, VALUE val, ValNext next) {
|
101
72
|
if (stack->end <= stack->tail) {
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
#
|
115
|
-
|
116
|
-
#
|
117
|
-
|
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.
|
79
|
+
if (stack->base == stack->head) {
|
80
|
+
head = OJ_R_ALLOC_N(struct _val, len + STACK_INC);
|
81
|
+
memcpy(head, stack->base, sizeof(struct _val) * len);
|
82
|
+
} else {
|
83
|
+
OJ_R_REALLOC_N(head, struct _val, len + STACK_INC);
|
84
|
+
}
|
85
|
+
#ifdef HAVE_PTHREAD_MUTEX_INIT
|
86
|
+
pthread_mutex_lock(&stack->mutex);
|
87
|
+
#else
|
88
|
+
rb_mutex_lock(stack->mutex);
|
118
89
|
#endif
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
#
|
123
|
-
|
124
|
-
#
|
125
|
-
|
90
|
+
stack->head = head;
|
91
|
+
stack->tail = stack->head + toff;
|
92
|
+
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);
|
126
97
|
#endif
|
127
98
|
}
|
128
|
-
stack->tail->val
|
129
|
-
stack->tail->next
|
130
|
-
stack->tail->classname =
|
131
|
-
stack->tail->
|
132
|
-
stack->tail->
|
133
|
-
stack->tail->
|
134
|
-
stack->tail->
|
135
|
-
stack->tail->
|
99
|
+
stack->tail->val = val;
|
100
|
+
stack->tail->next = next;
|
101
|
+
stack->tail->classname = NULL;
|
102
|
+
stack->tail->clas = Qundef;
|
103
|
+
stack->tail->odd_args = NULL;
|
104
|
+
stack->tail->key = 0;
|
105
|
+
stack->tail->key_val = Qundef;
|
106
|
+
stack->tail->clen = 0;
|
107
|
+
stack->tail->klen = 0;
|
108
|
+
stack->tail->kalloc = 0;
|
136
109
|
stack->tail++;
|
137
110
|
}
|
138
111
|
|
139
|
-
inline static size_t
|
140
|
-
stack_size(ValStack stack) {
|
112
|
+
inline static size_t stack_size(ValStack stack) {
|
141
113
|
return stack->tail - stack->head;
|
142
114
|
}
|
143
115
|
|
144
|
-
inline static Val
|
145
|
-
stack_peek(ValStack stack) {
|
116
|
+
inline static Val stack_peek(ValStack stack) {
|
146
117
|
if (stack->head < stack->tail) {
|
147
|
-
|
118
|
+
return stack->tail - 1;
|
148
119
|
}
|
149
120
|
return 0;
|
150
121
|
}
|
151
122
|
|
152
|
-
inline static Val
|
153
|
-
stack_peek_up(ValStack stack) {
|
123
|
+
inline static Val stack_peek_up(ValStack stack) {
|
154
124
|
if (stack->head < stack->tail - 1) {
|
155
|
-
|
125
|
+
return stack->tail - 2;
|
156
126
|
}
|
157
127
|
return 0;
|
158
128
|
}
|
159
129
|
|
160
|
-
inline static Val
|
161
|
-
stack_prev(ValStack stack) {
|
130
|
+
inline static Val stack_prev(ValStack stack) {
|
162
131
|
return stack->tail;
|
163
132
|
}
|
164
133
|
|
165
|
-
inline static VALUE
|
166
|
-
stack_head_val(ValStack stack) {
|
134
|
+
inline static VALUE stack_head_val(ValStack stack) {
|
167
135
|
if (Qundef != stack->head->val) {
|
168
|
-
|
136
|
+
return stack->head->val;
|
169
137
|
}
|
170
138
|
return Qnil;
|
171
139
|
}
|
172
140
|
|
173
|
-
inline static Val
|
174
|
-
stack_pop(ValStack stack) {
|
141
|
+
inline static Val stack_pop(ValStack stack) {
|
175
142
|
if (stack->head < stack->tail) {
|
176
|
-
|
177
|
-
|
143
|
+
stack->tail--;
|
144
|
+
return stack->tail;
|
178
145
|
}
|
179
146
|
return 0;
|
180
147
|
}
|
181
148
|
|
182
|
-
extern const char*
|
149
|
+
extern const char *oj_stack_next_string(ValNext n);
|
183
150
|
|
184
|
-
#endif /*
|
151
|
+
#endif /* OJ_VAL_STACK_H */
|