| 1 | /* $Id$ $Revision$ */ |
| 2 | /* vim:set shiftwidth=4 ts=8: */ |
| 3 | |
| 4 | /************************************************************************* |
| 5 | * Copyright (c) 2011 AT&T Intellectual Property |
| 6 | * All rights reserved. This program and the accompanying materials |
| 7 | * are made available under the terms of the Eclipse Public License v1.0 |
| 8 | * which accompanies this distribution, and is available at |
| 9 | * http://www.eclipse.org/legal/epl-v10.html |
| 10 | * |
| 11 | * Contributors: See CVS logs. Details at http://www.graphviz.org/ |
| 12 | *************************************************************************/ |
| 13 | |
| 14 | #include <cghdr.h> |
| 15 | |
| 16 | /* memory management discipline and entry points */ |
| 17 | static void *memopen(Agdisc_t* disc) |
| 18 | { |
| 19 | return NIL(void *); |
| 20 | } |
| 21 | |
| 22 | static void *memalloc(void *heap, size_t request) |
| 23 | { |
| 24 | void *rv; |
| 25 | |
| 26 | NOTUSED(heap); |
| 27 | rv = malloc(request); |
| 28 | memset(rv, 0, request); |
| 29 | return rv; |
| 30 | } |
| 31 | |
| 32 | static void *memresize(void *heap, void *ptr, size_t oldsize, |
| 33 | size_t request) |
| 34 | { |
| 35 | void *rv; |
| 36 | |
| 37 | NOTUSED(heap); |
| 38 | rv = realloc(ptr, request); |
| 39 | if (request > oldsize) |
| 40 | memset((char *) rv + oldsize, 0, request - oldsize); |
| 41 | return rv; |
| 42 | } |
| 43 | |
| 44 | static void memfree(void *heap, void *ptr) |
| 45 | { |
| 46 | NOTUSED(heap); |
| 47 | free(ptr); |
| 48 | } |
| 49 | |
| 50 | #ifndef WRONG |
| 51 | #define memclose 0 |
| 52 | #else |
| 53 | static void memclose(void *heap) |
| 54 | { |
| 55 | NOTUSED(heap); |
| 56 | } |
| 57 | #endif |
| 58 | |
| 59 | Agmemdisc_t AgMemDisc = |
| 60 | { memopen, memalloc, memresize, memfree, memclose }; |
| 61 | |
| 62 | void *agalloc(Agraph_t * g, size_t size) |
| 63 | { |
| 64 | void *mem; |
| 65 | |
| 66 | mem = AGDISC(g, mem)->alloc(AGCLOS(g, mem), size); |
| 67 | if (mem == NIL(void *)) |
| 68 | agerr(AGERR,"memory allocation failure" ); |
| 69 | return mem; |
| 70 | } |
| 71 | |
| 72 | void *agrealloc(Agraph_t * g, void *ptr, size_t oldsize, size_t size) |
| 73 | { |
| 74 | void *mem; |
| 75 | |
| 76 | if (size > 0) { |
| 77 | if (ptr == 0) |
| 78 | mem = agalloc(g, size); |
| 79 | else |
| 80 | mem = |
| 81 | AGDISC(g, mem)->resize(AGCLOS(g, mem), ptr, oldsize, size); |
| 82 | if (mem == NIL(void *)) |
| 83 | agerr(AGERR,"memory re-allocation failure" ); |
| 84 | } else |
| 85 | mem = NIL(void *); |
| 86 | return mem; |
| 87 | } |
| 88 | |
| 89 | void agfree(Agraph_t * g, void *ptr) |
| 90 | { |
| 91 | if (ptr) |
| 92 | (AGDISC(g, mem)->free) (AGCLOS(g, mem), ptr); |
| 93 | } |
| 94 | |
| 95 | #ifndef _VMALLOC_H |
| 96 | struct _vmalloc_s { |
| 97 | char unused; |
| 98 | }; |
| 99 | #endif |
| 100 | struct _vmalloc_s *agheap(Agraph_t * g) |
| 101 | { |
| 102 | return AGCLOS(g, mem); |
| 103 | } |
| 104 | |