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,
26 #include <sys/types.h>
33 #ifdef HAVE_STDNORETURN
34 #include <stdnoreturn.h>
40 #endif /* !GC_DEBUG */
42 #endif /* USE_BOEHM_GC */
51 static void defaultHandler(int bla
)
54 kill(getpid(), SIGABRT
);
59 static waborthandler
*aborthandler
= defaultHandler
;
61 static inline noreturn
void wAbort(int bla
)
67 waborthandler
*wsetabort(waborthandler
* handler
)
69 waborthandler
*old
= aborthandler
;
71 aborthandler
= handler
;
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
)
87 tmp
= GC_MALLOC(size
);
92 wwarning("malloc() failed. Retrying after 2s.");
95 tmp
= GC_MALLOC(size
);
101 fputs("Really Bad Error: recursive malloc() failure.", stderr
);
104 wfatal("virtual memory exhausted");
110 memset(tmp
, 0, size
);
114 void *wrealloc(void *ptr
, size_t newsize
)
119 nptr
= wmalloc(newsize
);
120 } else if (newsize
== 0) {
125 nptr
= GC_REALLOC(ptr
, newsize
);
127 nptr
= realloc(ptr
, newsize
);
130 wwarning("realloc() failed. Retrying after 2s.");
133 nptr
= GC_REALLOC(ptr
, newsize
);
135 nptr
= realloc(ptr
, newsize
);
139 fputs("Really Bad Error: recursive realloc() failure.", stderr
);
142 wfatal("virtual memory exhausted");
152 void *wretain(void *ptr
)
157 table
= WMCreateHashTable(WMIntHashCallbacks
);
160 refcount
= WMHashGet(table
, ptr
);
162 refcount
= wmalloc(sizeof(int));
164 WMHashInsert(table
, ptr
, refcount
);
166 printf("== %i (%p)\n", *refcount
, ptr
);
171 printf("+ %i (%p)\n", *refcount
, ptr
);
178 void wfree(void *ptr
)
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)
189 if (GC_base(ptr
) != 0)
199 void wrelease(void *ptr
)
203 refcount
= WMHashGet(table
, ptr
);
205 wwarning("trying to release unexisting data %p", ptr
);
210 printf("RELEASING %p\n", ptr
);
212 WMHashRemove(table
, ptr
);
218 printf("- %i (%p)\n", *refcount
, ptr
);