| 1 | #include "simdjson/jsonioutil.h" |
| 2 | #include <cstdlib> |
| 3 | #include <cstring> |
| 4 | #include <climits> |
| 5 | |
| 6 | namespace simdjson { |
| 7 | char *allocate_padded_buffer(size_t length) { |
| 8 | // we could do a simple malloc |
| 9 | // return (char *) malloc(length + SIMDJSON_PADDING); |
| 10 | // However, we might as well align to cache lines... |
| 11 | size_t totalpaddedlength = length + SIMDJSON_PADDING; |
| 12 | char *padded_buffer = aligned_malloc_char(64, totalpaddedlength); |
| 13 | memset(padded_buffer + length, 0, totalpaddedlength - length); |
| 14 | return padded_buffer; |
| 15 | } |
| 16 | |
| 17 | padded_string get_corpus(const std::string &filename) { |
| 18 | std::FILE *fp = std::fopen(filename.c_str(), "rb" ); |
| 19 | if (fp != nullptr) { |
| 20 | if(std::fseek(fp, 0, SEEK_END) < 0) { |
| 21 | std::fclose(fp); |
| 22 | throw std::runtime_error("cannot seek in the file" ); |
| 23 | } |
| 24 | long llen = std::ftell(fp); |
| 25 | if((llen < 0) || (llen == LONG_MAX)) { |
| 26 | std::fclose(fp); |
| 27 | throw std::runtime_error("cannot tell where we are in the file" ); |
| 28 | } |
| 29 | size_t len = (size_t) llen; |
| 30 | padded_string s(len); |
| 31 | if (s.data() == nullptr) { |
| 32 | std::fclose(fp); |
| 33 | throw std::runtime_error("could not allocate memory" ); |
| 34 | } |
| 35 | std::rewind(fp); |
| 36 | size_t readb = std::fread(s.data(), 1, len, fp); |
| 37 | std::fclose(fp); |
| 38 | if (readb != len) { |
| 39 | throw std::runtime_error("could not read the data" ); |
| 40 | } |
| 41 | return s; |
| 42 | } |
| 43 | throw std::runtime_error("could not load corpus" ); |
| 44 | } |
| 45 | } // namespace simdjson |
| 46 | |