strncpy's third argument should be the length of the dest buffer, not the source.
[wmaker-crm.git] / WINGs / memory.c
blob81476e453f245ee399411d05fbd5149c23f91341
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 memset(tmp, 0, size);
111 return tmp;
114 void *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 USE_BOEHM_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 USE_BOEHM_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.", stderr);
140 exit(-1);
141 } else {
142 wfatal("virtual memory exhausted");
143 Aborting = 1;
144 wAbort(False);
149 return nptr;
152 void *wretain(void *ptr)
154 int *refcount;
156 if (!table) {
157 table = WMCreateHashTable(WMIntHashCallbacks);
160 refcount = WMHashGet(table, ptr);
161 if (!refcount) {
162 refcount = wmalloc(sizeof(int));
163 *refcount = 1;
164 WMHashInsert(table, ptr, refcount);
165 #ifdef VERBOSE
166 printf("== %i (%p)\n", *refcount, ptr);
167 #endif
168 } else {
169 (*refcount)++;
170 #ifdef VERBOSE
171 printf("+ %i (%p)\n", *refcount, ptr);
172 #endif
175 return ptr;
178 void wfree(void *ptr)
180 if (ptr)
181 #ifdef USE_BOEHM_GC
182 /* This should eventually be removed, once the criss-cross
183 * of wmalloc()d memory being free()d, malloc()d memory being
184 * wfree()d, various misuses of calling wfree() on objects
185 * allocated by libc malloc() and calling libc free() on
186 * objects allocated by Boehm GC (think external libraries)
187 * is cleaned up.
189 if (GC_base(ptr) != 0)
190 GC_FREE(ptr);
191 else
192 free(ptr);
193 #else
194 free(ptr);
195 #endif
196 ptr = NULL;
199 void wrelease(void *ptr)
201 int *refcount;
203 refcount = WMHashGet(table, ptr);
204 if (!refcount) {
205 wwarning("trying to release unexisting data %p", ptr);
206 } else {
207 (*refcount)--;
208 if (*refcount < 1) {
209 #ifdef VERBOSE
210 printf("RELEASING %p\n", ptr);
211 #endif
212 WMHashRemove(table, ptr);
213 wfree(refcount);
214 wfree(ptr);
216 #ifdef VERBOSE
217 else {
218 printf("- %i (%p)\n", *refcount, ptr);
220 #endif