Icon creation in only one function
[wmaker-crm.git] / WINGs / memory.c
blob39c7fc9b083a9b718bf92135b0e46e9a7c69604e
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 memset(tmp, 0, size);
102 return tmp;
105 void *wrealloc(void *ptr, size_t newsize)
107 void *nptr;
109 if (!ptr) {
110 nptr = wmalloc(newsize);
111 } else if (newsize == 0) {
112 wfree(ptr);
113 nptr = NULL;
114 } else {
115 #ifdef USE_BOEHM_GC
116 nptr = GC_REALLOC(ptr, newsize);
117 #else
118 nptr = realloc(ptr, newsize);
119 #endif
120 if (nptr == NULL) {
121 wwarning("realloc() failed. Retrying after 2s.");
122 sleep(2);
123 #ifdef USE_BOEHM_GC
124 nptr = GC_REALLOC(ptr, newsize);
125 #else
126 nptr = realloc(ptr, newsize);
127 #endif
128 if (nptr == NULL) {
129 if (Aborting) {
130 fputs("Really Bad Error: recursive realloc() failure.", stderr);
131 exit(-1);
132 } else {
133 wfatal("virtual memory exhausted");
134 Aborting = 1;
135 wAbort(False);
140 return nptr;
143 void *wretain(void *ptr)
145 int *refcount;
147 if (!table) {
148 table = WMCreateHashTable(WMIntHashCallbacks);
151 refcount = WMHashGet(table, ptr);
152 if (!refcount) {
153 refcount = wmalloc(sizeof(int));
154 *refcount = 1;
155 WMHashInsert(table, ptr, refcount);
156 #ifdef VERBOSE
157 printf("== %i (%p)\n", *refcount, ptr);
158 #endif
159 } else {
160 (*refcount)++;
161 #ifdef VERBOSE
162 printf("+ %i (%p)\n", *refcount, ptr);
163 #endif
166 return ptr;
169 void wfree(void *ptr)
171 if (ptr)
172 #ifdef USE_BOEHM_GC
173 /* This should eventually be removed, once the criss-cross
174 * of wmalloc()d memory being free()d, malloc()d memory being
175 * wfree()d, various misuses of calling wfree() on objects
176 * allocated by libc malloc() and calling libc free() on
177 * objects allocated by Boehm GC (think external libraries)
178 * is cleaned up.
180 if (GC_base(ptr) != 0)
181 GC_FREE(ptr);
182 else
183 free(ptr);
184 #else
185 free(ptr);
186 #endif
187 ptr = NULL;
190 void wrelease(void *ptr)
192 int *refcount;
194 refcount = WMHashGet(table, ptr);
195 if (!refcount) {
196 wwarning("trying to release unexisting data %p", ptr);
197 } else {
198 (*refcount)--;
199 if (*refcount < 1) {
200 #ifdef VERBOSE
201 printf("RELEASING %p\n", ptr);
202 #endif
203 WMHashRemove(table, ptr);
204 wfree(refcount);
205 wfree(ptr);
207 #ifdef VERBOSE
208 else {
209 printf("- %i (%p)\n", *refcount, ptr);
211 #endif