1
/* $Xorg: Alloc.c,v 1.4 2001/02/09 02:03:53 xorgcvs Exp $ */
3
/***********************************************************
4
Copyright 1987, 1988 by Digital Equipment Corporation, Maynard, Massachusetts,
5
Copyright 1993 by Sun Microsystems, Inc. Mountain View, CA.
9
Permission to use, copy, modify, and distribute this software and its
10
documentation for any purpose and without fee is hereby granted,
11
provided that the above copyright notice appear in all copies and that
12
both that copyright notice and this permission notice appear in
13
supporting documentation, and that the names of Digital or Sun not be
14
used in advertising or publicity pertaining to distribution of the
15
software without specific, written prior permission.
17
DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
18
ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
19
DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
20
ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
21
WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
22
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
25
SUN DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
26
INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FIT-
27
NESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SUN BE LI-
28
ABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
29
ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
30
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
31
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH
32
THE USE OR PERFORMANCE OF THIS SOFTWARE.
34
******************************************************************/
38
Copyright 1987, 1988, 1998 The Open Group
40
Permission to use, copy, modify, distribute, and sell this software and its
41
documentation for any purpose is hereby granted without fee, provided that
42
the above copyright notice appear in all copies and that both that
43
copyright notice and this permission notice appear in supporting
46
The above copyright notice and this permission notice shall be included in
47
all copies or substantial portions of the Software.
49
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
50
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
51
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
52
OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
53
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
54
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
56
Except as contained in this notice, the name of The Open Group shall not be
57
used in advertising or otherwise to promote the sale, use or other dealings
58
in this Software without prior written authorization from The Open Group.
61
/* $XFree86: xc/lib/Xt/Alloc.c,v 1.9 2001/12/14 19:56:07 dawes Exp $ */
64
* X Toolkit Memory Allocation Routines
66
* Uses Xlib memory management, which is spec'd to be re-entrant.
69
#include "IntrinsicI.h"
75
#define Xmalloc(size) malloc((size))
76
#define Xrealloc(ptr, size) realloc((ptr), (size))
77
#define Xcalloc(nelem, elsize) calloc((nelem), (elsize))
78
#define Xfree(ptr) free(ptr)
80
#ifdef _XNEEDBCOPYFUNC
81
void _XtBcopy(src, dst, length)
97
void _XtAllocError(type)
100
Cardinal num_params = 1;
101
if (type == NULL) type = "local memory allocation";
102
XtErrorMsg("allocError", type, XtCXtToolkitError,
103
"Cannot perform %s", &type, &num_params);
106
void _XtHeapInit(heap)
110
heap->bytes_remaining = 0;
113
#ifndef XTTRACEMEMORY
120
#if defined (MALLOC_0_RETURNS_NULL) && defined(XTMALLOC_BC)
121
/* preserve this (broken) behavior until everyone fixes their apps */
124
if ((ptr = Xmalloc(size)) == NULL)
125
_XtAllocError("malloc");
130
char *XtRealloc(ptr, size)
135
#if MALLOC_0_RETURNS_NULL
138
return(XtMalloc(size));
139
} else if ((ptr = Xrealloc(ptr, size)) == NULL
140
#if MALLOC_0_RETURNS_NULL
144
_XtAllocError("realloc");
149
char *XtCalloc(num, size)
154
#if defined (MALLOC_0_RETURNS_NULL) && defined(XTMALLOC_BC)
155
/* preserve this (broken) behavior until everyone fixes their apps */
156
if (!size) num = size = 1;
158
if ((ptr = Xcalloc(num, size)) == NULL)
159
_XtAllocError("calloc");
167
if (ptr != NULL) Xfree(ptr);
170
char* __XtMalloc(size)
173
#ifdef MALLOC_0_RETURNS_NULL
176
return XtMalloc (size);
179
char* __XtCalloc(num, size)
182
#ifdef MALLOC_0_RETURNS_NULL
183
if (!size) num = size = 1;
185
return XtCalloc(num, size);
188
#ifndef HEAP_SEGMENT_SIZE
189
#define HEAP_SEGMENT_SIZE 1492
192
char* _XtHeapAlloc(heap, bytes)
196
register char* heap_loc;
197
if (heap == NULL) return XtMalloc(bytes);
198
if (heap->bytes_remaining < (int)bytes) {
199
if ((bytes + sizeof(char*)) >= (HEAP_SEGMENT_SIZE>>1)) {
200
/* preserve current segment; insert this one in front */
202
printf( "allocating large segment (%d bytes) on heap %#x\n",
205
heap_loc = XtMalloc(bytes + sizeof(char*));
207
*(char**)heap_loc = *(char**)heap->start;
208
*(char**)heap->start = heap_loc;
211
*(char**)heap_loc = NULL;
212
heap->start = heap_loc;
214
return heap_loc + sizeof(char*);
216
/* else discard remainder of this segment */
218
printf( "allocating new segment on heap %#x\n", heap );
220
heap_loc = XtMalloc((unsigned)HEAP_SEGMENT_SIZE);
221
*(char**)heap_loc = heap->start;
222
heap->start = heap_loc;
223
heap->current = heap_loc + sizeof(char*);
224
heap->bytes_remaining = HEAP_SEGMENT_SIZE - sizeof(char*);
226
bytes = (bytes + (sizeof(long) - 1)) & (~(sizeof(long) - 1));
227
heap_loc = heap->current;
228
heap->current += bytes;
229
heap->bytes_remaining -= bytes; /* can be negative, if rounded */
233
void _XtHeapFree(heap)
236
char* segment = heap->start;
237
while (segment != NULL) {
238
char* next_segment = *(char**)segment;
240
segment = next_segment;
243
heap->bytes_remaining = 0;
249
* X Toolkit Memory Trace Allocation Routines
257
typedef struct _Stats *StatsPtr;
258
typedef struct _Stats {
267
static StatsPtr XtMemory = (StatsPtr)NULL;
268
static unsigned long ActiveXtMemory = 0;
269
static unsigned long XtSeqId = 0;
270
static unsigned long XtSeqBreakpoint = ~0;
272
#define StatsSize(n) ((((n) + (sizeof(long) - 1)) & ~(sizeof(long) - 1)) + sizeof(Stats))
273
#define ToStats(ptr) ((StatsPtr)(ptr - sizeof(Stats)))
274
#define ToMem(ptr) (((char *)ptr) + sizeof(Stats))
276
#define CHAIN(ptr,len,hp) \
277
ptr->next = XtMemory; \
279
XtMemory->prev = ptr; \
281
ptr->prev = (StatsPtr)NULL; \
287
ActiveXtMemory += len; \
288
ptr->seq = XtSeqId; \
289
if (XtSeqId == XtSeqBreakpoint) \
290
_XtBreakpoint(ptr); \
294
static void _XtBreakpoint(mem)
297
mem->seq = XtSeqId; /* avoid being optimized out of existence */
300
char *_XtMalloc(size, file, line)
310
newsize = StatsSize(size);
311
if ((ptr = (StatsPtr)Xmalloc(newsize)) == NULL)
312
_XtAllocError("malloc");
313
CHAIN(ptr, size, NULL);
314
retval = (ToMem(ptr));
322
return _XtMalloc(size, (char *)NULL, 0);
325
char *_XtRealloc(ptr, size, file, line)
334
newptr = _XtMalloc(size, file, line);
336
unsigned copysize = ToStats(ptr)->size;
337
if (copysize > size) copysize = size;
338
memmove(newptr, ptr, copysize);
345
char *XtRealloc(ptr, size)
349
return _XtRealloc(ptr, size, (char *)NULL, 0);
352
char *_XtCalloc(num, size, file, line)
358
unsigned total, newsize;
363
newsize = StatsSize(total);
364
if ((ptr = (StatsPtr)Xcalloc(newsize, 1)) == NULL)
365
_XtAllocError("calloc");
366
CHAIN(ptr, total, NULL);
367
retval = (ToMem(ptr));
372
char *XtCalloc(num, size)
375
return _XtCalloc(num, size, (char *)NULL, 0);
378
Boolean _XtIsValidPointer(ptr)
381
register StatsPtr mem;
382
register StatsPtr stp = ToStats(ptr);
385
for (mem = XtMemory; mem; mem = mem->next) {
395
Boolean _XtValidateMemory = False;
400
register StatsPtr stp;
404
if (_XtValidateMemory && !_XtIsValidPointer(ptr))
408
ActiveXtMemory -= stp->size;
410
stp->prev->next = stp->next;
412
XtMemory = stp->next;
414
stp->next->prev = stp->prev;
426
char *_XtHeapMalloc(heap, size, file, line)
434
XtPointer hp = (XtPointer) heap;
438
newsize = StatsSize(size);
439
if ((ptr = (StatsPtr)Xmalloc(newsize)) == NULL)
440
_XtAllocError("malloc");
441
CHAIN(ptr, size, hp);
442
retval = (ToMem(ptr));
447
void _XtHeapFree(heap)
448
register XtPointer heap;
450
register StatsPtr mem, next;
453
for (mem = XtMemory; mem; mem = next) {
455
if (mem->heap == heap) {
457
ActiveXtMemory -= mem->size;
459
mem->prev->next = next;
463
next->prev = mem->prev;
472
void _XtPrintMemory(filename)
475
register StatsPtr mem;
478
if (filename == NULL)
481
f = fopen(filename, "w");
483
fprintf(f, "total size: %d\n", ActiveXtMemory);
484
for (mem = XtMemory; mem; mem = mem->next) {
486
fprintf(f, "size: %6d seq: %5d %12s(%4d) %s\n",
488
mem->file, mem->line, mem->heap ? "heap" : "");
491
if (filename) fclose(f);
494
#endif /* XTTRACEMEMORY */