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.
25 #include <sys/types.h>
43 static void defaultHandler(int bla)
46 kill(getpid(), SIGABRT);
51 static waborthandler *aborthandler = (waborthandler *) defaultHandler;
53 #define wAbort(a) (*aborthandler)(a)
55 waborthandler *wsetabort(waborthandler * handler)
57 waborthandler *old = aborthandler;
59 aborthandler = handler;
64 static int Aborting = 0; /* if we're in the middle of an emergency exit */
66 static WMHashTable *table = NULL;
68 void *wmalloc(size_t size)
75 tmp = GC_malloc(size);
80 wwarning("malloc() failed. Retrying after 2s.");
83 tmp = GC_malloc(size);
89 fputs("Really Bad Error: recursive malloc() failure.", stderr);
92 wfatal("virtual memory exhausted");
101 void *wmalloc0(size_t size)
103 void *ptr = wmalloc(size);
107 memset(ptr, 0, size);
112 void *wrealloc(void *ptr, size_t newsize)
117 nptr = wmalloc(newsize);
118 } else if (newsize == 0) {
123 nptr = GC_realloc(ptr, newsize);
125 nptr = realloc(ptr, newsize);
128 wwarning("realloc() failed. Retrying after 2s.");
131 nptr = GC_realloc(ptr, newsize);
133 nptr = realloc(ptr, newsize);
137 fputs("Really Bad Error: recursive realloc() failure.", stderr);
140 wfatal("virtual memory exhausted");
150 void *wretain(void *ptr)
155 table = WMCreateHashTable(WMIntHashCallbacks);
158 refcount = WMHashGet(table, ptr);
160 refcount = wmalloc(sizeof(int));
162 WMHashInsert(table, ptr, refcount);
164 printf("== %i (%p)\n", *refcount, ptr);
169 printf("+ %i (%p)\n", *refcount, ptr);
176 void wfree(void *ptr)
185 void wrelease(void *ptr)
189 refcount = WMHashGet(table, ptr);
191 wwarning("trying to release unexisting data %p", ptr);
196 printf("RELEASING %p\n", ptr);
198 WMHashRemove(table, ptr);
204 printf("- %i (%p)\n", *refcount, ptr);