+++ /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;
-};
-
-// 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
-
#include "da.lib.h"
#include "acc.lib.h"
+#undef malloc
+#undef free
//-----------------------------------------------------------------
//function definitions for accounting
return channel;
}
else if( acc_live_channels.mode == acc_SELF ){//accounting tracks itself
- channel = (AccChannel *)acc_malloc(sizeof(AccChannel), &acc_live_channels);
+ //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);
}
static void count_balance(void *element, void *closure){
int *counter = (int *)closure;
- if( (void *)element ) (*counter)++;
+ if( 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);
+ int count1 = 0;
+ da_foreach((Da *)channel->spurious_free, count_balance, &count1);
printf("There are %d spurious frees.\n", count);
}
static void print_pointer(void *element, void *closure){
int count = 0;
da_foreach((Da *)channel->outstanding_malloc, count_balance, &count);
printf("There are %d outstanding mallocs.\n", count);
- if( count < 10 ){
+ 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( count < 10 ){
+ 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 ){
- printf("Accounting mode is NULL.");
+ printf("Accounting mode is NULL.\n");
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.");
+ printf("This channel is in balance.\n");
}
else{
printf("This channel is out of balance.\n");
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.");
+ printf("This channel is in balance.\n");
}
else{
printf("This channel is out of balance.\n");
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.");
+ printf("There are no open channels.\n");
}
else {
printf("The accounting code is out of balance.\n");
#include <string.h>
#include <stdbool.h>
#include <da.h>
+#include <acc.h>
#include "test_da.lib.h"
+
// tests push
bool test_da_push_0(){
Da da;
- da_alloc(&da, sizeof(int)); // leaves room for 4 ints
+ 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
// tests manual expansion
bool test_da_expand_0(){
Da da;
- da_alloc(&da, sizeof(int)); // leaves room for 4 ints
+ 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
}
Da da;
- da_alloc(&da, sizeof(char));
+ da_init(&da, sizeof(char), NULL);
da_string_input(&da, file);
bool flag0 = !strcmp(da.base, "this is a test");
bool test_da_pop_0(){
Da da;
- da_alloc(&da, sizeof(int));
+ da_init(&da, sizeof(int), NULL);
int i = 5;
da_push(&da, &i);
bool test_da_cat_0(){
Da da0, da1;
- da_alloc(&da0, sizeof(int));
- da_alloc(&da1, sizeof(int));
+ da_init(&da0, sizeof(int), NULL);
+ da_init(&da1, sizeof(int), NULL);
int i = 5;
while(i < 8){
bool test_da_cat_1(){
Da dar0;
Da dar1;
- da_alloc(&dar0, sizeof(int));
- da_alloc(&dar1, sizeof(int));
+ da_init(&dar0, sizeof(int), NULL);
+ da_init(&dar1, sizeof(int), NULL);
int n = 2;
{
int m = 0;
bool test_da_rewind_0(){
int i = 10;
Da dar;
- da_alloc(&dar, sizeof(int));
+ da_init(&dar, sizeof(int), NULL);
while(i < 18){
da_push(&dar, &i);
++i;
bool test_da_index_0(){
Da dar;
Da *dar_pt = &dar;
- da_alloc(dar_pt, sizeof(int));
+ da_init(dar_pt, sizeof(int), NULL);
bool flag[4];
bool test_da_free_elements_0(){
Da dar;
Da *dar_pt = &dar;
- da_alloc(dar_pt, sizeof(int*));
+ da_init(dar_pt, sizeof(int*), NULL);
int i = 3;
int *i_pt = &i;
bool test_da_strings_exists_0(){
Da dar;
Da *dar_pt = &dar;
- da_alloc(dar_pt, sizeof(char *));
+ da_init(dar_pt, sizeof(char *), NULL);
//fill dar with strings
char *string0 = "nope";
//tests rebase
bool test_da_rebase_0(){
Da dar;
- da_alloc(&dar,sizeof(char));
+ da_init(&dar,sizeof(char), NULL);
- char **el_pt = (char **)MALLOC(4*(sizeof(char*)));
+ char **el_pt = (char **)acc_malloc(4*(sizeof(char*)), NULL);
{//push "temp" into dar, leave with pointer to last element
int i = 0;
char arr[4] = {'t','e','m','p'};
}
}
da_free(&dar);
- FREE(el_pt);
+ acc_free(el_pt, NULL);
return flag1 && flag2 && flag3 && result;
}
strcpy(a, "zsdf");
Da da;
Da *da_pt = &da;
- da_alloc(da_pt, sizeof(char *));
+ da_init(da_pt, sizeof(char *));
da_push(da_pt, a);
...
FREE(*(char *)da_index(da_pt, 0));
//tests da_boundq
bool test_da_boundq_0(){
Da dar;
- da_alloc(&dar,sizeof(char));
+ da_init(&dar,sizeof(char), NULL);
bool flag[5];
{//pushes onto dar and tests no runoff
return result;
}
-//tests map
-static void test_da_map_0_helper(void *pt, void *closure){
+//tests foreach
+static void test_da_foreach_0_helper(void *pt, void *closure){
int *n = (int *)closure;
*n += *(int *)pt;
}
-bool test_da_map_0(){
+bool test_da_foreach_0(){
Da dar;
- da_alloc(&dar,sizeof(int));
+ da_init(&dar, sizeof(int), NULL);
{//pushes onto dar
int arr[4] = {5,6,7,8};
int n = 0;
int *closure = &n;
- da_map(&dar, test_da_map_0_helper, (int *)closure);
+ da_foreach(&dar, test_da_foreach_0_helper, (int *)closure);
//rename to da_foreach
bool result = n == (5+6+7+8);
da_free(&dar);
return result;
}
-
+/*
//tests da_present
bool test_da_present_0(){
int dar_size = 0;
- Da **dar = MALLOC(3 * sizeof(Da *));
+ Da **dar = acc_malloc(3 * sizeof(Da *), NULL);
Da dap_0;
Da *dap_0_pt = &dap_0;
- da_alloc(dap_0_pt,sizeof(int));
+ da_init(dap_0_pt,sizeof(int), NULL);
Da dap_1;
Da *dap_1_pt = &dap_1;
- da_alloc(dap_1_pt,sizeof(char));
+ da_init(dap_1_pt,sizeof(char), NULL);
dar[dar_size] = dap_0_pt; dar_size++;
dar[dar_size] = dap_1_pt; dar_size++;
Da dap_2;
Da *dap_2_pt = &dap_2;
- da_alloc(dap_2_pt,sizeof(char *));
+ da_init(dap_2_pt,sizeof(char *), NULL);
dar[dar_size] = dap_2_pt; dar_size++;
- /*
+
Da ;
Da *matrix = ;
- da_alloc(dar, sizeof(Dap *));
- */
+ da_init(dar, sizeof(Dap *));
+
typedef struct{
Da *da;
bool found;
bool result = flag[0] && flag[1] && flag[2] && flag[3];
da_free(dap_0_pt); da_free(dap_1_pt); da_free(dap_2_pt);
- FREE(dar);
+ acc_free(dar, NULL);
return result;
}
+*/
//tests for da_exists and all
-bool test_exists(void *pt, void *closure){
+static bool test_exists_helper(void *pt, void *closure){
bool result = *(int*)pt == *(int *)closure;
return result;
}
bool test_da_exists_0(){
Da dar;
Da *dar_pt = &dar;
- da_alloc(dar_pt, sizeof(int));
+ da_init(dar_pt, sizeof(int), NULL);
int n[5] = {5,6,7,8,9};
int j = 0;
while(j < 5){
int *n_pt = n+j;
- flag[j] = da_exists(dar_pt, test_exists, n_pt);
+ flag[j] = da_exists(dar_pt, test_exists_helper, n_pt);
j++;
}
}
int j = 0;
while(j < 5){
int *n_pt = n+j;
- flag[j] = da_exists(dar_pt, test_exists, n_pt);
+ flag[j] = da_exists(dar_pt, test_exists_helper, n_pt);
j++;
}
}
bool test_da_exists_1(){//tests that expansion doesn't change results
Da dar;
Da *dar_pt = &dar;
- da_alloc(dar_pt, sizeof(int));
+ da_init(dar_pt, sizeof(int), NULL);
int n[8] = {20,21,22,23,24,25,26,27};
int j = 0;
while(j < 8){
int *n_pt = n+j;
- flag[j] = da_exists(dar_pt, test_exists, n_pt);
+ flag[j] = da_exists(dar_pt, test_exists_helper, n_pt);
j++;
}
}
int j = 0;
while(j < 8){
int *n_pt = n+j;
- flag[j] = da_exists(dar_pt, test_exists, n_pt);
+ flag[j] = da_exists(dar_pt, test_exists_helper, n_pt);
j++;
}
}
bool test_da_all_0(){
Da dar;
Da *dar_pt = &dar;
- da_alloc(dar_pt, sizeof(int));
+ da_init(dar_pt, sizeof(int), NULL);
int n = 5;
int *n_pt = &n;
da_push(dar_pt, n_pt);
//tests da_all is true
- bool flag1 = da_all(dar_pt, test_exists, n_pt);
+ bool flag1 = da_all(dar_pt, test_exists_helper, n_pt);
da_pop(dar_pt, NULL);
n = 6;
//tests da_all is false
- bool flag2 = !da_all(dar_pt, test_exists, n_pt);
+ bool flag2 = !da_all(dar_pt, test_exists_helper, n_pt);
da_free(dar_pt);
+
return flag1 && flag2;
}
-//tests da_alloc
-bool test_da_alloc_0(){
- Da da0; Da *da0_pt = &da0; da_alloc(da0_pt, sizeof(char));
- Da da1; Da *da1_pt = &da1; da_alloc(da1_pt, sizeof(int));
- Da da2; Da *da2_pt = &da2; da_alloc(da2_pt, sizeof(char *));
+//tests da_init
+bool test_da_init_0(){
+ Da da0; Da *da0_pt = &da0; da_init(da0_pt, sizeof(char), NULL);
+ Da da1; Da *da1_pt = &da1; da_init(da1_pt, sizeof(int), NULL);
+ Da da2; Da *da2_pt = &da2; da_init(da2_pt, sizeof(char *), NULL);
bool flag[6];
//check that da is allocated as array of 4 chars
bool test_da_free_0(){
Da da;
Da *da_pt = &da;
- da_alloc(da_pt, sizeof(int));
+ da_init(da_pt, sizeof(int), NULL);
//store location of da
Da *keep = da_pt;
Da *save = da_pt;
- //FREE da
+ //acc_free da
da_free(da_pt);
//re-allocate memory to dew da of chars
- da_alloc(keep, sizeof(char));
+ da_init(keep, sizeof(char), NULL);
//test that same memory is properly re-allocated
bool flag1 = keep == save;
return flag1 && flag2;
}
-//tests da_empty
-bool test_da_empty_0(){
+//tests da_is_empty
+bool test_da_is_empty_0(){
int i = 6;
Da da;
Da *da_pt = &da;
- da_alloc(da_pt, sizeof(int));
+ da_init(da_pt, sizeof(int), NULL);
while(i < 11){
da_push(da_pt, &i);
++i;
}
- bool flag1 = !da_empty(da_pt);
+ bool flag1 = !da_is_empty(da_pt);
da_rewind(da_pt);
- bool flag2 = da_empty(da_pt);
+ bool flag2 = da_is_empty(da_pt);
da_free(da_pt);
return flag1 && flag2;
}
int i = 1;
Da da;
Da *da_pt = &da;
- da_alloc(da_pt, sizeof(int));
+ da_init(da_pt, sizeof(int), NULL);
//test da_length, even pushing past expansions
bool result = true;
return result;
}
+/*
//------------------------------------------------
// Matrix function tests
//tests da_push_row
bool test_da_push_row_0(){
- Da dama; da_alloc(&dama, sizeof(Da)); Da *damp = &dama;
- Da row0; da_alloc(&row0, sizeof(int)); da_push_row(damp, &row0);
- Da row1; da_alloc(&row1, sizeof(int)); da_push_row(damp, &row1);
- Da row2; da_alloc(&row2, sizeof(int)); da_push_row(damp, &row2);
+ Da dama; da_init(&dama, sizeof(Da), NULL); Da *damp = &dama;
+ Da row0; da_init(&row0, sizeof(int), NULL); da_push_row(damp, &row0);
+ Da row1; da_init(&row1, sizeof(int), NULL); da_push_row(damp, &row1);
+ Da row2; da_init(&row2, sizeof(int), NULL); da_push_row(damp, &row2);
bool flag0 = da_equal(&row0, (Da *)(damp->base));
bool flag1 = da_equal(&row1, (Da *)(damp->base + damp->element_size));
//tests da_erase
bool test_da_erase_0(){
- Da dama; da_alloc(&dama, sizeof(Da)); Da *damp = &dama;
- Da row0; da_alloc(&row0, sizeof(int)); da_push_row(damp, &row0);
- Da row1; da_alloc(&row1, sizeof(int)); da_push_row(damp, &row1);
- Da row2; da_alloc(&row2, sizeof(int)); da_push_row(damp, &row2);
+ Da dama; da_init(&dama, sizeof(Da), NULL); Da *damp = &dama;
+ Da row0; da_init(&row0, sizeof(int), NULL); da_push_row(damp, &row0);
+ Da row1; da_init(&row1, sizeof(int), NULL); da_push_row(damp, &row1);
+ Da row2; da_init(&row2, sizeof(int), NULL); da_push_row(damp, &row2);
//store location of da
Da *keep = damp;
Da *save = damp;
- //FREE da
+ //free da
da_free_elements(damp);
da_erase(damp);
//re-allocate memory to dew da of chars
- da_alloc(keep, sizeof(char));
+ da_init(keep, sizeof(char), NULL);
//test that same memory is properly re-allocated
bool flag1 = keep == save;
//tests da_longer
bool test_da_longer_0(){
- Da dama; Da *damp = &dama; da_alloc(damp, sizeof(Da));
+ Da dama; Da *damp = &dama; da_init(damp, sizeof(Da), NULL);
- Da row0; Da *r0 = &row0; da_alloc(r0, sizeof(int));
+ Da row0; Da *r0 = &row0; da_init(r0, sizeof(int), NULL);
{//fills first row with 4 integers
int i = 10;
while(i<14){
}
da_push_row(damp, r0);
- Da row1; Da *r1 = &row1; da_alloc(r1, sizeof(int));
+ Da row1; Da *r1 = &row1; da_init(r1, sizeof(int), NULL);
{//fills second row with 4 different integers
int i = 20;
while(i<24){
}
da_push_row(damp, r1);
- Da row2; Da *r2 = &row2; da_alloc(r2, sizeof(int));
+ Da row2; Da *r2 = &row2; da_init(r2, sizeof(int), NULL);
{//fills third row with 6 integers
int i = 30;
while(i<36){
//tests da_longest
bool test_da_longest_0(){
- Da dama; Da *damp = &dama; da_alloc(damp, sizeof(Da));
+ Da dama; Da *damp = &dama; da_init(damp, sizeof(Da), NULL);
- Da row0; Da *r0 = &row0; da_alloc(r0, sizeof(int));
+ Da row0; Da *r0 = &row0; da_init(r0, sizeof(int), NULL);
{//fills first row with 4 integers
int i = 10;
while(i<14){
}
da_push_row(damp, r0);
- Da row1; Da *r1 = &row1; da_alloc(r1, sizeof(int));
+ Da row1; Da *r1 = &row1; da_init(r1, sizeof(int), NULL);
{//fills second row with 4 different integers
int i = 20;
while(i<24){
}
da_push_row(damp, r1);
- Da row2; Da *r2 = &row2; da_alloc(r2, sizeof(int));
+ Da row2; Da *r2 = &row2; da_init(r2, sizeof(int), NULL);
{//fills third row with 6 integers
int i = 30;
while(i<36){
da_erase(damp);
return flag;
}
+*/
+
+bool test_da_accounting_0(){
+ AccChannel acc0;
+ AccChannel *acc0_pt = acc_open(&acc0, acc_NULL);
+
+ //Da da0;
+ //Da *dap = &da0;
+ //da_init(dap, sizeof(int), acc0_pt);
+
+ 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);
+ bool result = true;
+ return result;
+}
-/*
+/*need to update list
Functions
--da_alloc
+-da_init
-da_free
-da_rewind
--da_empty
+-da_is_empty
-da_length
-da_rebase
-da_expand
-da_push
-da_pop
da_endq
--da_map
+-da_foreach
da_free_elements
da_ints_print
da_integer_repeats
test_da_strings_exists_0
test_da_rebase_0
test_da_boundq_0
-test_da_map_0
+test_da_foreach_0
test_da_present_0
test_da_exists_0
test_da_exists_1
test_da_all_0
-test_da_alloc_0
+test_da_init_0
test_da_free_0
-test_da_empty_0
+test_da_is_empty_0
test_da_length_0
//matrix