Update Serbian translation from master branch
[wmaker-crm.git] / WINGs / memory.c
blobe05e17a8ab57528d8c804948d15c2717dea979e3
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., 51 Franklin St, Fifth Floor, Boston,
19 * MA 02110-1301, 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 HAVE_STDNORETURN
34 #include <stdnoreturn.h>
35 #endif
37 #ifdef USE_BOEHM_GC
38 #ifndef GC_DEBUG
39 #define GC_DEBUG
40 #endif /* !GC_DEBUG */
41 #include <gc/gc.h>
42 #endif /* USE_BOEHM_GC */
44 #ifndef False
45 # define False 0
46 #endif
47 #ifndef True
48 # define True 1
49 #endif
51 static void defaultHandler(int bla)
53 if (bla)
54 kill(getpid(), SIGABRT);
55 else
56 exit(1);
59 static waborthandler *aborthandler = defaultHandler;
61 static inline noreturn void wAbort(int bla)
63 (*aborthandler)(bla);
64 exit(-1);
67 waborthandler *wsetabort(waborthandler * handler)
69 waborthandler *old = aborthandler;
71 aborthandler = handler;
73 return old;
76 static int Aborting = 0; /* if we're in the middle of an emergency exit */
78 static WMHashTable *table = NULL;
80 void *wmalloc(size_t size)
82 void *tmp;
84 assert(size > 0);
86 #ifdef USE_BOEHM_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 USE_BOEHM_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 if (tmp != NULL)
111 memset(tmp, 0, size);
112 return tmp;
115 void *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 USE_BOEHM_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 USE_BOEHM_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.", stderr);
141 exit(-1);
142 } else {
143 wfatal("virtual memory exhausted");
144 Aborting = 1;
145 wAbort(False);
150 return nptr;
153 void *wretain(void *ptr)
155 int *refcount;
157 if (!table) {
158 table = WMCreateHashTable(WMIntHashCallbacks);
161 refcount = WMHashGet(table, ptr);
162 if (!refcount) {
163 refcount = wmalloc(sizeof(int));
164 *refcount = 1;
165 WMHashInsert(table, ptr, refcount);
166 #ifdef VERBOSE
167 printf("== %i (%p)\n", *refcount, ptr);
168 #endif
169 } else {
170 (*refcount)++;
171 #ifdef VERBOSE
172 printf("+ %i (%p)\n", *refcount, ptr);
173 #endif
176 return ptr;
179 void wfree(void *ptr)
181 if (ptr)
182 #ifdef USE_BOEHM_GC
183 /* This should eventually be removed, once the criss-cross
184 * of wmalloc()d memory being free()d, malloc()d memory being
185 * wfree()d, various misuses of calling wfree() on objects
186 * allocated by libc malloc() and calling libc free() on
187 * objects allocated by Boehm GC (think external libraries)
188 * is cleaned up.
190 if (GC_base(ptr) != 0)
191 GC_FREE(ptr);
192 else
193 free(ptr);
194 #else
195 free(ptr);
196 #endif
197 ptr = NULL;
200 void wrelease(void *ptr)
202 int *refcount;
204 refcount = WMHashGet(table, ptr);
205 if (!refcount) {
206 wwarning("trying to release unexisting data %p", ptr);
207 } else {
208 (*refcount)--;
209 if (*refcount < 1) {
210 #ifdef VERBOSE
211 printf("RELEASING %p\n", ptr);
212 #endif
213 WMHashRemove(table, ptr);
214 wfree(refcount);
215 wfree(ptr);
217 #ifdef VERBOSE
218 else {
219 printf("- %i (%p)\n", *refcount, ptr);
221 #endif