From: glenrendes Date: Fri, 3 May 2019 18:09:12 +0000 (+0200) Subject: da has working accounting for heap allocation X-Git-Url: https://git.reasoningtechnology.com/style/rt_dark_doc.css?a=commitdiff_plain;h=c1f586927dcd2384264a4ef53bdd0951c670edac;p=subu da has working accounting for heap allocation --- diff --git a/module/da/makefile b/module/da/makefile index a744187..b8832c6 100644 --- a/module/da/makefile +++ b/module/da/makefile @@ -18,7 +18,6 @@ dep: .PHONY: lib lib: cp $(SRCDIR)/da.lib.h $(INCDIR)/da.h -# cat $(SRCDIR)/da.lib.h $(SRCDIR)/da_na.lib.h > $(INCDIR)/da.h $(MAKE) $@ .PHONY: exec diff --git a/module/da/src/#da.lib.h# b/module/da/src/#da.lib.h# deleted file mode 100644 index 7b354e0..0000000 --- a/module/da/src/#da.lib.h# +++ /dev/null @@ -1,116 +0,0 @@ -#ifndef DA_LIB_H -#define DA_LIB_H -#include -#include -#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; - -//#include "da_na.lib.h" - -#define RETURN(dap, r) \ - { da_free_elements(dap); return r; } - - -void da_alloc(Da *dap, size_t element_size); -void da_free(Da *dap); -void da_rewind(Da *dap); -bool da_empty(Da *dap); -size_t da_length(Da *dap); -void da_rebase(Da *dap, char *old_base, void *pta); -char *da_expand(Da *dap); -bool da_boundq(Da *dap); - -char *da_index(Da *dap, size_t i); -char *da_push_alloc(Da *dap); -char *da_push(Da *dap, void *element); -bool da_pop(Da *dap, void *element); - -bool da_endq(Da *dap, void *pt); -void da_map(Da *dap, void f(void *, void *), void *closure); - -void da_free_elements(Da *dap); - -void da_ints_print(Da *dap, char *sep); -bool da_integer_repeats(Da *dap); -int da_integer_sum(Da *dap); - - -void da_strings_print(Da *dap, char *sep); - -bool da_strings_exists(Da *string_arrp, char *test_string); -void da_strings_set_insert(Da *string_arrp, char *proffered_string, void destruct(void *)); -void da_strings_set_union(Da *string_arrp, Da *proffered_string_arrp, void destruct(void *)); - - -char *da_string_input(Da *dap, FILE *file); -void da_string_push(Da *dap0, char *string); - -void da_cat(Da *dap_base, Da *dap_cat); - -void da_present(Da **dar, int dar_size, void *closure); -bool da_equal(Da *da_el, Da *test_el); -void da_matrix_map(Da **dar, int dar_size, void f(void *,void*), void *closure); - -bool da_exists(Da *dap, bool f(void *, void*), void *closure); -bool da_all(Da *dap, bool f(void *, void*), void *closure); - -//matrix functions -void da_erase(Da *damp); -Da *da_push_row_alloc(Da *damp); -Da *da_push_row(Da *damp, Da *dap); -void da_push_column(Da *damp, Da *dap, void *fill); - -void da_every_row(Da *damp, void f(void *, void *), void *closure); -Da *da_longer(Da *dap0, Da *dap1); -Da *da_longest(Da *damp); -void da_every_column(Da *damp, void f(void *, void *), void *closure); - -Da *da_matrix_transpose(Da *damp, void *fill); - -bool da_length_equal(Da *dap0, Da *dap1); -bool da_all_rows_same_length(Da *damp); -bool da_integer_all_rows_repeat(Da *damp); -bool da_integer_all_columns_repeat(Da *damp); -bool da_integer_repeats_matrix(Da *damp); - -Da *da_integer_transpose(Da *damp, int *fill); -int *da_integer_to_raw_image_matrix(Da *damp, int fill); -int *da_integer_to_raw_image_transpose(Da *damp, int fill); - - -/* -Accounting for all the pointers for which we allocated memory on the heap. - -Replacement for malloc and free that allows us to check if all the memory we allocated was freed, and if all the memory we tried to free was actually allocated by keeping registers of pointers in Da structs. - */ - -#define MALLOC da_malloc_counted -#define FREE da_free_counted -#define ACCOUNT Da heap_acc; \ - Da extra_frees; \ - bool accounting = false; \ - da_start_accounting(heap_acc, extra_frees, accounting); - -#define BALANCE da_result_accounting() -#define CLOSE_ACC da_na_free(&heap_acc); da_na_free(&extra_frees); - -Da heap_acc; Da extra_frees; -void da_start_accounting(Da heap_acc, Da extra_frees, bool acc); -void *da_malloc_counted(size_t mem_size); -void da_free_counted(void *pt); -bool da_result_accounting(void); - -char *da_na_expand(Da *dap); -char *da_na_push_alloc(Da *dap); -char *da_na_push(Da *dap, void *element); -void da_na_free(Da *dap); - - -#endif - diff --git a/module/da/src/da.lib.h b/module/da/src/da.lib.h index c82c497..9043cda 100644 --- a/module/da/src/da.lib.h +++ b/module/da/src/da.lib.h @@ -92,15 +92,14 @@ Replacement for malloc and free that allows us to check if all the memory we all #define MALLOC da_malloc_counted #define FREE da_free_counted -#define ACCOUNT Da heap_acc; \ - Da extra_frees; \ - bool accounting = false; \ - da_start_accounting(heap_acc, extra_frees, accounting); +#define ACCOUNT da_start_accounting() #define BALANCE da_result_accounting() -#define CLOSE_ACC da_na_free(&heap_acc); da_na_free(&extra_frees); +#define CLOSE_ACC da_na_free(&heap_acc); da_na_free(&extra_frees) -Da heap_acc; Da extra_frees; -void da_start_accounting(Da heap_acc, Da extra_frees, bool acc); +//These variables must be declared at global scope in file that will use them. +extern Da heap_acc; extern Da extra_frees; extern bool accounting; + +void da_start_accounting(); void *da_malloc_counted(size_t mem_size); void da_free_counted(void *pt); bool da_result_accounting(void); diff --git a/module/da/src/da_na.lib.c b/module/da/src/da_na.lib.c index 4a3cb53..bc8f183 100644 --- a/module/da/src/da_na.lib.c +++ b/module/da/src/da_na.lib.c @@ -1,3 +1,9 @@ +/* +Accounting for all the pointers for which we allocated memory on the heap. + +Replacement for malloc and free that allows us to check if all the memory we allocated was freed, and if all the memory we tried to free was actually allocated by keeping registers of pointers in Da structs. + */ + #include "da.lib.h" #include @@ -7,8 +13,7 @@ //------------------------------------------------------------------ //FREE and MALLOC replacement -static bool accounting = false; -void da_start_accounting(Da heap_acc, Da extra_frees, bool acc){//assigns properties of heap_acc and extra_frees, must be called before other code to be used +void da_start_accounting(){//assigns properties of heap_acc and extra_frees and sets accounting to true, must be called before other code to be used heap_acc.element_size = sizeof(void *); heap_acc.size = 4*sizeof(void *); heap_acc.base = malloc(heap_acc.size); @@ -17,32 +22,34 @@ void da_start_accounting(Da heap_acc, Da extra_frees, bool acc){//assigns proper extra_frees.size = 4*sizeof(void *); extra_frees.base = malloc(extra_frees.size); extra_frees.end = extra_frees.base; - acc = true; + accounting = true; } -void *da_malloc_counted(size_t mem_size){//pushed pointer onto heap_acc +void *da_malloc_counted(size_t mem_size){//pushes pointer onto heap_acc before mallocing void *temp = malloc(mem_size); if( accounting == true ) da_na_push(&heap_acc, &temp); return (void *)temp; } void da_free_counted(void *pt){ - if( accounting == true ) { - void *i = heap_acc.base; - bool present = false; - while( i < (void *)heap_acc.end ){ - if( pt == *(void **)i ){//matches and zeroes out pointer from heap_acc - *(int *)i = 0; - present = true; - if( (i + heap_acc.element_size) == heap_acc.end )//pops excess 0s from end + if( accounting == true ) { + void *i = heap_acc.base; + bool present = false; + while( i < (void *)heap_acc.end ){ + if( pt == *(void **)i ){//matches and zeroes out pointer from heap_acc + *(int *)i = 0; + present = true; + if( (i + heap_acc.element_size) == heap_acc.end ){//pops excess 0s from end of heap_acc + void *j = i; + while( (*(int *)j == 0) && ((void *)j >= (void *)heap_acc.base) ){ da_pop(&heap_acc, NULL); - } - i++; - } - if( present == false ) da_push(&extra_frees, &pt);//stores pointer in extra_frees if tried to free one that we didn't count + j-=heap_acc.element_size; + }}} + i++; } - free(pt); + if( present == false ) da_push(&extra_frees, &pt);//stores pointer in extra_frees if tried to free one that we didn't count + } + free(pt);//frees pointer } -//returns false if accounting wasn't being used or heap_acc is not empty or if we tried to free more pointers than we malloced for -bool da_result_accounting(){ +bool da_result_accounting(){//returns false if accounting wasn't being used or heap_acc is not empty or if we tried to free more pointers than we malloced for if ( accounting == true ){ bool flag1 = da_empty(&heap_acc); bool flag2 = da_empty(&extra_frees); @@ -50,6 +57,8 @@ bool da_result_accounting(){ } else return false; } +//------------------------------------------------------------------------- + //these are redefined with malloc instead of MALLOC because da_malloc_counted will not make it past the push once heap_acc needs to expand char *da_na_expand(Da *dap){ char *old_base = dap->base; diff --git a/module/da/src/da_na.lib.h b/module/da/src/da_na.lib.h deleted file mode 100644 index 426c2fe..0000000 --- a/module/da/src/da_na.lib.h +++ /dev/null @@ -1,30 +0,0 @@ -/* -Accounting for all the pointers for which we allocated memory on the heap. - -Replacement for malloc and free that allows us to check if all the memory we allocated was freed, and if all the memory we tried to free was actually allocated by keeping registers of pointers in Da structs. - */ - -#ifndef DA_NA_LIB_H -#define DA_NA_LIB_H - -#define MALLOC da_malloc_counted -#define FREE da_free_counted -#define ACCOUNT Da heap_acc; \ - Da extra_frees; \ - bool accounting = false; \ - da_start_accounting(heap_acc, extra_frees, accounting); -#define BALANCE da_result_accounting() -#define CLOSE_ACC da_na_free(&heap_acc); da_na_free(&extra_frees); - -Da heap_acc; Da extra_frees; -void da_start_accounting(Da heap_acc, Da extra_frees, bool acc); -void *da_malloc_counted(size_t mem_size); -void da_free_counted(void *pt); -bool da_result_accounting(void); - -char *da_na_expand(Da *dap); -char *da_na_push_alloc(Da *dap); -char *da_na_push(Da *dap, void *element); -void da_na_free(Da *dap); - -#endif diff --git a/module/da/test/results/results_2019-05-03T18:06:21Z_passed b/module/da/test/results/results_2019-05-03T18:06:21Z_passed new file mode 100644 index 0000000..20c6e8c --- /dev/null +++ b/module/da/test/results/results_2019-05-03T18:06:21Z_passed @@ -0,0 +1,82 @@ +Hello Emacs + +2019-05-03T18:06:21Z +glendawest045@phoenix§~/subu/module/da§ +> make all +/usr/bin/make --no-print-directory -f /home/glendawest045/subu/tool/lib/makefile-cc version +makefile version 6.0 +gcc -v +Using built-in specs. +COLLECT_GCC=gcc +COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/8/lto-wrapper +OFFLOAD_TARGET_NAMES=nvptx-none +OFFLOAD_TARGET_DEFAULT=1 +Target: x86_64-linux-gnu +Configured with: ../src/configure -v --with-pkgversion='Ubuntu 8.3.0-6ubuntu1' --with-bugurl=file:///usr/share/doc/gcc-8/README.Bugs --enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++ --prefix=/usr --with-gcc-major-version-only --program-suffix=-8 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --enable-bootstrap --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-libmpx --enable-plugin --enable-default-pie --with-system-zlib --with-target-system-zlib --enable-objc-gc=auto --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-offload-targets=nvptx-none --without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu +Thread model: posix +gcc version 8.3.0 (Ubuntu 8.3.0-6ubuntu1) +g++ -v +Using built-in specs. +COLLECT_GCC=g++ +COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/8/lto-wrapper +OFFLOAD_TARGET_NAMES=nvptx-none +OFFLOAD_TARGET_DEFAULT=1 +Target: x86_64-linux-gnu +Configured with: ../src/configure -v --with-pkgversion='Ubuntu 8.3.0-6ubuntu1' --with-bugurl=file:///usr/share/doc/gcc-8/README.Bugs --enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++ --prefix=/usr --with-gcc-major-version-only --program-suffix=-8 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --enable-bootstrap --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-libmpx --enable-plugin --enable-default-pie --with-system-zlib --with-target-system-zlib --enable-objc-gc=auto --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-offload-targets=nvptx-none --without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu +Thread model: posix +gcc version 8.3.0 (Ubuntu 8.3.0-6ubuntu1) +make -v +GNU Make 4.2.1 +Built for x86_64-pc-linux-gnu +Copyright (C) 1988-2016 Free Software Foundation, Inc. +License GPLv3+: GNU GPL version 3 or later +This is free software: you are free to change and redistribute it. +There is NO WARRANTY, to the extent permitted by law. + +2019-05-03T18:06:24Z +glendawest045@phoenix§~/subu/module/da§ +> 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 +cp src/da.lib.h include/da.h +/usr/bin/make --no-print-directory -f /home/glendawest045/subu/tool/lib/makefile-cc lib +make[1]: Nothing to be done for 'lib'. +/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 +make[3]: Nothing to be done for 'sub_exec'. + +2019-05-03T18:06:45Z +glendawest045@phoenix§~/subu/module/da§ +> cd test/ + +2019-05-03T18:06:49Z +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-05-03T18:06:55Z +glendawest045@phoenix§~/subu/module/da/test§ +> cd exec/ + +2019-05-03T18:06:59Z +glendawest045@phoenix§~/subu/module/da/test/exec§ +> ./test_da +passed all 26 tests + +2019-05-03T18:07:01Z +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 515ba55..696d43e 100644 --- a/module/da/test/src/test_da.cli.c +++ b/module/da/test/src/test_da.cli.c @@ -4,8 +4,12 @@ #include "test_da.lib.h" #include +Da heap_acc; +Da extra_frees; +bool accounting = false; int main(){ + ACCOUNT; // enumeration of tests typedef bool (*test_fun)(); test_fun tests[] = @@ -66,9 +70,7 @@ int main(){ "test_da_longest_0", "da_result_accounting", NULL}; - // call tests - ACCOUNT bool da_0_passed = true; unsigned int passed = 0; unsigned int failed = 0; @@ -97,7 +99,7 @@ int main(){ else printf("failed %u of %u tests\n", failed, passed + failed); - + CLOSE_ACC; if( passed == 0 || failed != 0 ) return 1; return 0; } diff --git a/module/share/include/da.h b/module/share/include/da.h index 8195c4a..9043cda 100644 --- a/module/share/include/da.h +++ b/module/share/include/da.h @@ -4,13 +4,15 @@ #include #include -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; size_t element_size; } Da; +//#include "da_na.lib.h" + #define RETURN(dap, r) \ { da_free_elements(dap); return r; } @@ -18,7 +20,7 @@ typedef struct Da{ void da_alloc(Da *dap, size_t element_size); void da_free(Da *dap); void da_rewind(Da *dap); -bool da_emptyq(Da *dap); +bool da_empty(Da *dap); size_t da_length(Da *dap); void da_rebase(Da *dap, char *old_base, void *pta); char *da_expand(Da *dap); @@ -31,12 +33,16 @@ bool da_pop(Da *dap, void *element); bool da_endq(Da *dap, void *pt); void da_map(Da *dap, void f(void *, void *), void *closure); - + void da_free_elements(Da *dap); void da_ints_print(Da *dap, char *sep); +bool da_integer_repeats(Da *dap); +int da_integer_sum(Da *dap); + void da_strings_print(Da *dap, char *sep); + bool da_strings_exists(Da *string_arrp, char *test_string); void da_strings_set_insert(Da *string_arrp, char *proffered_string, void destruct(void *)); void da_strings_set_union(Da *string_arrp, Da *proffered_string_arrp, void destruct(void *)); @@ -47,5 +53,62 @@ void da_string_push(Da *dap0, char *string); void da_cat(Da *dap_base, Da *dap_cat); +void da_present(Da **dar, int dar_size, void *closure); +bool da_equal(Da *da_el, Da *test_el); +void da_matrix_map(Da **dar, int dar_size, void f(void *,void*), void *closure); + +bool da_exists(Da *dap, bool f(void *, void*), void *closure); +bool da_all(Da *dap, bool f(void *, void*), void *closure); + +//matrix functions +void da_erase(Da *damp); +Da *da_push_row_alloc(Da *damp); +Da *da_push_row(Da *damp, Da *dap); +void da_push_column(Da *damp, Da *dap, void *fill); + +void da_every_row(Da *damp, void f(void *, void *), void *closure); +Da *da_longer(Da *dap0, Da *dap1); +Da *da_longest(Da *damp); +void da_every_column(Da *damp, void f(void *, void *), void *closure); + +Da *da_matrix_transpose(Da *damp, void *fill); + +bool da_length_equal(Da *dap0, Da *dap1); +bool da_all_rows_same_length(Da *damp); +bool da_integer_all_rows_repeat(Da *damp); +bool da_integer_all_columns_repeat(Da *damp); +bool da_integer_repeats_matrix(Da *damp); + +Da *da_integer_transpose(Da *damp, int *fill); +int *da_integer_to_raw_image_matrix(Da *damp, int fill); +int *da_integer_to_raw_image_transpose(Da *damp, int fill); + + +/* +Accounting for all the pointers for which we allocated memory on the heap. + +Replacement for malloc and free that allows us to check if all the memory we allocated was freed, and if all the memory we tried to free was actually allocated by keeping registers of pointers in Da structs. + */ + +#define MALLOC da_malloc_counted +#define FREE da_free_counted +#define ACCOUNT da_start_accounting() +#define BALANCE da_result_accounting() +#define CLOSE_ACC da_na_free(&heap_acc); da_na_free(&extra_frees) + +//These variables must be declared at global scope in file that will use them. +extern Da heap_acc; extern Da extra_frees; extern bool accounting; + +void da_start_accounting(); +void *da_malloc_counted(size_t mem_size); +void da_free_counted(void *pt); +bool da_result_accounting(void); + +char *da_na_expand(Da *dap); +char *da_na_push_alloc(Da *dap); +char *da_na_push(Da *dap, void *element); +void da_na_free(Da *dap); + + #endif diff --git a/module/share/lib b/module/share/lib new file mode 100644 index 0000000..eb95acc Binary files /dev/null and b/module/share/lib differ