mpd/src/tag/TagPool.cxx

172 lines
3.8 KiB
C++
Raw Normal View History

/*
2017-01-03 20:48:59 +01:00
* Copyright 2003-2017 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"
2013-01-07 10:36:27 +01:00
#include "TagPool.hxx"
#include "TagItem.hxx"
2013-12-26 11:42:34 +01:00
#include "util/Cast.hxx"
#include "util/VarSize.hxx"
2015-09-30 22:03:01 +02:00
#include "util/StringView.hxx"
2013-01-30 22:53:12 +01:00
2016-03-14 08:07:22 +01:00
#include <limits>
#include <assert.h>
2013-07-30 20:11:57 +02:00
#include <string.h>
#include <stdlib.h>
Mutex tag_pool_lock;
static constexpr size_t NUM_SLOTS = 4093;
struct TagPoolSlot {
TagPoolSlot *next;
unsigned char ref;
2013-07-30 20:11:57 +02:00
TagItem item;
2016-03-14 08:07:22 +01:00
static constexpr unsigned MAX_REF = std::numeric_limits<decltype(ref)>::max();
TagPoolSlot(TagPoolSlot *_next, TagType type,
2015-09-30 22:03:01 +02:00
StringView value)
:next(_next), ref(1) {
item.type = type;
2015-09-30 22:03:01 +02:00
memcpy(item.value, value.data, value.size);
item.value[value.size] = 0;
}
static TagPoolSlot *Create(TagPoolSlot *_next, TagType type,
2015-09-30 22:03:01 +02:00
StringView value);
};
TagPoolSlot *
TagPoolSlot::Create(TagPoolSlot *_next, TagType type,
2015-09-30 22:03:01 +02:00
StringView value)
{
TagPoolSlot *dummy;
return NewVarSize<TagPoolSlot>(sizeof(dummy->item.value),
2015-09-30 22:03:01 +02:00
value.size + 1,
_next, type,
2015-09-30 22:03:01 +02:00
value);
}
static TagPoolSlot *slots[NUM_SLOTS];
static inline unsigned
2015-09-30 22:03:01 +02:00
calc_hash(TagType type, StringView p)
{
unsigned hash = 5381;
2015-09-30 22:03:01 +02:00
for (auto ch : p)
hash = (hash << 5) + hash + ch;
return hash ^ type;
}
static inline unsigned
calc_hash(TagType type, const char *p)
{
unsigned hash = 5381;
2013-01-07 10:36:27 +01:00
assert(p != nullptr);
while (*p != 0)
hash = (hash << 5) + hash + *p++;
return hash ^ type;
}
#if CLANG_OR_GCC_VERSION(4,7)
constexpr
#endif
static inline TagPoolSlot *
2013-07-30 20:11:57 +02:00
tag_item_to_slot(TagItem *item)
{
return &ContainerCast(*item, &TagPoolSlot::item);
}
static inline TagPoolSlot **
2015-09-30 22:03:01 +02:00
tag_value_slot_p(TagType type, StringView value)
{
2015-09-30 22:03:01 +02:00
return &slots[calc_hash(type, value) % NUM_SLOTS];
}
static inline TagPoolSlot **
tag_value_slot_p(TagType type, const char *value)
{
return &slots[calc_hash(type, value) % NUM_SLOTS];
}
2013-07-30 20:11:57 +02:00
TagItem *
2015-09-30 22:03:01 +02:00
tag_pool_get_item(TagType type, StringView value)
{
2015-09-30 22:03:01 +02:00
auto slot_p = tag_value_slot_p(type, value);
for (auto slot = *slot_p; slot != nullptr; slot = slot->next) {
if (slot->item.type == type &&
2015-09-30 22:03:01 +02:00
value.Equals(slot->item.value) &&
2016-03-14 08:07:22 +01:00
slot->ref < TagPoolSlot::MAX_REF) {
assert(slot->ref > 0);
++slot->ref;
return &slot->item;
}
}
2015-09-30 22:03:01 +02:00
auto slot = TagPoolSlot::Create(*slot_p, type, value);
*slot_p = slot;
return &slot->item;
}
2013-07-30 20:11:57 +02:00
TagItem *
tag_pool_dup_item(TagItem *item)
{
TagPoolSlot *slot = tag_item_to_slot(item);
assert(slot->ref > 0);
2016-03-14 08:07:22 +01:00
if (slot->ref < TagPoolSlot::MAX_REF) {
++slot->ref;
return item;
} else {
2016-03-14 08:07:22 +01:00
/* 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);
}
}
2013-07-30 20:11:57 +02:00
void
tag_pool_put_item(TagItem *item)
{
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) {
2013-01-07 10:36:27 +01:00
assert(*slot_p != nullptr);
}
*slot_p = slot->next;
DeleteVarSize(slot);
}