TagPool.cxx 3.77 KB
Newer Older
1
/*
2
 * Copyright 2003-2016 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 28
#include <limits>

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

33
Mutex tag_pool_lock;
34

35
static constexpr size_t NUM_SLOTS = 4093;
36

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

42 43
	static constexpr unsigned MAX_REF = std::numeric_limits<decltype(ref)>::max();

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

	static TagPoolSlot *Create(TagPoolSlot *_next, TagType type,
53
				   StringView value);
54
} gcc_packed;
55

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

67
static TagPoolSlot *slots[NUM_SLOTS];
68 69

static inline unsigned
70
calc_hash(TagType type, StringView p)
71 72 73
{
	unsigned hash = 5381;

74 75
	for (auto ch : p)
		hash = (hash << 5) + hash + ch;
76 77 78 79 80

	return hash ^ type;
}

static inline unsigned
81
calc_hash(TagType type, const char *p)
82 83 84
{
	unsigned hash = 5381;

Max Kellermann's avatar
Max Kellermann committed
85
	assert(p != nullptr);
86 87 88 89 90 91 92

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

	return hash ^ type;
}

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

102
static inline TagPoolSlot **
103
tag_value_slot_p(TagType type, StringView value)
104
{
105
	return &slots[calc_hash(type, value) % NUM_SLOTS];
106 107 108 109 110 111 112 113
}

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

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

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

	assert(slot->ref > 0);

140
	if (slot->ref < TagPoolSlot::MAX_REF) {
141 142 143
		++slot->ref;
		return item;
	} else {
144
		/* the reference counter overflows above MAX_REF;
145 146
		   obtain a reference to a different TagPoolSlot which
		   isn't yet "full" */
Max Kellermann's avatar
Max Kellermann committed
147
		return tag_pool_get_item(item->type, item->value);
148 149 150
	}
}

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

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

	if (slot->ref > 0)
		return;

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

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