1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
|
/*
* Copyright 2003-2018 The Music Player Daemon Project
* http://www.musicpd.org
*
* 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.
*
* 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.
*/
#include "config.h"
#include "Pool.hxx"
#include "Item.hxx"
#include "util/Cast.hxx"
#include "util/VarSize.hxx"
#include "util/StringView.hxx"
#include <limits>
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
Mutex tag_pool_lock;
static constexpr size_t NUM_SLOTS = 4093;
struct TagPoolSlot {
TagPoolSlot *next;
uint8_t ref = 1;
TagItem item;
static constexpr unsigned MAX_REF = std::numeric_limits<decltype(ref)>::max();
TagPoolSlot(TagPoolSlot *_next, TagType type,
StringView value) noexcept
:next(_next) {
item.type = type;
memcpy(item.value, value.data, value.size);
item.value[value.size] = 0;
}
static TagPoolSlot *Create(TagPoolSlot *_next, TagType type,
StringView value) noexcept;
};
TagPoolSlot *
TagPoolSlot::Create(TagPoolSlot *_next, TagType type,
StringView value) noexcept
{
TagPoolSlot *dummy;
return NewVarSize<TagPoolSlot>(sizeof(dummy->item.value),
value.size + 1,
_next, type,
value);
}
static TagPoolSlot *slots[NUM_SLOTS];
static inline unsigned
calc_hash(TagType type, StringView p) noexcept
{
unsigned hash = 5381;
for (auto ch : p)
hash = (hash << 5) + hash + ch;
return hash ^ type;
}
static inline unsigned
calc_hash(TagType type, const char *p) noexcept
{
unsigned hash = 5381;
assert(p != nullptr);
while (*p != 0)
hash = (hash << 5) + hash + *p++;
return hash ^ type;
}
static inline constexpr TagPoolSlot *
tag_item_to_slot(TagItem *item) noexcept
{
return &ContainerCast(*item, &TagPoolSlot::item);
}
static inline TagPoolSlot **
tag_value_slot_p(TagType type, StringView value) noexcept
{
return &slots[calc_hash(type, value) % NUM_SLOTS];
}
static inline TagPoolSlot **
tag_value_slot_p(TagType type, const char *value) noexcept
{
return &slots[calc_hash(type, value) % NUM_SLOTS];
}
TagItem *
tag_pool_get_item(TagType type, StringView value) noexcept
{
auto slot_p = tag_value_slot_p(type, value);
for (auto slot = *slot_p; slot != nullptr; slot = slot->next) {
if (slot->item.type == type &&
value.Equals(slot->item.value) &&
slot->ref < TagPoolSlot::MAX_REF) {
assert(slot->ref > 0);
++slot->ref;
return &slot->item;
}
}
auto slot = TagPoolSlot::Create(*slot_p, type, value);
*slot_p = slot;
return &slot->item;
}
TagItem *
tag_pool_dup_item(TagItem *item) noexcept
{
TagPoolSlot *slot = tag_item_to_slot(item);
assert(slot->ref > 0);
if (slot->ref < TagPoolSlot::MAX_REF) {
++slot->ref;
return item;
} else {
/* the reference counter overflows above MAX_REF;
obtain a reference to a different TagPoolSlot which
isn't yet "full" */
return tag_pool_get_item(item->type, item->value);
}
}
void
tag_pool_put_item(TagItem *item) noexcept
{
TagPoolSlot **slot_p, *slot;
slot = tag_item_to_slot(item);
assert(slot->ref > 0);
--slot->ref;
if (slot->ref > 0)
return;
for (slot_p = tag_value_slot_p(item->type, item->value);
*slot_p != slot;
slot_p = &(*slot_p)->next) {
assert(*slot_p != nullptr);
}
*slot_p = slot->next;
DeleteVarSize(slot);
}
|