| 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 | static Agraph_t *Ag_dictop_G; |
| 17 | |
| 18 | /* only indirect call through dtopen() is expected */ |
| 19 | void *agdictobjmem(Dict_t * dict, void * p, size_t size, Dtdisc_t * disc) |
| 20 | { |
| 21 | Agraph_t *g; |
| 22 | |
| 23 | NOTUSED(dict); |
| 24 | NOTUSED(disc); |
| 25 | g = Ag_dictop_G; |
| 26 | if (g) { |
| 27 | if (p) |
| 28 | agfree(g, p); |
| 29 | else |
| 30 | return agalloc(g, size); |
| 31 | } else { |
| 32 | if (p) |
| 33 | free(p); |
| 34 | else |
| 35 | return malloc(size); |
| 36 | } |
| 37 | return NIL(void *); |
| 38 | } |
| 39 | |
| 40 | void agdictobjfree(Dict_t * dict, void * p, Dtdisc_t * disc) |
| 41 | { |
| 42 | Agraph_t *g; |
| 43 | |
| 44 | NOTUSED(dict); |
| 45 | NOTUSED(disc); |
| 46 | g = Ag_dictop_G; |
| 47 | if (g) |
| 48 | agfree(g, p); |
| 49 | else |
| 50 | free(p); |
| 51 | } |
| 52 | |
| 53 | Dict_t *agdtopen(Agraph_t * g, Dtdisc_t * disc, Dtmethod_t * method) |
| 54 | { |
| 55 | Dtmemory_f memf; |
| 56 | Dict_t *d; |
| 57 | |
| 58 | memf = disc->memoryf; |
| 59 | disc->memoryf = agdictobjmem; |
| 60 | Ag_dictop_G = g; |
| 61 | d = dtopen(disc, method); |
| 62 | disc->memoryf = memf; |
| 63 | Ag_dictop_G = NIL(Agraph_t*); |
| 64 | return d; |
| 65 | } |
| 66 | |
| 67 | long agdtdelete(Agraph_t * g, Dict_t * dict, void *obj) |
| 68 | { |
| 69 | Ag_dictop_G = g; |
| 70 | return (long) dtdelete(dict, obj); |
| 71 | } |
| 72 | |
| 73 | int agobjfinalize(void * obj) |
| 74 | { |
| 75 | agfree(Ag_dictop_G, obj); |
| 76 | return 0; |
| 77 | } |
| 78 | |
| 79 | int agdtclose(Agraph_t * g, Dict_t * dict) |
| 80 | { |
| 81 | Dtmemory_f memf; |
| 82 | Dtdisc_t *disc; |
| 83 | |
| 84 | disc = dtdisc(dict, NIL(Dtdisc_t *), 0); |
| 85 | memf = disc->memoryf; |
| 86 | disc->memoryf = agdictobjmem; |
| 87 | Ag_dictop_G = g; |
| 88 | if (dtclose(dict)) |
| 89 | return 1; |
| 90 | disc->memoryf = memf; |
| 91 | Ag_dictop_G = NIL(Agraph_t*); |
| 92 | return 0; |
| 93 | } |
| 94 | |
| 95 | void agdtdisc(Agraph_t * g, Dict_t * dict, Dtdisc_t * disc) |
| 96 | { |
| 97 | if (disc && (dtdisc(dict, NIL(Dtdisc_t *), 0) != disc)) { |
| 98 | dtdisc(dict, disc, 0); |
| 99 | } |
| 100 | /* else unchanged, disc is same as old disc */ |
| 101 | } |
| 102 | |