Fix for issues 32, 50, 131, 137

* Improving portability.

* Revisiting faulty logic regarding same-page overruns.

* Disabling same-page overruns under VS.

* Clarifying the documentation

* Fix for issue 131 + being more explicit regarding memory realloc.

* Fix for issue 137.

* removing "using namespace std" throughout. Fix for 50

* Introducing typed malloc/free.

* Introducing a custom class (padded_string) that solves several minor usability issues.

* Updating amalgamation for testing.
This commit is contained in:
Daniel Lemire
2019-05-09 17:59:51 -04:00
committed by GitHub
parent c5a3f9ccd4
commit e370a65383
31 changed files with 1109 additions and 366 deletions
+6 -7
View File
@@ -7,28 +7,27 @@ char * allocate_padded_buffer(size_t length) {
//return (char *) malloc(length + SIMDJSON_PADDING);
// However, we might as well align to cache lines...
size_t totalpaddedlength = length + SIMDJSON_PADDING;
char *padded_buffer = (char *) aligned_malloc(64, totalpaddedlength);
char *padded_buffer = aligned_malloc_char(64, totalpaddedlength);
return padded_buffer;
}
std::string_view get_corpus(const std::string& filename) {
padded_string get_corpus(const std::string& filename) {
std::FILE *fp = std::fopen(filename.c_str(), "rb");
if (fp != nullptr) {
std::fseek(fp, 0, SEEK_END);
size_t len = std::ftell(fp);
char * buf = allocate_padded_buffer(len);
if(buf == nullptr) {
padded_string s(len);
if(s.data() == nullptr) {
std::fclose(fp);
throw std::runtime_error("could not allocate memory");
}
std::rewind(fp);
size_t readb = std::fread(buf, 1, len, fp);
size_t readb = std::fread(s.data(), 1, len, fp);
std::fclose(fp);
if(readb != len) {
aligned_free(buf);
throw std::runtime_error("could not read the data");
}
return std::string_view(buf,len);
return s;
}
throw std::runtime_error("could not load corpus");
}
+2 -3
View File
@@ -23,7 +23,6 @@
#ifdef SIMDJSON_UTF8VALIDATE
#include "simdjson/simdutf8check.h"
#endif
using namespace std;
#define TRANSPOSE
@@ -501,9 +500,9 @@ WARN_UNUSED
/*never_inline*/ bool find_structural_bits(const uint8_t *buf, size_t len,
ParsedJson &pj) {
if (len > pj.bytecapacity) {
cerr << "Your ParsedJson object only supports documents up to "
std::cerr << "Your ParsedJson object only supports documents up to "
<< pj.bytecapacity << " bytes but you are trying to process " << len
<< " bytes\n";
<< " bytes" << std::endl;
return false;
}
uint32_t *base_ptr = pj.structural_indexes;
+9 -2
View File
@@ -12,14 +12,15 @@
#define PATH_SEP '/'
using namespace std;
WARN_UNUSED
really_inline bool is_valid_true_atom(const uint8_t *loc) {
uint64_t tv = *reinterpret_cast<const uint64_t *>("true ");
uint64_t mask4 = 0x00000000ffffffff;
uint32_t error = 0;
uint64_t locval; // we want to avoid unaligned 64-bit loads (undefined in C/C++)
// this can read up to 7 bytes beyond the buffer size, but we require
// SIMDJSON_PADDING of padding
static_assert(sizeof(uint64_t) - 1 <= SIMDJSON_PADDING);
std::memcpy(&locval, loc, sizeof(uint64_t));
error = (locval & mask4) ^ tv;
error |= is_not_structural_or_whitespace(loc[4]);
@@ -40,6 +41,9 @@ really_inline bool is_valid_false_atom(const uint8_t *loc) {
// ignored
uint64_t error = 0;
uint64_t locval; // we want to avoid unaligned 64-bit loads (undefined in C/C++)
// this can read up to 7 bytes beyond the buffer size, but we require
// SIMDJSON_PADDING of padding
static_assert(sizeof(uint64_t) - 1 <= SIMDJSON_PADDING);
std::memcpy(&locval, loc, sizeof(uint64_t));
error = (locval & mask5) ^ fv;
error |= is_not_structural_or_whitespace(loc[5]);
@@ -52,6 +56,9 @@ really_inline bool is_valid_null_atom(const uint8_t *loc) {
uint64_t mask4 = 0x00000000ffffffff;
uint32_t error = 0;
uint64_t locval; // we want to avoid unaligned 64-bit loads (undefined in C/C++)
// this can read up to 7 bytes beyond the buffer size, but we require
// SIMDJSON_PADDING of padding
static_assert(sizeof(uint64_t) - 1 <= SIMDJSON_PADDING);
std::memcpy(&locval, loc, sizeof(uint64_t));
error = (locval & mask4) ^ nv;
error |= is_not_structural_or_whitespace(loc[4]);