Asterisk - The Open Source Telephony Project  21.4.1
Data Structures | Functions
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. 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)
 
int _ast_heap_push (struct ast_heap *h, void *elm, const char *file, int lineno, const char *func)
 
static void * _ast_heap_remove (struct ast_heap *h, unsigned int index)
 
struct ast_heapast_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.
 
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 russe.nosp@m.ll@d.nosp@m.igium.nosp@m..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
hthe heap
file,func,lineA 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 286 of file heap.c.

287 {
288  return __ast_rwlock_rdlock(file, line, func, &h->lock, "&h->lock");
289 }
int __ast_heap_unlock ( struct ast_heap h,
const char *  file,
const char *  func,
int  line 
)

Unlock a heap.

Parameters
hthe heap
file,func,line
Returns
see the documentation for pthread_rwlock_unlock()
Since
1.6.1

Definition at line 291 of file heap.c.

292 {
293  return __ast_rwlock_unlock(file, line, func, &h->lock, "&h->lock");
294 }
int __ast_heap_wrlock ( struct ast_heap h,
const char *  file,
const char *  func,
int  line 
)

Write-Lock a heap.

Parameters
hthe heap
file,func,lineA 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 281 of file heap.c.

282 {
283  return __ast_rwlock_wrlock(file, line, func, &h->lock, "&h->lock");
284 }
struct ast_heap* ast_heap_destroy ( struct ast_heap h)

Destroy a max heap.

Parameters
hthe heap to destroy
Return values
NULLfor convenience
Since
1.6.1

Definition at line 146 of file heap.c.

Referenced by ast_bridge_features_cleanup(), and ast_sched_context_destroy().

147 {
148  ast_free(h->heap);
149  h->heap = NULL;
150 
151  ast_rwlock_destroy(&h->lock);
152 
153  ast_free(h);
154 
155  return NULL;
156 }
void* ast_heap_peek ( struct ast_heap h,
unsigned int  index 
)

Peek at an element on a heap.

Parameters
hthe heap
indexindex 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:

1 struct ast_heap *h;
2 
3 ...
4 
5 size_t size, i;
6 void *cur_obj;
7 
8 ast_heap_rdlock(h);
9 
10 size = ast_heap_size(h);
11 
12 for (i = 1; i <= size && (cur_obj = ast_heap_peek(h, i)); i++) {
13  ... Do stuff with cur_obj ...
14 }
15 
16 ast_heap_unlock(h);
Since
1.6.1

Definition at line 267 of file heap.c.

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

268 {
269  if (!h->cur_len || !index || index > h->cur_len) {
270  return NULL;
271  }
272 
273  return heap_get(h, index);
274 }
void* ast_heap_pop ( struct ast_heap h)

Pop the max element off of the heap.

Parameters
hthe 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 262 of file heap.c.

Referenced by ast_bridge_features_cleanup(), ast_sched_context_destroy(), and ast_sched_runq().

263 {
264  return _ast_heap_remove(h, 1);
265 }
void* ast_heap_remove ( struct ast_heap h,
void *  elm 
)

Remove a specific element from a heap.

Parameters
hthe heap to remove from
elmthe element to remove
Returns
elm, if the removal was successful
Return values
NULLif 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 251 of file heap.c.

Referenced by ast_sched_clean_by_callback(), ast_sched_del_nonrunning(), and ast_unregister_timing_interface().

252 {
253  ssize_t i = get_index(h, elm);
254 
255  if (i == -1) {
256  return NULL;
257  }
258 
259  return _ast_heap_remove(h, i);
260 }
size_t ast_heap_size ( struct ast_heap h)

Get the current size of a heap.

Parameters
hthe heap
Returns
the number of elements currently in the heap
Since
1.6.1

Definition at line 276 of file heap.c.

Referenced by ast_sched_dump(), ast_sched_report(), and schedule().

277 {
278  return h->cur_len;
279 }
int ast_heap_verify ( struct ast_heap h)

Verify that a heap has been properly constructed.

Parameters
ha heap
Return values
0success
non-zerofailure
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 88 of file heap.c.

89 {
90  unsigned int i;
91 
92  for (i = 1; i <= (h->cur_len / 2); i++) {
93  int l = left_node(i);
94  int r = right_node(i);
95 
96  if (l <= h->cur_len) {
97  if (h->cmp_fn(heap_get(h, i), heap_get(h, l)) < 0) {
98  return -1;
99  }
100  }
101 
102  if (r <= h->cur_len) {
103  if (h->cmp_fn(heap_get(h, i), heap_get(h, r)) < 0) {
104  return -1;
105  }
106  }
107  }
108 
109  return 0;
110 }