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>
36 #endif /* !GC_DEBUG */
38 #endif /* USE_BOEHM_GC */
47 static void defaultHandler(int bla
)
50 kill(getpid(), SIGABRT
);
55 static waborthandler
*aborthandler
= (waborthandler
*) defaultHandler
;
57 #define wAbort(a) (*aborthandler)(a)
59 waborthandler
*wsetabort(waborthandler
* handler
)
61 waborthandler
*old
= aborthandler
;
63 aborthandler
= handler
;
68 static int Aborting
= 0; /* if we're in the middle of an emergency exit */
70 static WMHashTable
*table
= NULL
;
72 void *wmalloc(size_t size
)
79 tmp
= GC_MALLOC(size
);
84 wwarning("malloc() failed. Retrying after 2s.");
87 tmp
= GC_MALLOC(size
);
93 fputs("Really Bad Error: recursive malloc() failure.", stderr
);
96 wfatal("virtual memory exhausted");
102 memset(tmp
, 0, size
);
106 void *wrealloc(void *ptr
, size_t newsize
)
111 nptr
= wmalloc(newsize
);
112 } else if (newsize
== 0) {
117 nptr
= GC_REALLOC(ptr
, newsize
);
119 nptr
= realloc(ptr
, newsize
);
122 wwarning("realloc() failed. Retrying after 2s.");
125 nptr
= GC_REALLOC(ptr
, newsize
);
127 nptr
= realloc(ptr
, newsize
);
131 fputs("Really Bad Error: recursive realloc() failure.", stderr
);
134 wfatal("virtual memory exhausted");
144 void *wretain(void *ptr
)
149 table
= WMCreateHashTable(WMIntHashCallbacks
);
152 refcount
= WMHashGet(table
, ptr
);
154 refcount
= wmalloc(sizeof(int));
156 WMHashInsert(table
, ptr
, refcount
);
158 printf("== %i (%p)\n", *refcount
, ptr
);
163 printf("+ %i (%p)\n", *refcount
, ptr
);
170 void wfree(void *ptr
)
174 /* This should eventually be removed, once the criss-cross
175 * of wmalloc()d memory being free()d, malloc()d memory being
176 * wfree()d, various misuses of calling wfree() on objects
177 * allocated by libc malloc() and calling libc free() on
178 * objects allocated by Boehm GC (think external libraries)
181 if (GC_base(ptr
) != 0)
191 void wrelease(void *ptr
)
195 refcount
= WMHashGet(table
, ptr
);
197 wwarning("trying to release unexisting data %p", ptr
);
202 printf("RELEASING %p\n", ptr
);
204 WMHashRemove(table
, ptr
);
210 printf("- %i (%p)\n", *refcount
, ptr
);