Updating to version 0.20.2
[wmaker-crm.git] / WINGs / memory.c
blobe76e4154d9c8d932278b63c20a479715f47e871f
1 /*
2 * Window Maker miscelaneous function library
3 *
4 * Copyright (c) 1997 Alfredo K. Kojima
5 *
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.
22 #include "../src/config.h"
24 #include "WUtil.h"
26 #include <stdlib.h>
27 #include <sys/types.h>
28 #include <unistd.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <assert.h>
33 #ifndef False
34 # define False 0
35 #endif
36 #ifndef True
37 # define True 1
38 #endif
40 extern void wAbort(int);
42 static int Aborting=0; /* if we're in the middle of an emergency exit */
45 static WMHashTable *table = NULL;
48 void *wmalloc(size_t size)
50 void *tmp;
52 tmp = malloc(size);
53 if (tmp == NULL) {
54 wwarning("malloc() failed. Retrying after 2s.");
55 sleep(2);
56 tmp = malloc(size);
57 if (tmp == NULL) {
58 if (Aborting) {
59 puts("Real Bad Error: recursive malloc() failure.");
60 exit(-1);
61 } else {
62 wfatal("virtual memory exhausted");
63 Aborting=1;
64 wAbort(False);
68 return tmp;
72 void *wrealloc(void *ptr, size_t newsize)
74 void *nptr;
76 if (!ptr) {
77 nptr = malloc(newsize);
78 } else {
79 nptr=realloc(ptr, newsize);
81 if (nptr==NULL) {
82 printf("Could not do realloc");
83 return NULL;
85 return nptr;
89 void*
90 wretain(void *ptr)
92 int *refcount;
94 if (!table) {
95 table = WMCreateHashTable(WMIntHashCallbacks);
98 refcount = WMHashGet(table, ptr);
99 if (!refcount) {
100 refcount = wmalloc(sizeof(int));
101 *refcount = 1;
102 WMHashInsert(table, ptr, refcount);
103 #ifdef VERBOSE
104 printf("== %i (%p)\n", *refcount, ptr);
105 #endif
106 } else {
107 (*refcount)++;
108 #ifdef VERBOSE
109 printf("+ %i (%p)\n", *refcount, ptr);
110 #endif
113 return ptr;
117 void
118 wrelease(void *ptr)
120 int *refcount;
122 refcount = WMHashGet(table, ptr);
123 if (!refcount) {
124 wwarning("trying to release unexisting data %p", ptr);
125 } else {
126 (*refcount)--;
127 if (*refcount < 1) {
128 #ifdef VERBOSE
129 printf("RELEASING %p\n", ptr);
130 #endif
131 WMHashRemove(table, ptr);
132 free(refcount);
133 free(ptr);
135 #ifdef VERBOSE
136 else {
137 printf("- %i (%p)\n", *refcount, ptr);
139 #endif
144 char*
145 wstrdup(char *str)
147 assert(str!=NULL);
149 return strcpy(wmalloc(strlen(str)+1), str);
153 char*
154 wstrappend(char *dst, char *src)
156 char *str;
158 if (!dst)
159 return wstrdup(src);
160 else if (!src)
161 return wstrdup(dst);
163 str = wmalloc(strlen(dst)+strlen(src)+1);
164 strcpy(str, dst);
165 strcat(str, src);
167 return str;