diff options
author | Max Kellermann <max@duempel.org> | 2014-01-07 22:46:22 +0100 |
---|---|---|
committer | Max Kellermann <max@duempel.org> | 2014-01-07 23:24:59 +0100 |
commit | fe6094a8227c61e055a49fcfd8720b42c5276ac1 (patch) | |
tree | 7a1a3bc55c7cb3ed44ca090e0017f460f2645e02 | |
parent | 8a30c7992dd59f9a742969dceaad0a71ae2ed296 (diff) | |
download | mpd-fe6094a8227c61e055a49fcfd8720b42c5276ac1.tar.gz mpd-fe6094a8227c61e055a49fcfd8720b42c5276ac1.tar.xz mpd-fe6094a8227c61e055a49fcfd8720b42c5276ac1.zip |
tag/TagPool: use NewVarSize() to allocate TagPoolSlot
-rw-r--r-- | src/tag/TagPool.cxx | 48 |
1 files changed, 27 insertions, 21 deletions
diff --git a/src/tag/TagPool.cxx b/src/tag/TagPool.cxx index 7e2dfd6b5..189dbdcc4 100644 --- a/src/tag/TagPool.cxx +++ b/src/tag/TagPool.cxx @@ -21,8 +21,7 @@ #include "TagPool.hxx" #include "TagItem.hxx" #include "util/Cast.hxx" - -#include <glib.h> +#include "util/VarSize.hxx" #include <assert.h> #include <string.h> @@ -35,8 +34,30 @@ struct TagPoolSlot { TagPoolSlot *next; unsigned char ref; TagItem item; + + TagPoolSlot(TagPoolSlot *_next, TagType type, + const char *value, size_t length) + :next(_next), ref(1) { + item.type = type; + memcpy(item.value, value, length); + item.value[length] = 0; + } + + static TagPoolSlot *Create(TagPoolSlot *_next, TagType type, + const char *value, size_t length); } gcc_packed; +TagPoolSlot * +TagPoolSlot::Create(TagPoolSlot *_next, TagType type, + const char *value, size_t length) +{ + TagPoolSlot *dummy; + return NewVarSize<TagPoolSlot>(sizeof(dummy->item.value), + length + 1, + _next, type, + value, length); +} + static TagPoolSlot *slots[NUM_SLOTS]; static inline unsigned @@ -71,21 +92,6 @@ tag_item_to_slot(TagItem *item) return ContainerCast(item, TagPoolSlot, item); } -static TagPoolSlot * -slot_alloc(TagPoolSlot *next, TagType type, const char *value, int length) -{ - TagPoolSlot *slot; - - slot = (TagPoolSlot *) - g_malloc(sizeof(*slot) - sizeof(slot->item.value) + length + 1); - slot->next = next; - slot->ref = 1; - slot->item.type = type; - memcpy(slot->item.value, value, length); - slot->item.value[length] = 0; - return slot; -} - TagItem * tag_pool_get_item(TagType type, const char *value, size_t length) { @@ -103,7 +109,7 @@ tag_pool_get_item(TagType type, const char *value, size_t length) } } - slot = slot_alloc(*slot_p, type, value, length); + slot = TagPoolSlot::Create(*slot_p, type, value, length); *slot_p = slot; return &slot->item; } @@ -125,8 +131,8 @@ tag_pool_dup_item(TagItem *item) TagPoolSlot **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 = TagPoolSlot::Create(*slot_p, item->type, + item->value, strlen(item->value)); *slot_p = slot; return &slot->item; } @@ -151,5 +157,5 @@ tag_pool_put_item(TagItem *item) } *slot_p = slot->next; - g_free(slot); + DeleteVarSize(slot); } |