sofia-sip/heap.h

Go to the documentation of this file.
00001 /*
00002  * This file is part of the Sofia-SIP package
00003  *
00004  * Copyright (C) 2007 Nokia Corporation.
00005  *
00006  * Contact: Pekka Pessi <pekka.pessi@nokia-email.address.hidden>
00007  *
00008  * This library is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU Lesser General Public License
00010  * as published by the Free Software Foundation; either version 2.1 of
00011  * the License, or (at your option) any later version.
00012  *
00013  * This library is distributed in the hope that it will be useful, but
00014  * WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00016  * Lesser General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU Lesser General Public
00019  * License along with this library; if not, write to the Free Software
00020  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
00021  * 02110-1301 USA
00022  *
00023  */
00024 
00025 #ifndef SOFIA_SIP_HEAP_H
00026 
00027 #define SOFIA_SIP_HEAP_H
00028 
00077 #define HEAP_MIN_SIZE 31
00078 
00085 #define HEAP_TYPE struct { void *private; }
00086 
00111 #define HEAP_DECLARE(scope, heaptype, prefix, type) \
00112 scope int prefix##resize(void *, heaptype *, size_t); \
00113 scope int prefix##free(void *, heaptype *); \
00114 scope int prefix##is_full(heaptype const); \
00115 scope size_t prefix##size(heaptype const); \
00116 scope size_t prefix##used(heaptype const); \
00117 scope int prefix##add(heaptype, type); \
00118 scope type prefix##remove(heaptype, size_t); \
00119 scope type prefix##get(heaptype, size_t)
00120 
00159 #define HEAP_BODIES(scope, heaptype, prefix, type, less, set, alloc, null) \
00160 scope int prefix##resize(void *realloc_arg, heaptype h[1], size_t new_size) \
00161 { \
00162   struct prefix##priv { size_t _size, _used; type _heap[2]; }; \
00163   struct prefix##priv *_priv; \
00164   size_t _offset = \
00165     (offsetof(struct prefix##priv, _heap[1]) - 1) / sizeof (type); \
00166   size_t _min_size = 32 - _offset; \
00167   size_t _bytes; \
00168   size_t _used = 0; \
00169  \
00170   _priv = *(void **)h; \
00171  \
00172   if (_priv) { \
00173     if (new_size == 0) \
00174       new_size = 2 * _priv->_size + _offset + 1; \
00175     _used = _priv->_used; \
00176     if (new_size < _used) \
00177       new_size = _used; \
00178   } \
00179  \
00180   if (new_size < _min_size) \
00181     new_size = _min_size; \
00182  \
00183   _bytes = (_offset + 1 + new_size) * sizeof (type); \
00184  \
00185   (void)realloc_arg; /* avoid warning */ \
00186   _priv = alloc(realloc_arg, *(struct prefix##priv **)h, _bytes); \
00187   if (!_priv) \
00188     return -1; \
00189  \
00190   *(struct prefix##priv **)h = _priv; \
00191   _priv->_size = new_size; \
00192   _priv->_used = _used; \
00193  \
00194   return 0; \
00195 } \
00196  \
00197  \
00198 scope int prefix##free(void *realloc_arg, heaptype h[1]) \
00199 { \
00200   (void)realloc_arg; \
00201   *(void **)h = alloc(realloc_arg, *(void **)h, 0); \
00202   return 0; \
00203 } \
00204  \
00205  \
00206 scope int prefix##is_full(heaptype h) \
00207 { \
00208   struct prefix##priv { size_t _size, _used; type _heap[1];}; \
00209   struct prefix##priv *_priv = *(void **)&h; \
00210  \
00211   return _priv == NULL || _priv->_used >= _priv->_size; \
00212 } \
00213  \
00214  \
00215 scope int prefix##add(heaptype h, type e) \
00216 { \
00217   struct prefix##priv { size_t _size, _used; type _heap[1];};   \
00218   struct prefix##priv *_priv = *(void **)&h; \
00219   type *heap = _priv->_heap - 1; \
00220   size_t i, parent; \
00221  \
00222   if (_priv == NULL || _priv->_used >= _priv->_size) \
00223     return -1; \
00224  \
00225   for (i = ++_priv->_used; i > 1; i = parent) { \
00226     parent = i / 2; \
00227     if (!less(e, heap[parent])) \
00228       break; \
00229     set(heap, i, heap[parent]); \
00230   } \
00231  \
00232   set(heap, i, e); \
00233  \
00234   return 0; \
00235 } \
00236  \
00237  \
00238 scope type prefix##remove(heaptype h, size_t index) \
00239 { \
00240   struct prefix##priv { size_t _size, _used; type _heap[1];}; \
00241   struct prefix##priv *_priv = *(void **)&h; \
00242   type *heap = _priv->_heap - 1; \
00243   type retval; \
00244   type e; \
00245  \
00246   size_t top, left, right, move; \
00247  \
00248   move = _priv->_used; \
00249  \
00250   if (index - 1 >= _priv->_used) \
00251     return (null); \
00252  \
00253   move = _priv->_used--; \
00254   retval = heap[top = index]; \
00255  \
00256   for (;;) { \
00257     left = 2 * top; \
00258     right = 2 * top + 1; \
00259  \
00260     if (right >= move) \
00261       break; \
00262     if (less(heap[right], heap[left])) \
00263       top = right; \
00264     else \
00265       top = left; \
00266     set(heap, index, heap[top]); \
00267     index = top; \
00268   } \
00269  \
00270   if (index == move) \
00271     return retval; \
00272  \
00273   e = heap[move]; \
00274   for (; index > 1; index = top) { \
00275     top = index / 2; \
00276     if (!less(e, heap[top])) \
00277       break; \
00278     set(heap, index, heap[top]); \
00279   } \
00280  \
00281   set(heap, index, e); \
00282  \
00283   return retval; \
00284 } \
00285  \
00286 scope \
00287 type prefix##get(heaptype h, size_t index) \
00288 { \
00289   struct prefix##priv { size_t _size, _used; type _heap[1];}; \
00290   struct prefix##priv *_priv = *(void **)&h; \
00291  \
00292   if (--index >= _priv->_used) \
00293     return (null); \
00294  \
00295   return _priv->_heap[index]; \
00296 } \
00297  \
00298 scope \
00299 size_t prefix##size(heaptype const h) \
00300 { \
00301   struct prefix##priv { size_t _size, _used; type _heap[1];}; \
00302   struct prefix##priv *_priv = *(void **)&h; \
00303   return _priv ? _priv->_size : 0; \
00304 } \
00305  \
00306 scope \
00307 size_t prefix##used(heaptype const h) \
00308 { \
00309   struct prefix##priv { size_t _size, _used; type _heap[1];}; \
00310   struct prefix##priv *_priv = *(void **)&h; \
00311   return _priv ? _priv->_used : 0; \
00312 } \
00313 extern int const prefix##dummy_heap
00314 
00315 #endif 

Sofia-SIP 1.12.6work - Copyright (C) 2006 Nokia Corporation. All rights reserved. Licensed under the terms of the GNU Lesser General Public License.