2 * Copyright (C) 2003-2010 The Music Player Daemon Project
3 * http://www.musicpd.org
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 GMutex
*tag_pool_lock
= NULL
;
27 #define NUM_SLOTS 4096
35 static struct slot
*slots
[NUM_SLOTS
];
37 static inline unsigned
38 calc_hash_n(enum tag_type type
, const char *p
, size_t length
)
45 hash
= (hash
<< 5) + hash
+ *p
++;
50 static inline unsigned
51 calc_hash(enum tag_type type
, const char *p
)
58 hash
= (hash
<< 5) + hash
+ *p
++;
63 static inline struct slot
*
64 tag_item_to_slot(struct tag_item
*item
)
66 return (struct slot
*)(((char*)item
) - offsetof(struct slot
, item
));
69 static struct slot
*slot_alloc(struct slot
*next
,
71 const char *value
, int length
)
75 slot
= g_malloc(sizeof(*slot
) - sizeof(slot
->item
.value
) + length
+ 1);
78 slot
->item
.type
= type
;
79 memcpy(slot
->item
.value
, value
, length
);
80 slot
->item
.value
[length
] = 0;
84 void tag_pool_init(void)
86 g_assert(tag_pool_lock
== NULL
);
87 tag_pool_lock
= g_mutex_new();
90 void tag_pool_deinit(void)
92 g_assert(tag_pool_lock
!= NULL
);
93 g_mutex_free(tag_pool_lock
);
98 tag_pool_get_item(enum tag_type type
, const char *value
, size_t length
)
100 struct slot
**slot_p
, *slot
;
102 slot_p
= &slots
[calc_hash_n(type
, value
, length
) % NUM_SLOTS
];
103 for (slot
= *slot_p
; slot
!= NULL
; slot
= slot
->next
) {
104 if (slot
->item
.type
== type
&&
105 length
== strlen(slot
->item
.value
) &&
106 memcmp(value
, slot
->item
.value
, length
) == 0 &&
108 assert(slot
->ref
> 0);
114 slot
= slot_alloc(*slot_p
, type
, value
, length
);
119 struct tag_item
*tag_pool_dup_item(struct tag_item
*item
)
121 struct slot
*slot
= tag_item_to_slot(item
);
123 assert(slot
->ref
> 0);
125 if (slot
->ref
< 0xff) {
129 /* the reference counter overflows above 0xff;
130 duplicate the item, and start with 1 */
131 size_t length
= strlen(item
->value
);
132 struct slot
**slot_p
=
133 &slots
[calc_hash_n(item
->type
, item
->value
,
134 length
) % NUM_SLOTS
];
135 slot
= slot_alloc(*slot_p
, item
->type
,
136 item
->value
, strlen(item
->value
));
142 void tag_pool_put_item(struct tag_item
*item
)
144 struct slot
**slot_p
, *slot
;
146 slot
= tag_item_to_slot(item
);
147 assert(slot
->ref
> 0);
153 for (slot_p
= &slots
[calc_hash(item
->type
, item
->value
) % NUM_SLOTS
];
155 slot_p
= &(*slot_p
)->next
) {
156 assert(*slot_p
!= NULL
);
159 *slot_p
= slot
->next
;