From: hackbard Date: Tue, 28 Nov 2006 11:31:21 +0000 (+0000) Subject: list system updates for moldyn api, gets changed soon again (clean up)! X-Git-Url: https://hackdaworld.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4fb6339047ed741c431ee45b855b9221df299737;p=my-code%2Fapi.git list system updates for moldyn api, gets changed soon again (clean up)! --- diff --git a/list/list.c b/list/list.c index 64a104a..79d53f7 100644 --- a/list/list.c +++ b/list/list.c @@ -53,7 +53,7 @@ int list_del_element(t_list *list,t_list_element *element) { if(element->prev!=NULL) element->prev->next=element->next; if(element==list->start) list->start=element->next; list_reset(list); - if(element->data!=NULL) free(element->data); + if((element->data!=NULL)&&(element->data_len)) free(element->data); free(element); dprintf(list->outfd,"[list] element deleted\n"); @@ -97,6 +97,81 @@ int list_search_data(t_list *list,void *data,int first_bytes) { return L_NO_SUCH_ELEMENT; } +int list_add_immediate_ptr(t_list *list,void *ptr) { + + /* if this is the first element */ + if(list->start==NULL) { + list->start=(t_list_element *)malloc(sizeof(t_list_element)); + if(list->start==NULL) { + dprintf(list->outfd,"[list] unable to allocate list element memory\n"); + return L_E_MEM; + } + list->start->data=ptr; + list->start->data_len=0; + list->start->next=NULL; + list->start->prev=NULL; + list->current=list->start; + dprintf(list->outfd,"[list] added initial element\n"); + return L_SUCCESS; + } + + list->current->next=(t_list_element *)malloc(sizeof(t_list_element)); + if(list->current->next==NULL) { + dprintf(list->outfd,"[list] unable to allocate element memory\n"); + return L_E_MEM; + } + list->current->next->prev=list->current; + list->current->next->next=NULL; + list->current->next->data=ptr; + list->current->next->data_len=0; + + list->current=list->current->next; + + return L_SUCCESS; + +} + +int list_add_immediate(t_list *list,void *data,int data_len) { + + /* if this is the first element */ + if(list->start==NULL) { + list->start=(t_list_element *)malloc(sizeof(t_list_element)); + if(list->start==NULL) { + dprintf(list->outfd,"[list] unable to allocate list element memory\n"); + return L_E_MEM; + } + if((list->start->data=malloc(data_len))==NULL) { + dprintf(list->outfd,"[list] unable to allocate data memory\n"); + return L_E_MEM; + } + memcpy(list->start->data,data,data_len); + list->start->data_len=data_len; + list->start->next=NULL; + list->start->prev=NULL; + list->current=list->start; + dprintf(list->outfd,"[list] added initial element\n"); + return L_SUCCESS; + } + + list->current->next=(t_list_element *)malloc(sizeof(t_list_element)); + if(list->current->next==NULL) { + dprintf(list->outfd,"[list] unable to allocate element memory\n"); + return L_E_MEM; + } + list->current->next->prev=list->current; + list->current->next->next=NULL; + if((list->current->next->data=malloc(data_len))==NULL) { + dprintf(list->outfd,"[list] unable to allocate data memory\n"); + return L_E_MEM; + } + memcpy(list->current->next->data,data,data_len); + list->current->next->data_len=data_len; + + list->current=list->current->next; + + return L_SUCCESS; +} + int list_add_element(t_list *list,void *data,int data_len) { list_reset(list); diff --git a/list/list.h b/list/list.h index 5d63af9..4302d32 100644 --- a/list/list.h +++ b/list/list.h @@ -1,5 +1,8 @@ /* list.h -- list headers */ +#ifndef LIST_H +#define LIST_H + #define _GNU_SOURCE #include #include @@ -36,6 +39,9 @@ int list_del_element(t_list *list,t_list_element *element); int list_del_current(t_list *list); int list_destroy(t_list *list); int list_search_data(t_list *list,void *data,int first_bytes); +int list_add_immediate_ptr(t_list *list,void *ptr); +int list_add_immediate(t_list *list,void *data,int data_len); int list_add_element(t_list *list,void *data,int data_len); int list_count(t_list *list); +#endif