configure.ac: require libmpdclient 2.2
[ncmpc.git] / src / str_pool.c
blob57c4c4f9f724b2b0521b99c55c093526326958d3
1 /* ncmpc (Ncurses MPD Client)
2 * (c) 2004-2010 The Music Player Daemon Project
3 * Project homepage: http://musicpd.org
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "str_pool.h"
22 #include <assert.h>
23 #include <stddef.h>
24 #include <stdlib.h>
25 #include <string.h>
27 #define NUM_SLOTS 4096
29 struct slot {
30 struct slot *next;
31 unsigned char ref;
32 char value[1];
33 } __attribute__((packed));
35 static struct slot *slots[NUM_SLOTS];
37 static inline unsigned
38 calc_hash(const char *p)
40 unsigned hash = 5381;
42 assert(p != NULL);
44 while (*p != 0)
45 hash = (hash << 5) + hash + *p++;
47 return hash;
50 static inline struct slot *
51 value_to_slot(char *value)
53 return (struct slot*)(value - offsetof(struct slot, value));
56 static struct slot *slot_alloc(struct slot *next, const char *value)
58 size_t length = strlen(value);
59 struct slot *slot = malloc(sizeof(*slot) + length);
60 if (slot == NULL)
61 abort(); /* XXX */
63 slot->next = next;
64 slot->ref = 1;
65 memcpy(slot->value, value, length + 1);
66 return slot;
69 char *str_pool_get(const char *value)
71 struct slot **slot_p, *slot;
73 slot_p = &slots[calc_hash(value) % NUM_SLOTS];
74 for (slot = *slot_p; slot != NULL; slot = slot->next) {
75 if (strcmp(value, slot->value) == 0 && slot->ref < 0xff) {
76 assert(slot->ref > 0);
77 ++slot->ref;
78 return slot->value;
82 slot = slot_alloc(*slot_p, value);
83 *slot_p = slot;
84 return slot->value;
87 char *str_pool_dup(char *value)
89 struct slot *slot = value_to_slot(value);
91 assert(slot->ref > 0);
93 if (slot->ref < 0xff) {
94 ++slot->ref;
95 return value;
96 } else {
97 /* the reference counter overflows above 0xff;
98 duplicate the value, and start with 1 */
99 struct slot **slot_p =
100 &slots[calc_hash(slot->value) % NUM_SLOTS];
101 slot = slot_alloc(*slot_p, slot->value);
102 *slot_p = slot;
103 return slot->value;
107 void str_pool_put(char *value)
109 struct slot **slot_p, *slot;
111 slot = value_to_slot(value);
112 assert(slot->ref > 0);
113 --slot->ref;
115 if (slot->ref > 0)
116 return;
118 for (slot_p = &slots[calc_hash(value) % NUM_SLOTS];
119 *slot_p != slot;
120 slot_p = &(*slot_p)->next) {
121 assert(*slot_p != NULL);
124 *slot_p = slot->next;
125 free(slot);