oj_windows 3.16.15

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 (102) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +44 -0
  3. data/LICENSE +21 -0
  4. data/README.md +164 -0
  5. data/ext/oj_windows/buf.h +85 -0
  6. data/ext/oj_windows/cache.c +339 -0
  7. data/ext/oj_windows/cache.h +22 -0
  8. data/ext/oj_windows/cache8.c +105 -0
  9. data/ext/oj_windows/cache8.h +21 -0
  10. data/ext/oj_windows/circarray.c +64 -0
  11. data/ext/oj_windows/circarray.h +22 -0
  12. data/ext/oj_windows/code.c +214 -0
  13. data/ext/oj_windows/code.h +40 -0
  14. data/ext/oj_windows/compat.c +239 -0
  15. data/ext/oj_windows/custom.c +1074 -0
  16. data/ext/oj_windows/debug.c +126 -0
  17. data/ext/oj_windows/dump.c +1556 -0
  18. data/ext/oj_windows/dump.h +110 -0
  19. data/ext/oj_windows/dump_compat.c +901 -0
  20. data/ext/oj_windows/dump_leaf.c +162 -0
  21. data/ext/oj_windows/dump_object.c +710 -0
  22. data/ext/oj_windows/dump_strict.c +405 -0
  23. data/ext/oj_windows/encode.h +16 -0
  24. data/ext/oj_windows/err.c +57 -0
  25. data/ext/oj_windows/err.h +67 -0
  26. data/ext/oj_windows/extconf.rb +77 -0
  27. data/ext/oj_windows/fast.c +1710 -0
  28. data/ext/oj_windows/intern.c +325 -0
  29. data/ext/oj_windows/intern.h +22 -0
  30. data/ext/oj_windows/mem.c +320 -0
  31. data/ext/oj_windows/mem.h +53 -0
  32. data/ext/oj_windows/mimic_json.c +919 -0
  33. data/ext/oj_windows/object.c +726 -0
  34. data/ext/oj_windows/odd.c +245 -0
  35. data/ext/oj_windows/odd.h +43 -0
  36. data/ext/oj_windows/oj.c +2097 -0
  37. data/ext/oj_windows/oj.h +420 -0
  38. data/ext/oj_windows/parse.c +1317 -0
  39. data/ext/oj_windows/parse.h +113 -0
  40. data/ext/oj_windows/parser.c +1600 -0
  41. data/ext/oj_windows/parser.h +103 -0
  42. data/ext/oj_windows/rails.c +1484 -0
  43. data/ext/oj_windows/rails.h +18 -0
  44. data/ext/oj_windows/reader.c +222 -0
  45. data/ext/oj_windows/reader.h +137 -0
  46. data/ext/oj_windows/resolve.c +80 -0
  47. data/ext/oj_windows/resolve.h +12 -0
  48. data/ext/oj_windows/rxclass.c +144 -0
  49. data/ext/oj_windows/rxclass.h +26 -0
  50. data/ext/oj_windows/saj.c +675 -0
  51. data/ext/oj_windows/saj2.c +584 -0
  52. data/ext/oj_windows/saj2.h +23 -0
  53. data/ext/oj_windows/scp.c +187 -0
  54. data/ext/oj_windows/simd.h +47 -0
  55. data/ext/oj_windows/sparse.c +946 -0
  56. data/ext/oj_windows/stream_writer.c +329 -0
  57. data/ext/oj_windows/strict.c +189 -0
  58. data/ext/oj_windows/string_writer.c +517 -0
  59. data/ext/oj_windows/trace.c +72 -0
  60. data/ext/oj_windows/trace.h +55 -0
  61. data/ext/oj_windows/usual.c +1218 -0
  62. data/ext/oj_windows/usual.h +69 -0
  63. data/ext/oj_windows/util.c +136 -0
  64. data/ext/oj_windows/util.h +20 -0
  65. data/ext/oj_windows/val_stack.c +101 -0
  66. data/ext/oj_windows/val_stack.h +151 -0
  67. data/ext/oj_windows/validate.c +46 -0
  68. data/ext/oj_windows/wab.c +584 -0
  69. data/lib/oj/active_support_helper.rb +39 -0
  70. data/lib/oj/bag.rb +95 -0
  71. data/lib/oj/easy_hash.rb +52 -0
  72. data/lib/oj/error.rb +21 -0
  73. data/lib/oj/json.rb +188 -0
  74. data/lib/oj/mimic.rb +301 -0
  75. data/lib/oj/saj.rb +80 -0
  76. data/lib/oj/schandler.rb +143 -0
  77. data/lib/oj/state.rb +135 -0
  78. data/lib/oj/version.rb +4 -0
  79. data/lib/oj_windows/active_support_helper.rb +39 -0
  80. data/lib/oj_windows/bag.rb +95 -0
  81. data/lib/oj_windows/easy_hash.rb +52 -0
  82. data/lib/oj_windows/error.rb +21 -0
  83. data/lib/oj_windows/json.rb +188 -0
  84. data/lib/oj_windows/mimic.rb +301 -0
  85. data/lib/oj_windows/saj.rb +80 -0
  86. data/lib/oj_windows/schandler.rb +143 -0
  87. data/lib/oj_windows/state.rb +135 -0
  88. data/lib/oj_windows/version.rb +4 -0
  89. data/lib/oj_windows.rb +15 -0
  90. data/pages/Advanced.md +38 -0
  91. data/pages/Compatibility.md +49 -0
  92. data/pages/Custom.md +37 -0
  93. data/pages/Encoding.md +61 -0
  94. data/pages/InstallOptions.md +20 -0
  95. data/pages/JsonGem.md +60 -0
  96. data/pages/Modes.md +94 -0
  97. data/pages/Options.md +339 -0
  98. data/pages/Parser.md +134 -0
  99. data/pages/Rails.md +85 -0
  100. data/pages/Security.md +43 -0
  101. data/pages/WAB.md +12 -0
  102. metadata +242 -0
@@ -0,0 +1,325 @@
1
+ // Copyright (c) 2011, 2021 Peter Ohler. All rights reserved.
2
+ // Licensed under the MIT License. See LICENSE file in the project root for license details.
3
+
4
+ #include "intern.h"
5
+
6
+ #include <stdint.h>
7
+
8
+ #if defined(_WIN32)
9
+ #include <windows.h>
10
+ #elif HAVE_PTHREAD_MUTEX_INIT
11
+ #include <pthread.h>
12
+ #endif
13
+
14
+ #include "cache.h"
15
+ #include "mem.h"
16
+ #include "parse.h"
17
+
18
+ // Only used for the class cache so 256 should be sufficient.
19
+ #define HASH_SLOT_CNT ((uint64_t)256)
20
+ #define HASH_MASK (HASH_SLOT_CNT - 1)
21
+
22
+ // almost the Murmur hash algorithm
23
+ #define M 0x5bd1e995
24
+
25
+ typedef struct _keyVal {
26
+ struct _keyVal *next;
27
+ const char *key;
28
+ size_t len;
29
+ VALUE val;
30
+ } *KeyVal;
31
+
32
+ typedef struct _hash {
33
+ struct _keyVal slots[HASH_SLOT_CNT];
34
+ #if defined(_WIN32)
35
+ CRITICAL_SECTION cs;
36
+ #elif HAVE_PTHREAD_MUTEX_INIT
37
+ pthread_mutex_t mutex;
38
+ #else
39
+ VALUE mutex;
40
+ #endif
41
+ } *Hash;
42
+
43
+ struct _hash class_hash;
44
+ struct _hash attr_hash;
45
+
46
+ static VALUE str_cache_obj;
47
+
48
+ static VALUE sym_cache_obj;
49
+
50
+ static VALUE attr_cache_obj;
51
+
52
+ static VALUE form_str(const char *str, size_t len) {
53
+ return rb_str_freeze(rb_utf8_str_new(str, len));
54
+ }
55
+
56
+ static VALUE form_sym(const char *str, size_t len) {
57
+ return rb_to_symbol(rb_str_intern(rb_utf8_str_new(str, len)));
58
+ }
59
+
60
+ static VALUE form_attr(const char *str, size_t len) {
61
+ char buf[256];
62
+
63
+ if (sizeof(buf) - 2 <= len) {
64
+ char *b = OJ_R_ALLOC_N(char, len + 2);
65
+ ID id;
66
+
67
+ if ('~' == *str) {
68
+ memcpy(b, str + 1, len - 1);
69
+ b[len - 1] = '\0';
70
+ len -= 2;
71
+ } else {
72
+ *b = '@';
73
+ memcpy(b + 1, str, len);
74
+ b[len + 1] = '\0';
75
+ }
76
+ id = rb_intern3(buf, len + 1, oj_utf8_encoding);
77
+ OJ_R_FREE(b);
78
+ return id;
79
+ }
80
+ if ('~' == *str) {
81
+ memcpy(buf, str + 1, len - 1);
82
+ buf[len - 1] = '\0';
83
+ len -= 2;
84
+ } else {
85
+ *buf = '@';
86
+ memcpy(buf + 1, str, len);
87
+ buf[len + 1] = '\0';
88
+ }
89
+ return (VALUE)rb_intern3(buf, len + 1, oj_utf8_encoding);
90
+ }
91
+
92
+ static const rb_data_type_t oj_cache_type = {
93
+ "oj_windows/cache",
94
+ {
95
+ cache_mark,
96
+ cache_free,
97
+ NULL,
98
+ },
99
+ 0,
100
+ 0,
101
+ };
102
+
103
+ void oj_hash_init(void) {
104
+ VALUE cache_class = rb_define_class_under(Oj, "Cache", rb_cObject);
105
+ rb_undef_alloc_func(cache_class);
106
+
107
+ struct _cache *str_cache = cache_create(0, form_str, true, true);
108
+ str_cache_obj = TypedData_Wrap_Struct(cache_class, &oj_cache_type, str_cache);
109
+ rb_gc_register_address(&str_cache_obj);
110
+
111
+ struct _cache *sym_cache = cache_create(0, form_sym, true, true);
112
+ sym_cache_obj = TypedData_Wrap_Struct(cache_class, &oj_cache_type, sym_cache);
113
+ rb_gc_register_address(&sym_cache_obj);
114
+
115
+ struct _cache *attr_cache = cache_create(0, form_attr, false, true);
116
+ attr_cache_obj = TypedData_Wrap_Struct(cache_class, &oj_cache_type, attr_cache);
117
+ rb_gc_register_address(&attr_cache_obj);
118
+
119
+ memset(class_hash.slots, 0, sizeof(class_hash.slots));
120
+ #if defined(_WIN32)
121
+ InitializeCriticalSection(&class_hash.cs);
122
+ #elif HAVE_PTHREAD_MUTEX_INIT
123
+ pthread_mutex_init(&class_hash.mutex, NULL);
124
+ #else
125
+ class_hash.mutex = rb_mutex_new();
126
+ rb_gc_register_address(&class_hash.mutex);
127
+ #endif
128
+ }
129
+
130
+ VALUE
131
+ oj_str_intern(const char *key, size_t len) {
132
+ // For huge cache sizes over half a million the rb_enc_interned_str
133
+ // performs slightly better but at more "normal" size of a several
134
+ // thousands the cache intern performs about 20% better.
135
+ #if HAVE_RB_ENC_INTERNED_STR && 0
136
+ return rb_enc_interned_str(key, len, rb_utf8_encoding());
137
+ #else
138
+ Cache c;
139
+ TypedData_Get_Struct(str_cache_obj, struct _cache, &oj_cache_type, c);
140
+ return cache_intern(c, key, len);
141
+ #endif
142
+ }
143
+
144
+ VALUE
145
+ oj_sym_intern(const char *key, size_t len) {
146
+ Cache c;
147
+ TypedData_Get_Struct(sym_cache_obj, struct _cache, &oj_cache_type, c);
148
+ return cache_intern(c, key, len);
149
+ }
150
+
151
+ ID oj_attr_intern(const char *key, size_t len) {
152
+ Cache c;
153
+ TypedData_Get_Struct(attr_cache_obj, struct _cache, &oj_cache_type, c);
154
+ return cache_intern(c, key, len);
155
+ }
156
+
157
+ static uint64_t hash_calc(const uint8_t *key, size_t len) {
158
+ const uint8_t *end = key + len;
159
+ const uint8_t *endless = key + (len & 0xFFFFFFFC);
160
+ uint64_t h = (uint64_t)len;
161
+ uint64_t k;
162
+
163
+ while (key < endless) {
164
+ k = (uint64_t)*key++;
165
+ k |= (uint64_t)*key++ << 8;
166
+ k |= (uint64_t)*key++ << 16;
167
+ k |= (uint64_t)*key++ << 24;
168
+
169
+ k *= M;
170
+ k ^= k >> 24;
171
+ h *= M;
172
+ h ^= k * M;
173
+ }
174
+ if (1 < end - key) {
175
+ uint16_t k16 = (uint16_t)*key++;
176
+
177
+ k16 |= (uint16_t)*key++ << 8;
178
+ h ^= k16 << 8;
179
+ }
180
+ if (key < end) {
181
+ h ^= *key;
182
+ }
183
+ h *= M;
184
+ h ^= h >> 13;
185
+ h *= M;
186
+ h ^= h >> 15;
187
+
188
+ return h;
189
+ }
190
+
191
+ static VALUE resolve_classname(VALUE mod, const char *classname, int auto_define) {
192
+ VALUE clas;
193
+ ID ci = rb_intern(classname);
194
+
195
+ if (rb_const_defined_at(mod, ci)) {
196
+ clas = rb_const_get_at(mod, ci);
197
+ } else if (auto_define) {
198
+ clas = rb_define_class_under(mod, classname, oj_bag_class);
199
+ } else {
200
+ clas = Qundef;
201
+ }
202
+ return clas;
203
+ }
204
+
205
+ static VALUE resolve_classpath(ParseInfo pi, const char *name, size_t len, int auto_define, VALUE error_class) {
206
+ char class_name[1024];
207
+ VALUE clas;
208
+ char *end = class_name + sizeof(class_name) - 1;
209
+ char *s;
210
+ const char *n = name;
211
+ size_t nlen = len;
212
+
213
+ clas = rb_cObject;
214
+ for (s = class_name; 0 < len; n++, len--) {
215
+ if (':' == *n) {
216
+ *s = '\0';
217
+ n++;
218
+ len--;
219
+ if (':' != *n) {
220
+ return Qundef;
221
+ }
222
+ if (Qundef == (clas = resolve_classname(clas, class_name, auto_define))) {
223
+ return Qundef;
224
+ }
225
+ s = class_name;
226
+ } else if (end <= s) {
227
+ return Qundef;
228
+ } else {
229
+ *s++ = *n;
230
+ }
231
+ }
232
+ *s = '\0';
233
+ if (Qundef == (clas = resolve_classname(clas, class_name, auto_define))) {
234
+ if (sizeof(class_name) <= nlen) {
235
+ nlen = sizeof(class_name) - 1;
236
+ }
237
+ strncpy(class_name, name, nlen);
238
+ class_name[nlen] = '\0';
239
+ oj_set_error_at(pi, error_class, __FILE__, __LINE__, "class '%s' is not defined", class_name);
240
+ if (Qnil != error_class) {
241
+ pi->err_class = error_class;
242
+ }
243
+ }
244
+ return clas;
245
+ }
246
+
247
+ VALUE oj_class_intern(const char *key, size_t len, bool safe, ParseInfo pi, int auto_define, VALUE error_class) {
248
+ uint64_t h = hash_calc((const uint8_t *)key, len) & HASH_MASK;
249
+ KeyVal bucket = class_hash.slots + h;
250
+ KeyVal b;
251
+
252
+ if (safe) {
253
+ #if defined(_WIN32)
254
+ EnterCriticalSection(&class_hash.cs);
255
+ #elif HAVE_PTHREAD_MUTEX_INIT
256
+ pthread_mutex_lock(&class_hash.mutex);
257
+ #else
258
+ rb_mutex_lock(class_hash.mutex);
259
+ #endif
260
+ if (NULL != bucket->key) { // not the top slot
261
+ for (b = bucket; 0 != b; b = b->next) {
262
+ if (len == b->len && 0 == strncmp(b->key, key, len)) {
263
+ #if defined(_WIN32)
264
+ LeaveCriticalSection(&class_hash.cs);
265
+ #elif HAVE_PTHREAD_MUTEX_INIT
266
+ pthread_mutex_unlock(&class_hash.mutex);
267
+ #else
268
+ rb_mutex_unlock(class_hash.mutex);
269
+ #endif
270
+ return b->val;
271
+ }
272
+ bucket = b;
273
+ }
274
+ b = OJ_R_ALLOC(struct _keyVal);
275
+ b->next = NULL;
276
+ bucket->next = b;
277
+ bucket = b;
278
+ }
279
+ bucket->key = oj_strndup(key, len);
280
+ bucket->len = len;
281
+ bucket->val = resolve_classpath(pi, key, len, auto_define, error_class);
282
+ #if defined(_WIN32)
283
+ LeaveCriticalSection(&class_hash.cs);
284
+ #elif HAVE_PTHREAD_MUTEX_INIT
285
+ pthread_mutex_unlock(&class_hash.mutex);
286
+ #else
287
+ rb_mutex_unlock(class_hash.mutex);
288
+ #endif
289
+ } else {
290
+ if (NULL != bucket->key) {
291
+ for (b = bucket; 0 != b; b = b->next) {
292
+ if (len == b->len && 0 == strncmp(b->key, key, len)) {
293
+ return (ID)b->val;
294
+ }
295
+ bucket = b;
296
+ }
297
+ b = OJ_R_ALLOC(struct _keyVal);
298
+ b->next = NULL;
299
+ bucket->next = b;
300
+ bucket = b;
301
+ }
302
+ bucket->key = oj_strndup(key, len);
303
+ bucket->len = len;
304
+ bucket->val = resolve_classpath(pi, key, len, auto_define, error_class);
305
+ }
306
+ rb_gc_register_mark_object(bucket->val);
307
+ return bucket->val;
308
+ }
309
+
310
+ char *oj_strndup(const char *s, size_t len) {
311
+ char *d = OJ_R_ALLOC_N(char, len + 1);
312
+
313
+ memcpy(d, s, len);
314
+ d[len] = '\0';
315
+
316
+ return d;
317
+ }
318
+
319
+ /*
320
+ void intern_cleanup(void) {
321
+ cache_free(str_cache);
322
+ cache_free(sym_cache);
323
+ cache_free(attr_cache);
324
+ }
325
+ */
@@ -0,0 +1,22 @@
1
+ // Copyright (c) 2011, 2021 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_INTERN_H
5
+ #define OJ_INTERN_H
6
+
7
+ #include <ruby.h>
8
+ #include <stdbool.h>
9
+
10
+ struct _parseInfo;
11
+
12
+ extern void oj_hash_init(void);
13
+
14
+ extern VALUE oj_str_intern(const char *key, size_t len);
15
+ extern VALUE oj_sym_intern(const char *key, size_t len);
16
+ extern ID oj_attr_intern(const char *key, size_t len);
17
+ extern VALUE
18
+ oj_class_intern(const char *key, size_t len, bool safe, struct _parseInfo *pi, int auto_define, VALUE error_class);
19
+
20
+ extern char *oj_strndup(const char *s, size_t len);
21
+
22
+ #endif /* OJ_INTERN_H */
@@ -0,0 +1,320 @@
1
+ // Copyright (c) 2018, Peter Ohler, All rights reserved.
2
+
3
+ #include "mem.h"
4
+
5
+ #if !IS_WINDOWS
6
+ #include <pthread.h>
7
+ #endif
8
+ #include <ruby.h>
9
+ #include <stdbool.h>
10
+ #include <stdint.h>
11
+ #include <stdio.h>
12
+ #include <stdlib.h>
13
+ #include <string.h>
14
+
15
+ typedef struct _rec {
16
+ struct _rec *next;
17
+ const void *ptr;
18
+ size_t size;
19
+ const char *file;
20
+ int line;
21
+ bool ruby;
22
+ } *Rec;
23
+
24
+ typedef struct _rep {
25
+ struct _rep *next;
26
+ size_t size;
27
+ const char *file;
28
+ int line;
29
+ int cnt;
30
+ } *Rep;
31
+
32
+ #ifdef MEM_DEBUG
33
+
34
+ static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
35
+ static Rec recs = NULL;
36
+ static const char mem_pad[] = "--- This is a memory pad and should not change until being freed. ---";
37
+
38
+ void *oj_malloc(size_t size, const char *file, int line) {
39
+ void *ptr = malloc(size + sizeof(mem_pad));
40
+
41
+ if (NULL != ptr) {
42
+ Rec r = (Rec)malloc(sizeof(struct _rec));
43
+
44
+ if (NULL != r) {
45
+ strcpy(((char *)ptr) + size, mem_pad);
46
+ r->ptr = ptr;
47
+ r->size = size;
48
+ r->file = file;
49
+ r->line = line;
50
+ r->ruby = false;
51
+ pthread_mutex_lock(&lock);
52
+ r->next = recs;
53
+ recs = r;
54
+ pthread_mutex_unlock(&lock);
55
+ } else {
56
+ free(ptr);
57
+ ptr = NULL;
58
+ }
59
+ }
60
+ return ptr;
61
+ }
62
+
63
+ void *oj_realloc(void *orig, size_t size, const char *file, int line) {
64
+ void *ptr = realloc(orig, size + sizeof(mem_pad));
65
+ Rec r;
66
+
67
+ if (NULL != ptr) {
68
+ strcpy(((char *)ptr) + size, mem_pad);
69
+ pthread_mutex_lock(&lock);
70
+ for (r = recs; NULL != r; r = r->next) {
71
+ if (orig == r->ptr) {
72
+ r->ptr = ptr;
73
+ r->size = size;
74
+ r->file = file;
75
+ r->line = line;
76
+ r->ruby = false;
77
+ break;
78
+ }
79
+ }
80
+ pthread_mutex_unlock(&lock);
81
+ if (NULL == r) {
82
+ printf("Realloc at %s:%d (%p) not allocated.\n", file, line, orig);
83
+ }
84
+ }
85
+ return ptr;
86
+ }
87
+
88
+ void *oj_calloc(size_t count, size_t size, const char *file, int line) {
89
+ void *ptr;
90
+
91
+ size *= count;
92
+ if (NULL != (ptr = malloc(size + sizeof(mem_pad)))) {
93
+ Rec r = (Rec)malloc(sizeof(struct _rec));
94
+
95
+ if (NULL != r) {
96
+ memset(ptr, 0, size);
97
+ strcpy(((char *)ptr) + size, mem_pad);
98
+ r->ptr = ptr;
99
+ r->size = size;
100
+ r->file = file;
101
+ r->line = line;
102
+ r->ruby = false;
103
+ pthread_mutex_lock(&lock);
104
+ r->next = recs;
105
+ recs = r;
106
+ pthread_mutex_unlock(&lock);
107
+ } else {
108
+ free(ptr);
109
+ ptr = NULL;
110
+ }
111
+ }
112
+ return ptr;
113
+ }
114
+
115
+ void *oj_r_alloc(size_t size, const char *file, int line) {
116
+ void *ptr = ruby_xmalloc(size + sizeof(mem_pad));
117
+
118
+ if (NULL != ptr) {
119
+ Rec r = (Rec)malloc(sizeof(struct _rec));
120
+
121
+ if (NULL != r) {
122
+ strcpy(((char *)ptr) + size, mem_pad);
123
+ r->ptr = ptr;
124
+ r->size = size;
125
+ r->file = file;
126
+ r->line = line;
127
+ r->ruby = true;
128
+ pthread_mutex_lock(&lock);
129
+ r->next = recs;
130
+ recs = r;
131
+ pthread_mutex_unlock(&lock);
132
+ } else {
133
+ free(ptr);
134
+ ptr = NULL;
135
+ }
136
+ }
137
+ return ptr;
138
+ }
139
+
140
+ void *oj_r_realloc(void *orig, size_t size, const char *file, int line) {
141
+ void *ptr = ruby_xrealloc2(orig, 1, size + sizeof(mem_pad));
142
+ Rec r;
143
+
144
+ if (NULL != ptr) {
145
+ strcpy(((char *)ptr) + size, mem_pad);
146
+ pthread_mutex_lock(&lock);
147
+ for (r = recs; NULL != r; r = r->next) {
148
+ if (orig == r->ptr) {
149
+ r->ptr = ptr;
150
+ r->size = size;
151
+ r->file = file;
152
+ r->line = line;
153
+ r->ruby = true;
154
+ break;
155
+ }
156
+ }
157
+ pthread_mutex_unlock(&lock);
158
+ if (NULL == r) {
159
+ printf("Realloc at %s:%d (%p) not allocated.\n", file, line, orig);
160
+ }
161
+ }
162
+ return ptr;
163
+ }
164
+
165
+ void oj_freed(void *ptr, const char *file, int line, bool ruby) {
166
+ if (NULL != ptr) {
167
+ Rec r = NULL;
168
+ Rec prev = NULL;
169
+
170
+ pthread_mutex_lock(&lock);
171
+ for (r = recs; NULL != r; r = r->next) {
172
+ if (ptr == r->ptr) {
173
+ if (NULL == prev) {
174
+ recs = r->next;
175
+ } else {
176
+ prev->next = r->next;
177
+ }
178
+ break;
179
+ }
180
+ prev = r;
181
+ }
182
+ pthread_mutex_unlock(&lock);
183
+ if (NULL == r) {
184
+ printf("Free at %s:%d (%p) not allocated or already freed.\n", file, line, ptr);
185
+ } else {
186
+ char *pad = (char *)r->ptr + r->size;
187
+
188
+ if (r->ruby != ruby) {
189
+ if (r->ruby) {
190
+ printf("Memory at %s:%d (%p) allocated with Ruby allocator and freed with stdlib free.\n",
191
+ file,
192
+ line,
193
+ ptr);
194
+ } else {
195
+ printf("Memory at %s:%d (%p) allocated with stdlib allocator and freed with Ruby free.\n",
196
+ file,
197
+ line,
198
+ ptr);
199
+ }
200
+ }
201
+ if (0 != strcmp(mem_pad, pad)) {
202
+ uint8_t *p;
203
+ uint8_t *end = (uint8_t *)pad + sizeof(mem_pad);
204
+
205
+ printf("Memory at %s:%d (%p) write outside allocated.\n", file, line, ptr);
206
+ for (p = (uint8_t *)pad; p < end; p++) {
207
+ if (0x20 < *p && *p < 0x7f) {
208
+ printf("%c ", *p);
209
+ } else {
210
+ printf("%02x ", *(uint8_t *)p);
211
+ }
212
+ }
213
+ printf("\n");
214
+ }
215
+ free(r);
216
+ }
217
+ }
218
+ }
219
+
220
+ void oj_r_free(void *ptr, const char *file, int line) {
221
+ oj_freed(ptr, file, line, true);
222
+ xfree(ptr);
223
+ }
224
+
225
+ void oj_free(void *ptr, const char *file, int line) {
226
+ oj_freed(ptr, file, line, false);
227
+ free(ptr);
228
+ }
229
+
230
+ char *oj_mem_strdup(const char *str, const char *file, int line) {
231
+ size_t size = strlen(str) + 1;
232
+ char *ptr = (char *)malloc(size + sizeof(mem_pad));
233
+
234
+ if (NULL != ptr) {
235
+ Rec r = (Rec)malloc(sizeof(struct _rec));
236
+
237
+ if (NULL != r) {
238
+ strcpy(ptr, str);
239
+ strcpy(((char *)ptr) + size, mem_pad);
240
+ r->ptr = (void *)ptr;
241
+ r->size = size;
242
+ r->file = file;
243
+ r->line = line;
244
+ r->ruby = false;
245
+ pthread_mutex_lock(&lock);
246
+ r->next = recs;
247
+ recs = r;
248
+ pthread_mutex_unlock(&lock);
249
+ } else {
250
+ free(ptr);
251
+ ptr = NULL;
252
+ }
253
+ }
254
+ return ptr;
255
+ }
256
+
257
+ #endif
258
+
259
+ #ifdef MEM_DEBUG
260
+
261
+ static Rep update_reps(Rep reps, Rec r) {
262
+ Rep rp = reps;
263
+
264
+ for (; NULL != rp; rp = rp->next) {
265
+ if (rp->line == r->line && (rp->file == r->file || 0 == strcmp(rp->file, r->file))) {
266
+ rp->size += r->size;
267
+ rp->cnt++;
268
+ break;
269
+ }
270
+ }
271
+ if (NULL == rp && NULL != (rp = (Rep)malloc(sizeof(struct _rep)))) {
272
+ rp->size = r->size;
273
+ rp->file = r->file;
274
+ rp->line = r->line;
275
+ rp->cnt = 1;
276
+ rp->next = reps;
277
+ reps = rp;
278
+ }
279
+ return reps;
280
+ }
281
+
282
+ static void print_stats() {
283
+ printf("\n--- Memory Usage Report --------------------------------------------------------\n");
284
+ pthread_mutex_lock(&lock);
285
+
286
+ if (NULL == recs) {
287
+ printf("No memory leaks\n");
288
+ } else {
289
+ Rep reps = NULL;
290
+ Rep rp;
291
+ Rec r;
292
+ size_t leaked = 0;
293
+
294
+ for (r = recs; NULL != r; r = r->next) {
295
+ reps = update_reps(reps, r);
296
+ }
297
+ while (NULL != (rp = reps)) {
298
+ reps = rp->next;
299
+ printf("%16s:%3d %8lu bytes over %d occurances allocated and not freed.\n",
300
+ rp->file,
301
+ rp->line,
302
+ rp->size,
303
+ rp->cnt);
304
+ leaked += rp->size;
305
+ free(rp);
306
+ }
307
+ printf("%lu bytes leaked\n", leaked);
308
+ }
309
+ pthread_mutex_unlock(&lock);
310
+ printf("--------------------------------------------------------------------------------\n");
311
+ }
312
+
313
+ #endif
314
+
315
+ void oj_mem_report(void) {
316
+ #ifdef MEM_DEBUG
317
+ rb_gc();
318
+ print_stats();
319
+ #endif
320
+ }
@@ -0,0 +1,53 @@
1
+ // Copyright (c) 2018, Peter Ohler, All rights reserved.
2
+
3
+ #ifndef OJ_MEM_H
4
+ #define OJ_MEM_H
5
+
6
+ #include <stdio.h>
7
+ #include <stdlib.h>
8
+ #include <string.h>
9
+
10
+ #ifdef MEM_DEBUG
11
+
12
+ #define OJ_MALLOC(size) oj_malloc(size, __FILE__, __LINE__)
13
+ #define OJ_REALLOC(ptr, size) oj_realloc(ptr, size, __FILE__, __LINE__)
14
+ #define OJ_CALLOC(count, size) oj_calloc(count, size, __FILE__, __LINE__)
15
+ #define OJ_FREE(ptr) oj_free(ptr, __FILE__, __LINE__)
16
+
17
+ #define OJ_R_ALLOC(type) oj_r_alloc(sizeof(type), __FILE__, __LINE__)
18
+ #define OJ_R_ALLOC_N(type, n) (type *)oj_r_alloc(sizeof(type) * (n), __FILE__, __LINE__)
19
+ #define OJ_R_REALLOC_N(ptr, type, n) ((ptr) = (type *)oj_r_realloc(ptr, (sizeof(type) * (n)), __FILE__, __LINE__))
20
+ #define OJ_R_FREE(ptr) oj_r_free(ptr, __FILE__, __LINE__)
21
+
22
+ #define OJ_STRDUP(str) oj_mem_strdup(str, __FILE__, __LINE__)
23
+
24
+ extern void *oj_malloc(size_t size, const char *file, int line);
25
+ extern void *oj_realloc(void *ptr, size_t size, const char *file, int line);
26
+ extern void *oj_calloc(size_t count, size_t size, const char *file, int line);
27
+ extern void oj_free(void *ptr, const char *file, int line);
28
+
29
+ extern void *oj_r_alloc(size_t size, const char *file, int line);
30
+ extern void *oj_r_realloc(void *ptr, size_t size, const char *file, int line);
31
+ extern void oj_r_free(void *ptr, const char *file, int line);
32
+
33
+ extern char *oj_mem_strdup(const char *str, const char *file, int line);
34
+
35
+ #else
36
+
37
+ #define OJ_MALLOC(size) malloc(size)
38
+ #define OJ_REALLOC(ptr, size) realloc(ptr, size)
39
+ #define OJ_CALLOC(count, size) calloc(count, size)
40
+ #define OJ_FREE(ptr) free(ptr)
41
+
42
+ #define OJ_R_ALLOC(type) RB_ALLOC(type)
43
+ #define OJ_R_ALLOC_N(type, n) RB_ALLOC_N(type, n)
44
+ #define OJ_R_REALLOC_N(ptr, type, n) RB_REALLOC_N(ptr, type, n)
45
+ #define OJ_R_FREE(ptr) xfree(ptr)
46
+
47
+ #define OJ_STRDUP(str) strdup(str)
48
+
49
+ #endif
50
+
51
+ extern void oj_mem_report();
52
+
53
+ #endif /* OJ_MEM_H */