#ifndef ACC_LIB_H
#define ACC_LIB_H
+#include <stdlib.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <string.h>
+
#define malloc crash_and_burn_malloc
#define free crash_and_burn_free
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
+enum Mode_enum{acc_NULL = 0, acc_BALANCE = 1, acc_FULL = 2, acc_SELF = 3};//0,1,2,3
typedef enum Mode_enum Mode;
struct AccChannel_struct{
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
--- /dev/null
+#ifndef DA_LIB_H
+#define DA_LIB_H
+#include <stdlib.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <string.h>
+
+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;//assign during init, set to NULL during free
+};
+
+// 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
+
dynamic memory accounting
*/
-#include <stdlib.h>
-#include <stdbool.h>
-#include <stdio.h>
-#include <string.h>
+
#include "da.lib.h"
#include "acc.lib.h"
+
#undef malloc
#undef free
//function definitions for accounting
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;
- }
- 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 = 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;
- }
}
void *acc_malloc(size_t size, AccChannel *channel){
void *an_allocation_pt = malloc(size);
void **i = (void **)(((Da *)(channel->outstanding_malloc))->base);
bool present = false;
while( i < (void **)(((Da *)(channel->outstanding_malloc))->end) ){
- if( *i == pt ){
+ if( *i == pt ){//this is just da_exists, make it return a pointer
da_pts_nullify((Da *)(channel->outstanding_malloc), i);
present = true;
}
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( 0 < count && count < 10 ){
+ printf("There are %d outstanding mallocs.\n", (int)da_length((Da *)(channel->outstanding_malloc)));
+ printf("There are %d spurious frees.\n", (int)da_length((Da *)(channel->spurious_free)));
+ /*if( 0 < count && 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( 0 < count && 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 ){
}
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)) ){
+ if( false ){
printf("This channel is in balance.\n");
}
else{
}
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)) ){
+ if( true/*da_is_empty((Da *)(channel->outstanding_malloc)) && da_is_empty((Da *)(channel->spurious_free)) */){
printf("There are no open channels.\n");
}
else {
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;
+ return;
}
void *crash_and_burn_malloc(size_t size){}
#ifndef ACC_LIB_H
#define ACC_LIB_H
+#include <stdlib.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <string.h>
+
#define malloc crash_and_burn_malloc
#define free crash_and_burn_free
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
+enum Mode_enum{acc_NULL = 0, acc_BALANCE = 1, acc_FULL = 2, acc_SELF = 3};//0,1,2,3
typedef enum Mode_enum Mode;
struct AccChannel_struct{
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
}
bool da_exists(Da *dap, bool pred(void *, void*), void *closure){
return da_quantifier(true, dap, pred, closure);
-}
-bool da_all(Da *dap, bool pred(void *, void*), void *closure){
+}//return pointer to thing it found that exists
+bool da_exception(Da *dap, bool pred(void *, void*), void *closure){
return da_quantifier(false, dap, pred, closure);
-}
+}//make return pointer to exception and NULL (pointer) if no exception
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;
+ else return NULL; //if da_exists returns pointer, gets bool and pointer in one
}
/*
static da_pts_exists_0(void *element, void *pt){ return element == pt; }
#include <da.h>
#include <acc.h>
-AccChannel acc_live_channels;
+//AccChannel acc_live_channels;
int main(){
- acc_open(&acc_live_channels, acc_SELF);
+ //acc_open(&acc_live_channels, acc_SELF);
// enumeration of tests
typedef bool (*test_fun)();
test_fun tests[] =
//test_da_present_0,
test_da_exists_0,
test_da_exists_1,
- test_da_all_0,
+ //test_da_all_0,
test_da_init_0,
test_da_free_0,
test_da_is_empty_0,
tfp++;
tnp++;
}
- acc_report(&acc_live_channels);
+ //acc_report(&acc_live_channels);
// summarize results
if( passed == 0 && failed == 0)
printf("no tests ran\n");
// tests push
bool test_da_push_0(){
+ //AccChannel c;
+ //AccChannel *channel = acc_open(&c, acc_SELF);
Da da;
da_init(&da, sizeof(int), NULL); // leaves room for 4 ints
int i = 0;
}
bool flag3 = da_endq(&da, pt);
da_free(&da);
+ //acc_report(channel);
+ //acc_close(channel);
return flag0 && flag1 && flag2 && flag3;
}
// tests manual expansion
bool test_da_expand_0(){
Da da;
- da_init(&da, sizeof(int), NULL); // leaves room for 4 ints
+ Da *dap = da_init(&da, sizeof(int), NULL); // leaves room for 4 ints
int i = 0;
int *pt = (int *)da.base;
// will double, 4 -> 8, then double 8 -> 16
pt++;
}
bool flag3 = da_endq(&da, pt);
- da_free(&da);
+ da_free(dap);
return flag0 && flag1 && flag2 && flag3;
}
}
-
+/*
//tests da_all
bool test_da_all_0(){
Da dar;
return flag1 && flag2;
}
-
+*/
//tests da_init
Da da;
Da *da_pt = &da;
da_init(da_pt, sizeof(int), NULL);
+ bool flag0 = da_is_empty(da_pt);
while(i < 11){
da_push(da_pt, &i);
++i;
da_rewind(da_pt);
bool flag2 = da_is_empty(da_pt);
da_free(da_pt);
- return flag1 && flag2;
+ return flag0 && flag1 && flag2;
}
//tests da_length
bool test_da_accounting_0(){
AccChannel acc0;
- AccChannel *acc0_pt = acc_open(&acc0, acc_NULL);
+ AccChannel *acc0_pt = acc_open(&acc0, acc_FULL);
- //Da da0;
- //Da *dap = &da0;
- //da_init(dap, sizeof(int), acc0_pt);
+ Da da0;
+ Da *dap = (Da *)da_init(&da0, sizeof(int), NULL);
+
printf("Outstanding Malloc: %d\n", (int)(acc0_pt->outstanding_malloc));
printf("Spurious Free: %d\n", (int)(acc0_pt->spurious_free));
acc_report(acc0_pt);
- //da_free(dap);
- //acc_close(acc0_pt);
-
- //acc_close(acc0_pt);
+ // da_free(dap);
+
+ acc_close(acc0_pt);
bool result = true;
return result;
}
bool test_da_present_0();
bool test_da_exists_0();
bool test_da_exists_1();
-bool test_da_all_0();
+//bool test_da_all_0();
bool test_da_init_0();
bool test_da_free_0();
bool test_da_is_empty_0();