applied patch to use more gnustepish paths for some internal data files
[wmaker-crm.git] / WINGs / memory.c
blob0ae75eddef84745ed1a44fe58c33d3f0a6b2bc9b
1 /*
2 * Window Maker miscelaneous function library
4 * Copyright (c) 1997-2003 Alfredo K. Kojima
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 "wconfig.h"
23 #include "WUtil.h"
25 #include <stdlib.h>
26 #include <sys/types.h>
27 #include <unistd.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <assert.h>
31 #include <signal.h>
33 #ifdef TEST_WITH_GC
34 #include <gc/gc.h>
35 #endif
37 #ifndef False
38 # define False 0
39 #endif
40 #ifndef True
41 # define True 1
42 #endif
45 static void
46 defaultHandler(int bla)
48 if (bla)
49 kill(getpid(), SIGABRT);
50 else
51 exit(1);
55 static waborthandler *aborthandler = (waborthandler*)defaultHandler;
57 #define wAbort(a) (*aborthandler)(a)
60 waborthandler*
61 wsetabort(waborthandler *handler)
63 waborthandler *old = aborthandler;
65 aborthandler = handler;
67 return old;
72 static int Aborting=0; /* if we're in the middle of an emergency exit */
75 static WMHashTable *table = NULL;
78 void*
79 wmalloc(size_t size)
81 void *tmp;
83 assert(size > 0);
85 #ifdef TEST_WITH_GC
86 tmp = GC_malloc(size);
87 #else
88 tmp = malloc(size);
89 #endif
90 if (tmp == NULL) {
91 wwarning("malloc() failed. Retrying after 2s.");
92 sleep(2);
93 #ifdef TEST_WITH_GC
94 tmp = GC_malloc(size);
95 #else
96 tmp = malloc(size);
97 #endif
98 if (tmp == NULL) {
99 if (Aborting) {
100 fputs("Really Bad Error: recursive malloc() failure.", stderr);
101 exit(-1);
102 } else {
103 wfatal("virtual memory exhausted");
104 Aborting=1;
105 wAbort(False);
109 return tmp;
113 void*
114 wmalloc0(size_t size)
116 void *ptr= wmalloc(size);
117 if (!ptr)
118 return NULL;
120 memset(ptr, 0, size);
122 return ptr;
126 void*
127 wrealloc(void *ptr, size_t newsize)
129 void *nptr;
131 if (!ptr) {
132 nptr = wmalloc(newsize);
133 } else if (newsize==0) {
134 wfree(ptr);
135 nptr = NULL;
136 } else {
137 #ifdef TEST_WITH_GC
138 nptr = GC_realloc(ptr, newsize);
139 #else
140 nptr = realloc(ptr, newsize);
141 #endif
142 if (nptr==NULL) {
143 wwarning("realloc() failed. Retrying after 2s.");
144 sleep(2);
145 #ifdef TEST_WITH_GC
146 nptr = GC_realloc(ptr, newsize);
147 #else
148 nptr = realloc(ptr, newsize);
149 #endif
150 if (nptr == NULL) {
151 if (Aborting) {
152 fputs("Really Bad Error: recursive realloc() failure.",
153 stderr);
154 exit(-1);
155 } else {
156 wfatal("virtual memory exhausted");
157 Aborting=1;
158 wAbort(False);
163 return nptr;
167 void*
168 wretain(void *ptr)
170 int *refcount;
172 if (!table) {
173 table = WMCreateHashTable(WMIntHashCallbacks);
176 refcount = WMHashGet(table, ptr);
177 if (!refcount) {
178 refcount = wmalloc(sizeof(int));
179 *refcount = 1;
180 WMHashInsert(table, ptr, refcount);
181 #ifdef VERBOSE
182 printf("== %i (%p)\n", *refcount, ptr);
183 #endif
184 } else {
185 (*refcount)++;
186 #ifdef VERBOSE
187 printf("+ %i (%p)\n", *refcount, ptr);
188 #endif
191 return ptr;
196 void
197 wfree(void *ptr)
199 #ifdef TEST_WITH_GC
200 GC_free(ptr);
201 #else
202 free(ptr);
203 #endif
208 void
209 wrelease(void *ptr)
211 int *refcount;
213 refcount = WMHashGet(table, ptr);
214 if (!refcount) {
215 wwarning("trying to release unexisting data %p", ptr);
216 } else {
217 (*refcount)--;
218 if (*refcount < 1) {
219 #ifdef VERBOSE
220 printf("RELEASING %p\n", ptr);
221 #endif
222 WMHashRemove(table, ptr);
223 wfree(refcount);
224 wfree(ptr);
226 #ifdef VERBOSE
227 else {
228 printf("- %i (%p)\n", *refcount, ptr);
230 #endif