2004-02-24 00:41:20 +01:00
|
|
|
/* the Music Player Daemon (MPD)
|
2007-04-05 05:22:33 +02:00
|
|
|
* Copyright (C) 2003-2007 by Warren Dukes (warren.dukes@gmail.com)
|
2004-02-24 00:41:20 +01:00
|
|
|
* This project's homepage is: 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "tag.h"
|
2008-09-07 13:28:01 +02:00
|
|
|
#include "tag_internal.h"
|
tag: added a pool for tag items
The new source tag_pool.c manages a pool of reference counted tag_item
objects. This is used to merge tag items of the same type and value,
saving lots of memory. Formerly, only the value itself was pooled,
wasting memory for all the pointers and tag_item structs.
The following results were measured with massif. Started MPD on
amd64, typed "mpc", no song being played. My music database contains
35k tagged songs. The results are what massif reports as "peak".
0.13.2: total 14,131,392; useful 11,408,972; extra 2,722,420
eric: total 18,370,696; useful 15,648,182; extra 2,722,514
mk f34f694: total 15,833,952; useful 13,111,470; extra 2,722,482
mk now: total 12,837,632; useful 10,626,383; extra 2,211,249
This patch set saves 20% memory, and does a good job in reducing heap
fragmentation.
2008-08-29 09:38:37 +02:00
|
|
|
#include "tag_pool.h"
|
2004-04-13 04:38:09 +02:00
|
|
|
#include "log.h"
|
2004-10-05 19:16:26 +02:00
|
|
|
#include "conf.h"
|
2007-03-20 21:12:53 +01:00
|
|
|
#include "song.h"
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2008-10-31 13:57:10 +01:00
|
|
|
#include <glib.h>
|
2008-10-08 10:49:29 +02:00
|
|
|
#include <assert.h>
|
2008-11-01 14:33:14 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2008-10-08 10:49:29 +02:00
|
|
|
|
2008-09-06 15:31:55 +02:00
|
|
|
/**
|
|
|
|
* Maximum number of items managed in the bulk list; if it is
|
|
|
|
* exceeded, we switch back to "normal" reallocation.
|
|
|
|
*/
|
|
|
|
#define BULK_MAX 64
|
|
|
|
|
|
|
|
static struct {
|
|
|
|
#ifndef NDEBUG
|
|
|
|
int busy;
|
|
|
|
#endif
|
|
|
|
struct tag_item *items[BULK_MAX];
|
|
|
|
} bulk;
|
|
|
|
|
2008-02-05 11:17:33 +01:00
|
|
|
const char *mpdTagItemKeys[TAG_NUM_OF_ITEM_TYPES] = {
|
2004-11-10 22:58:27 +01:00
|
|
|
"Artist",
|
|
|
|
"Album",
|
2009-01-13 23:43:20 +01:00
|
|
|
"AlbumArtist",
|
2004-11-10 22:58:27 +01:00
|
|
|
"Title",
|
|
|
|
"Track",
|
|
|
|
"Name",
|
|
|
|
"Genre",
|
2005-03-05 23:24:10 +01:00
|
|
|
"Date",
|
|
|
|
"Composer",
|
|
|
|
"Performer",
|
2006-04-30 21:55:56 +02:00
|
|
|
"Comment",
|
|
|
|
"Disc"
|
2004-11-10 22:58:27 +01:00
|
|
|
};
|
|
|
|
|
2008-09-29 13:29:33 +02:00
|
|
|
int8_t ignoreTagItems[TAG_NUM_OF_ITEM_TYPES];
|
2004-11-10 22:58:27 +01:00
|
|
|
|
2008-09-07 19:14:43 +02:00
|
|
|
static size_t items_size(const struct tag *tag)
|
|
|
|
{
|
2008-09-07 19:14:47 +02:00
|
|
|
return (tag->numOfItems * sizeof(struct tag_item *));
|
2008-09-07 19:14:43 +02:00
|
|
|
}
|
|
|
|
|
2008-08-29 09:38:21 +02:00
|
|
|
void tag_lib_init(void)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2004-11-10 22:58:27 +01:00
|
|
|
int quit = 0;
|
2006-07-20 18:02:40 +02:00
|
|
|
char *temp;
|
|
|
|
char *s;
|
|
|
|
char *c;
|
|
|
|
ConfigParam *param;
|
2004-11-10 22:58:27 +01:00
|
|
|
int i;
|
2004-11-11 22:43:39 +01:00
|
|
|
|
|
|
|
/* parse the "metadata_to_use" config parameter below */
|
2006-07-20 18:02:40 +02:00
|
|
|
|
2004-11-10 22:58:27 +01:00
|
|
|
memset(ignoreTagItems, 0, TAG_NUM_OF_ITEM_TYPES);
|
2006-07-20 18:02:40 +02:00
|
|
|
ignoreTagItems[TAG_ITEM_COMMENT] = 1; /* ignore comments by default */
|
2004-11-10 22:58:27 +01:00
|
|
|
|
|
|
|
param = getConfigParam(CONF_METADATA_TO_USE);
|
2006-07-20 18:02:40 +02:00
|
|
|
|
|
|
|
if (!param)
|
|
|
|
return;
|
2004-11-10 22:58:27 +01:00
|
|
|
|
|
|
|
memset(ignoreTagItems, 1, TAG_NUM_OF_ITEM_TYPES);
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (0 == strcasecmp(param->value, "none"))
|
|
|
|
return;
|
2004-11-10 22:58:27 +01:00
|
|
|
|
2008-11-01 14:33:14 +01:00
|
|
|
temp = c = s = g_strdup(param->value);
|
2006-07-20 18:02:40 +02:00
|
|
|
while (!quit) {
|
|
|
|
if (*s == ',' || *s == '\0') {
|
|
|
|
if (*s == '\0')
|
|
|
|
quit = 1;
|
2004-11-10 22:58:27 +01:00
|
|
|
*s = '\0';
|
2006-07-20 18:02:40 +02:00
|
|
|
for (i = 0; i < TAG_NUM_OF_ITEM_TYPES; i++) {
|
|
|
|
if (strcasecmp(c, mpdTagItemKeys[i]) == 0) {
|
2004-11-10 22:58:27 +01:00
|
|
|
ignoreTagItems[i] = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2006-07-20 18:02:40 +02:00
|
|
|
if (strlen(c) && i == TAG_NUM_OF_ITEM_TYPES) {
|
2007-05-26 20:15:54 +02:00
|
|
|
FATAL("error parsing metadata item \"%s\" at "
|
2006-07-20 18:02:40 +02:00
|
|
|
"line %i\n", c, param->line);
|
2004-11-10 22:58:27 +01:00
|
|
|
}
|
|
|
|
s++;
|
|
|
|
c = s;
|
|
|
|
}
|
|
|
|
s++;
|
|
|
|
}
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2004-11-10 22:58:27 +01:00
|
|
|
free(temp);
|
2004-04-13 04:20:46 +02:00
|
|
|
}
|
|
|
|
|
2008-08-29 14:48:39 +02:00
|
|
|
struct tag *tag_ape_load(const char *file)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-08-29 09:38:11 +02:00
|
|
|
struct tag *ret = NULL;
|
2008-03-26 11:37:36 +01:00
|
|
|
FILE *fp;
|
2005-03-07 01:51:21 +01:00
|
|
|
int tagCount;
|
2006-07-20 18:02:40 +02:00
|
|
|
char *buffer = NULL;
|
|
|
|
char *p;
|
2008-03-26 11:38:07 +01:00
|
|
|
size_t tagLen;
|
|
|
|
size_t size;
|
2005-03-07 01:51:21 +01:00
|
|
|
unsigned long flags;
|
|
|
|
int i;
|
2006-07-20 18:02:40 +02:00
|
|
|
char *key;
|
2005-03-07 01:51:21 +01:00
|
|
|
|
|
|
|
struct {
|
|
|
|
unsigned char id[8];
|
2008-11-01 14:11:19 +01:00
|
|
|
uint32_t version;
|
|
|
|
uint32_t length;
|
|
|
|
uint32_t tagCount;
|
2005-03-07 01:51:21 +01:00
|
|
|
unsigned char flags[4];
|
|
|
|
unsigned char reserved[8];
|
|
|
|
} footer;
|
|
|
|
|
2008-02-05 11:17:33 +01:00
|
|
|
const char *apeItems[7] = {
|
2005-03-07 01:51:21 +01:00
|
|
|
"title",
|
|
|
|
"artist",
|
|
|
|
"album",
|
|
|
|
"comment",
|
|
|
|
"genre",
|
|
|
|
"track",
|
|
|
|
"year"
|
|
|
|
};
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
int tagItems[7] = {
|
2005-03-07 01:51:21 +01:00
|
|
|
TAG_ITEM_TITLE,
|
|
|
|
TAG_ITEM_ARTIST,
|
|
|
|
TAG_ITEM_ALBUM,
|
|
|
|
TAG_ITEM_COMMENT,
|
|
|
|
TAG_ITEM_GENRE,
|
|
|
|
TAG_ITEM_TRACK,
|
|
|
|
TAG_ITEM_DATE,
|
|
|
|
};
|
|
|
|
|
|
|
|
fp = fopen(file, "r");
|
2006-07-20 18:02:40 +02:00
|
|
|
if (!fp)
|
|
|
|
return NULL;
|
2005-03-07 01:51:21 +01:00
|
|
|
|
|
|
|
/* determine if file has an apeV2 tag */
|
2006-07-20 18:02:40 +02:00
|
|
|
if (fseek(fp, 0, SEEK_END))
|
|
|
|
goto fail;
|
2008-03-26 11:38:07 +01:00
|
|
|
size = (size_t)ftell(fp);
|
2006-07-20 18:02:40 +02:00
|
|
|
if (fseek(fp, size - sizeof(footer), SEEK_SET))
|
|
|
|
goto fail;
|
|
|
|
if (fread(&footer, 1, sizeof(footer), fp) != sizeof(footer))
|
|
|
|
goto fail;
|
|
|
|
if (memcmp(footer.id, "APETAGEX", sizeof(footer.id)) != 0)
|
|
|
|
goto fail;
|
2008-11-01 14:11:19 +01:00
|
|
|
if (GUINT32_FROM_LE(footer.version) != 2000)
|
2006-07-20 18:02:40 +02:00
|
|
|
goto fail;
|
2005-03-07 01:51:21 +01:00
|
|
|
|
2006-08-11 23:50:56 +02:00
|
|
|
/* find beginning of ape tag */
|
2008-11-01 14:11:19 +01:00
|
|
|
tagLen = GUINT32_FROM_LE(footer.length);
|
2006-07-20 18:02:40 +02:00
|
|
|
if (tagLen < sizeof(footer))
|
|
|
|
goto fail;
|
|
|
|
if (fseek(fp, size - tagLen, SEEK_SET))
|
|
|
|
goto fail;
|
2005-03-07 01:51:21 +01:00
|
|
|
|
|
|
|
/* read tag into buffer */
|
|
|
|
tagLen -= sizeof(footer);
|
2006-11-26 04:51:46 +01:00
|
|
|
if (tagLen <= 0)
|
|
|
|
goto fail;
|
2008-11-01 14:33:14 +01:00
|
|
|
buffer = g_malloc(tagLen);
|
2006-07-20 18:02:40 +02:00
|
|
|
if (fread(buffer, 1, tagLen, fp) != tagLen)
|
|
|
|
goto fail;
|
2005-03-07 01:51:21 +01:00
|
|
|
|
|
|
|
/* read tags */
|
2008-11-01 14:11:19 +01:00
|
|
|
tagCount = GUINT32_FROM_LE(footer.tagCount);
|
2005-03-07 01:51:21 +01:00
|
|
|
p = buffer;
|
2006-07-20 18:02:40 +02:00
|
|
|
while (tagCount-- && tagLen > 10) {
|
2008-11-01 14:11:19 +01:00
|
|
|
size = GUINT32_FROM_LE(*(const uint32_t *)p);
|
2005-03-07 01:51:21 +01:00
|
|
|
p += 4;
|
|
|
|
tagLen -= 4;
|
2008-11-01 14:11:19 +01:00
|
|
|
flags = GUINT32_FROM_LE(*(const uint32_t *)p);
|
2005-03-07 01:51:21 +01:00
|
|
|
p += 4;
|
|
|
|
tagLen -= 4;
|
|
|
|
|
|
|
|
/* get the key */
|
|
|
|
key = p;
|
2006-07-20 18:02:40 +02:00
|
|
|
while (tagLen - size > 0 && *p != '\0') {
|
2005-03-07 01:51:21 +01:00
|
|
|
p++;
|
|
|
|
tagLen--;
|
|
|
|
}
|
|
|
|
p++;
|
|
|
|
tagLen--;
|
|
|
|
|
|
|
|
/* get the value */
|
2008-03-26 11:38:07 +01:00
|
|
|
if (tagLen < size)
|
2006-07-20 18:02:40 +02:00
|
|
|
goto fail;
|
2005-03-07 01:51:21 +01:00
|
|
|
|
|
|
|
/* we only care about utf-8 text tags */
|
2006-07-20 18:02:40 +02:00
|
|
|
if (!(flags & (0x3 << 1))) {
|
|
|
|
for (i = 0; i < 7; i++) {
|
|
|
|
if (strcasecmp(key, apeItems[i]) == 0) {
|
|
|
|
if (!ret)
|
2008-08-29 09:38:21 +02:00
|
|
|
ret = tag_new();
|
|
|
|
tag_add_item_n(ret, tagItems[i],
|
|
|
|
p, size);
|
2005-03-07 01:51:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
p += size;
|
|
|
|
tagLen -= size;
|
|
|
|
}
|
2006-07-20 18:02:40 +02:00
|
|
|
|
2006-08-10 00:18:06 +02:00
|
|
|
fail:
|
2006-07-20 18:02:40 +02:00
|
|
|
if (fp)
|
|
|
|
fclose(fp);
|
|
|
|
if (buffer)
|
|
|
|
free(buffer);
|
2005-03-07 01:51:21 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-08-29 09:38:21 +02:00
|
|
|
struct tag *tag_new(void)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-11-01 14:33:14 +01:00
|
|
|
struct tag *ret = g_new(struct tag, 1);
|
2004-11-10 22:58:27 +01:00
|
|
|
ret->items = NULL;
|
2004-03-11 01:16:49 +01:00
|
|
|
ret->time = -1;
|
2004-11-10 22:58:27 +01:00
|
|
|
ret->numOfItems = 0;
|
2004-02-24 00:41:20 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-08-29 09:38:11 +02:00
|
|
|
static void deleteItem(struct tag *tag, int idx)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-01-26 13:46:21 +01:00
|
|
|
assert(idx < tag->numOfItems);
|
2007-03-09 15:44:09 +01:00
|
|
|
tag->numOfItems--;
|
2004-11-10 22:58:27 +01:00
|
|
|
|
2008-12-28 22:09:42 +01:00
|
|
|
g_mutex_lock(tag_pool_lock);
|
tag: added a pool for tag items
The new source tag_pool.c manages a pool of reference counted tag_item
objects. This is used to merge tag items of the same type and value,
saving lots of memory. Formerly, only the value itself was pooled,
wasting memory for all the pointers and tag_item structs.
The following results were measured with massif. Started MPD on
amd64, typed "mpc", no song being played. My music database contains
35k tagged songs. The results are what massif reports as "peak".
0.13.2: total 14,131,392; useful 11,408,972; extra 2,722,420
eric: total 18,370,696; useful 15,648,182; extra 2,722,514
mk f34f694: total 15,833,952; useful 13,111,470; extra 2,722,482
mk now: total 12,837,632; useful 10,626,383; extra 2,211,249
This patch set saves 20% memory, and does a good job in reducing heap
fragmentation.
2008-08-29 09:38:37 +02:00
|
|
|
tag_pool_put_item(tag->items[idx]);
|
2008-12-28 22:09:42 +01:00
|
|
|
g_mutex_unlock(tag_pool_lock);
|
2004-11-10 22:58:27 +01:00
|
|
|
|
2008-01-26 13:46:21 +01:00
|
|
|
if (tag->numOfItems - idx > 0) {
|
|
|
|
memmove(tag->items + idx, tag->items + idx + 1,
|
|
|
|
tag->numOfItems - idx);
|
2004-11-10 22:58:27 +01:00
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (tag->numOfItems > 0) {
|
2008-11-01 14:33:14 +01:00
|
|
|
tag->items = g_realloc(tag->items, items_size(tag));
|
2006-07-20 18:02:40 +02:00
|
|
|
} else {
|
2004-11-10 22:58:27 +01:00
|
|
|
free(tag->items);
|
|
|
|
tag->items = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-29 09:38:21 +02:00
|
|
|
void tag_clear_items_by_type(struct tag *tag, enum tag_type type)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-03-26 11:37:36 +01:00
|
|
|
int i;
|
2004-11-10 22:58:27 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
for (i = 0; i < tag->numOfItems; i++) {
|
2008-08-29 09:38:29 +02:00
|
|
|
if (tag->items[i]->type == type) {
|
2004-11-10 22:58:27 +01:00
|
|
|
deleteItem(tag, i);
|
|
|
|
/* decrement since when just deleted this node */
|
|
|
|
i--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-06 18:35:41 +02:00
|
|
|
void tag_free(struct tag *tag)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2004-11-10 22:58:27 +01:00
|
|
|
int i;
|
|
|
|
|
2008-12-28 22:09:42 +01:00
|
|
|
g_mutex_lock(tag_pool_lock);
|
2008-10-06 18:35:41 +02:00
|
|
|
for (i = tag->numOfItems; --i >= 0; )
|
tag: added a pool for tag items
The new source tag_pool.c manages a pool of reference counted tag_item
objects. This is used to merge tag items of the same type and value,
saving lots of memory. Formerly, only the value itself was pooled,
wasting memory for all the pointers and tag_item structs.
The following results were measured with massif. Started MPD on
amd64, typed "mpc", no song being played. My music database contains
35k tagged songs. The results are what massif reports as "peak".
0.13.2: total 14,131,392; useful 11,408,972; extra 2,722,420
eric: total 18,370,696; useful 15,648,182; extra 2,722,514
mk f34f694: total 15,833,952; useful 13,111,470; extra 2,722,482
mk now: total 12,837,632; useful 10,626,383; extra 2,211,249
This patch set saves 20% memory, and does a good job in reducing heap
fragmentation.
2008-08-29 09:38:37 +02:00
|
|
|
tag_pool_put_item(tag->items[i]);
|
2008-12-28 22:09:42 +01:00
|
|
|
g_mutex_unlock(tag_pool_lock);
|
2004-11-10 22:58:27 +01:00
|
|
|
|
2008-09-06 15:31:55 +02:00
|
|
|
if (tag->items == bulk.items) {
|
|
|
|
#ifndef NDEBUG
|
|
|
|
assert(bulk.busy);
|
|
|
|
bulk.busy = 0;
|
|
|
|
#endif
|
|
|
|
} else if (tag->items) {
|
2006-07-20 18:02:40 +02:00
|
|
|
free(tag->items);
|
2008-09-06 15:31:55 +02:00
|
|
|
}
|
|
|
|
|
2004-02-24 00:41:20 +01:00
|
|
|
free(tag);
|
|
|
|
}
|
|
|
|
|
2008-08-29 14:48:39 +02:00
|
|
|
struct tag *tag_dup(const struct tag *tag)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-08-29 09:38:11 +02:00
|
|
|
struct tag *ret;
|
2004-11-10 22:58:27 +01:00
|
|
|
int i;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (!tag)
|
|
|
|
return NULL;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2008-08-29 09:38:21 +02:00
|
|
|
ret = tag_new();
|
2004-11-10 22:58:27 +01:00
|
|
|
ret->time = tag->time;
|
2008-08-29 15:04:49 +02:00
|
|
|
ret->numOfItems = tag->numOfItems;
|
2008-11-01 14:33:14 +01:00
|
|
|
ret->items = ret->numOfItems > 0 ? g_malloc(items_size(tag)) : NULL;
|
2004-06-01 12:28:06 +02:00
|
|
|
|
2008-12-28 22:09:42 +01:00
|
|
|
g_mutex_lock(tag_pool_lock);
|
2008-09-07 19:14:45 +02:00
|
|
|
for (i = 0; i < tag->numOfItems; i++)
|
2008-08-29 15:04:49 +02:00
|
|
|
ret->items[i] = tag_pool_dup_item(tag->items[i]);
|
2008-12-28 22:09:42 +01:00
|
|
|
g_mutex_unlock(tag_pool_lock);
|
2004-06-01 12:28:06 +02:00
|
|
|
|
2004-11-10 22:58:27 +01:00
|
|
|
return ret;
|
2004-06-01 12:28:06 +02:00
|
|
|
}
|
|
|
|
|
2009-01-03 23:28:51 +01:00
|
|
|
struct tag *
|
|
|
|
tag_merge(const struct tag *base, const struct tag *add)
|
|
|
|
{
|
|
|
|
struct tag *ret;
|
|
|
|
unsigned n;
|
|
|
|
|
|
|
|
assert(base != NULL);
|
|
|
|
assert(add != NULL);
|
|
|
|
|
|
|
|
/* allocate new tag object */
|
|
|
|
|
|
|
|
ret = tag_new();
|
|
|
|
ret->time = add->time > 0 ? add->time : base->time;
|
|
|
|
ret->numOfItems = base->numOfItems + add->numOfItems;
|
|
|
|
ret->items = ret->numOfItems > 0 ? g_malloc(items_size(ret)) : NULL;
|
|
|
|
|
|
|
|
g_mutex_lock(tag_pool_lock);
|
|
|
|
|
|
|
|
/* copy all items from "add" */
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < add->numOfItems; ++i)
|
|
|
|
ret->items[i] = tag_pool_dup_item(add->items[i]);
|
|
|
|
|
|
|
|
n = add->numOfItems;
|
|
|
|
|
|
|
|
/* copy additional items from "base" */
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < base->numOfItems; ++i)
|
|
|
|
if (!tag_has_type(add, base->items[i]->type))
|
|
|
|
ret->items[n++] = tag_pool_dup_item(base->items[i]);
|
|
|
|
|
|
|
|
g_mutex_unlock(tag_pool_lock);
|
|
|
|
|
|
|
|
assert(n <= ret->numOfItems);
|
|
|
|
|
|
|
|
if (n < ret->numOfItems) {
|
|
|
|
/* some tags were not copied - shrink ret->items */
|
|
|
|
assert(n > 0);
|
|
|
|
|
|
|
|
ret->numOfItems = n;
|
|
|
|
ret->items = g_realloc(ret->items, items_size(ret));
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-01-15 00:21:08 +01:00
|
|
|
const char *
|
|
|
|
tag_get_value(const struct tag *tag, enum tag_type type)
|
2008-11-03 18:24:00 +01:00
|
|
|
{
|
|
|
|
assert(tag != NULL);
|
|
|
|
assert(type < TAG_NUM_OF_ITEM_TYPES);
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < tag->numOfItems; i++)
|
|
|
|
if (tag->items[i]->type == type)
|
2009-01-15 00:21:08 +01:00
|
|
|
return tag->items[i]->value;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
2008-11-03 18:24:00 +01:00
|
|
|
|
2009-01-15 00:21:08 +01:00
|
|
|
bool tag_has_type(const struct tag *tag, enum tag_type type)
|
|
|
|
{
|
|
|
|
return tag_get_value(tag, type) != NULL;
|
2008-11-03 18:24:00 +01:00
|
|
|
}
|
|
|
|
|
2008-08-29 14:48:39 +02:00
|
|
|
int tag_equal(const struct tag *tag1, const struct tag *tag2)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2004-11-10 22:58:27 +01:00
|
|
|
int i;
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (tag1 == NULL && tag2 == NULL)
|
|
|
|
return 1;
|
|
|
|
else if (!tag1 || !tag2)
|
|
|
|
return 0;
|
2004-06-01 12:28:06 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (tag1->time != tag2->time)
|
|
|
|
return 0;
|
2004-06-01 12:28:06 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (tag1->numOfItems != tag2->numOfItems)
|
|
|
|
return 0;
|
2004-11-10 22:58:27 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
for (i = 0; i < tag1->numOfItems; i++) {
|
2008-08-29 09:38:29 +02:00
|
|
|
if (tag1->items[i]->type != tag2->items[i]->type)
|
2006-07-20 18:02:40 +02:00
|
|
|
return 0;
|
2008-08-29 09:38:29 +02:00
|
|
|
if (strcmp(tag1->items[i]->value, tag2->items[i]->value)) {
|
2004-11-10 22:58:27 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2004-06-01 12:28:06 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
return 1;
|
2004-06-01 12:28:06 +02:00
|
|
|
}
|
2004-11-10 22:58:27 +01:00
|
|
|
|
2008-11-01 14:04:15 +01:00
|
|
|
static char *
|
2009-01-03 14:52:49 +01:00
|
|
|
fix_utf8(const char *str, size_t length)
|
2008-11-01 14:04:15 +01:00
|
|
|
{
|
|
|
|
char *temp;
|
2008-10-31 13:57:10 +01:00
|
|
|
gsize written;
|
2008-08-29 09:38:54 +02:00
|
|
|
|
2008-08-29 09:38:56 +02:00
|
|
|
assert(str != NULL);
|
|
|
|
|
2009-01-03 14:52:49 +01:00
|
|
|
if (g_utf8_validate(str, length, NULL))
|
2008-11-01 14:04:15 +01:00
|
|
|
return NULL;
|
2008-08-29 09:38:54 +02:00
|
|
|
|
|
|
|
DEBUG("not valid utf8 in tag: %s\n",str);
|
2009-01-03 14:52:49 +01:00
|
|
|
temp = g_convert(str, length, "utf-8", "iso-8859-1",
|
2009-01-04 19:50:22 +01:00
|
|
|
NULL, &written, NULL);
|
|
|
|
if (temp == NULL)
|
2008-11-01 14:04:15 +01:00
|
|
|
return NULL;
|
2008-10-31 13:57:10 +01:00
|
|
|
|
2008-08-29 09:38:54 +02:00
|
|
|
return temp;
|
2004-11-10 22:58:27 +01:00
|
|
|
}
|
|
|
|
|
tag: try not to reallocate tag.items in every add() call
If many tag_items are added at once while the tag cache is being
loaded, manage these items in a static fixed list, instead of
reallocating the list with every newly created item. This reduces
heap fragmentation.
Massif results again:
mk before: total 12,837,632; useful 10,626,383; extra 2,211,249
mk now: total 12,736,720; useful 10,626,383; extra 2,110,337
The "useful" value is the same since this patch only changes the way
we allocate the same amount of memory, but heap fragmentation was
reduced by 5%.
2008-08-29 09:39:08 +02:00
|
|
|
void tag_begin_add(struct tag *tag)
|
|
|
|
{
|
|
|
|
assert(!bulk.busy);
|
|
|
|
assert(tag != NULL);
|
|
|
|
assert(tag->items == NULL);
|
|
|
|
assert(tag->numOfItems == 0);
|
|
|
|
|
|
|
|
#ifndef NDEBUG
|
|
|
|
bulk.busy = 1;
|
|
|
|
#endif
|
|
|
|
tag->items = bulk.items;
|
|
|
|
}
|
|
|
|
|
|
|
|
void tag_end_add(struct tag *tag)
|
|
|
|
{
|
|
|
|
if (tag->items == bulk.items) {
|
|
|
|
assert(tag->numOfItems <= BULK_MAX);
|
|
|
|
|
|
|
|
if (tag->numOfItems > 0) {
|
|
|
|
/* copy the tag items from the bulk list over
|
|
|
|
to a new list (which fits exactly) */
|
2008-11-01 14:33:14 +01:00
|
|
|
tag->items = g_malloc(items_size(tag));
|
2008-09-07 19:14:43 +02:00
|
|
|
memcpy(tag->items, bulk.items, items_size(tag));
|
tag: try not to reallocate tag.items in every add() call
If many tag_items are added at once while the tag cache is being
loaded, manage these items in a static fixed list, instead of
reallocating the list with every newly created item. This reduces
heap fragmentation.
Massif results again:
mk before: total 12,837,632; useful 10,626,383; extra 2,211,249
mk now: total 12,736,720; useful 10,626,383; extra 2,110,337
The "useful" value is the same since this patch only changes the way
we allocate the same amount of memory, but heap fragmentation was
reduced by 5%.
2008-08-29 09:39:08 +02:00
|
|
|
} else
|
|
|
|
tag->items = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef NDEBUG
|
|
|
|
bulk.busy = 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2009-01-03 23:13:39 +01:00
|
|
|
static bool
|
|
|
|
char_is_non_printable(unsigned char ch)
|
|
|
|
{
|
|
|
|
return ch < 0x20;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *
|
|
|
|
find_non_printable(const char *p, size_t length)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < length; ++i)
|
|
|
|
if (char_is_non_printable(p[i]))
|
|
|
|
return p + i;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clears all non-printable characters, convert them to space.
|
|
|
|
* Returns NULL if nothing needs to be cleared.
|
|
|
|
*/
|
|
|
|
static char *
|
|
|
|
clear_non_printable(const char *p, size_t length)
|
|
|
|
{
|
|
|
|
const char *first = find_non_printable(p, length);
|
|
|
|
char *dest;
|
|
|
|
|
|
|
|
if (first == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
2009-01-04 21:24:22 +01:00
|
|
|
dest = g_strndup(p, length);
|
2009-01-03 23:13:39 +01:00
|
|
|
|
|
|
|
for (size_t i = first - p; i < length; ++i)
|
|
|
|
if (char_is_non_printable(dest[i]))
|
|
|
|
dest[i] = ' ';
|
|
|
|
|
|
|
|
return dest;
|
|
|
|
}
|
|
|
|
|
2009-01-03 14:52:49 +01:00
|
|
|
static char *
|
|
|
|
fix_tag_value(const char *p, size_t length)
|
|
|
|
{
|
2009-01-03 23:13:39 +01:00
|
|
|
char *utf8, *cleared;
|
2009-01-03 14:52:49 +01:00
|
|
|
|
|
|
|
utf8 = fix_utf8(p, length);
|
2009-01-03 23:13:39 +01:00
|
|
|
if (utf8 != NULL) {
|
|
|
|
p = utf8;
|
|
|
|
length = strlen(p);
|
|
|
|
}
|
2009-01-03 14:52:49 +01:00
|
|
|
|
2009-01-03 23:13:39 +01:00
|
|
|
cleared = clear_non_printable(p, length);
|
|
|
|
if (cleared == NULL)
|
|
|
|
cleared = utf8;
|
|
|
|
else
|
|
|
|
g_free(utf8);
|
2009-01-03 14:52:49 +01:00
|
|
|
|
2009-01-03 23:13:39 +01:00
|
|
|
return cleared;
|
2009-01-03 14:52:49 +01:00
|
|
|
}
|
|
|
|
|
2008-08-29 09:38:11 +02:00
|
|
|
static void appendToTagItems(struct tag *tag, enum tag_type type,
|
2008-08-28 20:02:17 +02:00
|
|
|
const char *value, size_t len)
|
2004-11-10 22:58:27 +01:00
|
|
|
{
|
2008-08-28 20:02:16 +02:00
|
|
|
unsigned int i = tag->numOfItems;
|
2009-01-03 14:52:49 +01:00
|
|
|
char *p;
|
2008-08-29 09:39:04 +02:00
|
|
|
|
2009-01-03 14:52:49 +01:00
|
|
|
p = fix_tag_value(value, len);
|
2009-01-03 23:13:39 +01:00
|
|
|
if (p != NULL) {
|
|
|
|
value = p;
|
|
|
|
len = strlen(value);
|
|
|
|
}
|
2004-11-10 22:58:27 +01:00
|
|
|
|
|
|
|
tag->numOfItems++;
|
tag: try not to reallocate tag.items in every add() call
If many tag_items are added at once while the tag cache is being
loaded, manage these items in a static fixed list, instead of
reallocating the list with every newly created item. This reduces
heap fragmentation.
Massif results again:
mk before: total 12,837,632; useful 10,626,383; extra 2,211,249
mk now: total 12,736,720; useful 10,626,383; extra 2,110,337
The "useful" value is the same since this patch only changes the way
we allocate the same amount of memory, but heap fragmentation was
reduced by 5%.
2008-08-29 09:39:08 +02:00
|
|
|
|
|
|
|
if (tag->items != bulk.items)
|
|
|
|
/* bulk mode disabled */
|
2008-11-01 14:33:14 +01:00
|
|
|
tag->items = g_realloc(tag->items, items_size(tag));
|
tag: try not to reallocate tag.items in every add() call
If many tag_items are added at once while the tag cache is being
loaded, manage these items in a static fixed list, instead of
reallocating the list with every newly created item. This reduces
heap fragmentation.
Massif results again:
mk before: total 12,837,632; useful 10,626,383; extra 2,211,249
mk now: total 12,736,720; useful 10,626,383; extra 2,110,337
The "useful" value is the same since this patch only changes the way
we allocate the same amount of memory, but heap fragmentation was
reduced by 5%.
2008-08-29 09:39:08 +02:00
|
|
|
else if (tag->numOfItems >= BULK_MAX) {
|
|
|
|
/* bulk list already full - switch back to non-bulk */
|
|
|
|
assert(bulk.busy);
|
|
|
|
|
2008-11-01 14:33:14 +01:00
|
|
|
tag->items = g_malloc(items_size(tag));
|
tag: try not to reallocate tag.items in every add() call
If many tag_items are added at once while the tag cache is being
loaded, manage these items in a static fixed list, instead of
reallocating the list with every newly created item. This reduces
heap fragmentation.
Massif results again:
mk before: total 12,837,632; useful 10,626,383; extra 2,211,249
mk now: total 12,736,720; useful 10,626,383; extra 2,110,337
The "useful" value is the same since this patch only changes the way
we allocate the same amount of memory, but heap fragmentation was
reduced by 5%.
2008-08-29 09:39:08 +02:00
|
|
|
memcpy(tag->items, bulk.items,
|
2008-09-07 19:14:47 +02:00
|
|
|
items_size(tag) - sizeof(struct tag_item *));
|
tag: try not to reallocate tag.items in every add() call
If many tag_items are added at once while the tag cache is being
loaded, manage these items in a static fixed list, instead of
reallocating the list with every newly created item. This reduces
heap fragmentation.
Massif results again:
mk before: total 12,837,632; useful 10,626,383; extra 2,211,249
mk now: total 12,736,720; useful 10,626,383; extra 2,110,337
The "useful" value is the same since this patch only changes the way
we allocate the same amount of memory, but heap fragmentation was
reduced by 5%.
2008-08-29 09:39:08 +02:00
|
|
|
}
|
2004-11-10 22:58:27 +01:00
|
|
|
|
2008-12-28 22:09:42 +01:00
|
|
|
g_mutex_lock(tag_pool_lock);
|
2009-01-03 23:13:39 +01:00
|
|
|
tag->items[i] = tag_pool_get_item(type, value, len);
|
2008-12-28 22:09:42 +01:00
|
|
|
g_mutex_unlock(tag_pool_lock);
|
2004-11-10 22:58:27 +01:00
|
|
|
|
2009-01-03 14:52:49 +01:00
|
|
|
g_free(p);
|
2004-11-10 22:58:27 +01:00
|
|
|
}
|
|
|
|
|
2008-08-29 09:38:21 +02:00
|
|
|
void tag_add_item_n(struct tag *tag, enum tag_type itemType,
|
|
|
|
const char *value, size_t len)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
|
|
|
if (ignoreTagItems[itemType])
|
2007-11-21 13:15:00 +01:00
|
|
|
{
|
2006-07-20 18:02:40 +02:00
|
|
|
return;
|
2007-11-21 13:15:00 +01:00
|
|
|
}
|
2006-07-20 18:02:40 +02:00
|
|
|
if (!value || !len)
|
|
|
|
return;
|
2004-11-10 23:13:30 +01:00
|
|
|
|
|
|
|
/* we can't hold more than 255 items */
|
2006-07-20 18:02:40 +02:00
|
|
|
if (tag->numOfItems == 255)
|
|
|
|
return;
|
|
|
|
|
2004-11-10 22:58:27 +01:00
|
|
|
appendToTagItems(tag, itemType, value, len);
|
|
|
|
}
|