fixed some compilation issues with gcc-2.9x
[wmaker-crm.git] / WINGs / memory.c
blobefd69bafdc4cb1f3a6e23741142ec7f9d3331c4b
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 wrealloc(void *ptr, size_t newsize)
116 void *nptr;
118 if (!ptr) {
119 nptr = wmalloc(newsize);
120 } else if (newsize==0) {
121 wfree(ptr);
122 nptr = NULL;
123 } else {
124 #ifdef TEST_WITH_GC
125 nptr = GC_realloc(ptr, newsize);
126 #else
127 nptr = realloc(ptr, newsize);
128 #endif
129 if (nptr==NULL) {
130 wwarning("realloc() failed. Retrying after 2s.");
131 sleep(2);
132 #ifdef TEST_WITH_GC
133 nptr = GC_realloc(ptr, newsize);
134 #else
135 nptr = realloc(ptr, newsize);
136 #endif
137 if (nptr == NULL) {
138 if (Aborting) {
139 fputs("Really Bad Error: recursive realloc() failure.",
140 stderr);
141 exit(-1);
142 } else {
143 wfatal("virtual memory exhausted");
144 Aborting=1;
145 wAbort(False);
150 return nptr;
154 void*
155 wretain(void *ptr)
157 int *refcount;
159 if (!table) {
160 table = WMCreateHashTable(WMIntHashCallbacks);
163 refcount = WMHashGet(table, ptr);
164 if (!refcount) {
165 refcount = wmalloc(sizeof(int));
166 *refcount = 1;
167 WMHashInsert(table, ptr, refcount);
168 #ifdef VERBOSE
169 printf("== %i (%p)\n", *refcount, ptr);
170 #endif
171 } else {
172 (*refcount)++;
173 #ifdef VERBOSE
174 printf("+ %i (%p)\n", *refcount, ptr);
175 #endif
178 return ptr;
183 void
184 wfree(void *ptr)
186 #ifdef TEST_WITH_GC
187 GC_free(ptr);
188 #else
189 free(ptr);
190 #endif
195 void
196 wrelease(void *ptr)
198 int *refcount;
200 refcount = WMHashGet(table, ptr);
201 if (!refcount) {
202 wwarning("trying to release unexisting data %p", ptr);
203 } else {
204 (*refcount)--;
205 if (*refcount < 1) {
206 #ifdef VERBOSE
207 printf("RELEASING %p\n", ptr);
208 #endif
209 WMHashRemove(table, ptr);
210 wfree(refcount);
211 wfree(ptr);
213 #ifdef VERBOSE
214 else {
215 printf("- %i (%p)\n", *refcount, ptr);
217 #endif