1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2010, 2011, 2012 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 #include "libpspp/intern.h"
24 #include "libpspp/assertion.h"
25 #include "libpspp/cast.h"
26 #include "libpspp/hash-functions.h"
27 #include "libpspp/hmap.h"
29 #include "gl/xalloc.h"
31 /* A single interned string. */
32 struct interned_string
34 struct hmap_node node; /* Node in hash table. */
35 size_t ref_cnt; /* Reference count. */
36 size_t length; /* strlen(string). */
37 char string[1]; /* Null-terminated string. */
40 /* All interned strings. */
41 static struct hmap interns = HMAP_INITIALIZER (interns);
43 /* Searches the table of interned strings for one equal to S, which has length
44 LENGTH and hash value HASH. */
45 static struct interned_string *
46 intern_lookup__ (const char *s, size_t length, unsigned int hash)
48 struct interned_string *is;
50 HMAP_FOR_EACH_WITH_HASH (is, struct interned_string, node, hash, &interns)
51 if (is->length == length && !memcmp (s, is->string, length))
57 /* Returns an interned version of string S. Pass the returned string to
58 intern_unref() to release it. */
60 intern_new (const char *s)
62 return intern_buffer (s, strlen (s));
66 intern_buffer (const char *s, size_t length)
68 unsigned int hash = hash_bytes (s, length, 0);
69 struct interned_string *is;
71 is = intern_lookup__ (s, length, hash);
76 is = xmalloc (length + sizeof *is);
77 hmap_insert (&interns, &is->node, hash);
80 memcpy (is->string, s, length + 1);
85 static struct interned_string *
86 interned_string_from_string (const char *s_)
88 char (*s)[1] = (char (*)[1]) s_;
89 struct interned_string *is = UP_CAST (s, struct interned_string, string);
90 assert (is->ref_cnt > 0);
94 /* Increases the reference count on S, which must be an interned string
95 returned by intern_new(). */
97 intern_ref (const char *s)
99 struct interned_string *is = interned_string_from_string (s);
104 /* Decreases the reference count on S, which must be an interned string
105 returned by intern_new(). If the reference count reaches 0, frees the
108 intern_unref (const char *s)
110 struct interned_string *is = interned_string_from_string (s);
111 if (--is->ref_cnt == 0)
113 hmap_delete (&interns, &is->node);
118 /* Returns the length of interned string S. */
120 intern_strlen (const char *s)
122 return interned_string_from_string (s)->length;
125 /* Given null-terminated string S, returns true if S is an interned string
126 returned by intern_string_new(), false otherwise.
128 This is appropriate for use in debug assertions, e.g.:
129 assert (is_interned_string (s));
132 is_interned_string (const char *s)
134 size_t length = strlen (s);
135 unsigned int hash = hash_bytes (s, length, 0);
136 return intern_lookup__ (s, length, hash) != NULL;