From b2bc08786b19dd5bd4d494d690263da5644b0061 Mon Sep 17 00:00:00 2001 From: Prem Chaitanya Prathi Date: Thu, 7 Mar 2024 16:02:47 +0530 Subject: [PATCH 1/3] fix: added functions to cleanup memory --- cpp/negentropy_wrapper.c | 14 +++++++++++++- cpp/negentropy_wrapper.h | 4 ++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/cpp/negentropy_wrapper.c b/cpp/negentropy_wrapper.c index 3d3790b..ffd41e2 100644 --- a/cpp/negentropy_wrapper.c +++ b/cpp/negentropy_wrapper.c @@ -34,6 +34,16 @@ void* storage_new(const char* db_path, const char* name){ return storage; } +void storage_delete(void* storage){ + negentropy::storage::BTreeMem* lmdbStorage = reinterpret_cast(storage); + delete lmdbStorage; +} + +void negentropy_delete(void* negentropy){ + Negentropy* ngn_inst = reinterpret_cast*>(negentropy); + delete ngn_inst; +} + void* negentropy_new(void* storage, uint64_t frameSizeLimit){ //TODO: Make these typecasts into macros?? negentropy::storage::BTreeMem* lmdbStorage; @@ -60,6 +70,7 @@ size_t negentropy_initiate(void* negentropy, buffer* out){ 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 return 0; } @@ -112,6 +123,7 @@ size_t reconcile(void* negentropy, buffer* query, buffer* 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; return 0; } memcpy( output->data, out->c_str() ,out->size()); @@ -149,7 +161,7 @@ int reconcile_with_ids(void* negentropy, buffer* query,reconcile_cbk cbk, char* transform(haveIds, have_ids); transform(needIds, need_ids); } catch(negentropy::err e){ - std::cout << "caught error "<< e.what() << std::endl; + std::cout << "exception raised in reconcile_with_ids"<< e.what() << std::endl; //TODO:Find a way to return this error and cleanup partially allocated memory if any return -1; } diff --git a/cpp/negentropy_wrapper.h b/cpp/negentropy_wrapper.h index dd3fd29..41c2a1c 100644 --- a/cpp/negentropy_wrapper.h +++ b/cpp/negentropy_wrapper.h @@ -26,8 +26,12 @@ typedef struct _result_ { EXTERNC void* storage_new(const char* db_path, const char* name); +EXTERNC void storage_delete(void* storage); + EXTERNC void* negentropy_new(void* storage, uint64_t frameSizeLimit); +EXTERNC void negentropy_delete(void* negentropy); + EXTERNC size_t negentropy_initiate(void* negentropy, buffer* output); EXTERNC void negentropy_setinitiator(void* negentropy); From c49d60ddee55a1498f691856d59798ba17b5b98d Mon Sep 17 00:00:00 2001 From: Prem Chaitanya Prathi Date: Sat, 9 Mar 2024 12:50:16 +0530 Subject: [PATCH 2/3] fix: using stack memory for return --- cpp/negentropy.h | 2 +- cpp/negentropy_wrapper.c | 66 ++++++++++++++++++++++++---------------- 2 files changed, 41 insertions(+), 27 deletions(-) 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); } From 1411f20219a0bf15fb7af456e06b92a879862e51 Mon Sep 17 00:00:00 2001 From: Prem Chaitanya Prathi Date: Mon, 11 Mar 2024 14:01:21 +0530 Subject: [PATCH 3/3] chore:added storage size method --- cpp/example/test.c | 4 +++- cpp/negentropy_wrapper.c | 7 ++++++- cpp/negentropy_wrapper.h | 2 ++ 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/cpp/example/test.c b/cpp/example/test.c index f3511db..7d77e93 100644 --- a/cpp/example/test.c +++ b/cpp/example/test.c @@ -83,6 +83,8 @@ int main(){ buffer b4 ; b4.len = 153600; b4.data = (unsigned char*)malloc(153600); + + printf("storage size of st2 is %d \n",storage_size(st2)); size_t outSize = negentropy_initiate(ngn_inst1, &b4); if(outSize == 0){ @@ -120,4 +122,4 @@ int main(){ free(b3.data); free(b4.data); -} \ No newline at end of file +} diff --git a/cpp/negentropy_wrapper.c b/cpp/negentropy_wrapper.c index 46585b5..697b645 100644 --- a/cpp/negentropy_wrapper.c +++ b/cpp/negentropy_wrapper.c @@ -39,6 +39,11 @@ void storage_delete(void* storage){ delete lmdbStorage; } +int storage_size(void* storage){ + negentropy::storage::BTreeMem* lmdbStorage = reinterpret_cast(storage); + return lmdbStorage->size(); +} + void negentropy_delete(void* negentropy){ Negentropy* ngn_inst = reinterpret_cast*>(negentropy); delete ngn_inst; @@ -229,7 +234,7 @@ void reconcile_with_ids_no_cbk(void* negentropy, buffer* query, result* result) /* 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; + //std::cout << "reconcile_with_ids_no_cbk output is empty " << std::endl; result->output.len = 0; result->output.data = NULL; } diff --git a/cpp/negentropy_wrapper.h b/cpp/negentropy_wrapper.h index 41c2a1c..985bfa2 100644 --- a/cpp/negentropy_wrapper.h +++ b/cpp/negentropy_wrapper.h @@ -28,6 +28,8 @@ EXTERNC void* storage_new(const char* db_path, const char* name); EXTERNC void storage_delete(void* storage); +EXTERNC int storage_size(void* storage); + EXTERNC void* negentropy_new(void* storage, uint64_t frameSizeLimit); EXTERNC void negentropy_delete(void* negentropy);