more code for proplist handling
[wmaker-crm.git] / WINGs / memory.c
blobdc75a0a675f49d1cdcd31db5c0fd599f44be85b6
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*
80 wmalloc(size_t size)
82 void *tmp;
84 assert(size > 0);
86 #ifdef TEST_WITH_GC
87 tmp = GC_malloc(size);
88 #else
89 tmp = malloc(size);
90 #endif
91 if (tmp == NULL) {
92 wwarning("malloc() failed. Retrying after 2s.");
93 sleep(2);
94 #ifdef TEST_WITH_GC
95 tmp = GC_malloc(size);
96 #else
97 tmp = malloc(size);
98 #endif
99 if (tmp == NULL) {
100 if (Aborting) {
101 fputs("Really Bad Error: recursive malloc() failure.", stderr);
102 exit(-1);
103 } else {
104 wfatal("virtual memory exhausted");
105 Aborting=1;
106 wAbort(False);
110 return tmp;
114 void*
115 wrealloc(void *ptr, size_t newsize)
117 void *nptr;
119 if (!ptr) {
120 nptr = wmalloc(newsize);
121 } else if (newsize==0) {
122 wfree(ptr);
123 nptr = NULL;
124 } else {
125 #ifdef TEST_WITH_GC
126 nptr = GC_realloc(ptr, newsize);
127 #else
128 nptr = realloc(ptr, newsize);
129 #endif
130 if (nptr==NULL) {
131 wwarning("realloc() failed. Retrying after 2s.");
132 sleep(2);
133 #ifdef TEST_WITH_GC
134 nptr = GC_realloc(ptr, newsize);
135 #else
136 nptr = realloc(ptr, newsize);
137 #endif
138 if (nptr == NULL) {
139 if (Aborting) {
140 fputs("Really Bad Error: recursive realloc() failure.",
141 stderr);
142 exit(-1);
143 } else {
144 wfatal("virtual memory exhausted");
145 Aborting=1;
146 wAbort(False);
151 return nptr;
155 void*
156 wretain(void *ptr)
158 int *refcount;
160 if (!table) {
161 table = WMCreateHashTable(WMIntHashCallbacks);
164 refcount = WMHashGet(table, ptr);
165 if (!refcount) {
166 refcount = wmalloc(sizeof(int));
167 *refcount = 1;
168 WMHashInsert(table, ptr, refcount);
169 #ifdef VERBOSE
170 printf("== %i (%p)\n", *refcount, ptr);
171 #endif
172 } else {
173 (*refcount)++;
174 #ifdef VERBOSE
175 printf("+ %i (%p)\n", *refcount, ptr);
176 #endif
179 return ptr;
184 void
185 wfree(void *ptr)
187 #ifdef TEST_WITH_GC
188 GC_free(ptr);
189 #else
190 free(ptr);
191 #endif
196 void
197 wrelease(void *ptr)
199 int *refcount;
201 refcount = WMHashGet(table, ptr);
202 if (!refcount) {
203 wwarning("trying to release unexisting data %p", ptr);
204 } else {
205 (*refcount)--;
206 if (*refcount < 1) {
207 #ifdef VERBOSE
208 printf("RELEASING %p\n", ptr);
209 #endif
210 WMHashRemove(table, ptr);
211 wfree(refcount);
212 wfree(ptr);
214 #ifdef VERBOSE
215 else {
216 printf("- %i (%p)\n", *refcount, ptr);
218 #endif