replaced free() with wfree() everywhere
[wmaker-crm.git] / WINGs / memory.c
blobd8aa69c675a9adcf03bca10ce0df6e21faa0dfd1
1 /*
2 * Window Maker miscelaneous function library
3 *
4 * Copyright (c) 1997 Alfredo K. Kojima
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include "../src/config.h"
24 #include "WUtil.h"
26 #include <stdlib.h>
27 #include <sys/types.h>
28 #include <unistd.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <assert.h>
32 #include <signal.h>
34 #ifdef TEST_WITH_GC
35 #include <gc/gc.h>
36 #endif
38 #ifndef False
39 # define False 0
40 #endif
41 #ifndef True
42 # define True 1
43 #endif
46 static void
47 defaultHandler(int bla)
49 if (bla)
50 kill(getpid(), SIGABRT);
51 else
52 exit(1);
56 static waborthandler *aborthandler = (waborthandler*)defaultHandler;
58 #define wAbort(a) (*aborthandler)(a)
61 waborthandler*
62 wsetabort(waborthandler *handler)
64 waborthandler *old = aborthandler;
66 aborthandler = handler;
68 return old;
73 static int Aborting=0; /* if we're in the middle of an emergency exit */
76 static WMHashTable *table = NULL;
79 void *wmalloc(size_t size)
81 void *tmp;
83 #ifdef TEST_WITH_GC
84 tmp = GC_malloc(size);
85 #else
86 tmp = malloc(size);
87 #endif
88 if (tmp == NULL) {
89 wwarning("malloc() failed. Retrying after 2s.");
90 sleep(2);
91 tmp = malloc(size);
92 if (tmp == NULL) {
93 if (Aborting) {
94 puts("Real Bad Error: recursive malloc() failure.");
95 exit(-1);
96 } else {
97 wfatal("virtual memory exhausted");
98 Aborting=1;
99 wAbort(False);
103 return tmp;
107 void *wrealloc(void *ptr, size_t newsize)
109 void *nptr;
111 if (!ptr) {
112 #ifdef TEST_WITH_GC
113 nptr = GC_malloc(newsize);
114 #else
115 nptr = malloc(newsize);
116 #endif
117 } else {
118 #ifdef TEST_WITH_GC
119 nptr = GC_realloc(ptr, newsize);
120 #else
121 nptr=realloc(ptr, newsize);
122 #endif
124 if (nptr==NULL) {
125 printf("Could not do realloc");
126 return NULL;
128 return nptr;
132 void*
133 wretain(void *ptr)
135 int *refcount;
137 if (!table) {
138 table = WMCreateHashTable(WMIntHashCallbacks);
141 refcount = WMHashGet(table, ptr);
142 if (!refcount) {
143 refcount = wmalloc(sizeof(int));
144 *refcount = 1;
145 WMHashInsert(table, ptr, refcount);
146 #ifdef VERBOSE
147 printf("== %i (%p)\n", *refcount, ptr);
148 #endif
149 } else {
150 (*refcount)++;
151 #ifdef VERBOSE
152 printf("+ %i (%p)\n", *refcount, ptr);
153 #endif
156 return ptr;
161 void
162 wfree(void *ptr)
164 #ifdef TEST_WITH_GC
165 GC_free(ptr);
166 #else
167 free(ptr);
168 #endif
173 void
174 wrelease(void *ptr)
176 int *refcount;
178 refcount = WMHashGet(table, ptr);
179 if (!refcount) {
180 wwarning("trying to release unexisting data %p", ptr);
181 } else {
182 (*refcount)--;
183 if (*refcount < 1) {
184 #ifdef VERBOSE
185 printf("RELEASING %p\n", ptr);
186 #endif
187 WMHashRemove(table, ptr);
188 wfree(refcount);
189 wfree(ptr);
191 #ifdef VERBOSE
192 else {
193 printf("- %i (%p)\n", *refcount, ptr);
195 #endif
200 char*
201 wstrdup(char *str)
203 assert(str!=NULL);
205 return strcpy(wmalloc(strlen(str)+1), str);
209 char*
210 wstrappend(char *dst, char *src)
212 char *str;
214 if (!dst)
215 return wstrdup(src);
216 else if (!src)
217 return wstrdup(dst);
219 str = wmalloc(strlen(dst)+strlen(src)+1);
220 strcpy(str, dst);
221 strcat(str, src);
223 return str;