00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 #include "list.h"
00036 #include "alloc.h"
00037
00038 #include <assert.h>
00039 #include <apr_pools.h>
00040
00041 #define INITIAL_LIST_SIZE 16
00042
00043
00044 bs_list *new_list(apr_pool_t *mp)
00045 {
00046 apr_status_t rv;
00047 apr_pool_t *pool;
00048 bs_list *list = NULL;
00049
00050
00051 rv = apr_pool_create(&pool, mp);
00052 if (rv != APR_SUCCESS) return NULL;
00053
00054
00055 list = apr_pcalloc(pool, sizeof(bs_list));
00056 list->size = 0;
00057 list->allocated = INITIAL_LIST_SIZE;
00058 list->pool = pool;
00059 list->data = apr_pcalloc(pool, sizeof(void * [INITIAL_LIST_SIZE]));
00060
00061 return list;
00062 }
00063
00064
00065 void delete_list(bs_list *list)
00066 {
00067 if (list != NULL) {
00068 apr_pool_destroy(list->pool);
00069 }
00070 }
00071
00072
00073 void list_remove(bs_list *l, unsigned index)
00074 {
00075 unsigned i;
00076 assert(l != NULL && index < l->size);
00077
00078 for (i = index + 1; i < l->size; i++) {
00079 l->data[i - 1] = l->data[i];
00080 }
00081 l->size--;
00082 }
00083
00084
00085 void list_append(bs_list *list, void *data)
00086 {
00087 assert(list != NULL);
00088
00089 if (list->size == list->allocated) {
00090 list->allocated *= 2;
00091 list->data = pool_realloc(list->data,
00092 list->size,
00093 sizeof(void *) * list->allocated,
00094 list->pool);
00095 }
00096
00097 list->data[list->size++] = data;
00098 }
00099
00100
00101 void list_insert(bs_list *l, unsigned index, void *data)
00102 {
00103 assert(l != NULL);
00104 if (index >= l->size) list_append(l, data);
00105 else {
00106 unsigned i;
00107
00108
00109 if (l->size == l->allocated) {
00110 l->allocated *= 2;
00111 l->data = pool_realloc(l->data, l->size,
00112 sizeof(void *) * l->allocated, l->pool);
00113 }
00114
00115
00116 for (i = l->size; i > index; i--) l->data[i] = l->data[i - 1];
00117 l->data[index] = data;
00118 }
00119 }
00120
00121
00122 void list_merge(bs_list *list_1, bs_list *list_2)
00123 {
00124 int i;
00125 assert(list_1 != NULL && list_2 != NULL);
00126
00127 for (i = 0; i < list_2->size; i++)
00128 list_append(list_1, list_2->data [i]);
00129
00130 delete_list(list_2);
00131 }
00132
00133
00134 inline unsigned list_size(const bs_list *list)
00135 {
00136 assert(list != NULL);
00137 return list->size;
00138 }
00139
00140
00141 inline void *list_index(const bs_list *list, unsigned index)
00142 {
00143 assert(list != NULL && index < list->size);
00144 return list->data[index];
00145 }