1515#include < unistd.h>
1616#endif
1717
18+ // The function that users are expected to call is json_parse.
19+ // We have more than one such function because we want to support several
20+ // instruction sets.
21+
1822// function pointer type for json_parse
1923using json_parse_functype = int (const uint8_t *buf, size_t len, ParsedJson &pj, bool reallocifneeded);
2024
2125// Pointer that holds the json_parse implementation corresponding to the available SIMD instruction set
2226extern json_parse_functype *json_parse_ptr;
2327
28+
29+ // json_parse_implementation is the generic function, it is specialized for various
30+ // SIMD instruction sets, e.g., as json_parse_implementation<simdjson::instruction_set::avx2>
31+ // or json_parse_implementation<simdjson::instruction_set::neon>
2432template <simdjson::instruction_set T>
2533int json_parse_implementation (const uint8_t *buf, size_t len, ParsedJson &pj, bool reallocifneeded = true ) {
2634 if (pj.bytecapacity < len) {
@@ -29,22 +37,22 @@ int json_parse_implementation(const uint8_t *buf, size_t len, ParsedJson &pj, bo
2937 bool reallocated = false ;
3038 if (reallocifneeded) {
3139#ifdef ALLOW_SAME_PAGE_BUFFER_OVERRUN
32- // realloc is needed if the end of the memory crosses a page
40+ // realloc is needed if the end of the memory crosses a page
3341#ifdef _MSC_VER
34- SYSTEM_INFO sysInfo;
35- GetSystemInfo (&sysInfo);
36- long pagesize = sysInfo.dwPageSize ;
42+ SYSTEM_INFO sysInfo;
43+ GetSystemInfo (&sysInfo);
44+ long pagesize = sysInfo.dwPageSize ;
3745#else
3846 long pagesize = sysconf (_SC_PAGESIZE);
3947#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+ // ////////////
49+ // We want to check that buf + len - 1 and buf + len - 1 + SIMDJSON_PADDING
50+ // are in the same page.
51+ // That is, we want to check that
52+ // (buf + len - 1) / pagesize == (buf + len - 1 + SIMDJSON_PADDING) / pagesize
53+ // That's true if (buf + len - 1) % pagesize + SIMDJSON_PADDING < pagesize.
54+ // /////////
55+ if ( (reinterpret_cast <uintptr_t >(buf + len - 1 ) % pagesize ) + SIMDJSON_PADDING < static_cast <uintptr_t >(pagesize) ) {
4856#else // SIMDJSON_SAFE_SAME_PAGE_READ_OVERRUN
4957 if (true ) { // if not SIMDJSON_SAFE_SAME_PAGE_READ_OVERRUN, we always reallocate
5058#endif
@@ -53,8 +61,8 @@ int json_parse_implementation(const uint8_t *buf, size_t len, ParsedJson &pj, bo
5361 if (buf == NULL ) return simdjson::MEMALLOC;
5462 memcpy ((void *)buf,tmpbuf,len);
5563 reallocated = true ;
56- }
57- }
64+ } // if (true) OR if ( (reinterpret_cast<uintptr_t>(buf + len - 1) % pagesize ) + SIMDJSON_PADDING < static_cast<uintptr_t>(pagesize) ) {
65+ } // if(reallocifneeded) {
5866 int stage1_is_ok = find_structural_bits<T>(buf, len, pj);
5967 if (stage1_is_ok != simdjson::SUCCESS) {
6068 pj.errorcode = stage1_is_ok;
@@ -81,7 +89,6 @@ int json_parse_implementation(const uint8_t *buf, size_t len, ParsedJson &pj, bo
8189// all bytes at and after buf + len are ignored (can be garbage).
8290// The ParsedJson object can be reused.
8391
84- WARN_UNUSED
8592inline int json_parse (const uint8_t *buf, size_t len, ParsedJson &pj, bool reallocifneeded = true ) {
8693 return json_parse_ptr (buf, len, pj, reallocifneeded);
8794}
@@ -102,7 +109,6 @@ inline int json_parse(const uint8_t *buf, size_t len, ParsedJson &pj, bool reall
102109// The input buf should be readable up to buf + len + SIMDJSON_PADDING if reallocifneeded is false,
103110// all bytes at and after buf + len are ignored (can be garbage).
104111// The ParsedJson object can be reused.
105- WARN_UNUSED
106112inline int json_parse (const char * buf, size_t len, ParsedJson &pj, bool reallocifneeded = true ) {
107113 return json_parse_ptr (reinterpret_cast <const uint8_t *>(buf), len, pj, reallocifneeded);
108114}
@@ -120,7 +126,6 @@ int json_parse(const char * buf, ParsedJson &pj) = delete;
120126//
121127// A temporary buffer is created when needed during processing
122128// (a copy of the input string is made).
123- WARN_UNUSED
124129inline int json_parse (const std::string &s, ParsedJson &pj) {
125130 return json_parse (s.data (), s.length (), pj, true );
126131}
@@ -135,7 +140,6 @@ inline int json_parse(const std::string &s, ParsedJson &pj) {
135140//
136141// You can also check validity
137142// by calling pj.isValid(). The same ParsedJson can be reused for other documents.
138- WARN_UNUSED
139143inline int json_parse (const padded_string &s, ParsedJson &pj) {
140144 return json_parse (s.data (), s.length (), pj, false );
141145}
0 commit comments