~ubuntu-branches/ubuntu/hardy/codeblocks/hardy-backports

« back to all changes in this revision

Viewing changes to src/plugins/compilergcc/depslib/src/newstr.c

  • Committer: Bazaar Package Importer
  • Author(s): Michael Casadevall
  • Date: 2008-07-17 04:39:23 UTC
  • Revision ID: james.westby@ubuntu.com-20080717043923-gmsy5cwkdjswghkm
Tags: upstream-8.02
ImportĀ upstreamĀ versionĀ 8.02

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* *** This file was borrowed from jam 2.5. The copyright statement from
 
2
 * *** jam.c appears below.
 
3
 */
 
4
/*
 
5
 * /+\
 
6
 * +\   Copyright 1993-2002 Christopher Seiwald and Perforce Software, Inc.
 
7
 * \+/
 
8
 *
 
9
 * This file is part of jam.
 
10
 *
 
11
 * License is hereby granted to use this software and distribute it
 
12
 * freely, as long as this copyright notice is retained and modifications 
 
13
 * are clearly marked.
 
14
 *
 
15
 * ALL WARRANTIES ARE HEREBY DISCLAIMED.
 
16
 */
 
17
 
 
18
/*
 
19
 * Copyright 1993, 1995 Christopher Seiwald.
 
20
 *
 
21
 * This file is part of Jam - see jam.c for Copyright information.
 
22
 */
 
23
 
 
24
/*
 
25
 * newstr.c - string manipulation routines
 
26
 *
 
27
 * To minimize string copying, string creation, copying, and freeing
 
28
 * is done through newstr.
 
29
 *
 
30
 * External functions:
 
31
 *
 
32
 *    newstr() - return a malloc'ed copy of a string
 
33
 *    copystr() - return a copy of a string previously returned by newstr()
 
34
 *    freestr() - free a string returned by newstr() or copystr()
 
35
 *    donestr() - free string tables
 
36
 *
 
37
 * Once a string is passed to newstr(), the returned string is readonly.
 
38
 *
 
39
 * This implementation builds a hash table of all strings, so that multiple 
 
40
 * calls of newstr() on the same string allocate memory for the string once.
 
41
 * Strings are never actually freed.
 
42
 *
 
43
 * 11/04/02 (seiwald) - const-ing for string literals
 
44
 */
 
45
 
 
46
# include "jam.h"
 
47
# include "newstr.h"
 
48
# include "hash.h"
 
49
 
 
50
#if 1 /* TNB */
 
51
#include "alloc.h"
 
52
static ALLOC *stralloc = 0;
 
53
#endif
 
54
 
 
55
typedef const char *STRING;
 
56
 
 
57
static struct hash *strhash = 0;
 
58
static int strtotal = 0;
 
59
 
 
60
/*
 
61
 * newstr() - return a malloc'ed copy of a string
 
62
 */
 
63
 
 
64
const char *
 
65
newstr( const char *string )
 
66
{
 
67
        STRING str, *s = &str;
 
68
 
 
69
        if( !strhash )
 
70
            strhash = hashinit( sizeof( STRING ), "strings" );
 
71
 
 
72
        *s = string;
 
73
 
 
74
        if( hashenter( strhash, (HASHDATA **)&s ) )
 
75
        {
 
76
            int l = strlen( string );
 
77
#if 1
 
78
                if (!stralloc)
 
79
                        stralloc = alloc2_init(4096);
 
80
                char *m = alloc2_enter(stralloc, l + 1);
 
81
#else
 
82
            char *m = (char *)malloc( l + 1 );
 
83
#endif
 
84
            if (DEBUG_MEM)
 
85
                    printf("newstr: allocating %d bytes\n", l + 1 );
 
86
 
 
87
            strtotal += l + 1;
 
88
            memcpy( m, string, l + 1 );
 
89
            *s = m;
 
90
        }
 
91
 
 
92
        return *s;
 
93
}
 
94
 
 
95
/*
 
96
 * copystr() - return a copy of a string previously returned by newstr()
 
97
 */
 
98
 
 
99
const char *
 
100
copystr( const char *s )
 
101
{
 
102
        return s;
 
103
}
 
104
 
 
105
/*
 
106
 * freestr() - free a string returned by newstr() or copystr()
 
107
 */
 
108
 
 
109
void
 
110
freestr( const char *s )
 
111
{
 
112
}
 
113
 
 
114
/*
 
115
 * donestr() - free string tables
 
116
 */
 
117
 
 
118
void
 
119
donestr()
 
120
{
 
121
        hashdone( strhash );
 
122
        strhash = 0; /* TNB */
 
123
        strtotal = 0; /* TNB */
 
124
        alloc_free(stralloc); /* TNB */
 
125
        stralloc = 0; /* TNB */
 
126
        if( DEBUG_MEM )
 
127
            printf( "%dK in strings\n", strtotal / 1024 );
 
128
}