oj 3.8.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (156) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +21 -0
  3. data/README.md +104 -0
  4. data/ext/oj/buf.h +103 -0
  5. data/ext/oj/cache8.c +107 -0
  6. data/ext/oj/cache8.h +48 -0
  7. data/ext/oj/circarray.c +68 -0
  8. data/ext/oj/circarray.h +23 -0
  9. data/ext/oj/code.c +235 -0
  10. data/ext/oj/code.h +42 -0
  11. data/ext/oj/compat.c +299 -0
  12. data/ext/oj/custom.c +1191 -0
  13. data/ext/oj/dump.c +1252 -0
  14. data/ext/oj/dump.h +96 -0
  15. data/ext/oj/dump_compat.c +977 -0
  16. data/ext/oj/dump_leaf.c +252 -0
  17. data/ext/oj/dump_object.c +837 -0
  18. data/ext/oj/dump_strict.c +433 -0
  19. data/ext/oj/encode.h +45 -0
  20. data/ext/oj/err.c +57 -0
  21. data/ext/oj/err.h +70 -0
  22. data/ext/oj/extconf.rb +47 -0
  23. data/ext/oj/fast.c +1771 -0
  24. data/ext/oj/hash.c +163 -0
  25. data/ext/oj/hash.h +46 -0
  26. data/ext/oj/hash_test.c +512 -0
  27. data/ext/oj/mimic_json.c +878 -0
  28. data/ext/oj/object.c +771 -0
  29. data/ext/oj/odd.c +231 -0
  30. data/ext/oj/odd.h +44 -0
  31. data/ext/oj/oj.c +1704 -0
  32. data/ext/oj/oj.h +385 -0
  33. data/ext/oj/parse.c +1086 -0
  34. data/ext/oj/parse.h +111 -0
  35. data/ext/oj/rails.c +1493 -0
  36. data/ext/oj/rails.h +21 -0
  37. data/ext/oj/reader.c +231 -0
  38. data/ext/oj/reader.h +151 -0
  39. data/ext/oj/resolve.c +102 -0
  40. data/ext/oj/resolve.h +14 -0
  41. data/ext/oj/rxclass.c +147 -0
  42. data/ext/oj/rxclass.h +27 -0
  43. data/ext/oj/saj.c +714 -0
  44. data/ext/oj/scp.c +224 -0
  45. data/ext/oj/sparse.c +910 -0
  46. data/ext/oj/stream_writer.c +363 -0
  47. data/ext/oj/strict.c +212 -0
  48. data/ext/oj/string_writer.c +534 -0
  49. data/ext/oj/trace.c +79 -0
  50. data/ext/oj/trace.h +28 -0
  51. data/ext/oj/util.c +136 -0
  52. data/ext/oj/util.h +19 -0
  53. data/ext/oj/val_stack.c +118 -0
  54. data/ext/oj/val_stack.h +185 -0
  55. data/ext/oj/wab.c +631 -0
  56. data/lib/oj.rb +21 -0
  57. data/lib/oj/active_support_helper.rb +41 -0
  58. data/lib/oj/bag.rb +88 -0
  59. data/lib/oj/easy_hash.rb +52 -0
  60. data/lib/oj/error.rb +22 -0
  61. data/lib/oj/json.rb +176 -0
  62. data/lib/oj/mimic.rb +267 -0
  63. data/lib/oj/saj.rb +66 -0
  64. data/lib/oj/schandler.rb +142 -0
  65. data/lib/oj/state.rb +131 -0
  66. data/lib/oj/version.rb +5 -0
  67. data/pages/Advanced.md +22 -0
  68. data/pages/Compatibility.md +25 -0
  69. data/pages/Custom.md +23 -0
  70. data/pages/Encoding.md +65 -0
  71. data/pages/JsonGem.md +79 -0
  72. data/pages/Modes.md +155 -0
  73. data/pages/Options.md +283 -0
  74. data/pages/Rails.md +116 -0
  75. data/pages/Security.md +20 -0
  76. data/pages/WAB.md +13 -0
  77. data/test/_test_active.rb +76 -0
  78. data/test/_test_active_mimic.rb +96 -0
  79. data/test/_test_mimic_rails.rb +126 -0
  80. data/test/activerecord/result_test.rb +27 -0
  81. data/test/activesupport4/decoding_test.rb +108 -0
  82. data/test/activesupport4/encoding_test.rb +531 -0
  83. data/test/activesupport4/test_helper.rb +41 -0
  84. data/test/activesupport5/decoding_test.rb +125 -0
  85. data/test/activesupport5/encoding_test.rb +485 -0
  86. data/test/activesupport5/encoding_test_cases.rb +90 -0
  87. data/test/activesupport5/test_helper.rb +50 -0
  88. data/test/activesupport5/time_zone_test_helpers.rb +24 -0
  89. data/test/bar.rb +25 -0
  90. data/test/files.rb +29 -0
  91. data/test/foo.rb +167 -0
  92. data/test/helper.rb +26 -0
  93. data/test/isolated/shared.rb +308 -0
  94. data/test/isolated/test_mimic_after.rb +13 -0
  95. data/test/isolated/test_mimic_alone.rb +12 -0
  96. data/test/isolated/test_mimic_as_json.rb +45 -0
  97. data/test/isolated/test_mimic_before.rb +13 -0
  98. data/test/isolated/test_mimic_define.rb +28 -0
  99. data/test/isolated/test_mimic_rails_after.rb +22 -0
  100. data/test/isolated/test_mimic_rails_before.rb +21 -0
  101. data/test/isolated/test_mimic_redefine.rb +15 -0
  102. data/test/json_gem/json_addition_test.rb +216 -0
  103. data/test/json_gem/json_common_interface_test.rb +148 -0
  104. data/test/json_gem/json_encoding_test.rb +107 -0
  105. data/test/json_gem/json_ext_parser_test.rb +20 -0
  106. data/test/json_gem/json_fixtures_test.rb +35 -0
  107. data/test/json_gem/json_generator_test.rb +383 -0
  108. data/test/json_gem/json_generic_object_test.rb +90 -0
  109. data/test/json_gem/json_parser_test.rb +470 -0
  110. data/test/json_gem/json_string_matching_test.rb +42 -0
  111. data/test/json_gem/test_helper.rb +18 -0
  112. data/test/perf.rb +107 -0
  113. data/test/perf_compat.rb +130 -0
  114. data/test/perf_fast.rb +164 -0
  115. data/test/perf_file.rb +64 -0
  116. data/test/perf_object.rb +138 -0
  117. data/test/perf_saj.rb +109 -0
  118. data/test/perf_scp.rb +151 -0
  119. data/test/perf_simple.rb +287 -0
  120. data/test/perf_strict.rb +145 -0
  121. data/test/perf_wab.rb +131 -0
  122. data/test/sample.rb +54 -0
  123. data/test/sample/change.rb +14 -0
  124. data/test/sample/dir.rb +19 -0
  125. data/test/sample/doc.rb +36 -0
  126. data/test/sample/file.rb +48 -0
  127. data/test/sample/group.rb +16 -0
  128. data/test/sample/hasprops.rb +16 -0
  129. data/test/sample/layer.rb +12 -0
  130. data/test/sample/line.rb +20 -0
  131. data/test/sample/oval.rb +10 -0
  132. data/test/sample/rect.rb +10 -0
  133. data/test/sample/shape.rb +35 -0
  134. data/test/sample/text.rb +20 -0
  135. data/test/sample_json.rb +37 -0
  136. data/test/test_compat.rb +509 -0
  137. data/test/test_custom.rb +503 -0
  138. data/test/test_debian.rb +53 -0
  139. data/test/test_fast.rb +470 -0
  140. data/test/test_file.rb +239 -0
  141. data/test/test_gc.rb +49 -0
  142. data/test/test_hash.rb +29 -0
  143. data/test/test_integer_range.rb +73 -0
  144. data/test/test_null.rb +376 -0
  145. data/test/test_object.rb +1018 -0
  146. data/test/test_saj.rb +186 -0
  147. data/test/test_scp.rb +433 -0
  148. data/test/test_strict.rb +410 -0
  149. data/test/test_various.rb +741 -0
  150. data/test/test_wab.rb +307 -0
  151. data/test/test_writer.rb +380 -0
  152. data/test/tests.rb +24 -0
  153. data/test/tests_mimic.rb +14 -0
  154. data/test/tests_mimic_addition.rb +7 -0
  155. data/test/zoo.rb +13 -0
  156. metadata +359 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 7e31b61d33e83ce9e5e20148749717af84c4cc9f02a6faeb14383f7243095439
4
+ data.tar.gz: 700b0823f12722d2dd2028875abd2bf624a85422b42b5a1251632f63069358f5
5
+ SHA512:
6
+ metadata.gz: 5daaaf6c3ac308e2cdca660804fc4ff2d87b1c724e49e3fd0a36bd223c7c2f64d0764d60a3d51d242ac4da370488d0f78db42214527f17afcccd9e950dc540c9
7
+ data.tar.gz: dd9d68dd43f073af2c16a87ac4725beff0592d1410296dbc06c862785a25f73ba1a4085beff9e2ecf73e7e5908f69f9de7be1ac3be9b725a99804db42130de17
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2012 Peter Ohler
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,104 @@
1
+ # [![{}j](http://www.ohler.com/dev/images/oj_comet_64.svg)](http://www.ohler.com/oj) gem
2
+
3
+ [![Build Status](https://img.shields.io/travis/ohler55/oj/master.svg)](http://travis-ci.org/ohler55/oj?branch=master) [![AppVeyor](https://img.shields.io/appveyor/ci/ohler55/oj/master.svg)](https://ci.appveyor.com/project/ohler55/oj) ![Gem](https://img.shields.io/gem/v/oj.svg) ![Gem](https://img.shields.io/gem/dt/oj.svg) [![SemVer compatibility](https://api.dependabot.com/badges/compatibility_score?dependency-name=oj&package-manager=bundler&version-scheme=semver)](https://dependabot.com/compatibility-score.html?dependency-name=oj&package-manager=bundler&version-scheme=semver) [![TideLift](https://tidelift.com/badges/github/ohler55/oj)](https://tidelift.com/subscription/pkg/rubygems-oj?utm_source=rubygems-oj&utm_medium=referral&utm_campaign=readme)
4
+
5
+ A *fast* JSON parser and Object marshaller as a Ruby gem.
6
+
7
+ Version 3.0 is out! 3.0 provides better json gem and Rails compatibility. It
8
+ also provides additional optimization options.
9
+
10
+ ## Using
11
+
12
+ ```ruby
13
+ require 'oj'
14
+
15
+ h = { 'one' => 1, 'array' => [ true, false ] }
16
+ json = Oj.dump(h)
17
+
18
+ # json =
19
+ # {
20
+ # "one":1,
21
+ # "array":[
22
+ # true,
23
+ # false
24
+ # ]
25
+ # }
26
+
27
+ h2 = Oj.load(json)
28
+ puts "Same? #{h == h2}"
29
+ # true
30
+ ```
31
+
32
+ ## Installation
33
+ ```
34
+ gem install oj
35
+ ```
36
+
37
+ or in Bundler:
38
+
39
+ ```
40
+ gem 'oj'
41
+ ```
42
+
43
+ ## Support
44
+
45
+ [Get supported Oj with a Tidelift Subscription.](https://tidelift.com/subscription/pkg/rubygems-oj?utm_source=rubygems-oj&utm_medium=referral&utm_campaign=readme)
46
+
47
+ ## Further Reading
48
+
49
+ For more details on options, modes, advanced features, and more follow these
50
+ links.
51
+
52
+ - [{file:Options.md}](pages/Options.md) for parse and dump options.
53
+ - [{file:Modes.md}](pages/Modes.md) for details on modes for strict JSON compliance, mimicing the JSON gem, and mimicing Rails and ActiveSupport behavior.
54
+ - [{file:JsonGem.md}](pages/JsonGem.md) includes more details on json gem compatibility and use.
55
+ - [{file:Rails.md}](pages/Rails.md) includes more details on Rails and ActiveSupport compatibility and use.
56
+ - [{file:Custom.md}](pages/Custom.md) includes more details on Custom mode.
57
+ - [{file:Encoding.md}](pages/Encoding.md) describes the :object encoding format.
58
+ - [{file:Compatibility.md}](pages/Compatibility.md) lists current compatibility with Rubys and Rails.
59
+ - [{file:Advanced.md}](pages/Advanced.md) for fast parser and marshalling features.
60
+ - [{file:Security.md}](pages/Security.md) for security considerations.
61
+
62
+ ## Releases
63
+
64
+ See [{file:CHANGELOG.md}](CHANGELOG.md)
65
+
66
+ ## Links
67
+
68
+ - *Documentation*: http://www.ohler.com/oj/doc, http://rubydoc.info/gems/oj
69
+
70
+ - *GitHub* *repo*: https://github.com/ohler55/oj
71
+
72
+ - *RubyGems* *repo*: https://rubygems.org/gems/oj
73
+
74
+ Follow [@peterohler on Twitter](http://twitter.com/peterohler) for announcements and news about the Oj gem.
75
+
76
+ #### Performance Comparisons
77
+
78
+ - [Oj Strict Mode Performance](http://www.ohler.com/dev/oj_misc/performance_strict.html) compares Oj strict mode parser performance to other JSON parsers.
79
+
80
+ - [Oj Compat Mode Performance](http://www.ohler.com/dev/oj_misc/performance_compat.html) compares Oj compat mode parser performance to other JSON parsers.
81
+
82
+ - [Oj Object Mode Performance](http://www.ohler.com/dev/oj_misc/performance_object.html) compares Oj object mode parser performance to other marshallers.
83
+
84
+ - [Oj Callback Performance](http://www.ohler.com/dev/oj_misc/performance_callback.html) compares Oj callback parser performance to other JSON parsers.
85
+
86
+ #### Links of Interest
87
+
88
+ - *Fast XML parser and marshaller on RubyGems*: https://rubygems.org/gems/ox
89
+
90
+ - *Fast XML parser and marshaller on GitHub*: https://github.com/ohler55/ox
91
+
92
+ - [Need for Speed](http://www.ohler.com/dev/need_for_speed/need_for_speed.html) for an overview of how Oj::Doc was designed.
93
+
94
+ - *OjC, a C JSON parser*: https://www.ohler.com/ojc also at https://github.com/ohler55/ojc
95
+
96
+ - *Agoo, a high performance Ruby web server supporting GraphQL on GitHub*: https://github.com/ohler55/agoo
97
+
98
+ - *Agoo-C, a high performance C web server supporting GraphQL on GitHub*: https://github.com/ohler55/agoo-c
99
+
100
+ #### Contributing
101
+
102
+ + Provide a Pull Request off the `develop` branch.
103
+ + Report a bug
104
+ + Suggest an idea
@@ -0,0 +1,103 @@
1
+ /* buf.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_BUF_H
32
+ #define OJ_BUF_H
33
+
34
+ #include "ruby.h"
35
+
36
+ typedef struct _buf {
37
+ char *head;
38
+ char *end;
39
+ char *tail;
40
+ char base[1024];
41
+ } *Buf;
42
+
43
+ inline static void
44
+ buf_init(Buf buf) {
45
+ buf->head = buf->base;
46
+ buf->end = buf->base + sizeof(buf->base) - 1;
47
+ buf->tail = buf->head;
48
+ }
49
+
50
+ inline static void
51
+ buf_cleanup(Buf buf) {
52
+ if (buf->base != buf->head) {
53
+ xfree(buf->head);
54
+ }
55
+ }
56
+
57
+ inline static size_t
58
+ buf_len(Buf buf) {
59
+ return buf->tail - buf->head;
60
+ }
61
+
62
+ inline static void
63
+ buf_append_string(Buf buf, const char *s, size_t slen) {
64
+ if (buf->end <= buf->tail + slen) {
65
+ size_t len = buf->end - buf->head;
66
+ size_t toff = buf->tail - buf->head;
67
+ size_t new_len = len + slen + len / 2;
68
+
69
+ if (buf->base == buf->head) {
70
+ buf->head = ALLOC_N(char, new_len);
71
+ memcpy(buf->head, buf->base, len);
72
+ } else {
73
+ REALLOC_N(buf->head, char, new_len);
74
+ }
75
+ buf->tail = buf->head + toff;
76
+ buf->end = buf->head + new_len - 1;
77
+ }
78
+ memcpy(buf->tail, s, slen);
79
+ buf->tail += slen;
80
+ }
81
+
82
+ inline static void
83
+ buf_append(Buf buf, char c) {
84
+ if (buf->end <= buf->tail) {
85
+ size_t len = buf->end - buf->head;
86
+ size_t toff = buf->tail - buf->head;
87
+ size_t new_len = len + len / 2;
88
+
89
+ if (buf->base == buf->head) {
90
+ buf->head = ALLOC_N(char, new_len);
91
+ memcpy(buf->head, buf->base, len);
92
+ } else {
93
+ REALLOC_N(buf->head, char, new_len);
94
+ }
95
+ buf->tail = buf->head + toff;
96
+ buf->end = buf->head + new_len - 1;
97
+ }
98
+ *buf->tail = c;
99
+ buf->tail++;
100
+ //*buf->tail = '\0'; // for debugging
101
+ }
102
+
103
+ #endif /* OJ_BUF_H */
@@ -0,0 +1,107 @@
1
+
2
+ #include <stdlib.h>
3
+ #include <errno.h>
4
+ #include <stdio.h>
5
+ #include <string.h>
6
+ #include <stdarg.h>
7
+
8
+ #include "ruby.h"
9
+ #include "cache8.h"
10
+
11
+ #define BITS 4
12
+ #define MASK 0x000000000000000FULL
13
+ #define SLOT_CNT 16
14
+ #define DEPTH 16
15
+
16
+ typedef union {
17
+ struct _cache8 *child;
18
+ slot_t value;
19
+ } Bucket;
20
+
21
+ struct _cache8 {
22
+ Bucket buckets[SLOT_CNT];
23
+ };
24
+
25
+ static void cache8_delete(Cache8 cache, int depth);
26
+ static void slot_print(Cache8 cache, sid_t key, unsigned int depth);
27
+
28
+ void
29
+ oj_cache8_new(Cache8 *cache) {
30
+ Bucket *b;
31
+ int i;
32
+
33
+ *cache = ALLOC(struct _cache8);
34
+ for (i = SLOT_CNT, b = (*cache)->buckets; 0 < i; i--, b++) {
35
+ b->value = 0;
36
+ }
37
+ }
38
+
39
+ void
40
+ oj_cache8_delete(Cache8 cache) {
41
+ cache8_delete(cache, 0);
42
+ }
43
+
44
+ static void
45
+ cache8_delete(Cache8 cache, int depth) {
46
+ Bucket *b;
47
+ unsigned int i;
48
+
49
+ for (i = 0, b = cache->buckets; i < SLOT_CNT; i++, b++) {
50
+ if (0 != b->child) {
51
+ if (DEPTH - 1 != depth) {
52
+ cache8_delete(b->child, depth + 1);
53
+ }
54
+ }
55
+ }
56
+ xfree(cache);
57
+ }
58
+
59
+ slot_t
60
+ oj_cache8_get(Cache8 cache, sid_t key, slot_t **slot) {
61
+ Bucket *b;
62
+ int i;
63
+ sid_t k8 = (sid_t)key;
64
+ sid_t k;
65
+
66
+ for (i = 64 - BITS; 0 < i; i -= BITS) {
67
+ k = (k8 >> i) & MASK;
68
+ b = cache->buckets + k;
69
+ if (0 == b->child) {
70
+ oj_cache8_new(&b->child);
71
+ }
72
+ cache = b->child;
73
+ }
74
+ *slot = &(cache->buckets + (k8 & MASK))->value;
75
+
76
+ return **slot;
77
+ }
78
+
79
+ void
80
+ oj_cache8_print(Cache8 cache) {
81
+ /*printf("-------------------------------------------\n"); */
82
+ slot_print(cache, 0, 0);
83
+ }
84
+
85
+ static void
86
+ slot_print(Cache8 c, sid_t key, unsigned int depth) {
87
+ Bucket *b;
88
+ unsigned int i;
89
+ sid_t k8 = (sid_t)key;
90
+ sid_t k;
91
+
92
+ for (i = 0, b = c->buckets; i < SLOT_CNT; i++, b++) {
93
+ if (0 != b->child) {
94
+ k = (k8 << BITS) | i;
95
+ /*printf("*** key: 0x%016llx depth: %u i: %u\n", k, depth, i); */
96
+ if (DEPTH - 1 == depth) {
97
+ #if IS_WINDOWS
98
+ printf("0x%016lx: %4lu\n", (long unsigned int)k, (long unsigned int)b->value);
99
+ #else
100
+ printf("0x%016llx: %4llu\n", (long long unsigned int)k, (long long unsigned int)b->value);
101
+ #endif
102
+ } else {
103
+ slot_print(b->child, k, depth + 1);
104
+ }
105
+ }
106
+ }
107
+ }
@@ -0,0 +1,48 @@
1
+ /* cache8.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_CACHE8_H
32
+ #define OJ_CACHE8_H
33
+
34
+ #include "ruby.h"
35
+ #include "stdint.h"
36
+
37
+ typedef struct _cache8 *Cache8;
38
+ typedef uint64_t slot_t;
39
+ typedef uint64_t sid_t;
40
+
41
+ extern void oj_cache8_new(Cache8 *cache);
42
+ extern void oj_cache8_delete(Cache8 cache);
43
+
44
+ extern slot_t oj_cache8_get(Cache8 cache, sid_t key, slot_t **slot);
45
+
46
+ extern void oj_cache8_print(Cache8 cache);
47
+
48
+ #endif /* OJ_CACHE8_H */
@@ -0,0 +1,68 @@
1
+ /* circarray.c
2
+ * Copyright (c) 2012, Peter Ohler
3
+ * All rights reserved.
4
+ */
5
+
6
+ #include "circarray.h"
7
+
8
+ CircArray
9
+ oj_circ_array_new() {
10
+ CircArray ca;
11
+
12
+ if (0 == (ca = ALLOC(struct _circArray))) {
13
+ rb_raise(rb_eNoMemError, "not enough memory\n");
14
+ }
15
+ ca->objs = ca->obj_array;
16
+ ca->size = sizeof(ca->obj_array) / sizeof(VALUE);
17
+ ca->cnt = 0;
18
+
19
+ return ca;
20
+ }
21
+
22
+ void
23
+ oj_circ_array_free(CircArray ca) {
24
+ if (ca->objs != ca->obj_array) {
25
+ xfree(ca->objs);
26
+ }
27
+ xfree(ca);
28
+ }
29
+
30
+ void
31
+ oj_circ_array_set(CircArray ca, VALUE obj, unsigned long id) {
32
+ if (0 < id && 0 != ca) {
33
+ unsigned long i;
34
+
35
+ if (ca->size < id) {
36
+ unsigned long cnt = id + 512;
37
+
38
+ if (ca->objs == ca->obj_array) {
39
+ if (0 == (ca->objs = ALLOC_N(VALUE, cnt))) {
40
+ rb_raise(rb_eNoMemError, "not enough memory\n");
41
+ }
42
+ memcpy(ca->objs, ca->obj_array, sizeof(VALUE) * ca->cnt);
43
+ } else {
44
+ REALLOC_N(ca->objs, VALUE, cnt);
45
+ }
46
+ ca->size = cnt;
47
+ }
48
+ id--;
49
+ for (i = ca->cnt; i < id; i++) {
50
+ ca->objs[i] = Qnil;
51
+ }
52
+ ca->objs[id] = obj;
53
+ if (ca->cnt <= id) {
54
+ ca->cnt = id + 1;
55
+ }
56
+ }
57
+ }
58
+
59
+ VALUE
60
+ oj_circ_array_get(CircArray ca, unsigned long id) {
61
+ VALUE obj = Qnil;
62
+
63
+ if (id <= ca->cnt && 0 != ca) {
64
+ obj = ca->objs[id - 1];
65
+ }
66
+ return obj;
67
+ }
68
+