From: glenrendes Date: Sat, 27 Apr 2019 21:12:16 +0000 (+0200) Subject: now counts mallocs and frees, failing this test so need to be freeing more places... X-Git-Url: https://git.reasoningtechnology.com/style/static/gitweb.css?a=commitdiff_plain;h=fccfcc2225d47a2d18d640768195b8cd24f590f9;p=subu now counts mallocs and frees, failing this test so need to be freeing more places on test.lib.c --- diff --git a/module/da/src/#temp.c# b/module/da/src/#temp.c# new file mode 100644 index 0000000..2218d12 --- /dev/null +++ b/module/da/src/#temp.c# @@ -0,0 +1,20 @@ +#include + + +typedef struct { + char *base; + char *end; // one byte/one element off the end of the array + size_t size; // size >= (end - base) + 1; + size_t element_size; +} Da; + + +Da heap_count = { + .element_size = sizeof(void *), + .size = 4*sizeof(void *), + .base = malloc(4*sizeof(void *)), + .end = heap_count.base +}; +void *da_malloc_counted(size_t mem_size); +void da_free_counted(void *pt); + diff --git a/module/da/src/da.lib.c b/module/da/src/da.lib.c index 207d09b..5063b48 100644 --- a/module/da/src/da.lib.c +++ b/module/da/src/da.lib.c @@ -10,17 +10,67 @@ Cannot expand an empty array. #include #include + +//------------------------------------------------------------------ +//FREE and MALLOC + +static bool counting = false; +void da_start_heap_counter(){//assigns properties of heap_counter, must be called before other code to be used + heap_count.element_size = sizeof(void *); + heap_count.size = 4*sizeof(void *); + heap_count.base = malloc(heap_count.size); + heap_count.end = heap_count.base; + counting = true; +} +static char *da_count_expand(Da *dap){//these are redefined with malloc instead of MALLOC becuase da_malloc_counted will not make it past the push once heap_count needs to expand + char *old_base = dap->base; + size_t end_offset = dap->end - old_base; + size_t new_size = dap->size << 1; + char *new_base = malloc( new_size ); + memcpy( new_base, old_base, end_offset + dap->element_size); + free(old_base); + dap->base = new_base; + dap->end = new_base + end_offset; + dap->size = new_size; + return old_base; +} +static char *da_count_push_alloc(Da *dap){ + size_t element_off = dap->end - dap->base; + dap->end += dap->element_size; + if( dap->end > dap->base + dap->size ) da_count_expand(dap); + return dap->base + element_off; +} +static char *da_count_push(Da *dap, void *element){ + char *element_pt = da_count_push_alloc(dap); + memcpy(element_pt, element, dap->element_size); + return element_pt; +} +void *da_malloc_counted(size_t mem_size){//pushed pointer onto heap_count + void *temp = malloc(mem_size); + if( counting == true ) da_count_push(&heap_count, temp); + return (void *)temp; +} +void da_free_counted(void *pt){//pops pointer from heap_count + if( counting == true ) da_pop(&heap_count, pt); + free(pt); +} +bool da_result_heap_counter(){//returns false if heap_count is not empty or if it was not being used + if ( counting == true ){ + return heap_count.base == heap_count.end; + } else return false; +} + //-------------------------------------------------------------------------------- // allocation void da_alloc(Da *dap, size_t element_size){ dap->element_size = element_size; dap->size = 4 * element_size; - dap->base = malloc(dap->size); + dap->base = MALLOC(dap->size); dap->end = dap->base; } void da_free(Da *dap){ - free(dap->base); + FREE(dap->base); dap->size = 0; } void da_rewind(Da *dap){ @@ -47,9 +97,9 @@ char *da_expand(Da *dap){ char *old_base = dap->base; size_t end_offset = dap->end - old_base; size_t new_size = dap->size << 1; - char *new_base = malloc( new_size ); + char *new_base = MALLOC( new_size ); memcpy( new_base, old_base, end_offset + dap->element_size); - free(old_base); + FREE(old_base); dap->base = new_base; dap->end = new_base + end_offset; dap->size = new_size; @@ -117,9 +167,9 @@ void da_map(Da *dap, void f(void *, void *), void *closure){ // da being used as a resource manager -// elements were malloced, now they will all be freed +// elements were MALLOCed, now they will all be FREEd static void da_free_element(void *pt, void *closure){ - free(*(char **)pt); // free does not care about the pointer type + FREE(*(char **)pt); // FREE does not care about the pointer type } void da_free_elements(Da *dap){ @@ -308,7 +358,7 @@ da_push(damp, dap1); */ void da_erase(Da *damp){//same as da_free, don't tell anyone - free(damp->base); + FREE(damp->base); damp->size = 0; } @@ -362,7 +412,7 @@ void da_every_column(Da *damp, void f(void *, void *), void *closure){//like eve size_t columns = da_length(da_longest(damp)); size_t j = 0; while( j < columns ){ - int *col = malloc(sizeof(rows*sizeof(int))); + int *col = MALLOC(sizeof(rows*sizeof(int))); size_t i = 0; while( i < rows ) { if (da_endq(dpt,(dpt->base + j*(dpt->element_size)))) @@ -469,7 +519,7 @@ Da *da_integer_transpose(Da *damp, int *fill){ int *da_integer_to_raw_image_matrix(Da *damp, int fill){ size_t rows = damp->size / damp->element_size; size_t columns = da_length(da_longest(damp)); - int *matrix = malloc(sizeof(rows*columns));//[rows][columns] + int *matrix = MALLOC(sizeof(rows*columns));//[rows][columns] int i = 0; Da *dpt = (Da *)(damp->base); while( i < rows ) @@ -493,7 +543,7 @@ int *da_integer_to_raw_image_transpose(Da *damp, int fill){ size_t rows = damp->size/damp->element_size; size_t columns = da_length(da_longest(damp)); int *matrix = da_integer_to_raw_image_matrix(damp, fill);//[rows][columns] - int *transpose = malloc(sizeof(columns*rows)); + int *transpose = MALLOC(sizeof(columns*rows)); int i, j; for(i=0;i #include #include +#define MALLOC da_malloc_counted +#define FREE da_free_counted -typedef struct Da{ +typedef struct { char *base; char *end; // one byte/one element off the end of the array size_t size; // size >= (end - base) + 1; @@ -14,6 +16,13 @@ typedef struct Da{ #define RETURN(dap, r) \ { da_free_elements(dap); return r; } +// assuring we freed everything we allocated + +Da heap_count; +void da_start_heap_counter(void); +void *da_malloc_counted(size_t mem_size); +void da_free_counted(void *pt); +bool da_result_heap_counter(void); void da_alloc(Da *dap, size_t element_size); void da_free(Da *dap); diff --git a/module/da/src/temp.c b/module/da/src/temp.c new file mode 100644 index 0000000..67014a1 --- /dev/null +++ b/module/da/src/temp.c @@ -0,0 +1,20 @@ +#include + + +typedef struct { + char *base; + char *end; // one byte/one element off the end of the array + size_t size; // size >= (end - base) + 1; + size_t element_size; +} Da; + + +Da heap_count = { + .element_size = sizeof(void *), + .size = 4*sizeof(void *), + .base = malloc(4*sizeof(void *)), + .end = this.base +}; +void *da_malloc_counted(size_t mem_size); +void da_free_counted(void *pt); + diff --git a/module/da/test/results/results_2019-04-27T21:08:59Z_failed b/module/da/test/results/results_2019-04-27T21:08:59Z_failed new file mode 100644 index 0000000..dd38722 --- /dev/null +++ b/module/da/test/results/results_2019-04-27T21:08:59Z_failed @@ -0,0 +1,60 @@ +Hello Emacs + +2019-04-27T21:08:59Z +glendawest045@phoenix§~§ +> cd subu/module/da/ + +2019-04-27T21:09:12Z +glendawest045@phoenix§~/subu/module/da§ +> make dist-clean +/usr/bin/make --no-print-directory -f /home/glendawest045/subu/tool/lib/makefile-cc dist-clean +for i in tmp/da.lib.o tmp/makefile-cc.deps; do rm $i || true; done +for i in ; do [ -e $i ] && rm $i || true; done +rm include/da.h || true +rm lib/libda.a || true + +2019-04-27T21:09:18Z +glendawest045@phoenix§~/subu/module/da§ +> make dep lib +if [ -e tmp/makefile-cc.deps ]; then rm tmp/makefile-cc.deps; fi +/usr/bin/make --no-print-directory -f /home/glendawest045/subu/tool/lib/makefile-cc dep +C compiler only deps +deps for C linking +cp src/da.lib.h include/da.h +/usr/bin/make --no-print-directory -f /home/glendawest045/subu/tool/lib/makefile-cc lib +gcc -std=gnu11 -fPIC -Isrc -Iinclude -I/home/glendawest045/subu/module/share/include -ggdb -O0 -Werror -DDEBUG -DDEBUGDB -o tmp/da.lib.o -c src/da.lib.c +ar rcs lib/libda.a tmp/da.lib.o + +2019-04-27T21:09:28Z +glendawest045@phoenix§~/subu/module/da§ +> cd test/ + +2019-04-27T21:09:31Z +glendawest045@phoenix§~/subu/module/da/test§ +> make dep lib exec +if [ -e tmp/makefile-cc.deps ]; then rm tmp/makefile-cc.deps; fi +/usr/bin/make --no-print-directory -f /home/glendawest045/subu/tool/lib/makefile-cc dep +C compiler only deps +deps for C linking +/usr/bin/make --no-print-directory -f /home/glendawest045/subu/tool/lib/makefile-cc lib +gcc -std=gnu11 -fPIC -Iinclude -I../include -ggdb -DDEBUG -DDEBUGDB -o tmp/test_da.lib.o -c src/test_da.lib.c +ar rcs lib/libtest.a tmp/test_da.lib.o +/usr/bin/make --no-print-directory -f /home/glendawest045/subu/tool/lib/makefile-cc exec +make sub_exec +/usr/bin/make --no-print-directory -f /home/glendawest045/subu/tool/lib/makefile-cc sub_exec +gcc -std=gnu11 -fPIC -Iinclude -I../include -ggdb -DDEBUG -DDEBUGDB -o tmp/test_da.cli.o -c src/test_da.cli.c +gcc -o exec/test_da tmp/test_da.cli.o -Llib -L../lib -ltest -lda + +2019-04-27T21:09:39Z +glendawest045@phoenix§~/subu/module/da/test§ +> cd exec/ + +2019-04-27T21:09:43Z +glendawest045@phoenix§~/subu/module/da/test/exec§ +> ./test_da +da_result_heap_counter failed +failed 1 of 26 tests + +2019-04-27T21:09:48Z +glendawest045@phoenix§~/subu/module/da/test/exec§ +> \ No newline at end of file diff --git a/module/da/test/src/test_da.cli.c b/module/da/test/src/test_da.cli.c index 045ab23..10d0652 100644 --- a/module/da/test/src/test_da.cli.c +++ b/module/da/test/src/test_da.cli.c @@ -2,10 +2,13 @@ #include #include #include "test_da.lib.h" +#include + int main(){ // enumeration of tests typedef bool (*test_fun)(); + da_start_heap_counter(); test_fun tests[] = { test_da_push_0, @@ -33,6 +36,7 @@ int main(){ test_da_erase_0, test_da_longer_0, test_da_longest_0, + da_result_heap_counter, NULL}; char *test_names[] = { @@ -61,6 +65,7 @@ int main(){ "test_da_erase_0", "test_da_longer_0", "test_da_longest_0", + "da_result_heap_counter", NULL}; // call tests diff --git a/module/da/test/src/test_da.lib.c b/module/da/test/src/test_da.lib.c index a33ae54..4d040a4 100644 --- a/module/da/test/src/test_da.lib.c +++ b/module/da/test/src/test_da.lib.c @@ -301,7 +301,7 @@ bool test_da_rebase_0(){ Da dar; da_alloc(&dar,sizeof(char)); - char **el_pt = (char **)malloc(4*(sizeof(char*))); + char **el_pt = (char **)MALLOC(4*(sizeof(char*))); {//push 2-5 into dar, leave with pointer to last element int i = 0; char arr[4] = {'t','e','s','t'}; @@ -347,15 +347,15 @@ bool test_da_rebase_0(){ return flag1 && flag2 && flag3 && result; } -/* {//problems with free and malloc - char *a = malloc(10); +/* {//problems with FREE and MALLOC + char *a = MALLOC(10); strcpy(a, "zsdf"); Da da; Da *da_pt = &da; da_alloc(da_pt, sizeof(char *)); da_push(da_pt, a); ... - free(*(char *)da_index(da_pt, 0)); + FREE(*(char *)da_index(da_pt, 0)); da_free(da_pt); } */ @@ -425,7 +425,7 @@ bool test_da_map_0(){ //tests da_present bool test_da_present_0(){ int dar_size = 0; - Da **dar = malloc(3 * sizeof(Da *)); + Da **dar = MALLOC(3 * sizeof(Da *)); Da dap_0; Da *dap_0_pt = &dap_0; @@ -649,7 +649,7 @@ bool test_da_free_0(){ Da *keep = da_pt; Da *save = da_pt; - //free da + //FREE da da_free(da_pt); //re-allocate memory to dew da of chars @@ -732,7 +732,7 @@ bool test_da_erase_0(){ Da *keep = damp; Da *save = damp; - //free da + //FREE da da_erase(damp); //re-allocate memory to dew da of chars diff --git a/module/da/test/src/test_da.lib.h b/module/da/test/src/test_da.lib.h index cb83d5a..9c9cd32 100644 --- a/module/da/test/src/test_da.lib.h +++ b/module/da/test/src/test_da.lib.h @@ -1,5 +1,5 @@ -#ifndef DA_LIB_H -#define DA_LIB_H +#ifndef TEST_DA_LIB_H +#define TEST_DA_LIB_H bool test_da_push_0(); bool test_da_expand_0();