TagPool.cxx 3.96 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2014 The Music Player Daemon Project
3
 * http://www.musicpd.org
4 5 6 7 8 9 10 11 12 13
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
14 15 16 17
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 19
 */

20
#include "config.h"
Max Kellermann's avatar
Max Kellermann committed
21
#include "TagPool.hxx"
22
#include "TagItem.hxx"
23
#include "util/Cast.hxx"
24
#include "util/VarSize.hxx"
25

26
#include <assert.h>
Max Kellermann's avatar
Max Kellermann committed
27
#include <string.h>
28
#include <stdlib.h>
29

30
Mutex tag_pool_lock;
31

32
static constexpr size_t NUM_SLOTS = 4096;
33

34 35
struct TagPoolSlot {
	TagPoolSlot *next;
36
	unsigned char ref;
Max Kellermann's avatar
Max Kellermann committed
37
	TagItem item;
38 39 40 41 42 43 44 45 46 47 48

	TagPoolSlot(TagPoolSlot *_next, TagType type,
		    const char *value, size_t length)
		:next(_next), ref(1) {
		item.type = type;
		memcpy(item.value, value, length);
		item.value[length] = 0;
	}

	static TagPoolSlot *Create(TagPoolSlot *_next, TagType type,
				   const char *value, size_t length);
49
} gcc_packed;
50

51 52 53 54 55 56 57 58 59 60 61
TagPoolSlot *
TagPoolSlot::Create(TagPoolSlot *_next, TagType type,
		    const char *value, size_t length)
{
	TagPoolSlot *dummy;
	return NewVarSize<TagPoolSlot>(sizeof(dummy->item.value),
				       length + 1,
				       _next, type,
				       value, length);
}

62
static TagPoolSlot *slots[NUM_SLOTS];
63 64

static inline unsigned
65
calc_hash_n(TagType type, const char *p, size_t length)
66 67 68
{
	unsigned hash = 5381;

Max Kellermann's avatar
Max Kellermann committed
69
	assert(p != nullptr);
70 71 72 73 74 75 76 77

	while (length-- > 0)
		hash = (hash << 5) + hash + *p++;

	return hash ^ type;
}

static inline unsigned
78
calc_hash(TagType type, const char *p)
79 80 81
{
	unsigned hash = 5381;

Max Kellermann's avatar
Max Kellermann committed
82
	assert(p != nullptr);
83 84 85 86 87 88 89

	while (*p != 0)
		hash = (hash << 5) + hash + *p++;

	return hash ^ type;
}

90
#if CLANG_OR_GCC_VERSION(4,7)
91 92 93
	constexpr
#endif
static inline TagPoolSlot *
Max Kellermann's avatar
Max Kellermann committed
94
tag_item_to_slot(TagItem *item)
95
{
96
	return &ContainerCast(*item, &TagPoolSlot::item);
97 98
}

99 100 101 102 103 104 105 106 107 108 109 110
static inline TagPoolSlot **
tag_value_slot_p(TagType type, const char *value, size_t length)
{
	return &slots[calc_hash_n(type, value, length) % NUM_SLOTS];
}

static inline TagPoolSlot **
tag_value_slot_p(TagType type, const char *value)
{
	return &slots[calc_hash(type, value) % NUM_SLOTS];
}

Max Kellermann's avatar
Max Kellermann committed
111
TagItem *
112
tag_pool_get_item(TagType type, const char *value, size_t length)
113
{
114 115
	auto slot_p = tag_value_slot_p(type, value, length);
	for (auto slot = *slot_p; slot != nullptr; slot = slot->next) {
116
		if (slot->item.type == type &&
117 118 119
		    length == strlen(slot->item.value) &&
		    memcmp(value, slot->item.value, length) == 0 &&
		    slot->ref < 0xff) {
120 121 122 123 124 125
			assert(slot->ref > 0);
			++slot->ref;
			return &slot->item;
		}
	}

126
	auto slot = TagPoolSlot::Create(*slot_p, type, value, length);
127 128 129 130
	*slot_p = slot;
	return &slot->item;
}

Max Kellermann's avatar
Max Kellermann committed
131 132
TagItem *
tag_pool_dup_item(TagItem *item)
133
{
134
	TagPoolSlot *slot = tag_item_to_slot(item);
135 136 137 138 139 140 141 142 143 144

	assert(slot->ref > 0);

	if (slot->ref < 0xff) {
		++slot->ref;
		return item;
	} else {
		/* the reference counter overflows above 0xff;
		   duplicate the item, and start with 1 */
		size_t length = strlen(item->value);
145 146
		auto slot_p = tag_value_slot_p(item->type,
					       item->value, length);
147 148
		slot = TagPoolSlot::Create(*slot_p, item->type,
					   item->value, strlen(item->value));
149 150 151 152 153
		*slot_p = slot;
		return &slot->item;
	}
}

Max Kellermann's avatar
Max Kellermann committed
154 155
void
tag_pool_put_item(TagItem *item)
156
{
157
	TagPoolSlot **slot_p, *slot;
158 159 160 161 162 163 164 165

	slot = tag_item_to_slot(item);
	assert(slot->ref > 0);
	--slot->ref;

	if (slot->ref > 0)
		return;

166
	for (slot_p = tag_value_slot_p(item->type, item->value);
167 168
	     *slot_p != slot;
	     slot_p = &(*slot_p)->next) {
Max Kellermann's avatar
Max Kellermann committed
169
		assert(*slot_p != nullptr);
170 171 172
	}

	*slot_p = slot->next;
173
	DeleteVarSize(slot);
174
}