|
- #include <stdio.h>
- #include <stdlib.h>
- #include <assert.h>
- #include "list.h"
-
- struct ListHead* list_create()
- {
- return calloc(1, sizeof(struct ListHead));
- }
-
- int list_add(struct ListHead* head, void* data, size_t size)
- {
- assert(head);
-
- struct ListElem* p = malloc(sizeof(struct ListElem) + size);
- if(!p)
- return -1;
-
- p->next = NULL;
- p->dataSize = size;
- memcpy((char*)p + sizeof(struct ListElem), data, size);
- p->dataStart = (char*)p + sizeof(struct ListElem);
-
- // First elem to ever be added?
- if(!head->end)
- {
- head->end = head->start = p;
- }
- else
- {
- head->end->next = p;
- head->end = p;
- }
-
- return 0;
- }
-
- // todo:
- int list_free(struct ListHead* head)
- {
- struct ListElem* p = head->start;
-
- while(p)
- {
- struct ListElem* pOld = p;
- p = p->next;
-
- free(pOld);
- }
-
- free(head);
-
- return 0;
- }
|