TagPool.cxx 3.72 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2015 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
#include "util/StringView.hxx"
26

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

31
Mutex tag_pool_lock;
32

33
static constexpr size_t NUM_SLOTS = 4096;
34

35 36
struct TagPoolSlot {
	TagPoolSlot *next;
37
	unsigned char ref;
Max Kellermann's avatar
Max Kellermann committed
38
	TagItem item;
39 40

	TagPoolSlot(TagPoolSlot *_next, TagType type,
41
		    StringView value)
42 43
		:next(_next), ref(1) {
		item.type = type;
44 45
		memcpy(item.value, value.data, value.size);
		item.value[value.size] = 0;
46 47 48
	}

	static TagPoolSlot *Create(TagPoolSlot *_next, TagType type,
49
				   StringView value);
50
} gcc_packed;
51

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

63
static TagPoolSlot *slots[NUM_SLOTS];
64 65

static inline unsigned
66
calc_hash(TagType type, StringView p)
67 68 69
{
	unsigned hash = 5381;

70 71
	for (auto ch : p)
		hash = (hash << 5) + hash + ch;
72 73 74 75 76

	return hash ^ type;
}

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

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

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

	return hash ^ type;
}

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

98
static inline TagPoolSlot **
99
tag_value_slot_p(TagType type, StringView value)
100
{
101
	return &slots[calc_hash(type, value) % NUM_SLOTS];
102 103 104 105 106 107 108 109
}

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
110
TagItem *
111
tag_pool_get_item(TagType type, StringView value)
112
{
113
	auto slot_p = tag_value_slot_p(type, value);
114
	for (auto slot = *slot_p; slot != nullptr; slot = slot->next) {
115
		if (slot->item.type == type &&
116
		    value.Equals(slot->item.value) &&
117
		    slot->ref < 0xff) {
118 119 120 121 122 123
			assert(slot->ref > 0);
			++slot->ref;
			return &slot->item;
		}
	}

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

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

	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 */
142
		auto slot_p = tag_value_slot_p(item->type, item->value);
143
		slot = TagPoolSlot::Create(*slot_p, item->type,
144
					   item->value);
145 146 147 148 149
		*slot_p = slot;
		return &slot->item;
	}
}

Max Kellermann's avatar
Max Kellermann committed
150 151
void
tag_pool_put_item(TagItem *item)
152
{
153
	TagPoolSlot **slot_p, *slot;
154 155 156 157 158 159 160 161

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

	if (slot->ref > 0)
		return;

162
	for (slot_p = tag_value_slot_p(item->type, item->value);
163 164
	     *slot_p != slot;
	     slot_p = &(*slot_p)->next) {
Max Kellermann's avatar
Max Kellermann committed
165
		assert(*slot_p != nullptr);
166 167 168
	}

	*slot_p = slot->next;
169
	DeleteVarSize(slot);
170
}