release-1.10release-1.6release-1.7release-1.8release-1.9
Last change
on this file since d537350 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
|
Line | |
---|
1 | #include "owl.h" |
---|
2 | #include <stdlib.h> |
---|
3 | |
---|
4 | #define INITSIZE 10 |
---|
5 | #define GROWBY 3 / 2 |
---|
6 | |
---|
7 | int owl_list_create(owl_list *l) |
---|
8 | { |
---|
9 | l->size=0; |
---|
10 | l->list=owl_malloc(INITSIZE*sizeof(void *)); |
---|
11 | l->avail=INITSIZE; |
---|
12 | if (l->list==NULL) return(-1); |
---|
13 | return(0); |
---|
14 | } |
---|
15 | |
---|
16 | int owl_list_get_size(const owl_list *l) |
---|
17 | { |
---|
18 | return(l->size); |
---|
19 | } |
---|
20 | |
---|
21 | |
---|
22 | static void owl_list_grow(owl_list *l, int n) |
---|
23 | { |
---|
24 | void *ptr; |
---|
25 | |
---|
26 | if ((l->size+n) > l->avail) { |
---|
27 | int avail = MAX(l->avail * GROWBY, l->size + n); |
---|
28 | ptr = owl_realloc(l->list, avail * sizeof(void *)); |
---|
29 | if (ptr==NULL) abort(); |
---|
30 | l->list=ptr; |
---|
31 | l->avail = avail; |
---|
32 | } |
---|
33 | |
---|
34 | } |
---|
35 | |
---|
36 | void *owl_list_get_element(const owl_list *l, int n) |
---|
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) |
---|
43 | { |
---|
44 | int i; |
---|
45 | if(at < 0 || at > l->size) return -1; |
---|
46 | owl_list_grow(l, 1); |
---|
47 | |
---|
48 | for (i=l->size; i>at; i--) { |
---|
49 | l->list[i]=l->list[i-1]; |
---|
50 | } |
---|
51 | |
---|
52 | l->list[at] = element; |
---|
53 | l->size++; |
---|
54 | return(0); |
---|
55 | } |
---|
56 | |
---|
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 | |
---|
67 | int owl_list_remove_element(owl_list *l, int n) |
---|
68 | { |
---|
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 | |
---|
79 | void owl_list_cleanup(owl_list *l, void (*elefree)(void *)) |
---|
80 | { |
---|
81 | int i; |
---|
82 | |
---|
83 | if (elefree) { |
---|
84 | for (i = 0; i < l->size; i++) { |
---|
85 | (elefree)(l->list[i]); |
---|
86 | } |
---|
87 | } |
---|
88 | owl_free(l->list); |
---|
89 | } |
---|
Note: See
TracBrowser
for help on using the repository browser.