Wed Apr 6 11:30:05 2011

Asterisk developer's documentation


heap.c File Reference

Max Heap data structure. More...

#include "asterisk.h"
#include "asterisk/heap.h"
#include "asterisk/utils.h"
#include "asterisk/cli.h"

Go to the source code of this file.

Data Structures

struct  ast_heap

Functions

int __ast_heap_rdlock (struct ast_heap *h, const char *file, const char *func, int line)
 Read-Lock a heap.
int __ast_heap_unlock (struct ast_heap *h, const char *file, const char *func, int line)
 Unlock a heap.
int __ast_heap_wrlock (struct ast_heap *h, const char *file, const char *func, int line)
 Write-Lock a heap.
static void * _ast_heap_remove (struct ast_heap *h, unsigned int index)
ast_heapast_heap_create (unsigned int init_height, ast_heap_cmp_fn cmp_fn, ssize_t index_offset)
 Create a max heap.
ast_heapast_heap_destroy (struct ast_heap *h)
 Destroy a max heap.
void * ast_heap_peek (struct ast_heap *h, unsigned int index)
 Peek at an element on a heap.
void * ast_heap_pop (struct ast_heap *h)
 Pop the max element off of the heap.
int ast_heap_push (struct ast_heap *h, void *elm)
 Push an element on to a heap.
void * ast_heap_remove (struct ast_heap *h, void *elm)
 Remove a specific element from a heap.
size_t ast_heap_size (struct ast_heap *h)
 Get the current size of a heap.
int ast_heap_verify (struct ast_heap *h)
 Verify that a heap has been properly constructed.
static int bubble_up (struct ast_heap *h, int i)
static ssize_t get_index (struct ast_heap *h, void *elm)
static int grow_heap (struct ast_heap *h)
 Add a row of additional storage for the heap.
static void * heap_get (struct ast_heap *h, int i)
static void heap_set (struct ast_heap *h, int i, void *elm)
static void heap_swap (struct ast_heap *h, int i, int j)
static int left_node (int i)
static void max_heapify (struct ast_heap *h, int i)
static int parent_node (int i)
static int right_node (int i)


Detailed Description

Max Heap data structure.

Author:
Russell Bryant <russell@digium.com>

Definition in file heap.c.


Function Documentation

int __ast_heap_rdlock ( struct ast_heap h,
const char *  file,
const char *  func,
int  line 
)

Read-Lock a heap.

Parameters:
h the heap
A lock is provided for convenience. It can be assumed that none of the ast_heap API calls are thread safe. This lock does not have to be used if another one is already available to protect the heap.

Returns:
see the documentation for pthread_rwlock_rdlock()
Since:
1.6.1

Definition at line 314 of file heap.c.

References __ast_rwlock_rdlock(), and ast_heap::lock.

00315 {
00316    return __ast_rwlock_rdlock(&h->lock, "&h->lock", file, line, func);
00317 }

int __ast_heap_unlock ( struct ast_heap h,
const char *  file,
const char *  func,
int  line 
)

Unlock a heap.

Parameters:
h the heap
Returns:
see the documentation for pthread_rwlock_unlock()
Since:
1.6.1

Definition at line 319 of file heap.c.

References __ast_rwlock_unlock(), and ast_heap::lock.

00320 {
00321    return __ast_rwlock_unlock(&h->lock, "&h->lock", file, line, func);
00322 }

int __ast_heap_wrlock ( struct ast_heap h,
const char *  file,
const char *  func,
int  line 
)

Write-Lock a heap.

Parameters:
h the heap
A lock is provided for convenience. It can be assumed that none of the ast_heap API calls are thread safe. This lock does not have to be used if another one is already available to protect the heap.

Returns:
see the documentation for pthread_rwlock_wrlock()
Since:
1.6.1

Definition at line 309 of file heap.c.

References __ast_rwlock_wrlock(), and ast_heap::lock.

00310 {
00311    return __ast_rwlock_wrlock(&h->lock, "&h->lock", file, line, func);
00312 }

static void* _ast_heap_remove ( struct ast_heap h,
unsigned int  index 
) [static]

Definition at line 263 of file heap.c.

References bubble_up(), ast_heap::cur_len, heap_get(), heap_set(), and max_heapify().

Referenced by ast_heap_pop(), and ast_heap_remove().

00264 {
00265    void *ret;
00266 
00267    if (!index || index > h->cur_len) {
00268       return NULL;
00269    }
00270 
00271    ret = heap_get(h, index);
00272    heap_set(h, index, heap_get(h, (h->cur_len)--));
00273    index = bubble_up(h, index);
00274    max_heapify(h, index);
00275 
00276    return ret;
00277 }

struct ast_heap* ast_heap_create ( unsigned int  init_height,
ast_heap_cmp_fn  cmp_fn,
ssize_t  index_offset 
)

Create a max heap.

Parameters:
init_height The initial height of the heap to allocate space for. To start out, there will be room for (2 ^ init_height) - 1 entries. However, the heap will grow as needed.
cmp_fn The function that should be used to compare elements in the heap.
index_offset This parameter is optional, but must be provided to be able to use ast_heap_remove(). This is the number of bytes into the element where an ssize_t has been made available for the heap's internal use. The heap will use this field to keep track of the element's current position in the heap. The offsetof() macro is useful for providing a proper value for this argument. If ast_heap_remove() will not be used, then a negative value can be provided to indicate that no field for an offset has been allocated.
Example Usage:

 struct myobj {
    int foo;
    int bar;
    char stuff[8];
    char things[8];
    ssize_t __heap_index;
 };

 ...

 static int myobj_cmp(void *obj1, void *obj2);

 ...

 struct ast_heap *h;

 h = ast_heap_create(8, myobj_cmp, offsetof(struct myobj, __heap_index));

Returns:
An instance of a max heap
Since:
1.6.1

Definition at line 114 of file heap.c.

References __ast_calloc(), ast_calloc, ast_free, ast_log(), ast_rwlock_init, and LOG_ERROR.

Referenced by ast_timing_init(), load_resource_list(), and sched_context_create().

00117 {
00118    struct ast_heap *h;
00119 
00120    if (!cmp_fn) {
00121       ast_log(LOG_ERROR, "A comparison function must be provided\n");
00122       return NULL;
00123    }
00124 
00125    if (!init_height) {
00126       init_height = 8;
00127    }
00128 
00129    if (!(h =
00130 #ifdef MALLOC_DEBUG
00131          __ast_calloc(1, sizeof(*h), file, lineno, func)
00132 #else
00133          ast_calloc(1, sizeof(*h))
00134 #endif
00135       )) {
00136       return NULL;
00137    }
00138 
00139    h->cmp_fn = cmp_fn;
00140    h->index_offset = index_offset;
00141    h->avail_len = (1 << init_height) - 1;
00142 
00143    if (!(h->heap =
00144 #ifdef MALLOC_DEBUG
00145          __ast_calloc(1, h->avail_len * sizeof(void *), file, lineno, func)
00146 #else
00147          ast_calloc(1, h->avail_len * sizeof(void *))
00148 #endif
00149       )) {
00150       ast_free(h);
00151       return NULL;
00152    }
00153 
00154    ast_rwlock_init(&h->lock);
00155 
00156    return h;
00157 }

struct ast_heap* ast_heap_destroy ( struct ast_heap h  ) 

Destroy a max heap.

Parameters:
h the heap to destroy
Returns:
NULL for convenience
Since:
1.6.1

Definition at line 159 of file heap.c.

References ast_free, ast_rwlock_destroy, ast_heap::heap, and ast_heap::lock.

Referenced by load_resource_list(), and sched_context_destroy().

00160 {
00161    ast_free(h->heap);
00162    h->heap = NULL;
00163 
00164    ast_rwlock_destroy(&h->lock);
00165 
00166    ast_free(h);
00167 
00168    return NULL;
00169 }

void* ast_heap_peek ( struct ast_heap h,
unsigned int  index 
)

Peek at an element on a heap.

Parameters:
h the heap
index index of the element to return. The first element is at index 1, and the last element is at the index == the size of the heap.
Returns:
an element at the specified index on the heap. This element will not be removed before being returned.
Note:
If this function is being used in combination with ast_heap_size() for purposes of traversing the heap, the heap must be locked for the entire duration of the traversal.
Example code for a traversal:
 struct ast_heap *h;

 ...

 size_t size, i;
 void *cur_obj;

 ast_heap_rdlock(h);

 size = ast_heap_size(h);

 for (i = 1; i <= size && (cur_obj = ast_heap_peek(h, i)); i++) {
     ... Do stuff with cur_obj ...
 }

 ast_heap_unlock(h);
Since:
1.6.1

Definition at line 295 of file heap.c.

References ast_heap::cur_len, and heap_get().

Referenced by ast_sched_dump(), ast_sched_report(), ast_sched_runq(), ast_sched_wait(), and ast_timer_open().

00296 {
00297    if (!h->cur_len || !index || index > h->cur_len) {
00298       return NULL;
00299    }
00300 
00301    return heap_get(h, index);
00302 }

void* ast_heap_pop ( struct ast_heap h  ) 

Pop the max element off of the heap.

Parameters:
h the heap
Returns:
this will return the element on the top of the heap, which has the largest value according to the element comparison function that was provided when the heap was created. The element will be removed before being returned.
Since:
1.6.1

Definition at line 290 of file heap.c.

References _ast_heap_remove().

Referenced by ast_sched_runq(), load_resource_list(), and sched_context_destroy().

00291 {
00292    return _ast_heap_remove(h, 1);
00293 }

int ast_heap_push ( struct ast_heap h,
void *  elm 
)

Push an element on to a heap.

Parameters:
h the heap being added to
elm the element being put on the heap
Return values:
0 success
non-zero failure
Since:
1.6.1

Definition at line 245 of file heap.c.

References ast_heap::avail_len, bubble_up(), ast_heap::cur_len, grow_heap(), and heap_set().

Referenced by _ast_register_timing_interface(), load_resource(), and schedule().

00247 {
00248    if (h->cur_len == h->avail_len && grow_heap(h
00249 #ifdef MALLOC_DEBUG
00250       , file, lineno, func
00251 #endif
00252       )) {
00253       return -1;
00254    }
00255 
00256    heap_set(h, ++(h->cur_len), elm);
00257 
00258    bubble_up(h, h->cur_len);
00259 
00260    return 0;
00261 }

void* ast_heap_remove ( struct ast_heap h,
void *  elm 
)

Remove a specific element from a heap.

Parameters:
h the heap to remove from
elm the element to remove
Returns:
elm, if the removal was successful, or NULL if it failed
Note:
the index_offset parameter to ast_heap_create() is required to be able to use this function.
Since:
1.6.1

Definition at line 279 of file heap.c.

References _ast_heap_remove(), and get_index().

Referenced by ast_sched_del(), and ast_unregister_timing_interface().

00280 {
00281    ssize_t i = get_index(h, elm);
00282 
00283    if (i == -1) {
00284       return NULL;
00285    }
00286 
00287    return _ast_heap_remove(h, i);
00288 }

size_t ast_heap_size ( struct ast_heap h  ) 

Get the current size of a heap.

Parameters:
h the heap
Returns:
the number of elements currently in the heap
Since:
1.6.1

Definition at line 304 of file heap.c.

References ast_heap::cur_len.

Referenced by ast_sched_dump(), and ast_sched_report().

00305 {
00306    return h->cur_len;
00307 }

int ast_heap_verify ( struct ast_heap h  ) 

Verify that a heap has been properly constructed.

Parameters:
h a heap
Return values:
0 success
non-zero failure
Note:
This function is mostly for debugging purposes. It traverses an existing heap and verifies that every node is properly placed relative to its children.
Since:
1.6.1

Definition at line 86 of file heap.c.

References ast_heap::cmp_fn, ast_heap::cur_len, heap_get(), left_node(), and right_node().

00087 {
00088    unsigned int i;
00089 
00090    for (i = 1; i <= (h->cur_len / 2); i++) {
00091       int l = left_node(i);
00092       int r = right_node(i);
00093 
00094       if (l <= h->cur_len) {
00095          if (h->cmp_fn(heap_get(h, i), heap_get(h, l)) < 0) {
00096             return -1;
00097          }
00098       }
00099 
00100       if (r <= h->cur_len) {
00101          if (h->cmp_fn(heap_get(h, i), heap_get(h, r)) < 0) {
00102             return -1;
00103          }
00104       }
00105    }
00106 
00107    return 0;
00108 }

static int bubble_up ( struct ast_heap h,
int  i 
) [static]

Definition at line 232 of file heap.c.

References ast_heap::cmp_fn, heap_get(), heap_swap(), and parent_node().

Referenced by _ast_heap_remove(), and ast_heap_push().

00233 {
00234    while (i > 1 && h->cmp_fn(heap_get(h, parent_node(i)), heap_get(h, i)) < 0) {
00235       heap_swap(h, i, parent_node(i));
00236       i = parent_node(i);
00237    }
00238 
00239    return i;
00240 }

static ssize_t get_index ( struct ast_heap h,
void *  elm 
) [inline, static]

Definition at line 63 of file heap.c.

References ast_heap::index_offset.

00064 {
00065    ssize_t *index;
00066 
00067    if (h->index_offset < 0) {
00068       return -1;
00069    }
00070 
00071    index = elm + h->index_offset;
00072 
00073    return *index;
00074 }

static int grow_heap ( struct ast_heap h  )  [static]

Add a row of additional storage for the heap.

Definition at line 174 of file heap.c.

References __ast_realloc(), and ast_realloc.

Referenced by ast_heap_push().

00179 {
00180    h->avail_len = h->avail_len * 2 + 1;
00181 
00182    if (!(h->heap =
00183 #ifdef MALLOC_DEBUG
00184          __ast_realloc(h->heap, h->avail_len * sizeof(void *), file, lineno, func)
00185 #else
00186          ast_realloc(h->heap, h->avail_len * sizeof(void *))
00187 #endif
00188       )) {
00189       h->cur_len = h->avail_len = 0;
00190       return -1;
00191    }
00192 
00193    return 0;
00194 }

static void* heap_get ( struct ast_heap h,
int  i 
) [inline, static]

Definition at line 58 of file heap.c.

References ast_heap::heap.

Referenced by _ast_heap_remove(), ast_heap_peek(), ast_heap_verify(), bubble_up(), heap_swap(), and max_heapify().

00059 {
00060    return h->heap[i - 1];
00061 }

static void heap_set ( struct ast_heap h,
int  i,
void *  elm 
) [inline, static]

Definition at line 76 of file heap.c.

References ast_heap::heap, and ast_heap::index_offset.

Referenced by _ast_heap_remove(), ast_heap_push(), and heap_swap().

00077 {
00078    h->heap[i - 1] = elm;
00079 
00080    if (h->index_offset >= 0) {
00081       ssize_t *index = elm + h->index_offset;
00082       *index = i;
00083    }
00084 }

static void heap_swap ( struct ast_heap h,
int  i,
int  j 
) [inline, static]

Definition at line 196 of file heap.c.

References heap_get(), and heap_set().

Referenced by bubble_up(), and max_heapify().

00197 {
00198    void *tmp;
00199 
00200    tmp = heap_get(h, i);
00201    heap_set(h, i, heap_get(h, j));
00202    heap_set(h, j, tmp);
00203 }

static int left_node ( int  i  )  [inline, static]

Definition at line 43 of file heap.c.

Referenced by ast_heap_verify(), and max_heapify().

00044 {
00045    return 2 * i;
00046 }

static void max_heapify ( struct ast_heap h,
int  i 
) [inline, static]

Definition at line 205 of file heap.c.

References ast_heap::cmp_fn, ast_heap::cur_len, heap_get(), heap_swap(), left_node(), and right_node().

Referenced by _ast_heap_remove().

00206 {
00207    for (;;) {
00208       int l = left_node(i);
00209       int r = right_node(i);
00210       int max;
00211 
00212       if (l <= h->cur_len && h->cmp_fn(heap_get(h, l), heap_get(h, i)) > 0) {
00213          max = l;
00214       } else {
00215          max = i;
00216       }
00217 
00218       if (r <= h->cur_len && h->cmp_fn(heap_get(h, r), heap_get(h, max)) > 0) {
00219          max = r;
00220       }
00221 
00222       if (max == i) {
00223          break;
00224       }
00225 
00226       heap_swap(h, i, max);
00227 
00228       i = max;
00229    }
00230 }

static int parent_node ( int  i  )  [inline, static]

Definition at line 53 of file heap.c.

Referenced by bubble_up().

00054 {
00055    return i / 2;
00056 }

static int right_node ( int  i  )  [inline, static]

Definition at line 48 of file heap.c.

Referenced by ast_heap_verify(), and max_heapify().

00049 {
00050    return 2 * i + 1;
00051 }


Generated on Wed Apr 6 11:30:05 2011 for Asterisk - The Open Source Telephony Project by  doxygen 1.4.7