Still working

This commit is contained in:
Daniel Lemire
2018-11-27 10:10:39 -05:00
parent 50defa510f
commit 5fae7b2100
15 changed files with 134 additions and 91 deletions
+14 -7
View File
@@ -5,7 +5,11 @@
// returns NULL if memory cannot be allocated
// This structure is meant to be reused from document to document, as needed.
// you can use deallocate_ParsedJson to deallocate the memory.
ParsedJson *allocate_ParsedJson(size_t len) {
ParsedJson *allocate_ParsedJson(size_t len, size_t maxdepth) {
if((maxdepth == 0) || (len == 0)) {
std::cerr << "capacities must be non-zero " << std::endl;
return NULL;
}
ParsedJson *pj_ptr = new ParsedJson;
if (pj_ptr == NULL) {
std::cerr << "Could not allocate memory for core struct." << std::endl;
@@ -32,13 +36,15 @@ ParsedJson *allocate_ParsedJson(size_t len) {
pj.string_buf = new u8[ROUNDUP_N(len, 64)];
pj.number_buf = new u8[4 * ROUNDUP_N(len, 64)];
pj.tape = new u64[ROUNDUP_N(len, 64)];
size_t depthcapacity = ROUNDUP_N(len, 64);
pj.tape_locs = new u32[depthcapacity];
pj.containing_scope_offset = new u32[maxdepth];
pj.ret_address = new void*[maxdepth];
if ((pj.string_buf == NULL) || (pj.number_buf == NULL) || (pj.tape == NULL) || (pj.tape_locs == NULL)) {
if ((pj.string_buf == NULL) || (pj.number_buf == NULL) || (pj.tape == NULL)
|| (pj.containing_scope_offset == NULL) || (pj.ret_address == NULL) ) {
std::cerr << "Could not allocate memory"
<< std::endl;
delete[] pj.tape_locs;
delete[] pj.ret_address;
delete[] pj.containing_scope_offset;
delete[] pj.tape;
delete[] pj.number_buf;
delete[] pj.string_buf;
@@ -49,14 +55,15 @@ ParsedJson *allocate_ParsedJson(size_t len) {
}
pj.bytecapacity = len;
pj.depthcapacity = depthcapacity;
pj.depthcapacity = maxdepth;
return pj_ptr;
}
void deallocate_ParsedJson(ParsedJson *pj_ptr) {
if (pj_ptr == NULL)
return;
delete[] pj_ptr->tape_locs;
delete[] pj_ptr->ret_address;
delete[] pj_ptr->containing_scope_offset;
delete[] pj_ptr->tape;
delete[] pj_ptr->number_buf;
delete[] pj_ptr->string_buf;