xlib_playground

Xlib playground for experiments.
Log | Files | Refs

commit eadd9855ae9657076e01f3af4f412ffa6310bc85
parent cd859b7cef13a1860575de3de06d534cf2da8268
Author: Matsuda Kenji <info@mtkn.jp>
Date:   Sun, 15 Jan 2023 07:40:31 +0900

Add counters for memory debugging.
counters incremented with malloc and decremented with free
for each structs.
maybe List struct is the source of memory leak.

Diffstat:
Mex9/list.c | 11+++++++++++
Mex9/list.h | 1+
Mex9/main.c | 3+++
Mex9/object.c | 56+++++++++++++++++++++++++++++++++++++++++++++++++-------
Mex9/object.h | 2++
5 files changed, 66 insertions(+), 7 deletions(-)

diff --git a/ex9/list.c b/ex9/list.c @@ -8,6 +8,8 @@ static int __lqsort(List *p, List *tail, int (* cmp)(void *, void *)); static void __lprint(List *p, List *tail, void (* pfnc)(void *)); +static int list_count = 0; + /* * Laddfront() adds item in front of List p. * Returns NULL if error occured. @@ -26,6 +28,7 @@ laddfront(List *p, void *item) fprintf(stderr, "laddfront: Error. Could not allocate memory.\n"); return NULL; } + list_count++; newlp->item = item; newlp->next = p; return newlp; @@ -41,6 +44,7 @@ lfree(List *p) for (q = p; q != NULL; q = p) { p = q->next; free(q); + list_count--; } } @@ -55,9 +59,16 @@ lfreei(List *p, void (* ifree)(void *)) p = q->next; ifree(q->item); free(q); + list_count--; } } +int +get_list_count(void) +{ + return list_count; +} + void lprint(List *p, void (* pfnc)(void *)) { diff --git a/ex9/list.h b/ex9/list.h @@ -6,6 +6,7 @@ typedef struct List { List * laddfront(List *p, void *item); void lfree(List *p); +int get_list_count(void); void lfreei(List *p, void (* ifree)(void *)); void lprint(List *p, void (* pfnc)(void *)); int lbsort(List *p, int (* cmp)(void *, void *)); diff --git a/ex9/main.c b/ex9/main.c @@ -317,6 +317,9 @@ game_play(void) } } lfreei(ol, (void (*)(void *))&free_object); + printf("obj_count: %d\n", get_obj_count()); + printf("lld_count: %d\n", get_lld_count()); + printf("list_count: %d\n", get_list_count()); } void diff --git a/ex9/object.c b/ex9/object.c @@ -5,10 +5,6 @@ #include "list.h" #include "object.h" -static int test_collision_rr(Object *, Object *); -static void rect_next_tick(Object *, long); -static void rect_handle_collision_mf(Object *, Object *); - /* * Pair of two objects with their distance. * Used by lhandle_collision(); @@ -19,12 +15,24 @@ typedef struct LLD { float dist; } LLD; +static int test_collision_rr(Object *, Object *); +static void rect_next_tick(Object *, long); +static void rect_handle_collision_mf(Object *, Object *); +static LLD *create_lld(void); +static void lldfree(LLD *); + +static int obj_count = 0; +static int lld_count = 0; + + Object * create_object(enum object_type t, uint32_t color, float px, float py, float vx, float vy, float ax, float ay, int w, int h, int m, void (*next_tick)(Object *, long)) { + obj_count++; + Object *o; o = (Object *)malloc(sizeof(Object)); o->type = t; @@ -49,9 +57,16 @@ create_object(enum object_type t, uint32_t color, void free_object(Object *o) { + obj_count--; free(o); } +int +get_obj_count(void) +{ + return obj_count; +} + /* * 1 if collide, 0 if not */ @@ -242,6 +257,29 @@ lldprint(LLD *p) printf(", %2.0f}", p->dist); } +static LLD * +create_lld(void) +{ + LLD *p = NULL; + p = (LLD *)malloc(sizeof(LLD)); + + lld_count++; + return p; +} + +static void +lldfree(LLD *p) +{ + lld_count--; + free(p); +} + +int +get_lld_count(void) +{ + return lld_count; +} + /* * The order of list p will be changed. */ @@ -266,8 +304,12 @@ lhandle_collision(List *p) lld = NULL; for (s = stack; s != NULL && s->next != NULL; s = s->next) { for (t = s->next; t != NULL; t = t->next) { - LLD *l; - l = (LLD *)malloc(sizeof(LLD)); + LLD *l = NULL; + if((l = create_lld()) == NULL) { + fprintf(stderr, "lhandle_collision: Error. creating" + "lld.\n"); + exit(1); + } l->o0 = (Object *)s->item; l->o1 = (Object *)t->item; l->dist = object_dist((Object *)s->item, @@ -289,7 +331,7 @@ lhandle_collision(List *p) ((LLD *)tmplld->item)->o1); } } - lfreei(lld, &free); + lfreei(lld, &lldfree); lfree(stack); stack = NULL; } diff --git a/ex9/object.h b/ex9/object.h @@ -64,6 +64,8 @@ Object *create_object(enum object_type, uint32_t, int, int, int, void (*)(Object *, long)); void free_object(Object *); +int get_obj_count(void); +int get_lld_count(void); int is_on_floor(Object *, Object *); int is_on_floor_before(Object *, Object *); void oprint(Object *);