[svn-commits] russell: branch russell/heap r176016 - in /team/russell/heap: include/asteris...

SVN commits to the Digium repositories svn-commits at lists.digium.com
Sun Feb 15 21:26:35 CST 2009


Author: russell
Date: Sun Feb 15 21:26:35 2009
New Revision: 176016

URL: http://svn.digium.com/svn-view/asterisk?view=rev&rev=176016
Log:
Actually make the index_offset optional, as the docs claim it to be

Modified:
    team/russell/heap/include/asterisk/heap.h
    team/russell/heap/main/heap.c

Modified: team/russell/heap/include/asterisk/heap.h
URL: http://svn.digium.com/svn-view/asterisk/team/russell/heap/include/asterisk/heap.h?view=diff&rev=176016&r1=176015&r2=176016
==============================================================================
--- team/russell/heap/include/asterisk/heap.h (original)
+++ team/russell/heap/include/asterisk/heap.h Sun Feb 15 21:26:35 2009
@@ -54,15 +54,17 @@
  * \param cmp_fn The function that should be used to compare elements in the heap.
  * \param 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 "unsigned int" has been made available for the heap's internal
+ *        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.
+ *        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.
  *
  * \return An instance of a max heap
  */
 struct ast_heap *ast_heap_create(unsigned int init_height, ast_heap_cmp_fn cmp_fn,
-		size_t index_offset);
+		ssize_t index_offset);
 
 /*!
  * \brief Destroy a max heap

Modified: team/russell/heap/main/heap.c
URL: http://svn.digium.com/svn-view/asterisk/team/russell/heap/main/heap.c?view=diff&rev=176016&r1=176015&r2=176016
==============================================================================
--- team/russell/heap/main/heap.c (original)
+++ team/russell/heap/main/heap.c Sun Feb 15 21:26:35 2009
@@ -33,7 +33,7 @@
 
 struct ast_heap {
 	ast_heap_cmp_fn cmp_fn;
-	size_t index_offset;
+	ssize_t index_offset;
 	size_t cur_len;
 	size_t avail_len;
 	void **heap;
@@ -59,19 +59,27 @@
 	return h->heap[i - 1];
 }
 
-static inline size_t get_index(struct ast_heap *h, void *elm)
-{
-	size_t *index = elm + h->index_offset;
+static inline ssize_t get_index(struct ast_heap *h, void *elm)
+{
+	size_t *index;
+
+	if (h->index_offset < 0) {
+		return -1;
+	}
+
+	index = elm + h->index_offset;
 
 	return *index;
 }
 
 static inline void heap_set(struct ast_heap *h, int i, void *elm)
 {
-	size_t *index = elm + h->index_offset;
-
 	h->heap[i - 1] = elm;
-	*index = i;
+
+	if (h->index_offset >= 0) {
+		ssize_t *index = elm + h->index_offset;
+		*index = i;
+	}
 }
 
 int ast_heap_verify(struct ast_heap *h)
@@ -99,7 +107,7 @@
 }
 
 struct ast_heap *ast_heap_create(unsigned int init_height, ast_heap_cmp_fn cmp_fn,
-		size_t index_offset)
+		ssize_t index_offset)
 {
 	struct ast_heap *h;
 
@@ -226,7 +234,13 @@
 
 void *ast_heap_remove(struct ast_heap *h, void *elm)
 {
-	return _ast_heap_remove(h, get_index(h, elm));
+	ssize_t i = get_index(h, elm);
+
+	if (i == -1) {
+		return NULL;
+	}
+
+	return _ast_heap_remove(h, i);
 }
 
 void *ast_heap_pop(struct ast_heap *h)




More information about the svn-commits mailing list