diff options
-rw-r--r-- | src/main.c | 5 | ||||
-rw-r--r-- | src/tag.c | 27 | ||||
-rw-r--r-- | src/tag.h | 2 | ||||
-rw-r--r-- | src/tagTracker.c | 25 | ||||
-rw-r--r-- | src/tagTracker.h | 2 |
5 files changed, 48 insertions, 13 deletions
diff --git a/src/main.c b/src/main.c index 7cbfaf23f..65557a4b8 100644 --- a/src/main.c +++ b/src/main.c @@ -35,6 +35,7 @@ #include "inputPlugin.h" #include "inputStream.h" #include "tag.h" +#include "tagTracker.h" #include "../config.h" #include <stdio.h> @@ -427,8 +428,10 @@ int main(int argc, char * argv[]) { initAudioDriver(); initVolume(); initInterfaces(); - initInputStream(); + initInputStream(); + printMemorySavedByTagTracker(); + daemonize(&options); setupLogOutput(&options, out, err); @@ -136,10 +136,8 @@ MpdTag * getID3Info(struct id3_tag * tag, char * id, int type, MpdTag * mpdTag) utf8 = id3_ucs4_utf8duplicate(ucs4); if(!utf8) continue; - if(strlen(utf8)) { - if( NULL == mpdTag ) mpdTag = newMpdTag(); - addItemToMpdTag(mpdTag, type, utf8); - } + if( NULL == mpdTag ) mpdTag = newMpdTag(); + addItemToMpdTag(mpdTag, type, utf8); free(utf8); } @@ -200,7 +198,10 @@ MpdTag * newMpdTag() { static void deleteItem(MpdTag * tag, int index) { tag->numOfItems--; + assert(index < tag->numOfItems); + removeTagItemString(tag->items[index].type, tag->items[index].value); + //free(tag->items[index].value); if(tag->numOfItems-index > 0) { memmove(tag->items+index, tag->items+index+1, @@ -234,6 +235,7 @@ void clearMpdTag(MpdTag * tag) { for(i = 0; i < tag->numOfItems; i++) { removeTagItemString(tag->items[i].type, tag->items[i].value); + //free(tag->items[i].value); } if(tag->items) free(tag->items); @@ -300,24 +302,27 @@ inline static void appendToTagItems(MpdTag * tag, int type, char * value, { int i = tag->numOfItems; - char * t; - t = malloc(len+1); - strncpy(t, value, len); - t[len] = '\0'; + char * dup; + dup = malloc(len+1); + strncpy(dup, value, len); + dup[len] = '\0'; - fixUtf8(t); + fixUtf8(dup); tag->numOfItems++; tag->items = realloc(tag->items, tag->numOfItems*sizeof(MpdTagItem)); tag->items[i].type = type; - tag->items[i].value = getTagItemString(type, value); + tag->items[i].value = getTagItemString(type, dup); + //tag->items[i].value = strdup(dup); - free(t); + free(dup); } void addItemToMpdTagWithLen(MpdTag * tag, int itemType, char * value, int len) { if(ignoreTagItems[itemType]) return; + + if(!value || !len) return; appendToTagItems(tag, itemType, value, len); } @@ -65,6 +65,8 @@ MpdTag * id3Dup(char * file); MpdTag * newMpdTag(); +void initTagConfig(); + void clearItemsFromMpdTag(MpdTag * tag, int itemType); void clearMpdTag(MpdTag * tag); diff --git a/src/tagTracker.c b/src/tagTracker.c index 7bca526a7..23110c682 100644 --- a/src/tagTracker.c +++ b/src/tagTracker.c @@ -1,6 +1,7 @@ #include "tagTracker.h" #include "list.h" +#include "log.h" #include <assert.h> @@ -19,11 +20,12 @@ char * getTagItemString(int type, char * string) { ListNode * node; if(tagLists[type] == NULL) { - tagLists[type] = makeList(NULL); + tagLists[type] = makeList(free); } if((node = findNodeInList(tagLists[type], string))) { (*((int *)node->data))++; + printf("%s: %i\n", string, *((int *)node->data)); } else { int * intPtr = malloc(sizeof(int)); @@ -37,6 +39,8 @@ char * getTagItemString(int type, char * string) { void removeTagItemString(int type, char * string) { ListNode * node; + assert(tagLists[type]); + assert(string); if(tagLists[type] == NULL) return; node = findNodeInList(tagLists[type], string); @@ -58,3 +62,22 @@ int getNumberOfTagItems(int type) { return tagLists[type]->numberOfNodes; } + +void printMemorySavedByTagTracker() { + int i; + ListNode * node; + size_t sum = 0; + + for(i = 0; i < TAG_NUM_OF_ITEM_TYPES; i++) { + if(!tagLists[i]) continue; + + node = tagLists[i]->firstNode; + + while(node != NULL) { + sum += (strlen(node->key)+1)*(*((int *)node->data)); + node = node->nextNode; + } + } + + DEBUG("saved memory: %li\n", (long)sum); +} diff --git a/src/tagTracker.h b/src/tagTracker.h index 6b3049a0f..f7c007fe7 100644 --- a/src/tagTracker.h +++ b/src/tagTracker.h @@ -9,4 +9,6 @@ void removeTagItemString(int type, char * string); int getNumberOfTagItems(int type); +void printMemorySavedByTagTracker(); + #endif |