From f5c4c049bf8b6b246445a7e27361a16195c0b4ab Mon Sep 17 00:00:00 2001
From: Christian C <cc@localhost>
Date: Wed, 2 Apr 2025 18:19:43 -0700
Subject: Remove temporary allocator

---
 include/lib/mem/galloc.h | 16 --------------
 lib/algo/avl_tree.c      |  9 ++++----
 lib/data/image_types.c   | 57 ++++++++++++++++++++++++------------------------
 lib/dir.c                | 13 +++++------
 lib/mem/galloc.c         | 46 --------------------------------------
 lib/seg/mask_data.c      |  5 ++---
 lib/seg/util.c           | 35 +++++++++++++++--------------
 src/prog.c               | 20 ++++++++---------
 test/lib/dir.c           |  2 +-
 9 files changed, 67 insertions(+), 136 deletions(-)
 delete mode 100644 include/lib/mem/galloc.h
 delete mode 100644 lib/mem/galloc.c

diff --git a/include/lib/mem/galloc.h b/include/lib/mem/galloc.h
deleted file mode 100644
index 763b21a..0000000
--- a/include/lib/mem/galloc.h
+++ /dev/null
@@ -1,16 +0,0 @@
-#ifndef INC_LIB_MEM_GALLOC_H
-#define INC_LIB_MEM_GALLOC_H
-
-#include <sys/types.h>
-
-#define g_malloc(size) _g_malloc(size, __FILE__, __LINE__)
-#define g_calloc(n_memb, size) _g_calloc(n_memb, size, __FILE__, __LINE__)
-#define g_realloc(ptr, size) _g_realloc(ptr, size, __FILE__, __LINE__)
-#define g_free(ptr) _g_free(ptr, __FILE__, __LINE__)
-void *_g_malloc(size_t size, char* file, unsigned int line);
-void *_g_calloc(size_t n_memb, size_t size, char* file, unsigned int line);
-void *_g_realloc(void *ptr, size_t size, char* file, unsigned int line);
-void _g_free(void *ptr, char* file, unsigned int line);
-ssize_t g_outstanding_allocations();
-
-#endif
diff --git a/lib/algo/avl_tree.c b/lib/algo/avl_tree.c
index d8f887c..cc06254 100644
--- a/lib/algo/avl_tree.c
+++ b/lib/algo/avl_tree.c
@@ -1,5 +1,4 @@
 #include <lib/algo/avl_tree.h>
-#include <lib/mem/galloc.h>
 #include <lib/monad.h>
 
 #include <stddef.h>
@@ -56,7 +55,7 @@ AVLNode *left_rotate(AVLNode *parent) {
 
 // Create AVL node
 AVLNode *create_avl_node(void *data, AvlComparator compare) {
-  AVLNode *node = (AVLNode *)g_malloc(sizeof(AVLNode));
+  AVLNode *node = (AVLNode *)malloc(sizeof(AVLNode));
   if (node == NULL) {
     return NULL;
   }
@@ -135,7 +134,7 @@ void free_avl_tree(AVLNode *root) {
   if (root != NULL) {
     free_avl_tree(root->left);
     free_avl_tree(root->right);
-    g_free(root);
+    free(root);
   }
 }
 
@@ -145,8 +144,8 @@ void free_avl_tree_nodes(AVLNode *root) {
     free_avl_tree_nodes(root->left);
     free_avl_tree_nodes(root->right);
     if (root->data != NULL) {
-      g_free(root->data);
+      free(root->data);
     }
-    g_free(root);
+    free(root);
   }
 }
diff --git a/lib/data/image_types.c b/lib/data/image_types.c
index 6e8f6b4..3703f07 100644
--- a/lib/data/image_types.c
+++ b/lib/data/image_types.c
@@ -1,40 +1,39 @@
 #include <lib/data/image_types.h>
-#include <lib/mem/galloc.h>
 #include <stdio.h>
 #include <stdlib.h>
 
 Image *create_image(size_t width, size_t height, size_t channels) {
-  Image *ip = (Image *)g_malloc(sizeof(Image));
+  Image *ip = (Image *)malloc(sizeof(Image));
   if (ip == NULL) {
     return NULL;
   }
   ip->width = width;
   ip->height = height;
   ip->depth = channels;
-  ip->image = (ImageData_t ***)g_malloc(sizeof(ImageData_t **) * ip->height);
+  ip->image = (ImageData_t ***)malloc(sizeof(ImageData_t **) * ip->height);
   if (ip->image == NULL) {
     fprintf(stderr, "Memory allocation error\n");
-    g_free(ip);
+    free(ip);
     return NULL;
   }
   ImageData_t *image_data =
-      g_calloc(width * height * channels, sizeof(ImageData_t));
+      calloc(width * height * channels, sizeof(ImageData_t));
   if (image_data == NULL) {
     fprintf(stderr, "Memory allocation error\n");
-    g_free(ip->image);
-    g_free(ip);
+    free(ip->image);
+    free(ip);
     return NULL;
   }
   for (size_t y = 0; y < height; y++) {
-    ip->image[y] = (ImageData_t **)g_malloc(sizeof(ImageData_t *) * ip->width);
+    ip->image[y] = (ImageData_t **)malloc(sizeof(ImageData_t *) * ip->width);
     if (ip->image[y] == NULL) {
       fprintf(stderr, "Memory allocation error\n");
       for (size_t ty = 0; ty < y; ty++) {
-        g_free(ip->image[ty]);
+        free(ip->image[ty]);
       }
-      g_free(image_data);
-      g_free(ip->image);
-      g_free(ip);
+      free(image_data);
+      free(ip->image);
+      free(ip);
     }
     for (size_t x = 0; x < width; x++) {
       ip->image[y][x] = &image_data[(y * width + x) * channels];
@@ -44,21 +43,21 @@ Image *create_image(size_t width, size_t height, size_t channels) {
 }
 
 Mask *create_image_mask(size_t width, size_t height) {
-  Mask *ip = (Mask *)g_malloc(sizeof(Mask));
+  Mask *ip = (Mask *)malloc(sizeof(Mask));
   if (ip == NULL) {
     return NULL;
   }
   ip->width = width;
   ip->height = height;
-  ip->image = (MaskData_t **)g_malloc(sizeof(MaskData_t *) * ip->height);
+  ip->image = (MaskData_t **)malloc(sizeof(MaskData_t *) * ip->height);
   if (ip->image == NULL) {
-    g_free(ip);
+    free(ip);
     return NULL;
   }
-  MaskData_t *image_data = g_calloc(width * height, sizeof(MaskData_t));
+  MaskData_t *image_data = calloc(width * height, sizeof(MaskData_t));
   if (image_data == NULL) {
-    g_free(ip->image);
-    g_free(ip);
+    free(ip->image);
+    free(ip);
     return NULL;
   }
   for (size_t y = 0; y < height; y++) {
@@ -72,26 +71,26 @@ void free_image(Image *image) {
     return;
   }
   if (image->image == NULL) {
-    g_free(image);
+    free(image);
     return;
   }
   if (image->image[0] != NULL) {
     if (image->image[0][0] != NULL) {
-      g_free(image->image[0][0]);
+      free(image->image[0][0]);
     }
   } else {
-    g_free(image->image);
-    g_free(image);
+    free(image->image);
+    free(image);
     return;
   }
   for (size_t y = 0; y < image->height; y++) {
     if (image->image[y] != NULL) {
-      g_free(image->image[y]);
+      free(image->image[y]);
     }
   }
-  g_free(image->image);
+  free(image->image);
   image->image = NULL;
-  g_free(image);
+  free(image);
 }
 
 void free_image_mask(Mask *image_mask) {
@@ -99,14 +98,14 @@ void free_image_mask(Mask *image_mask) {
     return;
   }
   if (image_mask->image == NULL) {
-    g_free(image_mask);
+    free(image_mask);
     return;
   }
   if (image_mask->image[0] != NULL) {
-    g_free(image_mask->image[0]);
+    free(image_mask->image[0]);
     image_mask->image[0] = NULL;
   }
-  g_free(image_mask->image);
+  free(image_mask->image);
   image_mask->image = NULL;
-  g_free(image_mask);
+  free(image_mask);
 }
diff --git a/lib/dir.c b/lib/dir.c
index f0776ca..dfc615b 100644
--- a/lib/dir.c
+++ b/lib/dir.c
@@ -1,6 +1,5 @@
 #include <dirent.h>
 #include <lib/dir.h>
-#include <lib/mem/galloc.h>
 #include <stdlib.h>
 #include <string.h>
 #include <sys/stat.h>
@@ -21,7 +20,7 @@ char **list_directory(char *dirname) {
   DIR *d;
   struct dirent *dir;
   d = opendir(dirname);
-  char **file_names = (char **)g_malloc(sizeof(char *));
+  char **file_names = (char **)malloc(sizeof(char *));
   if (file_names == NULL) {
     return NULL;
   }
@@ -33,20 +32,20 @@ char **list_directory(char *dirname) {
         // When a regular file is reached
         /// Create space for it in the list
         char **temp =
-            g_realloc(file_names, (file_count + 1 + 1) * sizeof(char *));
+            realloc(file_names, (file_count + 1 + 1) * sizeof(char *));
         if (temp == NULL) {
           for (size_t file_idx = 0; file_idx < file_count; file_idx++) {
-	    g_free(file_names[file_idx]);
+	    free(file_names[file_idx]);
           }
 	  return NULL;
         }
         file_names = temp;
         /// Create space for the name
         file_names[file_count] =
-            g_calloc(strlen(dir->d_name) + 1, sizeof(char));
+            calloc(strlen(dir->d_name) + 1, sizeof(char));
         if (file_names[file_count] == NULL) {
           for (size_t file_idx = 0; file_idx < file_count; file_idx++) {
-	    g_free(file_names[file_idx]);
+	    free(file_names[file_idx]);
           }
 	  return NULL;
         }
@@ -66,7 +65,7 @@ char *full_path(char *dir, char *file) {
   char *fpath = NULL;
   size_t dir_len = strlen(dir);
   size_t file_len = strlen(file);
-  fpath = (char *)g_calloc(dir_len + file_len + 2, sizeof(char));
+  fpath = (char *)calloc(dir_len + file_len + 2, sizeof(char));
   if (fpath == NULL) {
     return NULL;
   }
diff --git a/lib/mem/galloc.c b/lib/mem/galloc.c
deleted file mode 100644
index ea4ea16..0000000
--- a/lib/mem/galloc.c
+++ /dev/null
@@ -1,46 +0,0 @@
- #include <lib/mem/galloc.h>
-#include <stdlib.h>
-#include <stdio.h>
-
-static ssize_t standing_allocations = 0;
-
-void *_g_malloc(size_t size, char* file, unsigned int line) {
-  void *ptr = malloc(size);
-  if (ptr == NULL) {
-    return ptr;
-  }
-  fprintf(stderr, "+%p :a: %s:%d\n", ptr, file, line);
-  standing_allocations++;
-  return ptr;
-}
-
-void *_g_calloc(size_t n_memb, size_t size, char* file, unsigned int line) {
-  void *ptr = calloc(n_memb, size);
-  if (ptr == NULL) {
-    return ptr;
-  }
-  fprintf(stderr, "+%p :c: %s:%d\n", ptr, file, line);
-  standing_allocations++;
-  return ptr;
-}
-
-void *_g_realloc(void *ptr, size_t size, char* file, unsigned int line) {
-  fprintf(stderr, "-%p :r: %s:%d\n", ptr, file, line);
-  void* temp = realloc(ptr, size);
-  if (temp == NULL) {
-    fprintf(stderr, "+%p :r: %s:%d\n", ptr, file, line);
-  } else {
-    fprintf(stderr, "+%p :r: %s:%d\n", temp, file, line);
-  }
-  return temp;
-}
-
-void _g_free(void *ptr, char* file, unsigned int line) {
-  if (ptr != NULL) {
-    fprintf(stderr, "-%p :f: %s:%d\n", ptr, file, line);
-    free(ptr);
-    standing_allocations--;
-  }
-}
-
-ssize_t g_outstanding_allocations() { return standing_allocations; }
diff --git a/lib/seg/mask_data.c b/lib/seg/mask_data.c
index 5789f08..144c7ca 100644
--- a/lib/seg/mask_data.c
+++ b/lib/seg/mask_data.c
@@ -1,4 +1,3 @@
-#include <lib/mem/galloc.h>
 #include <lib/seg/mask_data.h>
 #include <lib/seg/util.h>
 
@@ -6,7 +5,7 @@
 
 // Allocate Mask Data for Label
 MaskData *create_mask_data(MaskData_t label) {
-  MaskData *data = (MaskData *)g_malloc(sizeof(MaskData));
+  MaskData *data = (MaskData *)malloc(sizeof(MaskData));
   data->label = label;
   data->area = 0;
   data->perimeter = 0;
@@ -34,7 +33,7 @@ AVLNode *insert_mask_alloc(AVLNode *node, MaskData_t label) {
   MaskData *data = create_mask_data(label);
   Result result = insert_mask(node, data);
   if (!result.success) {
-    g_free(data);
+    free(data);
   }
   return (AVLNode *)result.data;
 }
diff --git a/lib/seg/util.c b/lib/seg/util.c
index a9db067..8af8199 100644
--- a/lib/seg/util.c
+++ b/lib/seg/util.c
@@ -1,6 +1,5 @@
 #include <assert.h>
 #include <lib/algo/flood_fill.h>
-#include <lib/mem/galloc.h>
 #include <lib/png.h>
 #include <lib/seg/util.h>
 #include <stdio.h>
@@ -81,8 +80,8 @@ MaskData_t *_dilate(const Mask *mask) {
     }
   }
   MaskData_t *ret_mask = new_mask->image[0];
-  g_free(new_mask->image);
-  g_free(new_mask);
+  free(new_mask->image);
+  free(new_mask);
   return ret_mask;
 }
 
@@ -90,7 +89,7 @@ MaskData_t *_dilate(const Mask *mask) {
 void dilate(Mask *mask) {
   MaskData_t *new_mask = _dilate(mask);
   if (new_mask != NULL) {
-    g_free(mask->image[0]);
+    free(mask->image[0]);
     for (size_t y = 0; y < mask->height; y++) {
       mask->image[y] = &(new_mask[y * mask->width]);
     }
@@ -133,8 +132,8 @@ MaskData_t *_erode(const Mask *mask) {
     }
   }
   MaskData_t *ret_mask = new_mask->image[0];
-  g_free(new_mask->image);
-  g_free(new_mask);
+  free(new_mask->image);
+  free(new_mask);
   return ret_mask;
 }
 
@@ -142,7 +141,7 @@ MaskData_t *_erode(const Mask *mask) {
 void erode(Mask *mask) {
   MaskData_t *new_mask = _erode(mask);
   if (new_mask != NULL) {
-    g_free(mask->image[0]);
+    free(mask->image[0]);
     for (size_t y = 0; y < mask->height; y++) {
       mask->image[y] = &(new_mask[y * mask->width]);
     }
@@ -170,8 +169,8 @@ MaskData_t *_closeup(Mask *mask, size_t num_pixels) {
     }
   }
   MaskData_t *ret_mask = new_mask->image[0];
-  g_free(new_mask->image);
-  g_free(new_mask);
+  free(new_mask->image);
+  free(new_mask);
   return ret_mask;
 }
 
@@ -180,7 +179,7 @@ MaskData_t *_closeup(Mask *mask, size_t num_pixels) {
 void closeup(Mask *mask, size_t num_pixels) {
   MaskData_t *new_mask = _closeup(mask, num_pixels);
   if (new_mask != NULL) {
-    g_free(mask->image[0]);
+    free(mask->image[0]);
     for (size_t y = 0; y < mask->height; y++) {
       mask->image[y] = &(new_mask[y * mask->width]);
     }
@@ -228,7 +227,7 @@ Mask *tif_to_labels(char *tif_file_name, MaskData_t *starting_label_p) {
   channels = (STRIP_LENGTH * STRIP_COUNT) / (width * height);
 
   //-TIFF-LOAD-DATA--------------------------------
-  void *buffer = g_malloc(STRIP_LENGTH * sizeof(ImageData_t));
+  void *buffer = malloc(STRIP_LENGTH * sizeof(ImageData_t));
   if (buffer == NULL) {
     fprintf(stderr, "Memory allocation error\n");
     TIFFClose(tif);
@@ -237,7 +236,7 @@ Mask *tif_to_labels(char *tif_file_name, MaskData_t *starting_label_p) {
   Image *image = create_image(width, height, channels);
   if (image == NULL) {
     fprintf(stderr, "Memory allocation error\n");
-    g_free(buffer);
+    free(buffer);
     TIFFClose(tif);
     return NULL;
   }
@@ -248,7 +247,7 @@ Mask *tif_to_labels(char *tif_file_name, MaskData_t *starting_label_p) {
       image->image[0][0][x + y * STRIP_LENGTH] = ((ImageData_t *)buffer)[x];
     }
   }
-  g_free(buffer);
+  free(buffer);
 
   //-FLOOD-FILL-SEGMENTATION-----------------------
   //-CONTIGUOUS-REGION-FINDING---------------------
@@ -280,13 +279,13 @@ Bitmap *image_mask_data_to_bitmap(const Mask *mask) {
   MaskData_t *buffer = mask->image[0];
   uint32_t width = mask->width;
   uint32_t height = mask->height;
-  Pixel *out_buffer = (Pixel *)g_calloc(width * height, sizeof(Pixel));
+  Pixel *out_buffer = (Pixel *)calloc(width * height, sizeof(Pixel));
   if (out_buffer == NULL) {
     return NULL;
   }
-  Bitmap *bitmap = (Bitmap *)g_malloc(sizeof(Bitmap));
+  Bitmap *bitmap = (Bitmap *)malloc(sizeof(Bitmap));
   if (bitmap == NULL) {
-    g_free(out_buffer);
+    free(out_buffer);
     return NULL;
   }
   for (size_t y = 0; y < height; y++) {
@@ -312,7 +311,7 @@ MaskData_t *_reduce_contiguous_regions(MaskData_t *masks, uint32_t width,
                                        MaskData_t *total_labels) {
   MaskData_t starting_label = 1;
   MaskData_t *new_masks =
-      (MaskData_t *)g_calloc(width * height, sizeof(MaskData_t));
+      (MaskData_t *)calloc(width * height, sizeof(MaskData_t));
   if (new_masks == NULL) {
     return NULL;
   }
@@ -342,7 +341,7 @@ void reduce_contiguous_regions(Mask *mask, MaskData_t *total_labels) {
   MaskData_t *new_masks = _reduce_contiguous_regions(
       mask->image[0], mask->width, mask->height, total_labels);
   if (new_masks != NULL) {
-    g_free(mask->image[0]);
+    free(mask->image[0]);
     for (size_t y = 0; y < mask->height; y++) {
       mask->image[y] = &new_masks[y * mask->width];
     }
diff --git a/src/prog.c b/src/prog.c
index 6999a32..a676034 100644
--- a/src/prog.c
+++ b/src/prog.c
@@ -9,7 +9,6 @@
 #include <lib/dir.h>
 #include <lib/file.h>
 #include <lib/lib.h>
-#include <lib/mem/galloc.h>
 #include <lib/monad.h>
 #include <lib/png.h>
 #include <lib/seg/mask_data.h>
@@ -98,7 +97,7 @@ int main(int arg_count, char **arg_value) {
           char *fname = file_list[index];
           if (is_tif_ext(fname) == FALSE) {
 	    fprintf(stderr, "Allocation error?\n");
-            g_free(file_list[index]);
+            free(file_list[index]);
             continue;
           }
           // If we have a tiff file
@@ -109,7 +108,7 @@ int main(int arg_count, char **arg_value) {
           char *fpath = full_path(process_directory, fname);
           if (fpath == NULL) {
 	    fprintf(stderr, "Allocation error?\n");
-            g_free(file_list[index]);
+            free(file_list[index]);
             continue;
           }
           if (!suppress_outputs_b) {
@@ -121,8 +120,8 @@ int main(int arg_count, char **arg_value) {
           Mask *file_im = tif_to_labels(fpath, &starting_label);
           if (file_im == NULL) {
 	    fprintf(stderr, "Allocation error?\n");
-            g_free(fpath);
-            g_free(file_list[index]);
+            free(fpath);
+            free(file_list[index]);
             continue;
           }
           //-----------------------------------------------
@@ -130,10 +129,10 @@ int main(int arg_count, char **arg_value) {
           //-----------------------------------------------
           masks_im = combine_masks(masks_im, file_im);
           free_image_mask(file_im);
-          g_free(fpath);
-          g_free(file_list[index]);
+          free(fpath);
+          free(file_list[index]);
         }
-        g_free(file_list);
+        free(file_list);
       }
     }
   }
@@ -209,8 +208,8 @@ int main(int arg_count, char **arg_value) {
     Bitmap *bitmap = image_mask_data_to_bitmap(masks_im);
     if (bitmap != NULL) {
       save_png(bitmap, png_output_file_fullpath);
-      g_free(bitmap->image_buffer);
-      g_free(bitmap);
+      free(bitmap->image_buffer);
+      free(bitmap);
     }
     write_array(bin_output_file_fullpath, masks_im->image[0],
                 width * height * sizeof(MaskData_t));
@@ -220,6 +219,5 @@ int main(int arg_count, char **arg_value) {
   if (!suppress_outputs_b) {
     printf("Finished in %f ms\n", 1000 * diff_time(&ts_g_end, &ts_g_start));
   }
-  printf("Outstanding allocations: %zu\n", g_outstanding_allocations());
   return 0;
 }
diff --git a/test/lib/dir.c b/test/lib/dir.c
index 121540e..85ef400 100644
--- a/test/lib/dir.c
+++ b/test/lib/dir.c
@@ -28,7 +28,7 @@ void _TEST_is_directory(bool_t *result, TestCount_t *test_count,
 bool_t test_full_path(char *dirname, char *file, char *result) {
   char *fpath = full_path(dirname, file);
   bool_t cmp_result = strcmp(result, fpath);
-  g_free(fpath);
+  free(fpath);
   if (cmp_result == 0) {
     return TRUE;
   }
-- 
cgit v1.2.1