configure.ac: Add subheaders to Input/Sticker/Autodiscovery/Metadata and Misc.
[mpd-mk.git] / src / tag_pool.c
blob6ad1e1f2d4581e9f6afd8a8be33268a75d3ef5a1
1 /*
2 * Copyright (C) 2003-2010 The Music Player Daemon Project
3 * http://www.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 "config.h"
21 #include "tag_pool.h"
23 #include <assert.h>
25 GMutex *tag_pool_lock = NULL;
27 #define NUM_SLOTS 4096
29 struct slot {
30 struct slot *next;
31 unsigned char ref;
32 struct tag_item item;
33 } mpd_packed;
35 static struct slot *slots[NUM_SLOTS];
37 static inline unsigned
38 calc_hash_n(enum tag_type type, const char *p, size_t length)
40 unsigned hash = 5381;
42 assert(p != NULL);
44 while (length-- > 0)
45 hash = (hash << 5) + hash + *p++;
47 return hash ^ type;
50 static inline unsigned
51 calc_hash(enum tag_type type, const char *p)
53 unsigned hash = 5381;
55 assert(p != NULL);
57 while (*p != 0)
58 hash = (hash << 5) + hash + *p++;
60 return hash ^ type;
63 static inline struct slot *
64 tag_item_to_slot(struct tag_item *item)
66 return (struct slot*)(((char*)item) - offsetof(struct slot, item));
69 static struct slot *slot_alloc(struct slot *next,
70 enum tag_type type,
71 const char *value, int length)
73 struct slot *slot;
75 slot = g_malloc(sizeof(*slot) - sizeof(slot->item.value) + length + 1);
76 slot->next = next;
77 slot->ref = 1;
78 slot->item.type = type;
79 memcpy(slot->item.value, value, length);
80 slot->item.value[length] = 0;
81 return slot;
84 void tag_pool_init(void)
86 g_assert(tag_pool_lock == NULL);
87 tag_pool_lock = g_mutex_new();
90 void tag_pool_deinit(void)
92 g_assert(tag_pool_lock != NULL);
93 g_mutex_free(tag_pool_lock);
94 tag_pool_lock = NULL;
97 struct tag_item *
98 tag_pool_get_item(enum tag_type type, const char *value, size_t length)
100 struct slot **slot_p, *slot;
102 slot_p = &slots[calc_hash_n(type, value, length) % NUM_SLOTS];
103 for (slot = *slot_p; slot != NULL; slot = slot->next) {
104 if (slot->item.type == type &&
105 length == strlen(slot->item.value) &&
106 memcmp(value, slot->item.value, length) == 0 &&
107 slot->ref < 0xff) {
108 assert(slot->ref > 0);
109 ++slot->ref;
110 return &slot->item;
114 slot = slot_alloc(*slot_p, type, value, length);
115 *slot_p = slot;
116 return &slot->item;
119 struct tag_item *tag_pool_dup_item(struct tag_item *item)
121 struct slot *slot = tag_item_to_slot(item);
123 assert(slot->ref > 0);
125 if (slot->ref < 0xff) {
126 ++slot->ref;
127 return item;
128 } else {
129 /* the reference counter overflows above 0xff;
130 duplicate the item, and start with 1 */
131 size_t length = strlen(item->value);
132 struct slot **slot_p =
133 &slots[calc_hash_n(item->type, item->value,
134 length) % NUM_SLOTS];
135 slot = slot_alloc(*slot_p, item->type,
136 item->value, strlen(item->value));
137 *slot_p = slot;
138 return &slot->item;
142 void tag_pool_put_item(struct tag_item *item)
144 struct slot **slot_p, *slot;
146 slot = tag_item_to_slot(item);
147 assert(slot->ref > 0);
148 --slot->ref;
150 if (slot->ref > 0)
151 return;
153 for (slot_p = &slots[calc_hash(item->type, item->value) % NUM_SLOTS];
154 *slot_p != slot;
155 slot_p = &(*slot_p)->next) {
156 assert(*slot_p != NULL);
159 *slot_p = slot->next;
160 g_free(slot);