From 033d915ea4d3dfb770a773e00bcb962df924750d Mon Sep 17 00:00:00 2001 From: Thomas Walker Lynch Date: Thu, 9 May 2019 22:04:49 +0200 Subject: [PATCH] separated Gs implenation of acc out of da.lib.{ch} --- .../acc_doc.txt => doc/acc_usermanual.txt} | 0 module/da/include/acc.h | 30 ++ module/da/include/da.h | 73 ++++ module/da/lib/libda.a | Bin 0 -> 37848 bytes module/da/makefile | 1 - module/da/src/acc.lib.c | 187 ++++++---- module/da/src/acc.lib.h | 36 +- module/da/src/da.lib.c | 320 +++--------------- module/da/src/da.lib.h | 45 +-- module/da/src/da_mat.lib.c_nocompile | 262 ++++++++++++++ module/da/src/da_mat.lib.h_nocompile | 25 ++ module/da/src/da_na.lib.c | 74 ---- module/da/src/da_na.lib.h | 27 -- module/da/src/struct_forward_example.c | 27 ++ module/da/src/temp.c | 35 -- module/da/src/update_Acc_channel.sed | 1 + .../src/{temp.sed => update_da_lib_names.sed} | 0 module/da/src/update_project_da_lib_names.sed | 2 + module/da/src1/da.lib.c | 306 ----------------- module/da/src1/da.lib.h | 88 ----- module/da/src1/temp.sed | 1 - 21 files changed, 621 insertions(+), 919 deletions(-) rename module/da/{src1/acc_doc.txt => doc/acc_usermanual.txt} (100%) create mode 100644 module/da/include/acc.h create mode 100644 module/da/include/da.h create mode 100644 module/da/lib/libda.a create mode 100644 module/da/src/da_mat.lib.c_nocompile create mode 100644 module/da/src/da_mat.lib.h_nocompile delete mode 100644 module/da/src/da_na.lib.c delete mode 100644 module/da/src/da_na.lib.h create mode 100644 module/da/src/struct_forward_example.c delete mode 100644 module/da/src/temp.c create mode 100644 module/da/src/update_Acc_channel.sed rename module/da/src/{temp.sed => update_da_lib_names.sed} (100%) create mode 100644 module/da/src/update_project_da_lib_names.sed delete mode 100644 module/da/src1/da.lib.c delete mode 100644 module/da/src1/da.lib.h delete mode 100644 module/da/src1/temp.sed diff --git a/module/da/src1/acc_doc.txt b/module/da/doc/acc_usermanual.txt similarity index 100% rename from module/da/src1/acc_doc.txt rename to module/da/doc/acc_usermanual.txt diff --git a/module/da/include/acc.h b/module/da/include/acc.h new file mode 100644 index 0000000..867cee9 --- /dev/null +++ b/module/da/include/acc.h @@ -0,0 +1,30 @@ +#ifndef ACC_LIB_H +#define ACC_LIB_H + +typedef struct AccChannel_struct AccChannel; +typedef struct Da_struct Da; // Da_struct defined in da.lib.h + +enum Mode_enum{acc_NULL, acc_BALANCE, acc_FULL, acc_SELF};//0,1,2,3 +typedef enum Mode_enum Mode; + +struct AccChannel_struct{ + Da *outstanding_malloc; + Da *spurious_free; + Mode mode; +}; //name instances of channels with handles + + +extern AccChannel acc_live_channels;//acc_NULL or acc_SELF to track acc channels or not, other options return invalid upon report + +//function declarations for accounting + AccChannel *acc_open(AccChannel *channel, Mode mode);//initializes channel structs + void *acc_malloc(size_t size, AccChannel *channel);//works for things besides Das too + void acc_free(void *pt, AccChannel *channel);//works for things besides Das too + AccChannel *acc_report(AccChannel *channel);//reports on channels based on mode + void acc_close(AccChannel *channel);//frees channel itself + + void *crash_and_burn_malloc(size_t size);//sends error message in case of accidental regular malloc + void crash_and_burn_free(void *);// sends error message in case of accidental regular free + + +#endif diff --git a/module/da/include/da.h b/module/da/include/da.h new file mode 100644 index 0000000..e9cc718 --- /dev/null +++ b/module/da/include/da.h @@ -0,0 +1,73 @@ +#ifndef DA_LIB_H +#define DA_LIB_H +#include +#include +#include +#include + +typedef struct AccChannel_struct AccChannel; // AccChannel_struct defined in acc.lib.h +typedef struct Da_struct Da; + +struct Da_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; + AccChannel *channel; +}; + +// constructors / destructors +// + Da *da_init(Da *dap, size_t element_size, AccChannel *channel);//calls da_malloc for base pointer + void da_free(Da *dap); + void da_rewind(Da *dap); + void da_rebase(Da *dap, char *old_base, void *pta); + char *da_expand(Da *dap); + bool da_boundq(Da *dap); + void da_erase(Da *dap); + +// status / attributes +// + bool da_is_empty(Da *dap); + bool da_equal(Da *da_0, Da *da_1); + size_t da_length(Da *dap); + bool da_length_equal(Da *dap0, Da *dap1); + +// accessing +// + char *da_index(Da *dap, size_t i); + char *da_push(Da *dap, void *element); + bool da_pop(Da *dap, void *element); + +// iteration, f is given a pointer to an element and a pointer to the closure + bool da_endq(Da *dap, void *pt); + bool da_right_bound(Da *dap, void *pt); + void da_foreach(Da *dap, void f(void *, void *), void *closure); //used to be da_map + bool da_exists(Da *dap, bool f(void *, void*), void *closure); + bool da_all(Da *dap, bool f(void *, void*), void *closure); + +// elements are pointers +// would be better if exists returned NULL or a pointer to the existing element- + void *da_pts_exists(Da *dap, void *test_element); + void da_pts_free_all(Da *dap); // calls free on all elements + void da_pts_nullify(Da *dap, void **ept); // sets *ept to NULL, pops all NULLs from top of stack + +// elements are integers + void da_ints_print(Da *dap, char *sep); + bool da_integer_repeats(Da *dap); + int da_integer_sum(Da *dap); + +// the array itself is a string +// careful if you add a null terminator it will become part of the da_string, affects iteration etc. + 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); + +// when the array holds pointers to C strings + 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 *)); + +#endif + diff --git a/module/da/lib/libda.a b/module/da/lib/libda.a new file mode 100644 index 0000000000000000000000000000000000000000..b53b22e0e32d34dc4c065ce131f0843de29abfaa GIT binary patch literal 37848 zcmeHw3w&Hvwf8xh?8)SvG<~FLTBc9>%%kr&DKusJf>Nlq(2`7&$piBuGgDdwiV1KV zW3>U)g82|XMM13>A0Sf1Se1uY1z(qnsFe#|u;8s0FS%0aU2E;N&g|Kp0blp?`+mP~ z|B^Xp|MzKfubox)?G#NHIHYwK)_IdGJGPvn}m&L%{9A`M}X24|5y-5?Fm8oJ`0 zO;_VtTeL3H(H+}M1>I$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EnNI?r-irjd~%N^U)ERu z#iAGa&sw<1|E+~DWWW8rYwutvpZtvn5~F^M7j5i~JWXT&|a|S@>_bzGp1?^8RZ&KfsHBUSs-2 z7JeK1p@wnso4k*RTJ+USKWO2<;C?t{$&vRRU$*GwKH;c^|B>g(A6fXjS^h69`SPA6 zGhg=?sjs|aE@WKlCGT-&SoF8B{6!W%z<8~Nujc-_#gZ@YkFK%k&v3nZEqZwmb)Q8) zQ&-3NibXHuy%ZmmTx z_H+gz_#Eqbn&Q;3reB=Li+KTNW4 zS%2W+P4qkjiC^B)h<^x#4|yK7@gNgmGjUT9Qe)v#uX+m?|J-Qd(k^#fxb&Z43zvR) z+`^?l!z(6AVyBFwpoPnLtg&#J2kI?c=9wEUT;{R6EnMc$VGEae`?!V6{4dTG`{g-1pvX;d1{xY~ga>e%!+4d4XPluEQ{MEpVGO^#7N%V`oQfBL44FxuawDb7Bqk^vo>G<(o zPb&Y_^TOcL63vPJ!)u7xgYWtoX=MwtGj{Q)8=QjUuXKRjm!pKMAlj9)Za&D52n`7n@=Qi!9ncr$=rvER z^%v+9aFWV@$Ju}NJh0Gzc&{S-;#aiC)(wzze da_init -//da_map -> da_foreach +#include +#include +#include +#include #include "da.lib.h" -#include "da_na.lib.h" #include "acc.lib.h" -Da *acc_live_checkers = NULL; +//----------------------------------------------------------------- +//function definitions for accounting -void acc_init(){ //open - if( acc_live_checkers == NULL ){ - acc_live_checkers = malloc(sizeof(Da)); - da_na_init(acc_live_checkers, sizeof(AccChecker)); +AccChannel *acc_open(AccChannel *channel, Mode mode){//acc init + if( channel == &acc_live_channels ) {//avoid pushing channel tracker onto itself + Da os; Da sf; + channel->outstanding_malloc = da_init(&os, sizeof(void *), NULL); + channel->spurious_free = da_init(&sf, sizeof(void *), NULL); + channel->mode = mode; + return channel; } - AccChecker *acp = malloc(sizeof(AccChecker)); - da_na_alloc(&(acp->outstanding_malloc), sizeof(void *)); - da_na_alloc(&(acp->spurious_free), sizeof(void *)); - da_na_push(acc_live_checkers, acp); -} -void acc_free(){//close - -} -void acc_report(){ -} - -void *acc_malloc(size_t mem_size){ - void *an_allocation_pt = malloc(mem_size); - - // push an_allocation_pt on to all live checkers - AccChecker *acp = acc_live_checkers->base; - while(!da_endq(acc_live_checkers, acp)){ - if(*acp) da_na_push(&(acp->outstanding_malloc), an_allocation_pt); - acp++; + else if( acc_live_channels.mode == acc_NULL ){//accounting NULL + //channel = (AccChannel *)acc_malloc(sizeof(AccChannel), NULL);//accounting channel still on the heap but not tracked in SELF mode + Da os; Da sf; + channel->outstanding_malloc = da_init(&os, sizeof(void *), NULL); + channel->spurious_free = da_init(&sf, sizeof(void *), NULL); + channel->mode = mode; + return channel; + } + else if( acc_live_channels.mode == acc_SELF ){//accounting tracks itself + channel = (AccChannel *)acc_malloc(sizeof(AccChannel), &acc_live_channels); + Da os; Da sf; + channel->outstanding_malloc = da_init(&os, sizeof(void *), NULL); + channel->spurious_free = da_init(&sf, sizeof(void *), NULL); + channel->mode = mode; + return channel; + } + else{ //cerr, optional acc_live_channels only tracks channels, not other mallocs/frees + return channel; } - - return an_allocation_pt; } - -void *acc_malloc(size_t mem_size){//pushes pointer onto heap_acc before mallocing - void *temp = malloc(mem_size); - da_na_push(&heap_acc, &temp); - return (void *)temp; +void *acc_malloc(size_t size, AccChannel *channel){ + void *an_allocation_pt = malloc(size); + if( channel ) da_push((Da *)(channel->outstanding_malloc), &an_allocation_pt); + return (void *)an_allocation_pt; } -void acc_free(void *pt){ - if( accounting == true ) { - void *i = heap_acc.base; +void acc_free(void *pt, AccChannel *channel){ + if( channel ){ + void **i = (void **)(((Da *)(channel->outstanding_malloc))->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; + while( i < (void **)(((Da *)(channel->outstanding_malloc))->end) ){ + if( *i == pt ){ + da_pts_nullify((Da *)(channel->outstanding_malloc), i); 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); - j-=heap_acc.element_size; - }}} + } i++; } - if( present == false ) da_push(&extra_frees, &pt);//stores pointer in extra_frees if tried to free one that we didn't count + if( present == false ) da_push((Da *)(channel->spurious_free), &pt); + } + free(pt); +} +static void count_balance(void *element, void *closure){ + int *counter = (int *)closure; + if( (void *)element ) (*counter)++; +} +static void acc_rep_helper_BALANCE(AccChannel *channel){ + int count = 0; + da_foreach((Da *)channel->outstanding_malloc, count_balance, &count); + printf("There are %d outstanding allocations.\n", count); + count = 0; + da_foreach((Da *)channel->spurious_free, count_balance, &count); + printf("There are %d spurious frees.\n", count); +} +static void print_pointer(void *element, void *closure){ + if( element ) printf("%d ", *(int *)element); +} +static void acc_rep_helper_FULL(AccChannel *channel){ + int count = 0; + da_foreach((Da *)channel->outstanding_malloc, count_balance, &count); + printf("There are %d outstanding mallocs.\n", count); + if( count < 10 ){ + printf("The outstanding allocated pointers are: "); + da_foreach((Da *)channel->outstanding_malloc, print_pointer, NULL); + printf(".\n"); + } + count = 0; + da_foreach((Da *)channel->spurious_free, count_balance, &count); + printf("There are %d spurious frees.\n", count); + if( count < 10 ){ + printf("The spuriously freed pointers are: "); + da_foreach((Da *)channel->outstanding_malloc, print_pointer, NULL); + printf(".\n"); + } +} +AccChannel *acc_report(AccChannel *channel){ + if( channel->mode == acc_NULL ){ + printf("Accounting mode is NULL."); + return channel; + } + if( channel->mode == acc_BALANCE ){ + printf("Accounting mode is BALANCE.\n"); + if( da_is_empty((Da *)(channel->outstanding_malloc)) && da_is_empty((Da *)(channel->spurious_free)) ){ + printf("This channel is in balance."); + } + else{ + printf("This channel is out of balance.\n"); + acc_rep_helper_BALANCE(channel); + } + return channel; + } + if( channel->mode == acc_FULL ){ + printf("Accounting mode is FULL.\n"); + if( da_is_empty((Da *)(channel->outstanding_malloc)) && da_is_empty((Da *)(channel->spurious_free)) ){ + printf("This channel is in balance."); + } + else{ + printf("This channel is out of balance.\n"); + acc_rep_helper_FULL(channel); + } + return channel; + } + if( channel->mode == acc_SELF ){ + printf("Accounting mode is SELF.\n"); + if( da_is_empty((Da *)(channel->outstanding_malloc)) && da_is_empty((Da *)(channel->spurious_free)) ){ + printf("There are no open channels."); + } + else { + printf("The accounting code is out of balance.\n"); + acc_rep_helper_FULL(channel); + } + return channel; } - free(pt);//frees pointer } -bool acc_result(){//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); - return flag1 && flag2; - } else return false; +void acc_close(AccChannel *channel){ + da_free((Da *)(channel->outstanding_malloc)); + da_free((Da *)(channel->spurious_free)); + if( (channel != &acc_live_channels) + && (acc_live_channels.mode == acc_SELF) ){ + acc_free(channel, &acc_live_channels); + return; + } + else return; } +void *crash_and_burn_malloc(size_t size){} +void crash_and_burn_free(void *pt){} + diff --git a/module/da/src/acc.lib.h b/module/da/src/acc.lib.h index 353f0ac..867cee9 100644 --- a/module/da/src/acc.lib.h +++ b/module/da/src/acc.lib.h @@ -1,18 +1,30 @@ #ifndef ACC_LIB_H #define ACC_LIB_H -typedef struct{ - Da outstanding_malloc; - Da spurious_free; -} AccChecker; - -extern Da *acc_live_checkers; - -#define MALLOC acc_malloc -#define FREE acc_free -#define ACCOUNT acc_start() -#define BALANCE acc_result() -#define CLOSE_ACC da_na_free(&heap_acc); da_na_free(&extra_frees) +typedef struct AccChannel_struct AccChannel; +typedef struct Da_struct Da; // Da_struct defined in da.lib.h + +enum Mode_enum{acc_NULL, acc_BALANCE, acc_FULL, acc_SELF};//0,1,2,3 +typedef enum Mode_enum Mode; + +struct AccChannel_struct{ + Da *outstanding_malloc; + Da *spurious_free; + Mode mode; +}; //name instances of channels with handles + + +extern AccChannel acc_live_channels;//acc_NULL or acc_SELF to track acc channels or not, other options return invalid upon report + +//function declarations for accounting + AccChannel *acc_open(AccChannel *channel, Mode mode);//initializes channel structs + void *acc_malloc(size_t size, AccChannel *channel);//works for things besides Das too + void acc_free(void *pt, AccChannel *channel);//works for things besides Das too + AccChannel *acc_report(AccChannel *channel);//reports on channels based on mode + void acc_close(AccChannel *channel);//frees channel itself + + void *crash_and_burn_malloc(size_t size);//sends error message in case of accidental regular malloc + void crash_and_burn_free(void *);// sends error message in case of accidental regular free #endif diff --git a/module/da/src/da.lib.c b/module/da/src/da.lib.c index fa403dd..f043357 100644 --- a/module/da/src/da.lib.c +++ b/module/da/src/da.lib.c @@ -8,27 +8,28 @@ Cannot expand an empty array. #include #include -#include "da.lib.h" #include "acc.lib.h" +#include "da.lib.h" //-------------------------------------------------------------------------------- // constructors / destructors -// this really should be called da_init, as it doesn't do any allocation ... -void da_alloc(Da *dap, size_t element_size){ +Da *da_init(Da *dap, size_t element_size, AccChannel *channel){ dap->element_size = element_size; dap->size = 4 * element_size; - dap->base = MALLOC(dap->size); + dap->base = acc_malloc(dap->size, channel); dap->end = dap->base; + dap->channel = channel; + return dap; } void da_free(Da *dap){ - FREE(dap->base); + acc_free(dap->base, dap->channel); dap->size = 0; + dap->channel = NULL; } void da_rewind(Da *dap){ dap->end = dap->base; } - void da_rebase(Da *dap, char *old_base, void *pta){ char **pt = (char **)pta; size_t offset = *pt - old_base; @@ -41,9 +42,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 = acc_malloc(new_size, (AccChannel *)(dap->channel)); memcpy( new_base, old_base, end_offset + dap->element_size); - FREE(old_base); + acc_free(old_base, (AccChannel *)(dap->channel)); dap->base = new_base; dap->end = new_base + end_offset; dap->size = new_size; @@ -55,15 +56,18 @@ bool da_boundq(Da *dap){ return dap->end > (dap->base + dap->size); } -void da_erase(Da *dap){//same as da_free, don't tell anyone - FREE(dap->base); - dap->size = 0; +// erases all the elements in the array +// a push followed by an erase should work, but I don't think it does right now +/* +void da_erase(Da *dap){ + da_free(Da *dap); } +*/ //-------------------------------------------------------------------------------- // status / attributes -bool da_empty(Da *dap){ +bool da_is_empty(Da *dap){ return dap->end == dap->base; } @@ -127,7 +131,7 @@ bool da_right_bound(Da *dap, void *pt){ // passed in f(element_pt, arg_pt) // We have no language support closures, so we pass in an argument for it. // The closure may be set to NULL if it is not needed. -void da_map(Da *dap, void f(void *, void *), void *closure){ +void da_foreach(Da *dap, void f(void *, void *), void *closure){ char *pt = dap->base; while( pt != dap->end ){ f(pt, closure); @@ -141,7 +145,7 @@ static bool da_quantifier(bool complement, Da *dap, bool pred(void *, void*), vo char *pt = dap->base; bool result = false; while( (complement? !result : result) && (pt != dap->end) ){ - result = f(pt, closure); + result = pred(pt, closure); pt += dap->element_size; } return result; @@ -160,18 +164,27 @@ bool da_all(Da *dap, bool pred(void *, void*), void *closure){ //-------------------------------------------------------------------------------- // elements are pointers - +// elements are pointers +// +static bool da_pts_exists_0(void *element, void *test_element){ return element == test_element; } +void *da_pts_exists(Da *dap, void *test_element){ + if( da_exists(dap, da_pts_exists_0, test_element) ) return test_element; + else return NULL; +} +/* static da_pts_exists_0(void *element, void *pt){ return element == pt; } bool da_pts_exists(Da *dap, void *test_element){ return da_exists(dap, da_pts_exists_0, test_element); } - -// elements were MALLOCed, now they will all be FREEd +*/ +// elements were allocated, now they will all be freed static void da_pts_free_all_0(void *pt, void *closure){ - FREE(*(char **)pt); // FREE does not care about the pointer type + acc_free(*(char **)pt, closure); + // FREE(*(char **)pt); // FREE does not care about the pointer type } void da_pts_free_all(Da *dap){ - da_map(dap, da_pts_free_all_0, NULL); + da_foreach(dap, da_pts_free_all_0, dap->channel); + // da_map(dap, da_pts_free_all_0, NULL); da_rewind(dap); } @@ -179,14 +192,16 @@ void da_pts_free_all(Da *dap){ // ept points at an element, we set *ept to NULL // we pop all NULLs off the top of the stack void da_pts_nullify(Da *dap, void **ept){ - if(ept >= dap->base && ept < dap->end){ + if(ept >= (void **)(dap->base) && ept < (void **)(dap->end)){ + // if(ept >= dap->base && ept < dap->end){ *ept = NULL; } while( dap->end > dap->base && - *(void **)(dap->end - dap->element_size) == NULL - ){ + // *(void **)(dap->end - dap->element_size) == NULL + // ){ + *(void **)(dap->end - dap->element_size) == NULL){ da_pop(dap, NULL); } } @@ -265,7 +280,7 @@ bool da_strings_exists(Da *string_arrp, char *test_string){ da_strings_exists_closure sec; sec.string = test_string; sec.found = false; - da_map(string_arrp, string_equal, &sec); + da_foreach(string_arrp, string_equal, &sec); return sec.found; } @@ -335,262 +350,3 @@ void da_cat(Da *dap0, Da *dap1){ } -//----------------------------------------------------- - - -//------------------------------------------------------- - -// all things Da matrix -// a DaMa (Doubling array Matrix) is a Da whose elements are Da's -// forms a matrix if you treat what's pointed to by base pointers of the elements of the DaMa as the first column of elements and fill in each row with the contents of the Das -// The "row major" nomenclature is used to remain consistent with database logic. -/* Example: -Da dar0; Da *dap0 = &dar0; da_alloc(dap0, sizeof(int)); -Da dar1; Da *dap1 = &dar1; da_alloc(dap1, sizeof(int)); -Da dama; Da *damp = &dama; da_alloc(damp, sizeof(Da)); -da_push(damp, dap0); -da_push(damp, dap1); -*/ - -Da *da_mat_push_row_alloc(Da *damp){ - size_t row_off = (Da *)(damp->end) - (Da *)(damp->base); - damp->end += damp->element_size; - if( damp->end > damp->base + damp->size ) da_expand(damp); - return (Da *)(damp->base) + row_off; -} -Da *da_mat_push_row(Da *damp, Da *dap){// Dama won't track changes to Das after pushing onto rows - Da *row_pt = da_mat_push_row_alloc(damp); - memcpy(row_pt, dap, damp->element_size); - return row_pt; -} - -void da_mat_push_column(Da *damp, Da *dap, void *fill){ - Da *tran = da_mat_transpose(damp, fill); - da_mat_push_row(tran, dap); - Da *new_dama = da_mat_transpose(tran, fill); - //add protection against memory overwrite - expand if necessary - memcpy(damp, new_dama, new_dama->size); -} - -void da_mat_every_row(Da *damp, void f(void *, void *), void *closure){//like every but for rows instead of elements - Da *dpt = (Da *)(damp->base); - while( dpt != (Da *)damp->end ){ - f(dpt, closure); - dpt++; - } -} - -// da_mat_every_column uses da_mat_longest and therefore da_mat_longer, written for the purpose of terminating the while loop in the appropriate place -// will return dap1 if equal, cannot determine equality -Da *da_mat_longer(Da *dap0, Da *dap1){ - if (da_length(dap0) > da_length(dap1)) return dap0; - else return dap1; -} -// returns Da in DaMa with longest length -Da *da_mat_longest(Da *damp){ - Da *dap = (Da *)(damp->base); - Da *longest = (Da *)((damp->base) + sizeof(Da)); - while( dap < (Da *)(damp->end) ){ - longest = da_mat_longer(dap,longest); - dap++; - } - return longest; -} -void da_mat_every_column(Da *damp, void f(void *, void *), void *closure){//like every but for columns instead of elements - Da *dpt = (Da *)(damp->base); - size_t rows = damp->size/damp->element_size; - size_t columns = da_length(da_mat_longest(damp)); - size_t j = 0; - while( j < columns ){ - int *col = MALLOC(sizeof(rows*sizeof(int))); - size_t i = 0; - while( i < rows ) { - if (da_endq(dpt,(dpt->base + j*(dpt->element_size)))) - *(col+i) = 0; - else *(col+i) = *(dpt->base + j*(dpt->element_size)); - dpt++; - i++; - } - f(col, closure); - j++; - } -} - -Da *da_mat_transpose(Da *damp, void *fill){// all Das must have same element type, will sort to integer, char, or char * transpose function - Da *dap = (Da *)(damp->base); - Da tran; da_alloc(&tran, sizeof(damp->element_size)); - Da *transpose = &tran; - if( dap->element_size == sizeof(int) ){ - int *filler = (int *)fill; - transpose = da_mat_ints_transpose(damp, filler); - } - /*else if( dap->element_size == sizeof(char) ){ - char *filler = (char *)fill; - transpose = da_character_transpose(damp, filler); - } - else if( dap->element_size == sizeof(char *) ){ - char **filler = (char **)fill; - transpose = da_c_string_transpose(damp, filler); - }*/ - //else error? - return transpose; -} - - -//-------------------------------------------------------------------- -// DaMa is a matrix of integers (stored in Das as columns) - -// integer repeats across columns -bool da_mat_all_rows_same_length(Da *damp){ - Da *dap = (Da *)(damp->base); - Da *pt = dap; - bool flag = true; - while( flag && pt != (Da *)(damp->end) ){ - flag = da_length_equal(dap, pt); - } - return flag; -} -bool da_mat_ints_all_rows_repeat(Da *damp){// if rows are made of repeating integers, then all columns read the same thing - Da *dpt = (Da *)(damp->base); - bool flag = false; - if( da_mat_all_rows_same_length((Da *)damp) ){// columns can't be equal if rows not all same length, will return false - flag = true; - while( flag && dpt != (Da *)(damp->end) ){ - flag = da_integer_repeats(dpt); // in "da is array of integers" section - dpt++; - } - return flag; - } - else return flag; -} -bool da_mat_ints_all_columns_repeat(Da *damp){// rows are repeating in transpose = columns are repeating - int x = 0; //have to pass in fill for transpose, this nullifies effect same_length test - Da *test_da = da_mat_transpose(damp, &x); - return da_mat_ints_all_rows_repeat(test_da); -} -bool da_mat_ints_repeats_matrix(Da *damp){// all elements in matrix are same - bool flag1 = da_mat_ints_all_rows_repeat(damp); - bool flag2 = da_mat_ints_all_columns_repeat(damp); - return flag1 && flag2; -} - -// to transpose directly from one DaMa to another -Da *da_mat_ints_transpose(Da *damp, int *fill){ - size_t rows = damp->size/damp->element_size; - size_t columns = da_length(da_mat_longest(damp)); - Da *matrix = damp; - Da tran; - da_alloc(&tran, sizeof(Da)); - Da *transpose = &tran; - - Da *dpt = (Da *)(matrix->base); - int i = 0, j = 0; - while( j < columns ){ - Da new_row; da_alloc(&new_row, sizeof(int)); - int *ept = fill; - while( i < rows ){ - if( !da_endq(dpt, (dpt->base + j*(dpt->element_size))) ){ - *ept = *(dpt->base + j*(dpt->element_size)); - } - da_push(&new_row, ept); - dpt++; - i++; - } - da_mat_push_row(transpose, &new_row); - j++; - } - return transpose; -} - -//to create raw matrix image in memory, no longer a Da struct -int *da_mat_ints_to_raw_image_matrix(Da *damp, int fill){ - size_t rows = damp->size / damp->element_size; - size_t columns = da_length(da_mat_longest(damp)); - int *matrix = MALLOC(sizeof(rows*columns));//[rows][columns] - int i = 0; - Da *dpt = (Da *)(damp->base); - while( i < rows ) - { - int *ept = (int *)(dpt->base); - int j = 0; - while( j < columns ) - {//matrix[i][j] - if (da_endq(dpt,(dpt->base + j*(dpt->element_size)))) - *(matrix + (i*columns + j)*sizeof(int)) = fill; - else *(matrix + (i*columns + j)*sizeof(int)) = *(ept); - ept++; - j++; - } - dpt++; - i++; - } - return matrix; -} -int *da_mat_ints_to_raw_image_transpose(Da *damp, int fill){ - size_t rows = damp->size/damp->element_size; - size_t columns = da_length(da_mat_longest(damp)); - int *matrix = da_mat_ints_to_raw_image_matrix(damp, fill);//[rows][columns] - int *transpose = MALLOC(sizeof(columns*rows)); - int i, j; - for(i=0;ibase; - char *ept = dpt->base; - while( dpt != damp->end ){ - while( ept != dpt->end ){ - f(ept, closure); - ept+=dpt->element_size; - } - dpt++; - } -*/ - - -//------------------------------------ - -//first pass at array of Das, exists, etc turned into this - -typedef struct{ - Da *da; - bool found; -} da_present_closure; - -void da_present(Da **dar, int dar_size, void *closure){ - Da **pt = dar; - da_present_closure *dpc = (da_present_closure *)closure; - Da *test_element = dpc->da; - int i = 0; - while (!dpc->found && i < dar_size){ - dpc->found = da_equal(*pt, test_element); - pt++; - i++; - } - return; -} - - -void da_mat_map(Da **dar, int dar_size, void f(void *, void *), void *closure){ - Da **pt = dar; - int i = 0; - while( i < dar_size ){ - f(*pt, closure); - pt++; - i++; - } - return; -} diff --git a/module/da/src/da.lib.h b/module/da/src/da.lib.h index 43ed5a1..e9cc718 100644 --- a/module/da/src/da.lib.h +++ b/module/da/src/da.lib.h @@ -3,21 +3,22 @@ #include #include #include +#include -typedef struct { +typedef struct AccChannel_struct AccChannel; // AccChannel_struct defined in acc.lib.h +typedef struct Da_struct Da; + +struct Da_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; - -#define RETURN(dap, r) \ - { da_pts_free_all(dap); return r; } - + AccChannel *channel; +}; // constructors / destructors // - void da_alloc(Da *dap, size_t element_size); + Da *da_init(Da *dap, size_t element_size, AccChannel *channel);//calls da_malloc for base pointer void da_free(Da *dap); void da_rewind(Da *dap); void da_rebase(Da *dap, char *old_base, void *pta); @@ -27,7 +28,7 @@ typedef struct { // status / attributes // - bool da_empty(Da *dap); + bool da_is_empty(Da *dap); bool da_equal(Da *da_0, Da *da_1); size_t da_length(Da *dap); bool da_length_equal(Da *dap0, Da *dap1); @@ -41,13 +42,13 @@ typedef struct { // iteration, f is given a pointer to an element and a pointer to the closure bool da_endq(Da *dap, void *pt); bool da_right_bound(Da *dap, void *pt); - void da_map(Da *dap, void f(void *, void *), void *closure); // this is really 'foreach' to be renamed + void da_foreach(Da *dap, void f(void *, void *), void *closure); //used to be da_map bool da_exists(Da *dap, bool f(void *, void*), void *closure); bool da_all(Da *dap, bool f(void *, void*), void *closure); // elements are pointers // would be better if exists returned NULL or a pointer to the existing element- - bool da_pts_exists(Da *dap, void *test_element); + void *da_pts_exists(Da *dap, void *test_element); void da_pts_free_all(Da *dap); // calls free on all elements void da_pts_nullify(Da *dap, void **ept); // sets *ept to NULL, pops all NULLs from top of stack @@ -68,29 +69,5 @@ typedef struct { 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 *)); -// matrix -// There is a top level master da array. Its elements are da arrays, these are said to be rows - void da_mat_map(Da **dar, int dar_size, void f(void *,void*), void *closure); - - Da *da_mat_push_row_alloc(Da *damp); - Da *da_mat_push_row(Da *damp, Da *dap); - void da_mat_push_column(Da *damp, Da *dap, void *fill); - - void da_mat_every_row(Da *damp, void f(void *, void *), void *closure); - Da *da_mat_longer(Da *dap0, Da *dap1); - Da *da_mat_longest(Da *damp); - void da_mat_every_column(Da *damp, void f(void *, void *), void *closure); - - Da *da_mat_transpose(Da *damp, void *fill); - - bool da_mat_all_rows_same_length(Da *damp); - bool da_mat_ints_all_rows_repeat(Da *damp); - bool da_mat_ints_all_columns_repeat(Da *damp); - bool da_mat_ints_repeats_matrix(Da *damp); - - Da *da_mat_ints_transpose(Da *damp, int *fill); - int *da_mat_ints_to_raw_image_matrix(Da *damp, int fill); - int *da_mat_ints_to_raw_image_transpose(Da *damp, int fill); - #endif diff --git a/module/da/src/da_mat.lib.c_nocompile b/module/da/src/da_mat.lib.c_nocompile new file mode 100644 index 0000000..fb7cef6 --- /dev/null +++ b/module/da/src/da_mat.lib.c_nocompile @@ -0,0 +1,262 @@ + +//----------------------------------------------------- + + +//------------------------------------------------------- + +// all things Da matrix +// a DaMa (Doubling array Matrix) is a Da whose elements are Da's +// forms a matrix if you treat what's pointed to by base pointers of the elements of the DaMa as the first column of elements and fill in each row with the contents of the Das +// The "row major" nomenclature is used to remain consistent with database logic. +/* Example: +Da dar0; Da *dap0 = &dar0; da_alloc(dap0, sizeof(int)); +Da dar1; Da *dap1 = &dar1; da_alloc(dap1, sizeof(int)); +Da dama; Da *damp = &dama; da_alloc(damp, sizeof(Da)); +da_push(damp, dap0); +da_push(damp, dap1); +*/ + +/* + +Da *da_mat_push_row_alloc(Da *damp){ + size_t row_off = (Da *)(damp->end) - (Da *)(damp->base); + damp->end += damp->element_size; + if( damp->end > damp->base + damp->size ) da_expand(damp); + return (Da *)(damp->base) + row_off; +} +Da *da_mat_push_row(Da *damp, Da *dap){// Dama won't track changes to Das after pushing onto rows + Da *row_pt = da_mat_push_row_alloc(damp); + memcpy(row_pt, dap, damp->element_size); + return row_pt; +} + +void da_mat_push_column(Da *damp, Da *dap, void *fill){ + Da *tran = da_mat_transpose(damp, fill); + da_mat_push_row(tran, dap); + Da *new_dama = da_mat_transpose(tran, fill); + //add protection against memory overwrite - expand if necessary + memcpy(damp, new_dama, new_dama->size); +} + +void da_mat_every_row(Da *damp, void f(void *, void *), void *closure){//like every but for rows instead of elements + Da *dpt = (Da *)(damp->base); + while( dpt != (Da *)damp->end ){ + f(dpt, closure); + dpt++; + } +} + +// da_mat_every_column uses da_mat_longest and therefore da_mat_longer, written for the purpose of terminating the while loop in the appropriate place +// will return dap1 if equal, cannot determine equality +Da *da_mat_longer(Da *dap0, Da *dap1){ + if (da_length(dap0) > da_length(dap1)) return dap0; + else return dap1; +} +// returns Da in DaMa with longest length +Da *da_mat_longest(Da *damp){ + Da *dap = (Da *)(damp->base); + Da *longest = (Da *)((damp->base) + sizeof(Da)); + while( dap < (Da *)(damp->end) ){ + longest = da_mat_longer(dap,longest); + dap++; + } + return longest; +} +void da_mat_every_column(Da *damp, void f(void *, void *), void *closure){//like every but for columns instead of elements + Da *dpt = (Da *)(damp->base); + size_t rows = damp->size/damp->element_size; + size_t columns = da_length(da_mat_longest(damp)); + size_t j = 0; + while( j < columns ){ + int *col = MALLOC(sizeof(rows*sizeof(int))); + size_t i = 0; + while( i < rows ) { + if (da_endq(dpt,(dpt->base + j*(dpt->element_size)))) + *(col+i) = 0; + else *(col+i) = *(dpt->base + j*(dpt->element_size)); + dpt++; + i++; + } + f(col, closure); + j++; + } +} + +Da *da_mat_transpose(Da *damp, void *fill){// all Das must have same element type, will sort to integer, char, or char * transpose function + Da *dap = (Da *)(damp->base); + Da tran; da_alloc(&tran, sizeof(damp->element_size)); + Da *transpose = &tran; + if( dap->element_size == sizeof(int) ){ + int *filler = (int *)fill; + transpose = da_mat_ints_transpose(damp, filler); + } + /*else if( dap->element_size == sizeof(char) ){ + char *filler = (char *)fill; + transpose = da_character_transpose(damp, filler); + } + else if( dap->element_size == sizeof(char *) ){ + char **filler = (char **)fill; + transpose = da_c_string_transpose(damp, filler); + }*/ + //else error? + return transpose; +} + + +//-------------------------------------------------------------------- +// DaMa is a matrix of integers (stored in Das as columns) + +// integer repeats across columns +bool da_mat_all_rows_same_length(Da *damp){ + Da *dap = (Da *)(damp->base); + Da *pt = dap; + bool flag = true; + while( flag && pt != (Da *)(damp->end) ){ + flag = da_length_equal(dap, pt); + } + return flag; +} +bool da_mat_ints_all_rows_repeat(Da *damp){// if rows are made of repeating integers, then all columns read the same thing + Da *dpt = (Da *)(damp->base); + bool flag = false; + if( da_mat_all_rows_same_length((Da *)damp) ){// columns can't be equal if rows not all same length, will return false + flag = true; + while( flag && dpt != (Da *)(damp->end) ){ + flag = da_integer_repeats(dpt); // in "da is array of integers" section + dpt++; + } + return flag; + } + else return flag; +} +bool da_mat_ints_all_columns_repeat(Da *damp){// rows are repeating in transpose = columns are repeating + int x = 0; //have to pass in fill for transpose, this nullifies effect same_length test + Da *test_da = da_mat_transpose(damp, &x); + return da_mat_ints_all_rows_repeat(test_da); +} +bool da_mat_ints_repeats_matrix(Da *damp){// all elements in matrix are same + bool flag1 = da_mat_ints_all_rows_repeat(damp); + bool flag2 = da_mat_ints_all_columns_repeat(damp); + return flag1 && flag2; +} + +// to transpose directly from one DaMa to another +Da *da_mat_ints_transpose(Da *damp, int *fill){ + size_t rows = damp->size/damp->element_size; + size_t columns = da_length(da_mat_longest(damp)); + Da *matrix = damp; + Da tran; + da_alloc(&tran, sizeof(Da)); + Da *transpose = &tran; + + Da *dpt = (Da *)(matrix->base); + int i = 0, j = 0; + while( j < columns ){ + Da new_row; da_alloc(&new_row, sizeof(int)); + int *ept = fill; + while( i < rows ){ + if( !da_endq(dpt, (dpt->base + j*(dpt->element_size))) ){ + *ept = *(dpt->base + j*(dpt->element_size)); + } + da_push(&new_row, ept); + dpt++; + i++; + } + da_mat_push_row(transpose, &new_row); + j++; + } + return transpose; +} + +//to create raw matrix image in memory, no longer a Da struct +int *da_mat_ints_to_raw_image_matrix(Da *damp, int fill){ + size_t rows = damp->size / damp->element_size; + size_t columns = da_length(da_mat_longest(damp)); + int *matrix = MALLOC(sizeof(rows*columns));//[rows][columns] + int i = 0; + Da *dpt = (Da *)(damp->base); + while( i < rows ) + { + int *ept = (int *)(dpt->base); + int j = 0; + while( j < columns ) + {//matrix[i][j] + if (da_endq(dpt,(dpt->base + j*(dpt->element_size)))) + *(matrix + (i*columns + j)*sizeof(int)) = fill; + else *(matrix + (i*columns + j)*sizeof(int)) = *(ept); + ept++; + j++; + } + dpt++; + i++; + } + return matrix; +} +int *da_mat_ints_to_raw_image_transpose(Da *damp, int fill){ + size_t rows = damp->size/damp->element_size; + size_t columns = da_length(da_mat_longest(damp)); + int *matrix = da_mat_ints_to_raw_image_matrix(damp, fill);//[rows][columns] + int *transpose = MALLOC(sizeof(columns*rows)); + int i, j; + for(i=0;ibase; + char *ept = dpt->base; + while( dpt != damp->end ){ + while( ept != dpt->end ){ + f(ept, closure); + ept+=dpt->element_size; + } + dpt++; + } +*/ + + +//------------------------------------ + +//first pass at array of Das, exists, etc turned into this + +typedef struct{ + Da *da; + bool found; +} da_present_closure; + +void da_present(Da **dar, int dar_size, void *closure){ + Da **pt = dar; + da_present_closure *dpc = (da_present_closure *)closure; + Da *test_element = dpc->da; + int i = 0; + while (!dpc->found && i < dar_size){ + dpc->found = da_equal(*pt, test_element); + pt++; + i++; + } + return; +} + + +void da_mat_map(Da **dar, int dar_size, void f(void *, void *), void *closure){ + Da **pt = dar; + int i = 0; + while( i < dar_size ){ + f(*pt, closure); + pt++; + i++; + } + return; +} diff --git a/module/da/src/da_mat.lib.h_nocompile b/module/da/src/da_mat.lib.h_nocompile new file mode 100644 index 0000000..c3bc5f8 --- /dev/null +++ b/module/da/src/da_mat.lib.h_nocompile @@ -0,0 +1,25 @@ +/* +// matrix +// There is a top level master da array. Its elements are da arrays, these are said to be rows + void da_mat_map(Da **dar, int dar_size, void f(void *,void*), void *closure); + + Da *da_mat_push_row_alloc(Da *damp); + Da *da_mat_push_row(Da *damp, Da *dap); + void da_mat_push_column(Da *damp, Da *dap, void *fill); + + void da_mat_every_row(Da *damp, void f(void *, void *), void *closure); + Da *da_mat_longer(Da *dap0, Da *dap1); + Da *da_mat_longest(Da *damp); + void da_mat_every_column(Da *damp, void f(void *, void *), void *closure); + + Da *da_mat_transpose(Da *damp, void *fill); + + bool da_mat_all_rows_same_length(Da *damp); + bool da_mat_ints_all_rows_repeat(Da *damp); + bool da_mat_ints_all_columns_repeat(Da *damp); + bool da_mat_ints_repeats_matrix(Da *damp); + + Da *da_mat_ints_transpose(Da *damp, int *fill); + int *da_mat_ints_to_raw_image_matrix(Da *damp, int fill); + int *da_mat_ints_to_raw_image_transpose(Da *damp, int fill); +*/ diff --git a/module/da/src/da_na.lib.c b/module/da/src/da_na.lib.c deleted file mode 100644 index 33f0640..0000000 --- a/module/da/src/da_na.lib.c +++ /dev/null @@ -1,74 +0,0 @@ -/* -Alternative versions of the da array constructors and destructors for avoiding the accounting -versions of malloc and free. - - */ - -#include -#include -#include - -#include "da.lib.h" -#include "da_na.lib.h" - -#undef MALLOC -#undef FREE -#define MALLOC malloc -#define FREE free - -#define da_alloc da_na_alloc -#define da_free da_na_free -#define da_expand da_na_expand -#define da_push_alloc da_na_push_alloc -#define da_push da_na_push -#define da_pts_free_all_0 da_na_pts_free_all_0 -#define da_pts_free_all da_na_pts_free_all - -// the following is just cut and paste from da.lib.c -// ... - -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->end = dap->base; -} -void da_free(Da *dap){ - FREE(dap->base); - dap->size = 0; -} - -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 ); - 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; -} - -// allocate space for a new element at the end of the array -static char *da_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_expand(dap); - return dap->base + element_off; -} -char *da_push(Da *dap, void *element){ - char *element_pt = da_push_alloc(dap); - memcpy(element_pt, element, dap->element_size); - return element_pt; -} - -static void da_pts_free_all_0(void *pt, void *closure){ - FREE(*(char **)pt); // FREE does not care about the pointer type -} -void da_pts_free_all(Da *dap){ - da_map(dap, da_pts_free_all_0, NULL); - da_rewind(dap); -} - diff --git a/module/da/src/da_na.lib.h b/module/da/src/da_na.lib.h deleted file mode 100644 index aca05cc..0000000 --- a/module/da/src/da_na.lib.h +++ /dev/null @@ -1,27 +0,0 @@ -#ifndef DA_NA_LIB_H -#define DA_NA_LIB_H -#include -#include -#include - -/* -Alternative versions of the da array constructors and destructors for avoiding the accounting -versions of malloc and free. We use these in the accounting code itself. - -Note the accounting software does not use int, string, strings, or matrix da methods, so those -have not been replaced. - -Why are we afraid to let accounting account for itself? Well otherwise there is a possibility -of a hickup where an array in the accounting code expands while testing a block -of code. - -*/ - -void da_na_alloc(Da *dap, size_t element_size); -void da_na_free(Da *dap); -char *da_na_expand(Da *dap); -char *da_na_push_alloc(Da *dap); -char *da_na_push(Da *dap, void *element); -void da_na_pts_free_all(Da *dap); - -#endif diff --git a/module/da/src/struct_forward_example.c b/module/da/src/struct_forward_example.c new file mode 100644 index 0000000..d8fec98 --- /dev/null +++ b/module/da/src/struct_forward_example.c @@ -0,0 +1,27 @@ + + +typedef struct AccChannel_struct AccChannel; +typedef struct Da_struct Da; + +typedef long long int size_t; + +struct AccChannel_struct{ + struct Da *outstanding_malloc; + Da *spurious_free; + // enum Mode mode; +}; //name instances of channels with handles + +struct Da_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; + AccChannel *channel;//assign during init, set to NULL during free +}; + + +int main(){ + Da x; + AccChannel y; + return 5; +} diff --git a/module/da/src/temp.c b/module/da/src/temp.c deleted file mode 100644 index abe405b..0000000 --- a/module/da/src/temp.c +++ /dev/null @@ -1,35 +0,0 @@ -#include - - -typedef struct { - int a; - int b; -}pair; - - -int main(){ - pair s0, s1, s2; - s0.a = 10; - s0.b = 11; - s1.a = 20; - s1.b = 21; - s2.a = 10; - s2.b = 11; - - if( s0 == s1 ) { - printf("s0 s1 are equal!\n"); - }else{ - printf("s0 s1 are not equal!\n"); - } - if( s0 == s2 ) { - printf("s0 s2 are equal!\n"); - }else{ - printf("s0 s2 are not equal!\n"); - } - - - return 0; -} - - - diff --git a/module/da/src/update_Acc_channel.sed b/module/da/src/update_Acc_channel.sed new file mode 100644 index 0000000..9861d73 --- /dev/null +++ b/module/da/src/update_Acc_channel.sed @@ -0,0 +1 @@ +s/Acc_channel/AccChannel/g diff --git a/module/da/src/temp.sed b/module/da/src/update_da_lib_names.sed similarity index 100% rename from module/da/src/temp.sed rename to module/da/src/update_da_lib_names.sed diff --git a/module/da/src/update_project_da_lib_names.sed b/module/da/src/update_project_da_lib_names.sed new file mode 100644 index 0000000..692744f --- /dev/null +++ b/module/da/src/update_project_da_lib_names.sed @@ -0,0 +1,2 @@ +s/da_alloc/da_init/g +s/da_map/da_foreach/g diff --git a/module/da/src1/da.lib.c b/module/da/src1/da.lib.c deleted file mode 100644 index 2fe8cf5..0000000 --- a/module/da/src1/da.lib.c +++ /dev/null @@ -1,306 +0,0 @@ -/* - Dynamic Array - - Cannot expand an empty array. - - Accounting built into code. Alternative version of malloc and free that will invoke accounting for pointers that are allocated - but not freed, or freed but not allocated. - -*/ - -#include "da.lib.h" - -//----------------------------------------------------------------- -//function definitions for accounting - -Acc_channel *acc_open(Acc_channel *channel, enum Mode mode){//acc init - if( channel == &acc_live_channels ) {//avoid pushing channel tracker onto itself - Da os; Da sf; - channel->outstanding_malloc = da_init(&os, sizeof(void *), NULL); - channel->spurious_free = da_init(&sf, sizeof(void *), NULL); - channel->mode = mode; - return channel; - } - else if( acc_live_channels.mode == acc_NULL ){//accounting NULL - //channel = (Acc_channel *)acc_malloc(sizeof(Acc_channel), NULL);//accounting channel still on the heap but not tracked in SELF mode - Da os; Da sf; - channel->outstanding_malloc = da_init(&os, sizeof(void *), NULL); - channel->spurious_free = da_init(&sf, sizeof(void *), NULL); - channel->mode = mode; - return channel; - } - else if( acc_live_channels.mode == acc_SELF ){//accounting tracks itself - channel = (Acc_channel *)acc_malloc(sizeof(Acc_channel), &acc_live_channels); - Da os; Da sf; - channel->outstanding_malloc = da_init(&os, sizeof(void *), NULL); - channel->spurious_free = da_init(&sf, sizeof(void *), NULL); - channel->mode = mode; - return channel; - } - else{ //cerr, optional acc_live_channels only tracks channels, not other mallocs/frees - return channel; - } -} -void *acc_malloc(size_t size, Acc_channel *channel){ - void *an_allocation_pt = malloc(size); - if( channel ) da_push((Da *)(channel->outstanding_malloc), &an_allocation_pt); - return (void *)an_allocation_pt; -} -void acc_free(void *pt, Acc_channel *channel){ - if( channel ){ - void **i = (void **)(((Da *)(channel->outstanding_malloc))->base); - bool present = false; - while( i < (void **)(((Da *)(channel->outstanding_malloc))->end) ){ - if( *i == pt ){ - da_pts_nullify((Da *)(channel->outstanding_malloc), i); - present = true; - } - i++; - } - if( present == false ) da_push((Da *)(channel->spurious_free), &pt); - } - free(pt); -} -static void count_balance(void *element, void *closure){ - int *counter = (int *)closure; - if( (void *)element ) (*counter)++; -} -static void acc_rep_helper_BALANCE(Acc_channel *channel){ - int count = 0; - da_foreach((Da *)channel->outstanding_malloc, count_balance, &count); - printf("There are %d outstanding allocations.\n", count); - count = 0; - da_foreach((Da *)channel->spurious_free, count_balance, &count); - printf("There are %d spurious frees.\n", count); -} -static void print_pointer(void *element, void *closure){ - if( element ) printf("%d ", *(int *)element); -} -static void acc_rep_helper_FULL(Acc_channel *channel){ - int count = 0; - da_foreach((Da *)channel->outstanding_malloc, count_balance, &count); - printf("There are %d outstanding mallocs.\n", count); - if( count < 10 ){ - printf("The outstanding allocated pointers are: "); - da_foreach((Da *)channel->outstanding_malloc, print_pointer, NULL); - printf(".\n"); - } - count = 0; - da_foreach((Da *)channel->spurious_free, count_balance, &count); - printf("There are %d spurious frees.\n", count); - if( count < 10 ){ - printf("The spuriously freed pointers are: "); - da_foreach((Da *)channel->outstanding_malloc, print_pointer, NULL); - printf(".\n"); - } -} -Acc_channel *acc_report(Acc_channel *channel){ - if( channel->mode == acc_NULL ){ - printf("Accounting mode is NULL."); - return channel; - } - if( channel->mode == acc_BALANCE ){ - printf("Accounting mode is BALANCE.\n"); - if( da_emptyq((Da *)(channel->outstanding_malloc)) && da_emptyq((Da *)(channel->spurious_free)) ){ - printf("This channel is in balance."); - } - else{ - printf("This channel is out of balance.\n"); - acc_rep_helper_BALANCE(channel); - } - return channel; - } - if( channel->mode == acc_FULL ){ - printf("Accounting mode is FULL.\n"); - if( da_emptyq((Da *)(channel->outstanding_malloc)) && da_emptyq((Da *)(channel->spurious_free)) ){ - printf("This channel is in balance."); - } - else{ - printf("This channel is out of balance.\n"); - acc_rep_helper_FULL(channel); - } - return channel; - } - if( channel->mode == acc_SELF ){ - printf("Accounting mode is SELF.\n"); - if( da_emptyq((Da *)(channel->outstanding_malloc)) && da_emptyq((Da *)(channel->spurious_free)) ){ - printf("There are no open channels."); - } - else { - printf("The accounting code is out of balance.\n"); - acc_rep_helper_FULL(channel); - } - return channel; - } -} -void acc_close(Acc_channel *channel){ - da_free((Da *)(channel->outstanding_malloc)); - da_free((Da *)(channel->spurious_free)); - if( (channel != &acc_live_channels) - && (acc_live_channels.mode == acc_SELF) ){ - acc_free(channel, &acc_live_channels); - return; - } - else return; -} - -void *crash_and_burn_malloc(size_t size){} -void crash_and_burn_free(void *pt){} - - -//----------------------------------------------------------------- -//functions definitions for Das - -// constructors / destructors -Da *da_init(Da *dap, size_t element_size, Acc_channel *channel){ - dap->element_size = element_size; - dap->size = 4 * element_size; - dap->base = acc_malloc(dap->size, channel); - dap->end = dap->base; - dap->channel = channel; - return dap; -} -void da_free(Da *dap){ - acc_free(dap->base, dap->channel); - dap->size = 0; - dap->channel = NULL; -} -void da_rewind(Da *dap){ - dap->end = dap->base; -} -void da_rebase(Da *dap, char *old_base, void *pta){ - char **pt = (char **)pta; - size_t offset = *pt - old_base; - *pt = dap->base + offset; -} -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 = acc_malloc(new_size, (Acc_channel *)(dap->channel)); - memcpy( new_base, old_base, end_offset + dap->element_size); - acc_free(old_base, (Acc_channel *)(dap->channel)); - dap->base = new_base; - dap->end = new_base + end_offset; - dap->size = new_size; - return old_base; -} -bool da_boundq(Da *dap){ - return dap->end > (dap->base + dap->size); -} - -// status / attributes -// -bool da_emptyq(Da *dap){ - return dap->end == dap->base; -} -bool da_equal(Da *da_0, Da *da_1){ - return !bcmp(da_0, da_1, sizeof(Da)); -} -size_t da_length(Da *dap){ - return (dap->end - dap->base)/dap->element_size; -} -bool da_length_equal(Da *dap0, Da *dap1){ - return da_length(dap0) == da_length(dap1); -} - -// accessing -// -char *da_index(Da *dap, size_t i){ - size_t offset = i * dap->element_size; - char *pt = dap->base + offset; - return pt; -} -// allocate space for a new element at the end of the array -static char *da_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_expand(dap); - return dap->base + element_off; -} -char *da_push(Da *dap, void *element){ - char *element_pt = da_push_alloc(dap); - memcpy(element_pt, element, dap->element_size); - return element_pt; -} -bool da_pop(Da *dap, void *element){ - bool flag = dap->end >= dap->base + dap->element_size; - if( flag ){ - dap->end -= dap->element_size; - if(element) memcpy(element, dap->end, dap->element_size); - } - return flag; -} - -// iteration, f is given a pointer to an element and a pointer to the closure - -// true when pt has run off the end -bool da_endq(Da *dap, void *pt){ - return (char *)pt >= dap->end; -} -// array is a row of elements, pt points at the rightmost element -bool da_right_bound(Da *dap, void *pt){ - return ((char *)pt + dap->element_size) >= dap->end; -} -// passed in f(element_pt, arg_pt) -// We have no language support closures, so we pass in an argument for it. -// The closure may be set to NULL if it is not needed. -void da_foreach(Da *dap, void f(void *, void *), void *closure){ - char *pt = dap->base; - while( pt != dap->end ){ - f(pt, closure); - pt += dap->element_size; - } -} -//would like da_exists and da_all to return pointer to element -//abstract helper for da_exists and da_all -static bool da_quantifier(bool complement, Da *dap, bool pred(void *, void*), void *closure){ - char *pt = dap->base; - bool result = false; - while( (complement? !result : result) && (pt != dap->end) ){ - result = pred(pt, closure); - pt += dap->element_size; - } - return result; -} -//∃, OR foreach -bool da_exists(Da *dap, bool pred(void *, void*), void *closure){ - return da_quantifier(true, dap, pred, closure); -} -//∀, AND foreach -bool da_all(Da *dap, bool pred(void *, void*), void *closure){ - return da_quantifier(false, dap, pred, closure); -} - -// elements are pointers -// -static bool da_pts_exists_0(void *element, void *test_element){ return element == test_element; } -void *da_pts_exists(Da *dap, void *test_element){ - if( da_exists(dap, da_pts_exists_0, test_element) ) return test_element; - else return NULL; -} -// elements were allocated, now they will all be freed -static void da_pts_free_all_0(void *pt, void *closure){ - acc_free(*(char **)pt, closure); -} -void da_pts_free_all(Da *dap){ - da_foreach(dap, da_pts_free_all_0, dap->channel); - da_rewind(dap); -} -// elements are pointers -// ept points at an element, we set *ept to NULL -// we pop all NULLs off the top of the stack -void da_pts_nullify(Da *dap, void **ept){ - if(ept >= (void **)(dap->base) && ept < (void **)(dap->end)){ - *ept = NULL; - } - while( - dap->end > dap->base - && - *(void **)(dap->end - dap->element_size) == NULL){ - da_pop(dap, NULL); - } -} - -// matrices - elements are das -//need to rename/sed a lot of functions before adding diff --git a/module/da/src1/da.lib.h b/module/da/src1/da.lib.h deleted file mode 100644 index 0975274..0000000 --- a/module/da/src1/da.lib.h +++ /dev/null @@ -1,88 +0,0 @@ -#ifndef DA_LIB_H -#define DA_LIB_H -#include -#include -#include -#include - -//#define malloc crash_and_burn_malloc -//#define free crash_and_burn_free -//shouldn't define these in header bc need to use malloc and free in functions, put definitions after header if going to use - -// See acc_doc.txt for explanation of process for using accounting code. - -enum Mode{acc_NULL, acc_BALANCE, acc_FULL, acc_SELF};//0,1,2,3 - -struct Da;//forward declaration because mutually referential structs - -typedef struct { - struct Da *outstanding_malloc; - struct Da *spurious_free; - enum Mode mode; -} Acc_channel; //name instances of channels with handles - -typedef struct Da { - 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; - Acc_channel *channel;//assign during init, set to NULL during free -} Da; - -extern Acc_channel acc_live_channels;//acc_NULL or acc_SELF to track acc channels or not, other options return invalid upon report - -//function declarations for accounting - Acc_channel *acc_open(Acc_channel *channel, enum Mode mode);//initializes channel structs - void *acc_malloc(size_t size, Acc_channel *channel);//works for things besides Das too - void acc_free(void *pt, Acc_channel *channel);//works for things besides Das too - Acc_channel *acc_report(Acc_channel *channel);//reports on channels based on mode - void acc_close(Acc_channel *channel);//frees channel itself - - void *crash_and_burn_malloc(size_t size);//sends error message in case of accidental regular malloc - void crash_and_burn_free(void *);// sends error message in case of accidental regular free - -//function declarations for Das - -// constructors / destructors - Da *da_init(Da *dap, size_t element_size, Acc_channel *channel);//calls da_malloc for base pointer - void da_free(Da *dap); - void da_rewind(Da *dap); - void da_rebase(Da *dap, char *old_base, void *pta); - char *da_expand(Da *dap); - bool da_boundq(Da *dap); - -// status / attributes -// - bool da_emptyq(Da *dap); - bool da_equal(Da *da_0, Da *da_1); - size_t da_length(Da *dap); - bool da_length_equal(Da *dap0, Da *dap1); - -// accessing -// - char *da_index(Da *dap, size_t i); - char *da_push(Da *dap, void *element); - bool da_pop(Da *dap, void *element); - -// iteration, f is given a pointer to an element and a pointer to the closure - bool da_endq(Da *dap, void *pt); - bool da_right_bound(Da *dap, void *pt); - void da_foreach(Da *dap, void f(void *, void *), void *closure); //used to be da_map - bool da_exists(Da *dap, bool f(void *, void*), void *closure); - bool da_all(Da *dap, bool f(void *, void*), void *closure); - -// elements are pointers -// - void *da_pts_exists(Da *dap, void *test_element); - void da_pts_free_all(Da *dap); // calls free on all elements - void da_pts_nullify(Da *dap, void **ept); // sets *ept to NULL, pops all NULLs from top of stack - - -// matrices - elements are das -//need to rename/sed a lot of functions before adding -void da_mat_erase(Da *dap);//same as free - - - -#endif - diff --git a/module/da/src1/temp.sed b/module/da/src1/temp.sed deleted file mode 100644 index 4f51424..0000000 --- a/module/da/src1/temp.sed +++ /dev/null @@ -1 +0,0 @@ -s/&/&/g \ No newline at end of file -- 2.20.1