~ubuntu-branches/ubuntu/lucid/graphviz/lucid-security

« back to all changes in this revision

Viewing changes to graph/refstr.c

  • Committer: Bazaar Package Importer
  • Author(s): Stephen M Moraco
  • Date: 2002-02-05 18:52:12 UTC
  • Revision ID: james.westby@ubuntu.com-20020205185212-8i04c70te00rc40y
Tags: upstream-1.7.16
ImportĀ upstreamĀ versionĀ 1.7.16

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    This software may only be used by you under license from AT&T Corp.
 
3
    ("AT&T").  A copy of AT&T's Source Code Agreement is available at
 
4
    AT&T's Internet website having the URL:
 
5
    <http://www.research.att.com/sw/tools/graphviz/license/source.html>
 
6
    If you received this software without first entering into a license
 
7
    with AT&T, you have an infringing copy of this software and cannot use
 
8
    it without violating AT&T's intellectual property rights.
 
9
*/
 
10
#pragma prototyped
 
11
 
 
12
 
 
13
 
 
14
#include        "libgraph.h"
 
15
 
 
16
#ifdef DMALLOC
 
17
#include "dmalloc.h"
 
18
#endif
 
19
 
 
20
typedef struct refstr_t {
 
21
        Dtlink_t                link;
 
22
        unsigned int    refcnt;
 
23
        char                    s[1];
 
24
} refstr_t;
 
25
 
 
26
static Dtdisc_t Refstrdisc = {
 
27
        offsetof(refstr_t,s[0]),
 
28
        0,
 
29
        0,
 
30
        ((Dtmake_f)0),
 
31
        ((Dtfree_f)0),
 
32
        ((Dtcompar_f)0),                        /* use strcmp */
 
33
        ((Dthash_f)0),
 
34
        ((Dtmemory_f)0),
 
35
        ((Dtevent_f)0)
 
36
};
 
37
 
 
38
static Dict_t*  StringDict;
 
39
 
 
40
#ifdef DEBUG
 
41
static int refstrprint(refstr_t* r)
 
42
{
 
43
        fprintf(stderr,"%s\n",r->s); return 0;
 
44
}
 
45
 
 
46
agrefstrdump(void)
 
47
{
 
48
        dtwalk(StringDict,refstrprint);
 
49
}
 
50
#endif
 
51
 
 
52
static void initialize_strings(void)
 
53
{
 
54
        StringDict      = dtopen(&Refstrdisc,Dttree);
 
55
}
 
56
 
 
57
char *agstrdup(char* s)
 
58
{
 
59
        refstr_t                *key,*r;
 
60
 
 
61
        if (StringDict == NULL) initialize_strings();
 
62
        if (s == NULL) return s;
 
63
 
 
64
        key = (refstr_t*)(s - offsetof(refstr_t,s[0]));
 
65
        r = (refstr_t*) dtsearch(StringDict,key);
 
66
        if (r) r->refcnt++;
 
67
        else {
 
68
                r = (refstr_t*) malloc(sizeof(refstr_t)+strlen(s));
 
69
                r->refcnt = 1;
 
70
                strcpy(r->s,s);
 
71
                dtinsert(StringDict,r);
 
72
        }
 
73
        return r->s;
 
74
}
 
75
 
 
76
void agstrfree(char* s)
 
77
{
 
78
        refstr_t                *key,*r;
 
79
 
 
80
        if ((StringDict == NULL) || (s == NULL)) return;
 
81
        key = (refstr_t*)(s - offsetof(refstr_t,s[0]));
 
82
        r = (refstr_t*) dtsearch(StringDict,key);
 
83
 
 
84
        if (r) {
 
85
                r->refcnt--;
 
86
                if (r->refcnt <= 0) {
 
87
                        dtdelete(StringDict,r);
 
88
                        free(r);
 
89
                }
 
90
        }
 
91
        else fprintf(stderr,"agstrfree lost %s\n",s);
 
92
}