Pool.cxx 3.79 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2021 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 21
#include "Pool.hxx"
#include "Item.hxx"
22
#include "util/Cast.hxx"
23
#include "util/VarSize.hxx"
24
#include "util/StringView.hxx"
25

26
#include <cassert>
27
#include <cstdint>
28 29
#include <limits>

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 = 16127;
36

37 38
struct TagPoolSlot {
	TagPoolSlot *next;
39
	uint8_t ref = 1;
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) noexcept
46
		:next(_next) {
47
		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) noexcept;
54
};
55

56 57
TagPoolSlot *
TagPoolSlot::Create(TagPoolSlot *_next, TagType type,
58
		    StringView value) noexcept
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) noexcept
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) noexcept
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
static constexpr TagPoolSlot *
94
tag_item_to_slot(TagItem *item) noexcept
95
{
96
	return &ContainerCast(*item, &TagPoolSlot::item);
97 98
}

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

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

Max Kellermann's avatar
Max Kellermann committed
111
TagItem *
112
tag_pool_get_item(TagType type, StringView value) noexcept
113
{
114
	auto slot_p = tag_value_slot_p(type, value);
115
	for (auto slot = *slot_p; slot != nullptr; slot = slot->next) {
116
		if (slot->item.type == type &&
117
		    value.Equals(slot->item.value) &&
118
		    slot->ref < TagPoolSlot::MAX_REF) {
119 120 121 122 123 124
			assert(slot->ref > 0);
			++slot->ref;
			return &slot->item;
		}
	}

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

Max Kellermann's avatar
Max Kellermann committed
130
TagItem *
131
tag_pool_dup_item(TagItem *item) noexcept
132
{
133
	TagPoolSlot *slot = tag_item_to_slot(item);
134 135 136

	assert(slot->ref > 0);

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

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

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

	if (slot->ref > 0)
		return;

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

	*slot_p = slot->next;
167
	DeleteVarSize(slot);
168
}