From 2fab53d11a9505beb746e55449601d4aad36b8b4 Mon Sep 17 00:00:00 2001 From: Qball Cow Date: Mon, 22 Oct 2007 12:05:12 +0200 Subject: [PATCH] Updating to non-pooling list --- example/speedtest/test-mpdata.c | 3 +- example/speedtest/test1.sh | 2 +- src/libmpd-internal.h | 25 ++----- src/libmpd.c | 156 ++++++++++++++-------------------------- 4 files changed, 61 insertions(+), 125 deletions(-) diff --git a/example/speedtest/test-mpdata.c b/example/speedtest/test-mpdata.c index 63045a6..d28396e 100644 --- a/example/speedtest/test-mpdata.c +++ b/example/speedtest/test-mpdata.c @@ -1,12 +1,13 @@ #include #include "libmpd.h" +#include "libmpd-internal.h" int main(int argc, char **argv) { MpdData *data = NULL; int i = 0; - for(i=0; i < 100000;i++) + for(i=0; i < 10000000;i++) { data =mpd_new_data_struct_append(data); diff --git a/example/speedtest/test1.sh b/example/speedtest/test1.sh index 11a85af..e262442 100755 --- a/example/speedtest/test1.sh +++ b/example/speedtest/test1.sh @@ -1,4 +1,4 @@ -gcc -g test.c ../../src/*.c -o test -I../../ -I../../src/ +gcc -g test-mpdata.c ../../src/*.c -o test -I../../ -I../../src/ #gcc -g test.c ../../src/*.c -o test2 -I../../ -I../../src/ #gcc -g test-mpdata.c ../../src/*.c -o test-mpdata -I../../ -I../../src/ #gcc -g playlist.c ../../src/*.c -o playlist -I../../ -I../../src/ diff --git a/src/libmpd-internal.h b/src/libmpd-internal.h index 07af02f..4880e6d 100644 --- a/src/libmpd-internal.h +++ b/src/libmpd-internal.h @@ -3,18 +3,11 @@ #include "libmpdclient.h" struct _MpdData_real; -struct _MpdDataPool; - -typedef struct _MpdData_head { - struct _MpdData_real *first; - struct _MpdDataPool *pool; - struct _MpdDataPool *current; -} MpdData_head; typedef struct _MpdData_real { /* MpdDataType */ MpdDataType type; - + union { struct { int tag_type; @@ -28,17 +21,10 @@ typedef struct _MpdData_real { struct _MpdData_real *next; /* Previous MpdData in the list */ struct _MpdData_real *prev; - /* First MpdData in the list */ - MpdData_head *head; + struct _MpdData_real *first; }MpdData_real; -#define MPD_DATA_POOL_SIZE 256 -typedef struct _MpdDataPool { - MpdData_real pool[MPD_DATA_POOL_SIZE]; - unsigned int space_left; - struct _MpdDataPool *next; -} MpdDataPool; - + /* queue struct */ typedef struct _MpdQueue MpdQueue; typedef struct _MpdServerState { @@ -159,9 +145,8 @@ MpdQueue * mpd_new_queue_struct (); void mpd_queue_get_next (MpdObj *mi); /* Internal Data struct functions */ -inline MpdData * mpd_new_data_struct (MpdData_head * const head); -inline MpdData * mpd_new_data_struct_append (MpdData * const data); -inline MpdData_head * mpd_data_get_head (MpdData const * const data); +inline MpdData * mpd_new_data_struct (void); +inline MpdData * mpd_new_data_struct_append (MpdData * data); inline MpdData * mpd_data_concatenate (MpdData * const first, MpdData * const second); inline MpdData * mpd_data_get_next_real (MpdData * const data, int kill_list); /* more internal stuff*/ diff --git a/src/libmpd.c b/src/libmpd.c index e56f19b..ea6e9c7 100644 --- a/src/libmpd.c +++ b/src/libmpd.c @@ -702,17 +702,10 @@ void mpd_signal_connect_connection_changed(MpdObj *mi, ConnectionChangedCallback /* more playlist */ /* MpdData Part */ -MpdData *mpd_new_data_struct(MpdData_head * const head) +MpdData *mpd_new_data_struct(void) { MpdData_real* data; - if (head->current->space_left == 0) { - head->current->next = malloc(sizeof(MpdDataPool)); - head->current = head->current->next; - head->current->space_left = MPD_DATA_POOL_SIZE; - head->current->next = NULL; - } - data = &(head->current->pool[MPD_DATA_POOL_SIZE - head->current->space_left]); - head->current->space_left--; + data = malloc(sizeof(*data)); data->type = 0; data->tag_type = 0; @@ -723,30 +716,25 @@ MpdData *mpd_new_data_struct(MpdData_head * const head) data->output_dev = NULL; data->next = NULL; data->prev = NULL; - data->head = head; + data->first = NULL; return (MpdData*)data; } -MpdData *mpd_new_data_struct_append(MpdData * const data) +MpdData *mpd_new_data_struct_append(MpdData * data) { MpdData_real *data_real = (MpdData_real*)data; - MpdData_head *head; if(data_real == NULL) { - head = (MpdData_head*)malloc(sizeof(MpdData_head)); - head->current = head->pool = (MpdDataPool*)malloc(sizeof(MpdDataPool)); - head->pool->space_left = MPD_DATA_POOL_SIZE; - head->pool->next = NULL; - data_real = (MpdData_real*)mpd_new_data_struct(head); - head->first = data_real; + data_real = (MpdData_real*)mpd_new_data_struct(); + data_real->first = data_real; } else { - data_real->next = (MpdData_real*)mpd_new_data_struct(data_real->head); + data_real->next = (MpdData_real*)mpd_new_data_struct(); data_real->next->prev = data_real; data_real = data_real->next; data_real->next = NULL; - + data_real->first = data_real->prev->first; } return (MpdData*)data_real; } @@ -756,15 +744,8 @@ MpdData * mpd_data_get_first(MpdData const * const data) MpdData_real const * const data_real = (MpdData_real const * const)data; if(data_real != NULL) { - if (data_real->head != NULL) - { - return (MpdData*)(mpd_data_get_head(data)->first); - } - else - { - return NULL; - } - } + return (MpdData*)data_real->first; + } return NULL; } @@ -804,18 +785,16 @@ int mpd_data_is_last(MpdData const * const data) } return FALSE; } - +/* MpdData_head *mpd_data_get_head(MpdData const * const data) { return ((MpdData_real*)data)->head; } - +*/ MpdData* mpd_data_concatenate( MpdData * const first, MpdData * const second) { MpdData_real *first_real = (MpdData_real*)first; MpdData_real *second_real = (MpdData_real*)second; - MpdData_head *first_head = NULL; - MpdData_head *second_head = NULL; - MpdDataPool *pool; + MpdData_real *first_head = NULL; if ( first == NULL ) { if ( second != NULL ) @@ -827,8 +806,7 @@ MpdData* mpd_data_concatenate( MpdData * const first, MpdData * const second) return (MpdData*)first_real; } - first_head = mpd_data_get_head(first); - second_head = mpd_data_get_head(second); + first_head = (MpdData_real *)mpd_data_get_first(first); /* find last element in first data list */ while (!mpd_data_is_last((MpdData*)first_real)) first_real = (MpdData_real*)mpd_data_get_next_real((MpdData*)first_real, FALSE); @@ -840,90 +818,62 @@ MpdData* mpd_data_concatenate( MpdData * const first, MpdData * const second) /* I need to set all the -> first correct */ while (second_real) { - second_real->head = first_head; + second_real->first = first_head; second_real = (MpdData_real*)mpd_data_get_next_real((MpdData*)second_real, FALSE); } - /* Need to concatenate the list of MpdDataPools - * Note that this is NOT efficient in any way... - */ - pool = first_head->current; - pool->next = second_head->pool; - first_head->current = second_head->current; - free(second_head); - return (MpdData*)first_head->first; + + return (MpdData*)first_head; } /** This function looks broken. This should be tested * Shamefully I don't know this part of the code to well */ MpdData * mpd_data_delete_item(MpdData *data) { - MpdData_real *temp = NULL, *data_real = (MpdData_real*)data; - if(data_real == NULL) return NULL; - if(data_real->head->first == data_real) - { - /* make temp the first item, and reset the head->first pointer */ - temp = data_real->head->first = data_real->next; - /* there is no item before this. */ - data_real->prev = NULL; - /* if it was the last item in the list, we need to free the list */ - if(temp == NULL){ - mpd_data_free(data); - } - } - else - { - - if (data_real->next) - { - data_real->next->prev = data_real->prev; - temp = data_real->next; - } - if (data_real->prev) - { - /* the next item of the previous is the next item of the current */ - data_real->prev->next = data_real->next; - /* temp is the previous item */ - temp = data_real->prev; - } - } - - - return (MpdData *)temp; + MpdData_real *temp = NULL, *data_real = (MpdData_real*)data; + if(data_real == NULL) return NULL; + + + if (data_real->next) + { + data_real->next->prev = data_real->prev; + temp = data_real->next; + } + if (data_real->prev) + { + /* the next item of the previous is the next item of the current */ + data_real->prev->next = data_real->next; + /* temp is the previous item */ + temp = data_real->prev; + } + + return (MpdData *)temp; } void mpd_data_free(MpdData *data) { - MpdData_head *head; - MpdDataPool *pool, *temp_pool; - MpdData_real *data_real; - unsigned int i; + MpdData_real *data_real,*temp; if(data == NULL) { debug_printf(DEBUG_ERROR, "data != NULL Failed"); return; } - head = mpd_data_get_head(data); - pool = head->pool; - do { - for (i = 0; i < MPD_DATA_POOL_SIZE - pool->space_left; i++) { - data_real = &(pool->pool[i]); - if (data_real->type == MPD_DATA_TYPE_SONG) { - if(data_real->song) mpd_freeSong(data_real->song); - } else if (data_real->type == MPD_DATA_TYPE_OUTPUT_DEV) { - mpd_freeOutputElement(data_real->output_dev); - } else if(data_real->type == MPD_DATA_TYPE_DIRECTORY) { - if(data_real->directory)free(data_real->directory); - } else if(data_real->type == MPD_DATA_TYPE_PLAYLIST) { - if(data_real->playlist)free(data_real->playlist); - } else { - free((void*)(data_real->tag)); - } - } - temp_pool = pool->next; - free (pool); - pool = temp_pool; - } while ( pool ); - free(head); + data_real = (MpdData_real *)mpd_data_get_first(data); + while(data_real){ + temp = data_real; + if (data_real->type == MPD_DATA_TYPE_SONG) { + if(data_real->song) mpd_freeSong(data_real->song); + } else if (data_real->type == MPD_DATA_TYPE_OUTPUT_DEV) { + mpd_freeOutputElement(data_real->output_dev); + } else if(data_real->type == MPD_DATA_TYPE_DIRECTORY) { + if(data_real->directory)free(data_real->directory); + } else if(data_real->type == MPD_DATA_TYPE_PLAYLIST) { + if(data_real->playlist)free(data_real->playlist); + } else { + free((void*)(data_real->tag)); + } + data_real = data_real->next; + free(temp); + } } /* clean this up.. make one while loop */ -- 2.11.4.GIT