summaryrefslogtreecommitdiff
path: root/src/libaudcore/strpool.cc
blob: 8f3679de46b97d6cd82cd64bf507e2214a6a09b0 (plain)
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*
 * strpool.c
 * Copyright 2011-2013 John Lindgren
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions, and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions, and the following disclaimer in the documentation
 *    provided with the distribution.
 *
 * This software is provided "as is" and without any warranty, express or
 * implied. In no event shall the authors be liable for any damages arising from
 * the use of this software.
 */

#include <assert.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>

#include "audstrings.h"
#include "internal.h"
#include "multihash.h"
#include "objects.h"
#include "runtime.h"

#ifdef VALGRIND_FRIENDLY

struct StrNode {
    unsigned hash;
    char magic;
    char str[];
};

#define NODE_SIZE_FOR(s) (offsetof (StrNode, str) + strlen (s) + 1)
#define NODE_OF(s) ((StrNode *) ((s) - offsetof (StrNode, str)))

EXPORT char * String::raw_get (const char * str)
{
    if (! str)
        return nullptr;

    StrNode * node = (StrNode *) malloc (NODE_SIZE_FOR (str));
    if (! node)
        throw std::bad_alloc ();

    node->magic = '@';
    node->hash = str_calc_hash (str);

    strcpy (node->str, str);
    return node->str;
}

EXPORT char * String::raw_ref (const char * str)
{
    if (! str)
        return nullptr;

    StrNode * node = NODE_OF (str);
    assert (node->magic == '@');
    assert (str_calc_hash (str) == node->hash);

    return raw_get (str);
}

EXPORT void String::raw_unref (char * str)
{
    if (! str)
        return;

    StrNode * node = NODE_OF (str);
    assert (node->magic == '@');
    assert (str_calc_hash (str) == node->hash);

    node->magic = 0;
    free (node);
}

EXPORT unsigned String::raw_hash (const char * str)
{
    if (! str)
        return 0;

    StrNode * node = NODE_OF (str);
    assert (node->magic == '@');

    return str_calc_hash (str);
}

EXPORT bool String::raw_equal (const char * str1, const char * str2)
{
    assert (! str1 || NODE_OF (str1)->magic == '@');
    assert (! str2 || NODE_OF (str2)->magic == '@');

    return ! strcmp_safe (str1, str2);
}

EXPORT void string_leak_check ()
{
}

#else /* ! VALGRIND_FRIENDLY */

struct StrNode {
    MultiHash::Node base;
    unsigned refs;
    char magic;
    char str[1];  // variable size
};

#define NODE_SIZE_FOR(s) (offsetof (StrNode, str) + strlen (s) + 1)
#define NODE_OF(s) ((StrNode *) ((s) - offsetof (StrNode, str)))

static bool match_cb (const MultiHash::Node * node_, const void * data_)
{
    const StrNode * node = (const StrNode *) node_;
    const char * data = (const char *) data_;

    return data == node->str || ! strcmp (data, node->str);
}

static MultiHash strpool_table (match_cb);

static MultiHash::Node * add_cb (const void * data_, void * state)
{
    const char * data = (const char *) data_;

    StrNode * node = (StrNode *) malloc (NODE_SIZE_FOR (data));
    if (! node)
        throw std::bad_alloc ();

    node->refs = 1;
    node->magic = '@';
    strcpy (node->str, data);

    * ((char * *) state) = node->str;
    return (MultiHash::Node *) node;
}

static bool ref_cb (MultiHash::Node * node_, void * state)
{
    StrNode * node = (StrNode *) node_;

    __sync_fetch_and_add (& node->refs, 1);

    * ((char * *) state) = node->str;
    return false;
}

/* If the pool contains a copy of <str>, increments its reference count.
 * Otherwise, adds a copy of <str> to the pool with a reference count of one.
 * In either case, returns the copy.  Because this copy may be shared by other
 * parts of the code, it should not be modified.  If <str> is null, simply
 * returns null with no side effects. */
EXPORT char * String::raw_get (const char * str)
{
    if (! str)
        return nullptr;

    char * ret = nullptr;
    strpool_table.lookup (str, str_calc_hash (str), add_cb, ref_cb, & ret);
    return ret;
}

/* Increments the reference count of <str>, where <str> is the address of a
 * string already in the pool.  Faster than calling raw_get() a second time.
 * Returns <str> for convenience.  If <str> is null, simply returns null with no
 * side effects. */
EXPORT char * String::raw_ref (const char * str)
{
    if (! str)
        return nullptr;

    StrNode * node = NODE_OF (str);
    assert (node->magic == '@');

    __sync_fetch_and_add (& node->refs, 1);

    return (char *) str;
}

static bool remove_cb (MultiHash::Node * node_, void * state)
{
    StrNode * node = (StrNode *) node_;

    if (! __sync_bool_compare_and_swap (& node->refs, 1, 0))
        return false;

    node->magic = 0;
    free (node);
    return true;
}

/* Decrements the reference count of <str>, where <str> is the address of a
 * string in the pool.  If the reference count drops to zero, releases the
 * memory used by <str>.   If <str> is null, simply returns null with no side
 * effects. */
EXPORT void String::raw_unref (char * str)
{
    if (! str)
        return;

    StrNode * node = NODE_OF (str);
    assert (node->magic == '@');

    while (1)
    {
        int refs = __sync_fetch_and_add (& node->refs, 0);

        if (refs > 1)
        {
            if (__sync_bool_compare_and_swap (& node->refs, refs, refs - 1))
                break;
        }
        else
        {
            int status = strpool_table.lookup (node->str, node->base.hash, nullptr,
             remove_cb, nullptr);

            assert (status & MultiHash::Found);
            if (status & MultiHash::Removed)
                break;
        }
    }
}

static bool leak_cb (MultiHash::Node * node, void * state)
{
    AUDWARN ("String leaked: %s\n", ((StrNode *) node)->str);
    return false;
}

void string_leak_check ()
{
    strpool_table.iterate (leak_cb, nullptr);
}

/* Returns the cached hash value of a pooled string (or 0 for null). */
EXPORT unsigned String::raw_hash (const char * str)
{
    if (! str)
        return 0;

    StrNode * node = NODE_OF (str);
    assert (node->magic == '@');

    return node->base.hash;
}


/* Checks whether two pooled strings are equal.  Since the pool never contains
 * duplicate strings, this is a simple pointer comparison and thus much faster
 * than strcmp().  null is considered equal to null but not equal to any string. */
EXPORT bool String::raw_equal (const char * str1, const char * str2)
{
    assert (! str1 || NODE_OF (str1)->magic == '@');
    assert (! str2 || NODE_OF (str2)->magic == '@');

    return str1 == str2;
}

#endif /* ! VALGRIND_FRIENDLY */