diff --git a/cpp/negentropy.h b/cpp/negentropy.h index 9a8e4ee..57dc97c 100644 --- a/cpp/negentropy.h +++ b/cpp/negentropy.h @@ -50,7 +50,7 @@ struct Negentropy { std::string output; output.push_back(PROTOCOL_VERSION); - std::cout << "storage size" << storage.size() << std::endl; + std::cout << "storage size:" << storage.size() << std::endl; output += splitRange(0, storage.size(), Bound(MAX_U64)); return output; diff --git a/cpp/negentropy_wrapper.c b/cpp/negentropy_wrapper.c index ffd41e2..46585b5 100644 --- a/cpp/negentropy_wrapper.c +++ b/cpp/negentropy_wrapper.c @@ -67,8 +67,8 @@ size_t negentropy_initiate(void* negentropy, buffer* out){ std::string* output = new std::string(); try { *output = ngn_inst->initiate(); - std::cout << "output of initiate is, len:" << output->size() << ", output:"; - printHexString(std::string_view(*output)); +/* std::cout << "output of initiate is, len:" << output->size() << ", output:"; + printHexString(std::string_view(*output)); */ } catch(negentropy::err e){ std::cout << "Exception raised in initiate " << e.what() << std::endl; //TODO:Find a way to return this error @@ -93,8 +93,8 @@ bool storage_insert(void* storage, uint64_t createdAt, buffer* id){ lmdbStorage = reinterpret_cast(storage); std::string_view data(reinterpret_cast< char const* >(id->data), id->len); - std::cout << "inserting entry in storage, createdAt:" << createdAt << ",id:"; - printHexString(data); +/* std::cout << "inserting entry in storage, createdAt:" << createdAt << ",id:"; + printHexString(data); */ //TODO: Error handling. Is it required? //How does out of memory get handled? @@ -106,8 +106,8 @@ bool storage_erase(void* storage, uint64_t createdAt, buffer* id){ lmdbStorage = reinterpret_cast(storage); std::string_view data(reinterpret_cast< char const* >(id->data), id->len); - std::cout << "erasing entry from storage, createdAt:" << createdAt << ",id:"; - printHexString(data); +/* std::cout << "erasing entry from storage, createdAt:" << createdAt << ",id:"; + printHexString(data); */ //TODO: Error handling return lmdbStorage->erase(createdAt, data); @@ -119,8 +119,8 @@ size_t reconcile(void* negentropy, buffer* query, buffer* output){ std::string* out = new std::string(); try { *out = ngn_inst->reconcile(std::string_view(reinterpret_cast< char const* >(query->data), query->len)); - std::cout << "reconcile output of reconcile is, len:" << out->size() << ", output:"; - printHexString(std::string_view(*out)); +/* std::cout << "reconcile output of reconcile is, len:" << out->size() << ", output:"; + printHexString(std::string_view(*out)); */ } catch(negentropy::err e){ //TODO:Find a way to return this error std::cout << "Exception raised in reconcile " << e.what() << std::endl; @@ -187,7 +187,7 @@ int reconcile_with_ids(void* negentropy, buffer* query,reconcile_cbk cbk, char* void transform_with_alloc(std::vector &from_ids, buffer* to_ids) { for (int i=0; i < from_ids.size(); i ++){ - to_ids[i].data = (unsigned char*) malloc(from_ids[i].size()*sizeof(unsigned char)); + to_ids[i].data = (unsigned char*) calloc(from_ids[i].size(), sizeof(unsigned char)); to_ids[i].len = from_ids[i].size(); memcpy(to_ids[i].data, from_ids[i].c_str(),to_ids[i].len); } @@ -201,16 +201,20 @@ void reconcile_with_ids_no_cbk(void* negentropy, buffer* query, result* result) std::vector haveIds, needIds; try { out = ngn_inst->reconcile(std::string_view(reinterpret_cast< char const* >(query->data), query->len), haveIds, needIds); - result->have_ids_len = haveIds.size(); result->need_ids_len = needIds.size(); - result->have_ids = (buffer*)malloc(result->have_ids_len*sizeof(buffer)); - result->need_ids = (buffer*)malloc(result->need_ids_len*sizeof(buffer)); + if (haveIds.size() > 0){ + result->have_ids = (buffer*)calloc(result->have_ids_len, sizeof(buffer)); + transform_with_alloc(haveIds, result->have_ids); + } - std::cout << "have_ids_len:" << result->have_ids_len << "need_ids_len:" << result->need_ids_len << std::endl; + if (needIds.size() > 0) { + result->need_ids = (buffer*)calloc(result->need_ids_len, sizeof(buffer)); + transform_with_alloc(needIds, result->need_ids); + } + + // std::cout << "have_ids_len:" << result->have_ids_len << "need_ids_len:" << result->need_ids_len << std::endl; - transform_with_alloc(haveIds, result->have_ids); - transform_with_alloc(needIds, result->need_ids); } catch(negentropy::err e){ std::cout << "caught error "<< e.what() << std::endl; @@ -220,25 +224,35 @@ void reconcile_with_ids_no_cbk(void* negentropy, buffer* query, result* result) buffer output = {0,NULL}; if (out) { result->output.len = out.value().size(); - result->output.data = (unsigned char*)malloc(out.value().size()*sizeof(unsigned char)); - result->output.data = (unsigned char*)out.value().c_str(); - std::cout << "reconcile_with_ids output of reconcile is, len:" << out.value().size() << ", output:"; - printHexString(std::string_view(out.value())); + result->output.data = (unsigned char*)calloc(out.value().size(), sizeof(unsigned char)); + memcpy(result->output.data, (unsigned char*)out.value().c_str(),result->output.len) ; +/* std::cout << "reconcile_with_ids output of reconcile is, len:" << out.value().size() << ", output:"; + printHexString(std::string_view(out.value())); */ + }else { + std::cout << "reconcile_with_ids_no_cbk output is empty " << std::endl; + result->output.len = 0; + result->output.data = NULL; } return ; } //Note: This function assumes that all relevant heap memory is alloced and just tries to free void free_result(result* r){ - free((void *) r->output.data); + if (r->output.len > 0) { + free((void *) r->output.data); + } - for (int i = 0; i < r->have_ids_len; i++) { - free((void *) r->have_ids[i].data); + if (r->have_ids_len > 0){ + for (int i = 0; i < r->have_ids_len; i++) { + free((void *) r->have_ids[i].data); + } + free((void *)r->have_ids); } - free((void *)r->have_ids); - for (int i = 0; i < r->need_ids_len; i++) { - free((void *) r->need_ids[i].data); + if (r->need_ids_len > 0) { + for (int i = 0; i < r->need_ids_len; i++) { + free((void *) r->need_ids[i].data); + } + free((void *)r->need_ids); } - free((void *)r->need_ids); }