release-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change
on this file since 61de085 was
0f15f12,
checked in by Anders Kaseorg <andersk@mit.edu>, 15 years ago
|
owl_list_grow, owl_dict_insert_element: Don’t use fuzzy math.
• Make sure to allocate at least as many elements as were requested.
• Don’t use floating point math to compute the expansion amount.
• In owl_dict_insert_element, reallocate using the right element size
instead of relying on the GROWAT fudge factor (WTF?).
Signed-off-by: Anders Kaseorg <andersk@mit.edu>
|
-
Property mode set to
100644
|
File size:
1.6 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 | |
---|
[ef6e2d1] | 22 | void owl_list_grow(owl_list *l, int n) /*noproto*/ |
---|
[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 | |
---|
[53f421b] | 79 | void owl_list_free_all(owl_list *l, void (*elefree)(void *)) |
---|
| 80 | { |
---|
[7d4fbcd] | 81 | int i; |
---|
| 82 | |
---|
| 83 | for (i=0; i<l->size; i++) { |
---|
| 84 | (elefree)(l->list[i]); |
---|
| 85 | } |
---|
| 86 | owl_free(l->list); |
---|
| 87 | } |
---|
| 88 | |
---|
[53f421b] | 89 | void owl_list_free_simple(owl_list *l) |
---|
| 90 | { |
---|
[7d4fbcd] | 91 | if (l->list) owl_free(l->list); |
---|
| 92 | } |
---|
Note: See
TracBrowser
for help on using the repository browser.