Files
wmaker/WINGs/memory.c
T

223 lines
4.2 KiB
C
Raw Normal View History

1998-09-29 22:36:29 +00:00
/*
1998-10-21 14:43:47 +00:00
* Window Maker miscelaneous function library
2004-10-12 21:28:27 +00:00
*
2003-01-16 23:30:45 +00:00
* Copyright (c) 1997-2003 Alfredo K. Kojima
2004-10-12 21:28:27 +00:00
*
1998-09-29 22:36:29 +00:00
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "../src/config.h"
#include "WUtil.h"
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
1999-01-25 19:06:50 +00:00
#include <signal.h>
1998-09-29 22:36:29 +00:00
1999-10-09 20:07:23 +00:00
#ifdef TEST_WITH_GC
#include <gc/gc.h>
#endif
1998-09-29 22:36:29 +00:00
#ifndef False
# define False 0
#endif
#ifndef True
# define True 1
#endif
1999-01-25 19:06:50 +00:00
static void
defaultHandler(int bla)
{
if (bla)
2004-10-12 21:28:27 +00:00
kill(getpid(), SIGABRT);
1999-01-25 19:06:50 +00:00
else
2004-10-12 21:28:27 +00:00
exit(1);
1999-01-25 19:06:50 +00:00
}
static waborthandler *aborthandler = (waborthandler*)defaultHandler;
#define wAbort(a) (*aborthandler)(a)
waborthandler*
wsetabort(waborthandler *handler)
{
waborthandler *old = aborthandler;
aborthandler = handler;
return old;
}
1998-09-29 22:36:29 +00:00
static int Aborting=0; /* if we're in the middle of an emergency exit */
static WMHashTable *table = NULL;
void*
wmalloc(size_t size)
1998-09-29 22:36:29 +00:00
{
void *tmp;
1999-10-09 20:07:23 +00:00
2000-09-25 19:21:45 +00:00
assert(size > 0);
1999-10-09 20:07:23 +00:00
#ifdef TEST_WITH_GC
tmp = GC_malloc(size);
#else
1998-09-29 22:36:29 +00:00
tmp = malloc(size);
1999-10-09 20:07:23 +00:00
#endif
1998-09-29 22:36:29 +00:00
if (tmp == NULL) {
2004-10-12 21:28:27 +00:00
wwarning("malloc() failed. Retrying after 2s.");
sleep(2);
#ifdef TEST_WITH_GC
tmp = GC_malloc(size);
#else
tmp = malloc(size);
#endif
2004-10-12 21:28:27 +00:00
if (tmp == NULL) {
if (Aborting) {
fputs("Really Bad Error: recursive malloc() failure.", stderr);
exit(-1);
} else {
wfatal("virtual memory exhausted");
Aborting=1;
wAbort(False);
}
}
1998-09-29 22:36:29 +00:00
}
return tmp;
}
void*
wrealloc(void *ptr, size_t newsize)
1998-09-29 22:36:29 +00:00
{
void *nptr;
if (!ptr) {
2004-10-12 21:28:27 +00:00
nptr = wmalloc(newsize);
} else if (newsize==0) {
wfree(ptr);
nptr = NULL;
} else {
1999-10-09 20:07:23 +00:00
#ifdef TEST_WITH_GC
2004-10-12 21:28:27 +00:00
nptr = GC_realloc(ptr, newsize);
1999-10-09 20:07:23 +00:00
#else
2004-10-12 21:28:27 +00:00
nptr = realloc(ptr, newsize);
1999-10-09 20:07:23 +00:00
#endif
if (nptr==NULL) {
wwarning("realloc() failed. Retrying after 2s.");
sleep(2);
1999-10-09 20:07:23 +00:00
#ifdef TEST_WITH_GC
nptr = GC_realloc(ptr, newsize);
1999-10-09 20:07:23 +00:00
#else
nptr = realloc(ptr, newsize);
1999-10-09 20:07:23 +00:00
#endif
if (nptr == NULL) {
if (Aborting) {
fputs("Really Bad Error: recursive realloc() failure.",
stderr);
exit(-1);
} else {
wfatal("virtual memory exhausted");
Aborting=1;
wAbort(False);
}
}
}
1998-09-29 22:36:29 +00:00
}
return nptr;
}
void*
wretain(void *ptr)
{
int *refcount;
2004-10-12 21:28:27 +00:00
1998-09-29 22:36:29 +00:00
if (!table) {
2004-10-12 21:28:27 +00:00
table = WMCreateHashTable(WMIntHashCallbacks);
1998-09-29 22:36:29 +00:00
}
2004-10-12 21:28:27 +00:00
1998-09-29 22:36:29 +00:00
refcount = WMHashGet(table, ptr);
if (!refcount) {
2004-10-12 21:28:27 +00:00
refcount = wmalloc(sizeof(int));
*refcount = 1;
WMHashInsert(table, ptr, refcount);
1998-09-29 22:36:29 +00:00
#ifdef VERBOSE
2004-10-12 21:28:27 +00:00
printf("== %i (%p)\n", *refcount, ptr);
1998-09-29 22:36:29 +00:00
#endif
} else {
2004-10-12 21:28:27 +00:00
(*refcount)++;
1998-09-29 22:36:29 +00:00
#ifdef VERBOSE
2004-10-12 21:28:27 +00:00
printf("+ %i (%p)\n", *refcount, ptr);
1998-09-29 22:36:29 +00:00
#endif
}
2004-10-12 21:28:27 +00:00
1998-09-29 22:36:29 +00:00
return ptr;
}
1999-10-09 20:07:23 +00:00
void
wfree(void *ptr)
{
#ifdef TEST_WITH_GC
GC_free(ptr);
#else
free(ptr);
#endif
}
1998-09-29 22:36:29 +00:00
void
wrelease(void *ptr)
{
int *refcount;
2004-10-12 21:28:27 +00:00
1998-09-29 22:36:29 +00:00
refcount = WMHashGet(table, ptr);
if (!refcount) {
2004-10-12 21:28:27 +00:00
wwarning("trying to release unexisting data %p", ptr);
1998-09-29 22:36:29 +00:00
} else {
2004-10-12 21:28:27 +00:00
(*refcount)--;
if (*refcount < 1) {
1998-09-29 22:36:29 +00:00
#ifdef VERBOSE
2004-10-12 21:28:27 +00:00
printf("RELEASING %p\n", ptr);
1998-09-29 22:36:29 +00:00
#endif
2004-10-12 21:28:27 +00:00
WMHashRemove(table, ptr);
wfree(refcount);
wfree(ptr);
}
1998-09-29 22:36:29 +00:00
#ifdef VERBOSE
2004-10-12 21:28:27 +00:00
else {
printf("- %i (%p)\n", *refcount, ptr);
}
1998-09-29 22:36:29 +00:00
#endif
}
}