release-1.10release-1.6release-1.7release-1.8release-1.9
Last change
on this file since e2cbbbe was
5e5f08f,
checked in by Anders Kaseorg <andersk@mit.edu>, 15 years ago
|
Make owl_list_free_simple a special case of owl_list_cleanup.
Signed-off-by: Anders Kaseorg <andersk@mit.edu>
Reviewed-by: Nelson Elhage <nelhage@mit.edu>
|
-
Property mode set to
100644
|
File size:
1.5 KB
|
Rev | Line | |
---|
[7d4fbcd] | 1 | #include "owl.h" |
---|
| 2 | #include <stdlib.h> |
---|
| 3 | |
---|
[50622a5] | 4 | #define INITSIZE 10 |
---|
[0f15f12] | 5 | #define GROWBY 3 / 2 |
---|
[7d4fbcd] | 6 | |
---|
[53f421b] | 7 | int owl_list_create(owl_list *l) |
---|
| 8 | { |
---|
[7d4fbcd] | 9 | l->size=0; |
---|
[4d86e06] | 10 | l->list=owl_malloc(INITSIZE*sizeof(void *)); |
---|
[7d4fbcd] | 11 | l->avail=INITSIZE; |
---|
| 12 | if (l->list==NULL) return(-1); |
---|
| 13 | return(0); |
---|
| 14 | } |
---|
| 15 | |
---|
[77bced3] | 16 | int owl_list_get_size(const owl_list *l) |
---|
[53f421b] | 17 | { |
---|
[7d4fbcd] | 18 | return(l->size); |
---|
| 19 | } |
---|
| 20 | |
---|
| 21 | |
---|
[e9c6fc8] | 22 | static void owl_list_grow(owl_list *l, int n) |
---|
[53f421b] | 23 | { |
---|
[d09e5a1] | 24 | void *ptr; |
---|
[ef6e2d1] | 25 | |
---|
[50622a5] | 26 | if ((l->size+n) > l->avail) { |
---|
[0f15f12] | 27 | int avail = MAX(l->avail * GROWBY, l->size + n); |
---|
| 28 | ptr = owl_realloc(l->list, avail * sizeof(void *)); |
---|
[ef6e2d1] | 29 | if (ptr==NULL) abort(); |
---|
[d09e5a1] | 30 | l->list=ptr; |
---|
[0f15f12] | 31 | l->avail = avail; |
---|
[7d4fbcd] | 32 | } |
---|
| 33 | |
---|
| 34 | } |
---|
| 35 | |
---|
[77bced3] | 36 | void *owl_list_get_element(const owl_list *l, int n) |
---|
[ef6e2d1] | 37 | { |
---|
| 38 | if (n>l->size-1) return(NULL); |
---|
| 39 | return(l->list[n]); |
---|
| 40 | } |
---|
| 41 | |
---|
| 42 | int owl_list_insert_element(owl_list *l, int at, void *element) |
---|
[53f421b] | 43 | { |
---|
[7d4fbcd] | 44 | int i; |
---|
[ef6e2d1] | 45 | if(at < 0 || at > l->size) return -1; |
---|
| 46 | owl_list_grow(l, 1); |
---|
[7d4fbcd] | 47 | |
---|
[ef6e2d1] | 48 | for (i=l->size; i>at; i--) { |
---|
[7d4fbcd] | 49 | l->list[i]=l->list[i-1]; |
---|
| 50 | } |
---|
[ef6e2d1] | 51 | |
---|
| 52 | l->list[at] = element; |
---|
[7d4fbcd] | 53 | l->size++; |
---|
| 54 | return(0); |
---|
| 55 | } |
---|
| 56 | |
---|
[ef6e2d1] | 57 | int owl_list_append_element(owl_list *l, void *element) |
---|
| 58 | { |
---|
| 59 | return owl_list_insert_element(l, l->size, element); |
---|
| 60 | } |
---|
| 61 | |
---|
| 62 | int owl_list_prepend_element(owl_list *l, void *element) |
---|
| 63 | { |
---|
| 64 | return owl_list_insert_element(l, 0, element); |
---|
| 65 | } |
---|
| 66 | |
---|
[53f421b] | 67 | int owl_list_remove_element(owl_list *l, int n) |
---|
| 68 | { |
---|
[7d4fbcd] | 69 | int i; |
---|
| 70 | |
---|
| 71 | if (n>l->size-1) return(-1); |
---|
| 72 | for (i=n; i<l->size-1; i++) { |
---|
| 73 | l->list[i]=l->list[i+1]; |
---|
| 74 | } |
---|
| 75 | l->size--; |
---|
| 76 | return(0); |
---|
| 77 | } |
---|
| 78 | |
---|
[8c59178] | 79 | void owl_list_cleanup(owl_list *l, void (*elefree)(void *)) |
---|
[53f421b] | 80 | { |
---|
[7d4fbcd] | 81 | int i; |
---|
| 82 | |
---|
[5e5f08f] | 83 | if (elefree) { |
---|
| 84 | for (i = 0; i < l->size; i++) { |
---|
| 85 | (elefree)(l->list[i]); |
---|
| 86 | } |
---|
[7d4fbcd] | 87 | } |
---|
| 88 | owl_free(l->list); |
---|
| 89 | } |
---|
Note: See
TracBrowser
for help on using the repository browser.