debugging
authorglenrendes <glenda@reasoningtechnology.com>
Fri, 18 Oct 2019 20:37:21 +0000 (22:37 +0200)
committerglenrendes <glenda@reasoningtechnology.com>
Fri, 18 Oct 2019 20:37:21 +0000 (22:37 +0200)
14 files changed:
module/da/include/acc.h
module/da/include/da.h [new file with mode: 0644]
module/da/lib/libda.a [new file with mode: 0644]
module/da/src/acc.lib.c
module/da/src/acc.lib.h
module/da/src/acc.lib.o [new file with mode: 0644]
module/da/src/da.lib.c
module/da/src/da.lib.o [new file with mode: 0644]
module/da/test/exec/test_da [new file with mode: 0755]
module/da/test/lib/libtest.a [new file with mode: 0644]
module/da/test/src/test_da.cli.c
module/da/test/src/test_da.lib.c
module/da/test/src/test_da.lib.h
module/da/test/src/test_da.lib.h.gch [new file with mode: 0644]

index 5369191..cd90111 100644 (file)
@@ -1,13 +1,18 @@
 #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{
@@ -16,9 +21,6 @@ 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
diff --git a/module/da/include/da.h b/module/da/include/da.h
new file mode 100644 (file)
index 0000000..3611064
--- /dev/null
@@ -0,0 +1,73 @@
+#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
+
diff --git a/module/da/lib/libda.a b/module/da/lib/libda.a
new file mode 100644 (file)
index 0000000..7b3adea
Binary files /dev/null and b/module/da/lib/libda.a differ
index 5e956c2..d98f2f9 100644 (file)
@@ -2,13 +2,11 @@
 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
 
@@ -16,32 +14,11 @@ dynamic memory accounting
 //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);
@@ -53,7 +30,7 @@ void acc_free(void *pt, AccChannel *channel){
     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;
       } 
@@ -79,22 +56,18 @@ 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( 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 ){
@@ -114,7 +87,7 @@ AccChannel *acc_report(AccChannel *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)) ){
+    if( false ){
       printf("This channel is in balance.\n");
     }
     else{
@@ -125,7 +98,7 @@ AccChannel *acc_report(AccChannel *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)) ){
+    if( true/*da_is_empty((Da *)(channel->outstanding_malloc)) && da_is_empty((Da *)(channel->spurious_free)) */){
       printf("There are no open channels.\n");
     }
     else {
@@ -138,12 +111,7 @@ AccChannel *acc_report(AccChannel *channel){
 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){}
index 5369191..cd90111 100644 (file)
@@ -1,13 +1,18 @@
 #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{
@@ -16,9 +21,6 @@ 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
diff --git a/module/da/src/acc.lib.o b/module/da/src/acc.lib.o
new file mode 100644 (file)
index 0000000..c3a1382
Binary files /dev/null and b/module/da/src/acc.lib.o differ
index 768a0a8..34d6b68 100644 (file)
@@ -152,10 +152,10 @@ static bool da_quantifier(bool complement, Da *dap, bool pred(void *, void*), vo
 }
 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
 
 
 
@@ -169,7 +169,7 @@ bool da_all(Da *dap, bool pred(void *, void*), void *closure){
 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; }
diff --git a/module/da/src/da.lib.o b/module/da/src/da.lib.o
new file mode 100644 (file)
index 0000000..8031e38
Binary files /dev/null and b/module/da/src/da.lib.o differ
diff --git a/module/da/test/exec/test_da b/module/da/test/exec/test_da
new file mode 100755 (executable)
index 0000000..da6f030
Binary files /dev/null and b/module/da/test/exec/test_da differ
diff --git a/module/da/test/lib/libtest.a b/module/da/test/lib/libtest.a
new file mode 100644 (file)
index 0000000..c1955ef
Binary files /dev/null and b/module/da/test/lib/libtest.a differ
index 1b87cae..4b14d8e 100644 (file)
@@ -5,10 +5,10 @@
 #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[] =
@@ -29,7 +29,7 @@ int main(){
       //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,
@@ -87,7 +87,7 @@ int main(){
   tfp++;
   tnp++;
   }
-  acc_report(&acc_live_channels);
+  //acc_report(&acc_live_channels);
   // summarize results
   if( passed == 0 && failed == 0)
     printf("no tests ran\n");
index 453fb45..8a41111 100644 (file)
@@ -14,6 +14,8 @@ Tests for Da.
 
 // 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;
@@ -37,13 +39,15 @@ bool test_da_push_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
@@ -69,7 +73,7 @@ bool test_da_expand_0(){
   pt++;
   }
   bool flag3 = da_endq(&da, pt);
-  da_free(&da);
+  da_free(dap);
   return flag0 && flag1 && flag2 && flag3;
 }
 
@@ -602,7 +606,7 @@ bool test_da_exists_1(){//tests that expansion doesn't change results
 }
 
 
-
+/*
 //tests da_all
 bool test_da_all_0(){
   Da dar;
@@ -628,7 +632,7 @@ bool test_da_all_0(){
   
   return flag1 && flag2;
 }
-
+*/
 
 
 //tests da_init
@@ -688,6 +692,7 @@ bool test_da_is_empty_0(){
   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;
@@ -696,7 +701,7 @@ bool test_da_is_empty_0(){
   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
@@ -874,20 +879,19 @@ bool test_da_longest_0(){
 
 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;
 }
index 958dd38..9872c4f 100644 (file)
@@ -17,7 +17,7 @@ bool test_da_foreach_0();
 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();
diff --git a/module/da/test/src/test_da.lib.h.gch b/module/da/test/src/test_da.lib.h.gch
new file mode 100644 (file)
index 0000000..f5782f9
Binary files /dev/null and b/module/da/test/src/test_da.lib.h.gch differ