Repair TEST_WITH_GC
[wmaker-crm.git] / WINGs / memory.c
blob0e350bf921796404a5ff4cdb6d65987867cb68ec
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.
21 #include "wconfig.h"
22 #include "WUtil.h"
24 #include <stdlib.h>
25 #include <sys/types.h>
26 #include <unistd.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <assert.h>
30 #include <signal.h>
32 #ifdef USE_BOEHM_GC
33 #ifndef GC_DEBUG
34 #define GC_DEBUG
35 #endif /* !GC_DEBUG */
36 #include <gc/gc.h>
37 #endif /* USE_BOEHM_GC */
39 #ifndef False
40 # define False 0
41 #endif
42 #ifndef True
43 # define True 1
44 #endif
46 static void defaultHandler(int bla)
48 if (bla)
49 kill(getpid(), SIGABRT);
50 else
51 exit(1);
54 static waborthandler *aborthandler = (waborthandler *) defaultHandler;
56 #define wAbort(a) (*aborthandler)(a)
58 waborthandler *wsetabort(waborthandler * handler)
60 waborthandler *old = aborthandler;
62 aborthandler = handler;
64 return old;
67 static int Aborting = 0; /* if we're in the middle of an emergency exit */
69 static WMHashTable *table = NULL;
71 void *wmalloc(size_t size)
73 void *tmp;
75 assert(size > 0);
77 #ifdef USE_BOEHM_GC
78 tmp = GC_MALLOC(size);
79 #else
80 tmp = malloc(size);
81 #endif
82 if (tmp == NULL) {
83 wwarning("malloc() failed. Retrying after 2s.");
84 sleep(2);
85 #ifdef USE_BOEHM_GC
86 tmp = GC_MALLOC(size);
87 #else
88 tmp = malloc(size);
89 #endif
90 if (tmp == NULL) {
91 if (Aborting) {
92 fputs("Really Bad Error: recursive malloc() failure.", stderr);
93 exit(-1);
94 } else {
95 wfatal("virtual memory exhausted");
96 Aborting = 1;
97 wAbort(False);
101 return tmp;
104 void *wrealloc(void *ptr, size_t newsize)
106 void *nptr;
108 if (!ptr) {
109 nptr = wmalloc(newsize);
110 } else if (newsize == 0) {
111 wfree(ptr);
112 nptr = NULL;
113 } else {
114 #ifdef USE_BOEHM_GC
115 nptr = GC_REALLOC(ptr, newsize);
116 #else
117 nptr = realloc(ptr, newsize);
118 #endif
119 if (nptr == NULL) {
120 wwarning("realloc() failed. Retrying after 2s.");
121 sleep(2);
122 #ifdef USE_BOEHM_GC
123 nptr = GC_REALLOC(ptr, newsize);
124 #else
125 nptr = realloc(ptr, newsize);
126 #endif
127 if (nptr == NULL) {
128 if (Aborting) {
129 fputs("Really Bad Error: recursive realloc() failure.", stderr);
130 exit(-1);
131 } else {
132 wfatal("virtual memory exhausted");
133 Aborting = 1;
134 wAbort(False);
139 return nptr;
142 void *wretain(void *ptr)
144 int *refcount;
146 if (!table) {
147 table = WMCreateHashTable(WMIntHashCallbacks);
150 refcount = WMHashGet(table, ptr);
151 if (!refcount) {
152 refcount = wmalloc(sizeof(int));
153 *refcount = 1;
154 WMHashInsert(table, ptr, refcount);
155 #ifdef VERBOSE
156 printf("== %i (%p)\n", *refcount, ptr);
157 #endif
158 } else {
159 (*refcount)++;
160 #ifdef VERBOSE
161 printf("+ %i (%p)\n", *refcount, ptr);
162 #endif
165 return ptr;
168 void wfree(void *ptr)
170 if (ptr)
171 #ifdef USE_BOEHM_GC
172 /* This should eventually be removed, once the criss-cross
173 * of wmalloc()d memory being free()d, malloc()d memory being
174 * wfree()d, various misuses of calling wfree() on objects
175 * allocated by libc malloc() and calling libc free() on
176 * objects allocated by Boehm GC (think external libraries)
177 * is cleaned up.
179 if (GC_base(ptr) != 0)
180 GC_FREE(ptr);
181 else
182 free(ptr);
183 #else
184 free(ptr);
185 #endif
186 ptr = NULL;
189 void wrelease(void *ptr)
191 int *refcount;
193 refcount = WMHashGet(table, ptr);
194 if (!refcount) {
195 wwarning("trying to release unexisting data %p", ptr);
196 } else {
197 (*refcount)--;
198 if (*refcount < 1) {
199 #ifdef VERBOSE
200 printf("RELEASING %p\n", ptr);
201 #endif
202 WMHashRemove(table, ptr);
203 wfree(refcount);
204 wfree(ptr);
206 #ifdef VERBOSE
207 else {
208 printf("- %i (%p)\n", *refcount, ptr);
210 #endif