--- /dev/null
+#include <stdlib.h>
+
+
+typedef struct {
+ char *base;
+ char *end; // one byte/one element off the end of the array
+ size_t size; // size >= (end - base) + 1;
+ size_t element_size;
+} Da;
+
+
+Da heap_count = {
+ .element_size = sizeof(void *),
+ .size = 4*sizeof(void *),
+ .base = malloc(4*sizeof(void *)),
+ .end = heap_count.base
+};
+void *da_malloc_counted(size_t mem_size);
+void da_free_counted(void *pt);
+
#include<stdbool.h>
#include<string.h>
+
+//------------------------------------------------------------------
+//FREE and MALLOC
+
+static bool counting = false;
+void da_start_heap_counter(){//assigns properties of heap_counter, must be called before other code to be used
+ heap_count.element_size = sizeof(void *);
+ heap_count.size = 4*sizeof(void *);
+ heap_count.base = malloc(heap_count.size);
+ heap_count.end = heap_count.base;
+ counting = true;
+}
+static char *da_count_expand(Da *dap){//these are redefined with malloc instead of MALLOC becuase da_malloc_counted will not make it past the push once heap_count needs to expand
+ 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;
+}
+static char *da_count_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_count_expand(dap);
+ return dap->base + element_off;
+}
+static char *da_count_push(Da *dap, void *element){
+ char *element_pt = da_count_push_alloc(dap);
+ memcpy(element_pt, element, dap->element_size);
+ return element_pt;
+}
+void *da_malloc_counted(size_t mem_size){//pushed pointer onto heap_count
+ void *temp = malloc(mem_size);
+ if( counting == true ) da_count_push(&heap_count, temp);
+ return (void *)temp;
+}
+void da_free_counted(void *pt){//pops pointer from heap_count
+ if( counting == true ) da_pop(&heap_count, pt);
+ free(pt);
+}
+bool da_result_heap_counter(){//returns false if heap_count is not empty or if it was not being used
+ if ( counting == true ){
+ return heap_count.base == heap_count.end;
+ } else return false;
+}
+
//--------------------------------------------------------------------------------
// allocation
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->base = MALLOC(dap->size);
dap->end = dap->base;
}
void da_free(Da *dap){
- free(dap->base);
+ FREE(dap->base);
dap->size = 0;
}
void da_rewind(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 = MALLOC( new_size );
memcpy( new_base, old_base, end_offset + dap->element_size);
- free(old_base);
+ FREE(old_base);
dap->base = new_base;
dap->end = new_base + end_offset;
dap->size = new_size;
// da being used as a resource manager
-// elements were malloced, now they will all be freed
+// elements were MALLOCed, now they will all be FREEd
static void da_free_element(void *pt, void *closure){
- free(*(char **)pt); // free does not care about the pointer type
+ FREE(*(char **)pt); // FREE does not care about the pointer type
}
void da_free_elements(Da *dap){
*/
void da_erase(Da *damp){//same as da_free, don't tell anyone
- free(damp->base);
+ FREE(damp->base);
damp->size = 0;
}
size_t columns = da_length(da_longest(damp));
size_t j = 0;
while( j < columns ){
- int *col = malloc(sizeof(rows*sizeof(int)));
+ int *col = MALLOC(sizeof(rows*sizeof(int)));
size_t i = 0;
while( i < rows ) {
if (da_endq(dpt,(dpt->base + j*(dpt->element_size))))
int *da_integer_to_raw_image_matrix(Da *damp, int fill){
size_t rows = damp->size / damp->element_size;
size_t columns = da_length(da_longest(damp));
- int *matrix = malloc(sizeof(rows*columns));//[rows][columns]
+ int *matrix = MALLOC(sizeof(rows*columns));//[rows][columns]
int i = 0;
Da *dpt = (Da *)(damp->base);
while( i < rows )
size_t rows = damp->size/damp->element_size;
size_t columns = da_length(da_longest(damp));
int *matrix = da_integer_to_raw_image_matrix(damp, fill);//[rows][columns]
- int *transpose = malloc(sizeof(columns*rows));
+ int *transpose = MALLOC(sizeof(columns*rows));
int i, j;
for(i=0;i<rows;i++)
{
#include <stdlib.h>
#include <stdbool.h>
#include <stdio.h>
+#define MALLOC da_malloc_counted
+#define FREE da_free_counted
-typedef struct Da{
+typedef struct {
char *base;
char *end; // one byte/one element off the end of the array
size_t size; // size >= (end - base) + 1;
#define RETURN(dap, r) \
{ da_free_elements(dap); return r; }
+// assuring we freed everything we allocated
+
+Da heap_count;
+void da_start_heap_counter(void);
+void *da_malloc_counted(size_t mem_size);
+void da_free_counted(void *pt);
+bool da_result_heap_counter(void);
void da_alloc(Da *dap, size_t element_size);
void da_free(Da *dap);
--- /dev/null
+#include <stdlib.h>
+
+
+typedef struct {
+ char *base;
+ char *end; // one byte/one element off the end of the array
+ size_t size; // size >= (end - base) + 1;
+ size_t element_size;
+} Da;
+
+
+Da heap_count = {
+ .element_size = sizeof(void *),
+ .size = 4*sizeof(void *),
+ .base = malloc(4*sizeof(void *)),
+ .end = this.base
+};
+void *da_malloc_counted(size_t mem_size);
+void da_free_counted(void *pt);
+
--- /dev/null
+Hello Emacs
+
+2019-04-27T21:08:59Z
+glendawest045@phoenix§~§
+> cd subu/module/da/
+
+2019-04-27T21:09:12Z
+glendawest045@phoenix§~/subu/module/da§
+> make dist-clean
+/usr/bin/make --no-print-directory -f /home/glendawest045/subu/tool/lib/makefile-cc dist-clean
+for i in tmp/da.lib.o tmp/makefile-cc.deps; do rm $i || true; done
+for i in ; do [ -e $i ] && rm $i || true; done
+rm include/da.h || true
+rm lib/libda.a || true
+
+2019-04-27T21:09:18Z
+glendawest045@phoenix§~/subu/module/da§
+> make dep lib
+if [ -e tmp/makefile-cc.deps ]; then rm tmp/makefile-cc.deps; fi
+/usr/bin/make --no-print-directory -f /home/glendawest045/subu/tool/lib/makefile-cc dep
+C compiler only deps
+deps for C linking
+cp src/da.lib.h include/da.h
+/usr/bin/make --no-print-directory -f /home/glendawest045/subu/tool/lib/makefile-cc lib
+gcc -std=gnu11 -fPIC -Isrc -Iinclude -I/home/glendawest045/subu/module/share/include -ggdb -O0 -Werror -DDEBUG -DDEBUGDB -o tmp/da.lib.o -c src/da.lib.c
+ar rcs lib/libda.a tmp/da.lib.o
+
+2019-04-27T21:09:28Z
+glendawest045@phoenix§~/subu/module/da§
+> cd test/
+
+2019-04-27T21:09:31Z
+glendawest045@phoenix§~/subu/module/da/test§
+> make dep lib exec
+if [ -e tmp/makefile-cc.deps ]; then rm tmp/makefile-cc.deps; fi
+/usr/bin/make --no-print-directory -f /home/glendawest045/subu/tool/lib/makefile-cc dep
+C compiler only deps
+deps for C linking
+/usr/bin/make --no-print-directory -f /home/glendawest045/subu/tool/lib/makefile-cc lib
+gcc -std=gnu11 -fPIC -Iinclude -I../include -ggdb -DDEBUG -DDEBUGDB -o tmp/test_da.lib.o -c src/test_da.lib.c
+ar rcs lib/libtest.a tmp/test_da.lib.o
+/usr/bin/make --no-print-directory -f /home/glendawest045/subu/tool/lib/makefile-cc exec
+make sub_exec
+/usr/bin/make --no-print-directory -f /home/glendawest045/subu/tool/lib/makefile-cc sub_exec
+gcc -std=gnu11 -fPIC -Iinclude -I../include -ggdb -DDEBUG -DDEBUGDB -o tmp/test_da.cli.o -c src/test_da.cli.c
+gcc -o exec/test_da tmp/test_da.cli.o -Llib -L../lib -ltest -lda
+
+2019-04-27T21:09:39Z
+glendawest045@phoenix§~/subu/module/da/test§
+> cd exec/
+
+2019-04-27T21:09:43Z
+glendawest045@phoenix§~/subu/module/da/test/exec§
+> ./test_da
+da_result_heap_counter failed
+failed 1 of 26 tests
+
+2019-04-27T21:09:48Z
+glendawest045@phoenix§~/subu/module/da/test/exec§
+>
\ No newline at end of file
#include <stdio.h>
#include <stdbool.h>
#include "test_da.lib.h"
+#include <da.h>
+
int main(){
// enumeration of tests
typedef bool (*test_fun)();
+ da_start_heap_counter();
test_fun tests[] =
{
test_da_push_0,
test_da_erase_0,
test_da_longer_0,
test_da_longest_0,
+ da_result_heap_counter,
NULL};
char *test_names[] =
{
"test_da_erase_0",
"test_da_longer_0",
"test_da_longest_0",
+ "da_result_heap_counter",
NULL};
// call tests
Da dar;
da_alloc(&dar,sizeof(char));
- char **el_pt = (char **)malloc(4*(sizeof(char*)));
+ char **el_pt = (char **)MALLOC(4*(sizeof(char*)));
{//push 2-5 into dar, leave with pointer to last element
int i = 0;
char arr[4] = {'t','e','s','t'};
return flag1 && flag2 && flag3 && result;
}
-/* {//problems with free and malloc
- char *a = malloc(10);
+/* {//problems with FREE and MALLOC
+ char *a = MALLOC(10);
strcpy(a, "zsdf");
Da da;
Da *da_pt = &da;
da_alloc(da_pt, sizeof(char *));
da_push(da_pt, a);
...
- free(*(char *)da_index(da_pt, 0));
+ FREE(*(char *)da_index(da_pt, 0));
da_free(da_pt);
} */
//tests da_present
bool test_da_present_0(){
int dar_size = 0;
- Da **dar = malloc(3 * sizeof(Da *));
+ Da **dar = MALLOC(3 * sizeof(Da *));
Da dap_0;
Da *dap_0_pt = &dap_0;
Da *keep = da_pt;
Da *save = da_pt;
- //free da
+ //FREE da
da_free(da_pt);
//re-allocate memory to dew da of chars
Da *keep = damp;
Da *save = damp;
- //free da
+ //FREE da
da_erase(damp);
//re-allocate memory to dew da of chars
-#ifndef DA_LIB_H
-#define DA_LIB_H
+#ifndef TEST_DA_LIB_H
+#define TEST_DA_LIB_H
bool test_da_push_0();
bool test_da_expand_0();