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 #include "asterisk.h"
00031
00032 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 369001 $")
00033
00034 #include "asterisk/heap.h"
00035 #include "asterisk/utils.h"
00036 #include "asterisk/cli.h"
00037
00038 struct ast_heap {
00039 ast_rwlock_t lock;
00040 ast_heap_cmp_fn cmp_fn;
00041 ssize_t index_offset;
00042 size_t cur_len;
00043 size_t avail_len;
00044 void **heap;
00045 };
00046
00047 static inline int left_node(int i)
00048 {
00049 return 2 * i;
00050 }
00051
00052 static inline int right_node(int i)
00053 {
00054 return 2 * i + 1;
00055 }
00056
00057 static inline int parent_node(int i)
00058 {
00059 return i / 2;
00060 }
00061
00062 static inline void *heap_get(struct ast_heap *h, int i)
00063 {
00064 return h->heap[i - 1];
00065 }
00066
00067 static inline ssize_t get_index(struct ast_heap *h, void *elm)
00068 {
00069 ssize_t *index;
00070
00071 if (h->index_offset < 0) {
00072 return -1;
00073 }
00074
00075 index = elm + h->index_offset;
00076
00077 return *index;
00078 }
00079
00080 static inline void heap_set(struct ast_heap *h, int i, void *elm)
00081 {
00082 h->heap[i - 1] = elm;
00083
00084 if (h->index_offset >= 0) {
00085 ssize_t *index = elm + h->index_offset;
00086 *index = i;
00087 }
00088 }
00089
00090 int ast_heap_verify(struct ast_heap *h)
00091 {
00092 unsigned int i;
00093
00094 for (i = 1; i <= (h->cur_len / 2); i++) {
00095 int l = left_node(i);
00096 int r = right_node(i);
00097
00098 if (l <= h->cur_len) {
00099 if (h->cmp_fn(heap_get(h, i), heap_get(h, l)) < 0) {
00100 return -1;
00101 }
00102 }
00103
00104 if (r <= h->cur_len) {
00105 if (h->cmp_fn(heap_get(h, i), heap_get(h, r)) < 0) {
00106 return -1;
00107 }
00108 }
00109 }
00110
00111 return 0;
00112 }
00113
00114 #ifdef MALLOC_DEBUG
00115 struct ast_heap *_ast_heap_create(unsigned int init_height, ast_heap_cmp_fn cmp_fn,
00116 ssize_t index_offset, const char *file, int lineno, const char *func)
00117 #else
00118 struct ast_heap *ast_heap_create(unsigned int init_height, ast_heap_cmp_fn cmp_fn,
00119 ssize_t index_offset)
00120 #endif
00121 {
00122 struct ast_heap *h;
00123
00124 if (!cmp_fn) {
00125 ast_log(LOG_ERROR, "A comparison function must be provided\n");
00126 return NULL;
00127 }
00128
00129 if (!init_height) {
00130 init_height = 8;
00131 }
00132
00133 if (!(h =
00134 #ifdef MALLOC_DEBUG
00135 __ast_calloc(1, sizeof(*h), file, lineno, func)
00136 #else
00137 ast_calloc(1, sizeof(*h))
00138 #endif
00139 )) {
00140 return NULL;
00141 }
00142
00143 h->cmp_fn = cmp_fn;
00144 h->index_offset = index_offset;
00145 h->avail_len = (1 << init_height) - 1;
00146
00147 if (!(h->heap =
00148 #ifdef MALLOC_DEBUG
00149 __ast_calloc(1, h->avail_len * sizeof(void *), file, lineno, func)
00150 #else
00151 ast_calloc(1, h->avail_len * sizeof(void *))
00152 #endif
00153 )) {
00154 ast_free(h);
00155 return NULL;
00156 }
00157
00158 ast_rwlock_init(&h->lock);
00159
00160 return h;
00161 }
00162
00163 struct ast_heap *ast_heap_destroy(struct ast_heap *h)
00164 {
00165 ast_free(h->heap);
00166 h->heap = NULL;
00167
00168 ast_rwlock_destroy(&h->lock);
00169
00170 ast_free(h);
00171
00172 return NULL;
00173 }
00174
00175
00176
00177
00178 static int grow_heap(struct ast_heap *h
00179 #ifdef MALLOC_DEBUG
00180 , const char *file, int lineno, const char *func
00181 #endif
00182 )
00183 {
00184 h->avail_len = h->avail_len * 2 + 1;
00185
00186 if (!(h->heap =
00187 #ifdef MALLOC_DEBUG
00188 __ast_realloc(h->heap, h->avail_len * sizeof(void *), file, lineno, func)
00189 #else
00190 ast_realloc(h->heap, h->avail_len * sizeof(void *))
00191 #endif
00192 )) {
00193 h->cur_len = h->avail_len = 0;
00194 return -1;
00195 }
00196
00197 return 0;
00198 }
00199
00200 static inline void heap_swap(struct ast_heap *h, int i, int j)
00201 {
00202 void *tmp;
00203
00204 tmp = heap_get(h, i);
00205 heap_set(h, i, heap_get(h, j));
00206 heap_set(h, j, tmp);
00207 }
00208
00209 static inline void max_heapify(struct ast_heap *h, int i)
00210 {
00211 for (;;) {
00212 int l = left_node(i);
00213 int r = right_node(i);
00214 int max;
00215
00216 if (l <= h->cur_len && h->cmp_fn(heap_get(h, l), heap_get(h, i)) > 0) {
00217 max = l;
00218 } else {
00219 max = i;
00220 }
00221
00222 if (r <= h->cur_len && h->cmp_fn(heap_get(h, r), heap_get(h, max)) > 0) {
00223 max = r;
00224 }
00225
00226 if (max == i) {
00227 break;
00228 }
00229
00230 heap_swap(h, i, max);
00231
00232 i = max;
00233 }
00234 }
00235
00236 static int bubble_up(struct ast_heap *h, int i)
00237 {
00238 while (i > 1 && h->cmp_fn(heap_get(h, parent_node(i)), heap_get(h, i)) < 0) {
00239 heap_swap(h, i, parent_node(i));
00240 i = parent_node(i);
00241 }
00242
00243 return i;
00244 }
00245
00246 #ifdef MALLOC_DEBUG
00247 int _ast_heap_push(struct ast_heap *h, void *elm, const char *file, int lineno, const char *func)
00248 #else
00249 int ast_heap_push(struct ast_heap *h, void *elm)
00250 #endif
00251 {
00252 if (h->cur_len == h->avail_len && grow_heap(h
00253 #ifdef MALLOC_DEBUG
00254 , file, lineno, func
00255 #endif
00256 )) {
00257 return -1;
00258 }
00259
00260 heap_set(h, ++(h->cur_len), elm);
00261
00262 bubble_up(h, h->cur_len);
00263
00264 return 0;
00265 }
00266
00267 static void *_ast_heap_remove(struct ast_heap *h, unsigned int index)
00268 {
00269 void *ret;
00270
00271 if (!index || index > h->cur_len) {
00272 return NULL;
00273 }
00274
00275 ret = heap_get(h, index);
00276 heap_set(h, index, heap_get(h, (h->cur_len)--));
00277 index = bubble_up(h, index);
00278 max_heapify(h, index);
00279
00280 return ret;
00281 }
00282
00283 void *ast_heap_remove(struct ast_heap *h, void *elm)
00284 {
00285 ssize_t i = get_index(h, elm);
00286
00287 if (i == -1) {
00288 return NULL;
00289 }
00290
00291 return _ast_heap_remove(h, i);
00292 }
00293
00294 void *ast_heap_pop(struct ast_heap *h)
00295 {
00296 return _ast_heap_remove(h, 1);
00297 }
00298
00299 void *ast_heap_peek(struct ast_heap *h, unsigned int index)
00300 {
00301 if (!h->cur_len || !index || index > h->cur_len) {
00302 return NULL;
00303 }
00304
00305 return heap_get(h, index);
00306 }
00307
00308 size_t ast_heap_size(struct ast_heap *h)
00309 {
00310 return h->cur_len;
00311 }
00312
00313 int __ast_heap_wrlock(struct ast_heap *h, const char *file, const char *func, int line)
00314 {
00315 return __ast_rwlock_wrlock(file, line, func, &h->lock, "&h->lock");
00316 }
00317
00318 int __ast_heap_rdlock(struct ast_heap *h, const char *file, const char *func, int line)
00319 {
00320 return __ast_rwlock_rdlock(file, line, func, &h->lock, "&h->lock");
00321 }
00322
00323 int __ast_heap_unlock(struct ast_heap *h, const char *file, const char *func, int line)
00324 {
00325 return __ast_rwlock_unlock(file, line, func, &h->lock, "&h->lock");
00326 }