TagPool.cxx 3.44 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2013 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

24 25
#include <glib.h>

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

29
Mutex tag_pool_lock;
30

31 32 33 34 35
#define NUM_SLOTS 4096

struct slot {
	struct slot *next;
	unsigned char ref;
Max Kellermann's avatar
Max Kellermann committed
36
	TagItem item;
37 38
} mpd_packed;

39
static struct slot *slots[NUM_SLOTS];
40 41 42 43 44 45

static inline unsigned
calc_hash_n(enum tag_type type, const char *p, size_t length)
{
	unsigned hash = 5381;

Max Kellermann's avatar
Max Kellermann committed
46
	assert(p != nullptr);
47 48 49 50 51 52 53 54 55 56 57 58

	while (length-- > 0)
		hash = (hash << 5) + hash + *p++;

	return hash ^ type;
}

static inline unsigned
calc_hash(enum tag_type type, const char *p)
{
	unsigned hash = 5381;

Max Kellermann's avatar
Max Kellermann committed
59
	assert(p != nullptr);
60 61 62 63 64 65 66 67

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

	return hash ^ type;
}

static inline struct slot *
Max Kellermann's avatar
Max Kellermann committed
68
tag_item_to_slot(TagItem *item)
69 70 71 72
{
	return (struct slot*)(((char*)item) - offsetof(struct slot, item));
}

73 74 75 76
static struct slot *slot_alloc(struct slot *next,
			       enum tag_type type,
			       const char *value, int length)
{
77 78
	struct slot *slot;

Max Kellermann's avatar
Max Kellermann committed
79 80
	slot = (struct slot *)
		g_malloc(sizeof(*slot) - sizeof(slot->item.value) + length + 1);
81 82 83 84 85 86 87 88
	slot->next = next;
	slot->ref = 1;
	slot->item.type = type;
	memcpy(slot->item.value, value, length);
	slot->item.value[length] = 0;
	return slot;
}

Max Kellermann's avatar
Max Kellermann committed
89
TagItem *
90
tag_pool_get_item(enum tag_type type, const char *value, size_t length)
91 92 93 94
{
	struct slot **slot_p, *slot;

	slot_p = &slots[calc_hash_n(type, value, length) % NUM_SLOTS];
Max Kellermann's avatar
Max Kellermann committed
95
	for (slot = *slot_p; slot != nullptr; slot = slot->next) {
96
		if (slot->item.type == type &&
97 98 99
		    length == strlen(slot->item.value) &&
		    memcmp(value, slot->item.value, length) == 0 &&
		    slot->ref < 0xff) {
100 101 102 103 104 105
			assert(slot->ref > 0);
			++slot->ref;
			return &slot->item;
		}
	}

106
	slot = slot_alloc(*slot_p, type, value, length);
107 108 109 110
	*slot_p = slot;
	return &slot->item;
}

Max Kellermann's avatar
Max Kellermann committed
111 112
TagItem *
tag_pool_dup_item(TagItem *item)
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
{
	struct slot *slot = tag_item_to_slot(item);

	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 */
		size_t length = strlen(item->value);
		struct slot **slot_p =
			&slots[calc_hash_n(item->type, item->value,
					   length) % NUM_SLOTS];
		slot = slot_alloc(*slot_p, item->type,
				  item->value, strlen(item->value));
		*slot_p = slot;
		return &slot->item;
	}
}

Max Kellermann's avatar
Max Kellermann committed
135 136
void
tag_pool_put_item(TagItem *item)
137 138 139 140 141 142 143 144 145 146 147 148 149
{
	struct slot **slot_p, *slot;

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

	if (slot->ref > 0)
		return;

	for (slot_p = &slots[calc_hash(item->type, item->value) % NUM_SLOTS];
	     *slot_p != slot;
	     slot_p = &(*slot_p)->next) {
Max Kellermann's avatar
Max Kellermann committed
150
		assert(*slot_p != nullptr);
151 152 153
	}

	*slot_p = slot->next;
154
	g_free(slot);
155
}