Updating built in Io code to use += instead of x = x + y
[io/quag.git] / libs / basekit / source / Common.c
blobb68853f75dcfa514e9cc8c70918d4cee4bac123d
1 /*#io
2 docCopyright("Steve Dekorte", 2002)
3 docLicense("BSD revised")
4 */
6 #include "Common.h"
7 #include <stdio.h>
9 #ifdef IO_CHECK_ALLOC
11 static long allocs = 0;
12 static long reallocs = 0;
13 static long allocatedBytes = 0;
14 static long maxAllocatedBytes = 0;
15 static long frees = 0;
18 typedef struct
20 long allocs = 0;
21 long reallocs = 0;
22 long allocatedBytes = 0;
23 long maxAllocatedBytes = 0;
24 long frees = 0;
25 } Allocator;
27 Allocator *Allocator_new(void)
29 Allocator *self = calloc(1, sizeof(Allocator));
30 return self;
33 void Allocator_free(Allocator *self)
35 free(self);
38 size_t Allocator_allocs(Allocator *self)
40 return self->allocs;
43 size_t Allocator_frees(Allocator *self)
45 return self->frees;
48 size_t Allocator_allocatedBytes(Allocator *self)
50 return self->allocatedBytes;
53 size_t Allocator_maxAllocatedBytes(Allocator *self)
55 return self->maxAllocatedBytes;
58 void Allocator_resetMaxAllocatedBytes(Allocator *self)
60 self->maxAllocatedBytes = self->allocatedBytes;
63 void Allocator_show(Allocator *self)
65 printf("allocs %i\n", self->allocs);
66 printf("reallocs %i\n", self->reallocs);
67 printf("frees %i\n", self->frees);
68 printf("allocsMinusfrees %i\n", self->allocs - self->frees);
69 printf("allocatedBytes %i\n", self->allocatedBytes);
70 printf("maxAllocatedBytes %i\n", self->maxAllocatedBytes);
71 //printf("allocs %i bytes %i\n", self->allocs, self->allocatedBytes);
74 static Allocator *_globalAllocator;
76 Allocator *globalAllocator(void)
78 if(!_globalAllocator) Allocator_new();
79 return _globalAllocator;
83 // -------------------------------------------------------
85 typedef struct MemoryBlock MemoryBlock;
87 struct MemoryBlock
89 size_t size;
90 size_t allocNum;
91 char *file;
92 int line;
93 MemoryBlock *next;
94 MemoryBlock *prev;
95 char padding[40 - (sizeof(size_t) + sizeof(size_t) + sizeof(char *) + sizeof(int) + sizeof(void *) + sizeof(void *))];
98 MemoryBlock *PtrToMemoryBlock(void *ptr)
100 return (MemoryBlock *)(((char *)ptr) - sizeof(MemoryBlock));
103 void *MemoryBlockToPtr(MemoryBlock *self)
105 return (void *)(((char *)self) + sizeof(MemoryBlock));
108 static MemoryBlock *_baseblock = NULL;
110 inline MemoryBlock *baseblock(void)
112 if(!_baseblock) _baseblock = calloc(1, sizeof(MemoryBlock));
113 return _baseblock;
116 void MemoryBlock_remove(MemoryBlock *self)
118 if (self->next) self->next->prev = self->prev;
119 if (self->prev) self->prev->next = self->next;
122 void MemoryBlock_insertAfter_(MemoryBlock *self, MemoryBlock *other)
124 self->next = other->next;
125 self->prev = other;
126 other->next = self;
127 if (self->next) self->next->prev = self;
130 MemoryBlock *MemoryBlock_newWithSize_file_line_(size_t size, char *file, int line)
132 MemoryBlock *self = calloc(1, sizeof(MemoryBlock) + size);
133 self->size = size;
134 self->allocNum = allocs;
135 self->file = file;
136 self->line = line;
137 MemoryBlock_insertAfter_(self, baseblock());
139 allocs ++;
140 allocatedBytes += size;
141 if (allocatedBytes > maxAllocatedBytes) maxAllocatedBytes = allocatedBytes;
142 return self;
145 MemoryBlock *MemoryBlock_reallocToSize_(MemoryBlock *self, size_t size)
147 MemoryBlock *prev = self->prev;
148 MemoryBlock_remove(self);
149 allocatedBytes -= self->size;
150 allocatedBytes += size;
151 reallocs ++;
152 self = realloc(self, sizeof(MemoryBlock) + size);
153 self->size = size;
154 MemoryBlock_insertAfter_(self, prev);
155 return self;
158 void MemoryBlock_free(MemoryBlock *self)
160 MemoryBlock_remove(self);
161 allocatedBytes -= self->size;
162 frees ++;
163 free(self);
166 size_t MemoryBlock_size(MemoryBlock *self)
168 return self->size;
171 void MemoryBlock_show(MemoryBlock *self)
173 char *file = strrchr(self->file, '/');
174 file = file ? file + 1 : self->file;
175 //printf(" MemoryBlock %p:\n", (void *)self);
176 //printf("\tsize %i\n", self->size);
177 //printf("\tfile %s\n", file);
178 //printf("\tline %i\n", self->line);
179 printf("\t%i %p %s:%i\t\t%i bytes\n", self->allocNum, MemoryBlockToPtr(self), file, self->line, self->size);
182 // ----------------------------------------------------------------------------
184 void *io_real_malloc(size_t size, char *file, int line)
186 MemoryBlock *m = MemoryBlock_newWithSize_file_line_(size, file, line);
187 return MemoryBlockToPtr(m);
190 void *io_real_calloc(size_t count, size_t size, char *file, int line)
192 return io_real_malloc(count * size, file, line);
195 void *io_real_realloc(void *ptr, size_t size, char *file, int line)
197 if (ptr)
199 MemoryBlock *m = MemoryBlock_reallocToSize_(PtrToMemoryBlock(ptr), size);
200 return MemoryBlockToPtr(m);
203 return io_real_malloc(size, file, line);
206 void io_free(void *ptr)
208 MemoryBlock_free(PtrToMemoryBlock(ptr));
211 // --------------------------------------------------------------------------
213 void io_show_mem(char *s)
215 printf("\n--- %s ---\n", s ? s : "");
216 printf("allocs %i\n", allocs);
217 printf("reallocs %i\n", reallocs);
218 printf("frees %i\n", frees);
219 printf("allocsMinusfrees %i\n", allocs - frees);
220 printf("allocatedBytes %i\n", allocatedBytes);
221 printf("maxAllocatedBytes %i\n", maxAllocatedBytes);
222 //printf("allocs %i bytes %i\n", allocs, allocatedBytes);
223 //printf("\n");
226 size_t io_maxAllocatedBytes(void)
228 return maxAllocatedBytes;
231 void io_resetMaxAllocatedBytes(void)
233 maxAllocatedBytes = allocatedBytes;
236 size_t io_frees(void)
238 return frees;
241 size_t io_allocatedBytes(void)
243 return allocatedBytes;
246 size_t io_allocs(void)
248 return allocs;
251 void io_showUnfreed(void)
253 MemoryBlock *m = baseblock()->next;
254 size_t sum = 0;
255 int n = 0;
257 while (m)
259 MemoryBlock_show(m);
260 sum += m->size;
261 n ++;
262 m = m->next;
265 printf("\n %i bytes in %i blocks\n", (int)sum, n);
268 #endif
270 void *cpalloc(const void *p, size_t size)
272 void *n = io_malloc(size);
273 if(p) memcpy(n, p, size);
274 return n;
277 void *io_freerealloc(void *p, size_t size)
279 return realloc(p, size);
281 void *n = io_malloc(size);
283 if (p != NULL)
285 memcpy(n, p, size);
286 free(p);
289 return n;