properly free memory

master
orange 12 years ago
parent 63c2766938
commit b8f997bffb

@ -63,12 +63,16 @@ void list_add(list_t * list, list_node_t * item) {
}
}
void list_remove(list_t * list, list_node_t * item) {
list_node_t * list_remove(list_t * list, list_node_t * item) {
list_node_t *cur;
if (list_empty(list))
return NULL;
if (list->head->data == item->data) {
list_node_t * removed = list->head;
list->head = list->head->next;
return;
return removed;
}
cur = list->head;
@ -76,14 +80,18 @@ void list_remove(list_t * list, list_node_t * item) {
while (cur->next != NULL) {
if (cur->next->data == item->data) {
list_node_t * removed = cur->next;
cur->next = cur->next->next;
return; /* just the first */
return removed; /* just the first */
}
cur = cur->next;
}
return NULL;
}
void list_revert(list_t *list) {
/* FIXME: work without 'reverted?' */
list_t reverted;
reverted.head = NULL;
@ -101,8 +109,8 @@ void list_revert(list_t *list) {
list->head = reverted.head;
}
list_node_t* list_elementat(list_t *list, int i) {
if (i > list_length(list)-1) {
list_node_t* list_elementat(list_t *list, unsigned int i) {
if (list_empty(list) || i > list_length(list)-1) {
return NULL;
}
@ -115,17 +123,28 @@ list_node_t* list_elementat(list_t *list, int i) {
}
int main(int argc, char *argv[]) {
/* Test empty list */
list_t *emptylist = malloc(sizeof(list_t));
list_init(emptylist);
assert(list_length(emptylist) == 0);
assert(list_empty(emptylist) == true);
list_print(emptylist);
printf("list_length = %d\n", list_length(emptylist));
assert(list_elementat(emptylist, 0) == NULL);
assert(list_elementat(emptylist, 1) == NULL);
assert(list_elementat(emptylist, 2) == NULL);
free(emptylist);
/* Setup foolist for the following tests. */
list_t *foolist = malloc(sizeof(list_t));
list_init(foolist);
list_node_t foo = { 1, NULL };
list_add(foolist, &foo);
/* Add one element */
list_node_t * foo = malloc(sizeof(list_node_t));
assert(foo != NULL);
*foo = (list_node_t) { 1, NULL };
list_add(foolist, foo);
assert(list_length(foolist) == 1);
assert(list_empty(foolist) == false);
@ -133,8 +152,9 @@ int main(int argc, char *argv[]) {
printf("list_length = %d\n", list_length(foolist));
// append something to foo
list_node_t bar = { 2, NULL };
list_add(foolist, &bar);
list_node_t * bar = malloc(sizeof(list_node_t));
*bar = (list_node_t) { 23, NULL };
list_add(foolist, bar);
assert(list_length(foolist) == 2);
list_print(foolist);
@ -144,8 +164,7 @@ int main(int argc, char *argv[]) {
const int howmany = 100;
for (int i = 0; i < howmany; i++) {
list_node_t *newnode = malloc(sizeof(list_node_t));
newnode->data = i;
newnode->next = NULL;
*newnode = (list_node_t) { i, NULL };
list_add(foolist, newnode);
}
@ -155,28 +174,28 @@ int main(int argc, char *argv[]) {
// remove something
list_node_t toremove = { 23, NULL };
list_remove(foolist, &toremove);
free(list_remove(foolist, &toremove));
assert(list_length(foolist) == 2 + howmany - 1);
list_print(foolist);
printf("list_length = %d\n", list_length(foolist));
// remove last item
toremove.data = howmany - 1;
list_remove(foolist, &toremove);
free(list_remove(foolist, &toremove));
assert(list_length(foolist) == 2 + howmany - 2);
list_print(foolist);
printf("list_length = %d\n", list_length(foolist));
// remove something
toremove.data = 1;
list_remove(foolist, &toremove);
free(list_remove(foolist, &toremove));
assert(list_length(foolist) == 2 + howmany - 3);
list_print(foolist);
printf("list_length = %d\n", list_length(foolist));
// remove something
toremove.data = 1;
list_remove(foolist, &toremove);
free(list_remove(foolist, &toremove));
assert(list_length(foolist) == 2 + howmany - 4);
list_print(foolist);
printf("list_length = %d\n", list_length(foolist));
@ -184,9 +203,9 @@ int main(int argc, char *argv[]) {
/* remove everything */
for (int i = 0; i < howmany; i++) {
toremove.data = i;
list_remove(foolist, &toremove);
free(list_remove(foolist, &toremove));
}
assert(list_length(foolist) == 1);
assert(list_length(foolist) == 0);
list_print(foolist);
printf("list_length = %d\n", list_length(foolist));
@ -228,5 +247,16 @@ int main(int argc, char *argv[]) {
list_print(foolist);
printf("list_length = %d\n", list_length(foolist));
/* FIXME: frees */
/* Test upper bound */
assert(list_elementat(foolist, list_length(foolist)-1) != NULL);
assert(list_elementat(foolist, list_length(foolist) ) == NULL);
assert(list_elementat(foolist, list_length(foolist)+1) == NULL);
assert(list_elementat(foolist, list_length(foolist)+2) == NULL);
/* Free up remaining items and the list. */
while(!list_empty(foolist)) {
list_node_t *el = list_elementat(foolist, 0);
free(list_remove(foolist, el));
}
free(foolist);
}

Loading…
Cancel
Save