pcb 4.1.1
An interactive printed circuit board layout editor.

free_atexit.c

Go to the documentation of this file.
00001 
00031 #include <stdlib.h>
00032 #include <string.h>
00033 
00038 typedef unsigned int leaky_idx_t;
00039 
00040 
00053 typedef union {
00054   leaky_idx_t idx;
00055   void *ptr;
00056 } leaky_admin_t;
00057 
00058 static void         **free_list = NULL;
00059 static leaky_idx_t  free_size = 0;
00060 
00061 
00066 void *leaky_malloc (size_t size)
00067 {
00068   void *new_memory = malloc(size + sizeof(leaky_admin_t));
00069 
00070   free_list = (void **)realloc (free_list, (free_size + 1) * sizeof(void *));
00071   free_list[free_size] = new_memory;
00072   *(leaky_idx_t *)new_memory = free_size;
00073 
00074   free_size++;
00075   return new_memory + sizeof(leaky_admin_t);
00076 }
00077 
00081 void *leaky_calloc (size_t nmemb, size_t size)
00082 {
00083   size_t size_ = size * nmemb;
00084   void *new_memory = leaky_malloc (size_);
00085 
00086   memset (new_memory, 0, size_);
00087   return new_memory;
00088 }
00089 
00094 void *leaky_realloc (void* old_memory, size_t size)
00095 {
00096   void *new_memory;
00097   leaky_idx_t i;
00098 
00099   if (old_memory == NULL)
00100     return leaky_malloc (size);
00101 
00102   old_memory -= sizeof(leaky_admin_t);
00103 
00104   i = *(leaky_idx_t *)old_memory;
00105 
00106   new_memory = realloc (old_memory, size + sizeof(leaky_admin_t));
00107   free_list[i] = new_memory;
00108 
00109   return new_memory + sizeof(leaky_admin_t);
00110 }
00111 
00115 char *
00116 leaky_strdup (const char *src)
00117 {
00118   int len = strlen (src)+1;
00119   char *res = leaky_malloc (len);
00120   memcpy (res, src, len);
00121   return res;
00122 }
00123 
00127 void leaky_uninit (void)
00128 {
00129   int i;
00130 
00131   for (i = 0; i < free_size; i++)
00132     free (free_list[i]);
00133 
00134   free (free_list);
00135   free_size = 0;
00136 }
00137 
00143 void leaky_init (void)
00144 {
00145   atexit(leaky_uninit);
00146 }