1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
|
/* $Id: mem.c,v 1.3 2009/06/03 01:10:51 ellson Exp $ $Revision: 1.3 $ */
/* vim:set shiftwidth=4 ts=8: */
/**********************************************************
* This software is part of the graphviz package *
* http://www.graphviz.org/ *
* *
* Copyright (c) 1994-2004 AT&T Corp. *
* and is licensed under the *
* Common Public License, Version 1.0 *
* by AT&T Corp. *
* *
* Information and Software Systems Research *
* AT&T Research, Florham Park NJ *
**********************************************************/
#include <cghdr.h>
/* memory management discipline and entry points */
#if (HAVE_AST || HAVE_VMALLOC)
/* vmalloc based allocator */
static void *memopen(void)
{
#if DEBUG || MEMDEBUG
return vmopen(Vmdcheap, Vmdebug,
VM_MTDEBUG | VM_DBCHECK | VM_DBABORT | VM_TRACE);
#else
return vmopen(Vmdcheap, Vmbest, VM_MTBEST);
#endif
}
static void *memalloc(void *heap, size_t request)
{
void *rv;
rv = vmalloc((Vmalloc_t *) heap, request);
memset(rv, 0, request);
return rv;
}
static void *memresize(void *heap, void *ptr, size_t oldsize,
size_t request)
{
void *rv;
rv = vmresize((Vmalloc_t *) heap, ptr, request, VM_RSCOPY | VM_RSZERO);
return rv;
}
static void memfree(void *heap, void *ptr)
{
vmfree((Vmalloc_t *) heap, ptr);
}
static void memclose(void *heap)
{
vmclose((Vmalloc_t *) heap);
}
Agmemdisc_t AgMemDisc =
{ memopen, memalloc, memresize, memfree, memclose };
#else
/* malloc based allocator */
static void *memopen(void)
{
return NIL(void *);
}
static void *memalloc(void *heap, size_t request)
{
void *rv;
NOTUSED(heap);
rv = malloc(request);
memset(rv, 0, request);
return rv;
}
static void *memresize(void *heap, void *ptr, size_t oldsize,
size_t request)
{
void *rv;
NOTUSED(heap);
rv = realloc(ptr, request);
if (request > oldsize)
memset((char *) rv + oldsize, 0, request - oldsize);
return rv;
}
static void memfree(void *heap, void *ptr)
{
NOTUSED(heap);
free(ptr);
}
#ifndef WRONG
#define memclose 0
#else
static void memclose(void *heap)
{
NOTUSED(heap);
}
#endif
Agmemdisc_t AgMemDisc =
{ memopen, memalloc, memresize, memfree, memclose };
#endif
void *agalloc(Agraph_t * g, size_t size)
{
void *mem;
mem = AGDISC(g, mem)->alloc(AGCLOS(g, mem), size);
if (mem == NIL(void *))
agerr(AGERR,"memory allocation failure");
return mem;
}
void *agrealloc(Agraph_t * g, void *ptr, size_t oldsize, size_t size)
{
void *mem;
if (size > 0) {
if (ptr == 0)
mem = agalloc(g, size);
else
mem =
AGDISC(g, mem)->resize(AGCLOS(g, mem), ptr, oldsize, size);
if (mem == NIL(void *))
agerr(AGERR,"memory re-allocation failure");
} else
mem = NIL(void *);
return mem;
}
void agfree(Agraph_t * g, void *ptr)
{
if (ptr)
(AGDISC(g, mem)->free) (AGCLOS(g, mem), ptr);
}
#ifndef _VMALLOC_H
struct _vmalloc_s {
char unused;
};
#endif
struct _vmalloc_s *agheap(Agraph_t * g)
{
return AGCLOS(g, mem);
}
|