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>
35 #endif /* !GC_DEBUG */
37 #endif /* USE_BOEHM_GC */
46 static void defaultHandler(int bla
)
49 kill(getpid(), SIGABRT
);
54 static waborthandler
*aborthandler
= (waborthandler
*) defaultHandler
;
56 #define wAbort(a) (*aborthandler)(a)
58 waborthandler
*wsetabort(waborthandler
* handler
)
60 waborthandler
*old
= aborthandler
;
62 aborthandler
= handler
;
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
)
78 tmp
= GC_MALLOC(size
);
83 wwarning("malloc() failed. Retrying after 2s.");
86 tmp
= GC_MALLOC(size
);
92 fputs("Really Bad Error: recursive malloc() failure.", stderr
);
95 wfatal("virtual memory exhausted");
101 memset(tmp
, 0, size
);
105 void *wrealloc(void *ptr
, size_t newsize
)
110 nptr
= wmalloc(newsize
);
111 } else if (newsize
== 0) {
116 nptr
= GC_REALLOC(ptr
, newsize
);
118 nptr
= realloc(ptr
, newsize
);
121 wwarning("realloc() failed. Retrying after 2s.");
124 nptr
= GC_REALLOC(ptr
, newsize
);
126 nptr
= realloc(ptr
, newsize
);
130 fputs("Really Bad Error: recursive realloc() failure.", stderr
);
133 wfatal("virtual memory exhausted");
143 void *wretain(void *ptr
)
148 table
= WMCreateHashTable(WMIntHashCallbacks
);
151 refcount
= WMHashGet(table
, ptr
);
153 refcount
= wmalloc(sizeof(int));
155 WMHashInsert(table
, ptr
, refcount
);
157 printf("== %i (%p)\n", *refcount
, ptr
);
162 printf("+ %i (%p)\n", *refcount
, ptr
);
169 void wfree(void *ptr
)
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)
180 if (GC_base(ptr
) != 0)
190 void wrelease(void *ptr
)
194 refcount
= WMHashGet(table
, ptr
);
196 wwarning("trying to release unexisting data %p", ptr
);
201 printf("RELEASING %p\n", ptr
);
203 WMHashRemove(table
, ptr
);
209 printf("- %i (%p)\n", *refcount
, ptr
);