88#include " simdjson/stage1_find_marks.h"
99#include " simdjson/stage2_build_tape.h"
1010#include " simdjson/simdjson.h"
11+ #ifdef _MSC_VER
12+ #include < windows.h>
13+ #include < sysinfoapi.h>
14+ #else
15+ #include < unistd.h>
16+ #endif
17+
18+ // function pointer type for json_parse
19+ using json_parse_functype = int (const uint8_t *buf, size_t len, ParsedJson &pj, bool reallocifneeded);
20+
21+ // Pointer that holds the json_parse implementation corresponding to the available SIMD instruction set
22+ extern json_parse_functype *json_parse_ptr;
23+
24+ template <simdjson::instruction_set T>
25+ int json_parse_implementation (const uint8_t *buf, size_t len, ParsedJson &pj, bool reallocifneeded = true ) {
26+ if (pj.bytecapacity < len) {
27+ return simdjson::CAPACITY;
28+ }
29+ bool reallocated = false ;
30+ if (reallocifneeded) {
31+ #ifdef ALLOW_SAME_PAGE_BUFFER_OVERRUN
32+ // realloc is needed if the end of the memory crosses a page
33+ #ifdef _MSC_VER
34+ SYSTEM_INFO sysInfo;
35+ GetSystemInfo (&sysInfo);
36+ long pagesize = sysInfo.dwPageSize ;
37+ #else
38+ long pagesize = sysconf (_SC_PAGESIZE);
39+ #endif
40+ // ////////////
41+ // We want to check that buf + len - 1 and buf + len - 1 + SIMDJSON_PADDING
42+ // are in the same page.
43+ // That is, we want to check that
44+ // (buf + len - 1) / pagesize == (buf + len - 1 + SIMDJSON_PADDING) / pagesize
45+ // That's true if (buf + len - 1) % pagesize + SIMDJSON_PADDING < pagesize.
46+ // /////////
47+ if ( (reinterpret_cast <uintptr_t >(buf + len - 1 ) % pagesize ) + SIMDJSON_PADDING < static_cast <uintptr_t >(pagesize) ) {
48+ #else // SIMDJSON_SAFE_SAME_PAGE_READ_OVERRUN
49+ if (true ) { // if not SIMDJSON_SAFE_SAME_PAGE_READ_OVERRUN, we always reallocate
50+ #endif
51+ const uint8_t *tmpbuf = buf;
52+ buf = (uint8_t *) allocate_padded_buffer (len);
53+ if (buf == NULL ) return simdjson::MEMALLOC;
54+ memcpy ((void *)buf,tmpbuf,len);
55+ reallocated = true ;
56+ }
57+ }
58+ int stage1_is_ok = find_structural_bits<T>(buf, len, pj);
59+ if (stage1_is_ok != simdjson::SUCCESS) {
60+ pj.errorcode = stage1_is_ok;
61+ return pj.errorcode ;
62+ }
63+ int res = unified_machine (buf, len, pj);
64+ if (reallocated) { aligned_free ((void *)buf);}
65+ return res;
66+ }
1167
1268// Parse a document found in buf.
1369// You need to preallocate ParsedJson with a capacity of len (e.g., pj.allocateCapacity(len)).
2480// The input buf should be readable up to buf + len + SIMDJSON_PADDING if reallocifneeded is false,
2581// all bytes at and after buf + len are ignored (can be garbage).
2682// The ParsedJson object can be reused.
83+
2784WARN_UNUSED
28- int json_parse (const uint8_t *buf, size_t len, ParsedJson &pj, bool reallocifneeded = true );
85+ inline int json_parse (const uint8_t *buf, size_t len, ParsedJson &pj, bool reallocifneeded = true ) {
86+ return json_parse_ptr (buf, len, pj, reallocifneeded);
87+ }
2988
3089// Parse a document found in buf.
3190// You need to preallocate ParsedJson with a capacity of len (e.g., pj.allocateCapacity(len)).
@@ -45,7 +104,7 @@ int json_parse(const uint8_t *buf, size_t len, ParsedJson &pj, bool reallocifnee
45104// The ParsedJson object can be reused.
46105WARN_UNUSED
47106inline int json_parse (const char * buf, size_t len, ParsedJson &pj, bool reallocifneeded = true ) {
48- return json_parse (reinterpret_cast <const uint8_t *>(buf), len, pj, reallocifneeded);
107+ return json_parse_ptr (reinterpret_cast <const uint8_t *>(buf), len, pj, reallocifneeded);
49108}
50109
51110// We do not want to allow implicit conversion from C string to std::string.
@@ -140,4 +199,4 @@ inline ParsedJson build_parsed_json(const padded_string &s) {
140199
141200
142201
143- #endif
202+ #endif
0 commit comments