Asterisk - The Open Source Telephony Project
18.5.0
|
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. More... | |
int | __ast_heap_unlock (struct ast_heap *h, const char *file, const char *func, int line) |
Unlock a heap. More... | |
int | __ast_heap_wrlock (struct ast_heap *h, const char *file, const char *func, int line) |
Write-Lock a heap. More... | |
struct ast_heap * | _ast_heap_create (unsigned int init_height, ast_heap_cmp_fn cmp_fn, ssize_t index_offset, const char *file, int lineno, const char *func) |
Create a max heap. More... | |
int | _ast_heap_push (struct ast_heap *h, void *elm, const char *file, int lineno, const char *func) |
Push an element on to a heap. More... | |
static void * | _ast_heap_remove (struct ast_heap *h, unsigned int index) |
struct ast_heap * | ast_heap_destroy (struct ast_heap *h) |
Destroy a max heap. More... | |
void * | ast_heap_peek (struct ast_heap *h, unsigned int index) |
Peek at an element on a heap. More... | |
void * | ast_heap_pop (struct ast_heap *h) |
Pop the max element off of the heap. More... | |
void * | ast_heap_remove (struct ast_heap *h, void *elm) |
Remove a specific element from a heap. More... | |
size_t | ast_heap_size (struct ast_heap *h) |
Get the current size of a heap. More... | |
int | ast_heap_verify (struct ast_heap *h) |
Verify that a heap has been properly constructed. More... | |
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, const char *file, int lineno, const char *func) |
Add a row of additional storage for the heap. More... | |
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) |
Max Heap data structure.
Definition in file heap.c.
int __ast_heap_rdlock | ( | struct ast_heap * | h, |
const char * | file, | ||
const char * | func, | ||
int | line | ||
) |
Read-Lock a heap.
h | the heap |
file,func,line | 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. |
Definition at line 286 of file heap.c.
References __ast_rwlock_rdlock(), and ast_heap::lock.
int __ast_heap_unlock | ( | struct ast_heap * | h, |
const char * | file, | ||
const char * | func, | ||
int | line | ||
) |
Unlock a heap.
h | the heap |
file,func,line |
Definition at line 291 of file heap.c.
References __ast_rwlock_unlock(), and ast_heap::lock.
int __ast_heap_wrlock | ( | struct ast_heap * | h, |
const char * | file, | ||
const char * | func, | ||
int | line | ||
) |
Write-Lock a heap.
h | the heap |
file,func,line | 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. |
Definition at line 281 of file heap.c.
References __ast_rwlock_wrlock(), and ast_heap::lock.
struct ast_heap* _ast_heap_create | ( | unsigned int | init_height, |
ast_heap_cmp_fn | cmp_fn, | ||
ssize_t | index_offset, | ||
const char * | file, | ||
int | lineno, | ||
const char * | func | ||
) |
Create a max heap.
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:
Definition at line 112 of file heap.c.
References __ast_calloc(), __ast_malloc(), ast_free, ast_log, ast_rwlock_init, ast_heap::avail_len, ast_heap::cmp_fn, ast_heap::heap, ast_heap::index_offset, ast_heap::lock, LOG_ERROR, and NULL.
int _ast_heap_push | ( | struct ast_heap * | h, |
void * | elm, | ||
const char * | file, | ||
int | lineno, | ||
const char * | func | ||
) |
Push an element on to a heap.
h | the heap being added to |
elm | the element being put on the heap |
0 | success |
non-zero | failure |
Definition at line 222 of file heap.c.
References ast_heap::avail_len, bubble_up(), ast_heap::cur_len, grow_heap(), and heap_set().
|
static |
Definition at line 235 of file heap.c.
References bubble_up(), ast_heap::cur_len, heap_get(), heap_set(), max_heapify(), and NULL.
Referenced by ast_heap_pop(), and ast_heap_remove().
Destroy a max heap.
h | the heap to destroy |
Definition at line 146 of file heap.c.
References ast_free, ast_rwlock_destroy, ast_heap::heap, ast_heap::lock, and NULL.
Referenced by ast_bridge_features_cleanup(), ast_sched_context_destroy(), AST_TEST_DEFINE(), sorcery_memory_cache_destructor(), and timing_shutdown().
void* ast_heap_peek | ( | struct ast_heap * | h, |
unsigned int | index | ||
) |
Peek at an element on a heap.
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. |
Example code for a traversal:
Definition at line 267 of file heap.c.
References ast_heap::cur_len, heap_get(), and NULL.
Referenced by add_to_cache(), ast_bridge_features_merge(), ast_sched_clean_by_callback(), ast_sched_dump(), ast_sched_report(), ast_sched_runq(), ast_sched_wait(), ast_timer_open(), bridge_channel_handle_interval(), bridge_channel_next_interval(), expire_objects_from_cache(), hooks_remove_heap(), memory_cache_stale_check(), remove_from_cache(), sched_find(), and schedule_cache_expiration().
void* ast_heap_pop | ( | struct ast_heap * | h | ) |
Pop the max element off of the heap.
h | the heap |
Definition at line 262 of file heap.c.
References _ast_heap_remove().
Referenced by ast_bridge_features_cleanup(), ast_sched_context_destroy(), ast_sched_runq(), AST_TEST_DEFINE(), remove_all_from_cache(), and remove_oldest_from_cache().
void* ast_heap_remove | ( | struct ast_heap * | h, |
void * | elm | ||
) |
Remove a specific element from a heap.
h | the heap to remove from |
elm | the element to remove |
Definition at line 251 of file heap.c.
References _ast_heap_remove(), get_index(), and NULL.
Referenced by ast_sched_clean_by_callback(), ast_sched_del(), AST_TEST_DEFINE(), ast_unregister_timing_interface(), bridge_channel_handle_interval(), hooks_remove_heap(), and remove_from_cache().
size_t ast_heap_size | ( | struct ast_heap * | h | ) |
Get the current size of a heap.
h | the heap |
Definition at line 276 of file heap.c.
References ast_heap::cur_len.
Referenced by ast_sched_dump(), ast_sched_report(), hooks_remove_heap(), sched_find(), and schedule().
int ast_heap_verify | ( | struct ast_heap * | h | ) |
Verify that a heap has been properly constructed.
h | a heap |
0 | success |
non-zero | failure |
Definition at line 88 of file heap.c.
References ast_heap::cmp_fn, ast_heap::cur_len, heap_get(), left_node(), and right_node().
Referenced by AST_TEST_DEFINE().
|
static |
Definition at line 212 of file heap.c.
References ast_heap::cmp_fn, heap_get(), heap_swap(), and parent_node().
Referenced by _ast_heap_push(), and _ast_heap_remove().
|
inlinestatic |
Definition at line 65 of file heap.c.
References ast_heap::index_offset.
Referenced by ast_heap_remove().
|
static |
Add a row of additional storage for the heap.
Definition at line 161 of file heap.c.
References __ast_realloc(), ast_heap::avail_len, and ast_heap::heap.
Referenced by _ast_heap_push().
|
inlinestatic |
Definition at line 60 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().
|
inlinestatic |
Definition at line 78 of file heap.c.
References ast_heap::heap, and ast_heap::index_offset.
Referenced by _ast_heap_push(), _ast_heap_remove(), and heap_swap().
|
inlinestatic |
Definition at line 176 of file heap.c.
References heap_get(), heap_set(), and tmp().
Referenced by bubble_up(), and max_heapify().
|
inlinestatic |
Definition at line 45 of file heap.c.
Referenced by ast_heap_verify(), and max_heapify().
|
inlinestatic |
Definition at line 185 of file heap.c.
References ast_heap::cmp_fn, ast_heap::cur_len, heap_get(), heap_swap(), left_node(), max, and right_node().
Referenced by _ast_heap_remove().
|
inlinestatic |
|
inlinestatic |
Definition at line 50 of file heap.c.
Referenced by ast_heap_verify(), and max_heapify().