Annotation of embedaddon/dhcp/common/heap.c, revision 1.1.1.1

1.1       misho       1: /*
                      2:  * Copyright (C) 2004-2007  Internet Systems Consortium, Inc. ("ISC")
                      3:  * Copyright (C) 1997-2001  Internet Software Consortium.
                      4:  *
                      5:  * Permission to use, copy, modify, and distribute this software for any
                      6:  * purpose with or without fee is hereby granted, provided that the above
                      7:  * copyright notice and this permission notice appear in all copies.
                      8:  *
                      9:  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
                     10:  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
                     11:  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
                     12:  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
                     13:  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
                     14:  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
                     15:  * PERFORMANCE OF THIS SOFTWARE.
                     16:  */
                     17: 
1.1.1.1 ! misho      18: /* $Id: heap.c,v 1.2 2007/05/08 23:05:20 dhankins Exp $ */
1.1       misho      19: 
                     20: /*
                     21:  * This heap implementation is taken from the BIND 9 code. It has been
                     22:  * modified to use the DHCP memory management rather than BIND memory
                     23:  * contexts.
                     24:  */
                     25: 
                     26: /*! \file
                     27:  * Heap implementation of priority queues adapted from the following:
                     28:  *
                     29:  *     \li "Introduction to Algorithms," Cormen, Leiserson, and Rivest,
                     30:  *     MIT Press / McGraw Hill, 1990, ISBN 0-262-03141-8, chapter 7.
                     31:  *
                     32:  *     \li "Algorithms," Second Edition, Sedgewick, Addison-Wesley, 1988,
                     33:  *     ISBN 0-201-06673-4, chapter 11.
                     34:  */
                     35: 
                     36: #include "dhcpd.h"
                     37: #include "omapip/omapip.h"
                     38: #include "heap.h"
                     39: 
                     40: #include <assert.h>
                     41: #define REQUIRE assert
                     42: #define INSIST assert
                     43: 
                     44: /*@{*/
                     45: /*%
                     46:  * Note: to make heap_parent and heap_left easy to compute, the first
                     47:  * element of the heap array is not used; i.e. heap subscripts are 1-based,
                     48:  * not 0-based.  The parent is index/2, and the left-child is index*2.
                     49:  * The right child is index*2+1.
                     50:  */
                     51: #define heap_parent(i)                 ((i) >> 1)
                     52: #define heap_left(i)                   ((i) << 1)
                     53: /*@}*/
                     54: 
                     55: #define SIZE_INCREMENT                 1024
                     56: 
                     57: /*%
                     58:  * When the heap is in a consistent state, the following invariant
                     59:  * holds true: for every element i > 1, heap_parent(i) has a priority
                     60:  * higher than or equal to that of i.
                     61:  */
                     62: #define HEAPCONDITION(i) ((i) == 1 || \
                     63:                          ! heap->compare(heap->array[(i)], \
                     64:                                          heap->array[heap_parent(i)]))
                     65: 
                     66: /*% ISC heap structure. */
                     67: struct isc_heap {
                     68:        unsigned int                    size;
                     69:        unsigned int                    size_increment;
                     70:        unsigned int                    last;
                     71:        void                            **array;
                     72:        isc_heapcompare_t               compare;
                     73:        isc_heapindex_t                 index;
                     74: };
                     75: 
                     76: isc_result_t
                     77: isc_heap_create(isc_heapcompare_t compare,
                     78:                isc_heapindex_t index, unsigned int size_increment,
                     79:                isc_heap_t **heapp)
                     80: {
                     81:        isc_heap_t *heap;
                     82: 
                     83:        REQUIRE(heapp != NULL && *heapp == NULL);
                     84:        REQUIRE(compare != NULL);
                     85: 
                     86:        heap = dmalloc(sizeof(*heap), MDL);
                     87:        if (heap == NULL)
                     88:                return (ISC_R_NOMEMORY);
                     89:        heap->size = 0;
                     90:        if (size_increment == 0)
                     91:                heap->size_increment = SIZE_INCREMENT;
                     92:        else
                     93:                heap->size_increment = size_increment;
                     94:        heap->last = 0;
                     95:        heap->array = NULL;
                     96:        heap->compare = compare;
                     97:        heap->index = index;
                     98: 
                     99:        *heapp = heap;
                    100: 
                    101:        return (ISC_R_SUCCESS);
                    102: }
                    103: 
                    104: void
                    105: isc_heap_destroy(isc_heap_t **heapp) {
                    106:        isc_heap_t *heap;
                    107: 
                    108:        REQUIRE(heapp != NULL);
                    109:        heap = *heapp;
                    110: 
                    111:        if (heap->array != NULL)
                    112:                dfree(heap->array, MDL);
                    113:        dfree(heap, MDL);
                    114: 
                    115:        *heapp = NULL;
                    116: }
                    117: 
                    118: static isc_boolean_t
                    119: resize(isc_heap_t *heap) {
                    120:        void **new_array;
                    121:        size_t new_size;
                    122: 
                    123:        new_size = heap->size + heap->size_increment;
                    124:        new_array = dmalloc(new_size * sizeof(void *), MDL);
                    125:        if (new_array == NULL)
                    126:                return (ISC_FALSE);
                    127:        if (heap->array != NULL) {
                    128:                memcpy(new_array, heap->array, heap->size * sizeof(void *));
                    129:                dfree(heap->array, MDL);
                    130:        }
                    131:        heap->size = new_size;
                    132:        heap->array = new_array;
                    133: 
                    134:        return (ISC_TRUE);
                    135: }
                    136: 
                    137: static void
                    138: float_up(isc_heap_t *heap, unsigned int i, void *elt) {
                    139:        unsigned int p;
                    140: 
                    141:        for (p = heap_parent(i) ;
                    142:             i > 1 && heap->compare(elt, heap->array[p]) ;
                    143:             i = p, p = heap_parent(i)) {
                    144:                heap->array[i] = heap->array[p];
                    145:                if (heap->index != NULL)
                    146:                        (heap->index)(heap->array[i], i);
                    147:        }
                    148:        heap->array[i] = elt;
                    149:        if (heap->index != NULL)
                    150:                (heap->index)(heap->array[i], i);
                    151: 
                    152:        INSIST(HEAPCONDITION(i));
                    153: }
                    154: 
                    155: static void
                    156: sink_down(isc_heap_t *heap, unsigned int i, void *elt) {
                    157:        unsigned int j, size, half_size;
                    158:        size = heap->last;
                    159:        half_size = size / 2;
                    160:        while (i <= half_size) {
                    161:                /* Find the smallest of the (at most) two children. */
                    162:                j = heap_left(i);
                    163:                if (j < size && heap->compare(heap->array[j+1],
                    164:                                              heap->array[j]))
                    165:                        j++;
                    166:                if (heap->compare(elt, heap->array[j]))
                    167:                        break;
                    168:                heap->array[i] = heap->array[j];
                    169:                if (heap->index != NULL)
                    170:                        (heap->index)(heap->array[i], i);
                    171:                i = j;
                    172:        }
                    173:        heap->array[i] = elt;
                    174:        if (heap->index != NULL)
                    175:                (heap->index)(heap->array[i], i);
                    176: 
                    177:        INSIST(HEAPCONDITION(i));
                    178: }
                    179: 
                    180: isc_result_t
                    181: isc_heap_insert(isc_heap_t *heap, void *elt) {
                    182:        unsigned int i;
                    183: 
                    184:        i = ++heap->last;
                    185:        if (heap->last >= heap->size && !resize(heap))
                    186:                return (ISC_R_NOMEMORY);
                    187: 
                    188:        float_up(heap, i, elt);
                    189: 
                    190:        return (ISC_R_SUCCESS);
                    191: }
                    192: 
                    193: void
                    194: isc_heap_delete(isc_heap_t *heap, unsigned int index) {
                    195:        void *elt;
                    196:        isc_boolean_t less;
                    197: 
                    198:        REQUIRE(index >= 1 && index <= heap->last);
                    199: 
                    200:        if (index == heap->last) {
                    201:                heap->last--;
                    202:        } else {
                    203:                elt = heap->array[heap->last--];
                    204:                less = heap->compare(elt, heap->array[index]);
                    205:                heap->array[index] = elt;
                    206:                if (less)
                    207:                        float_up(heap, index, heap->array[index]);
                    208:                else
                    209:                        sink_down(heap, index, heap->array[index]);
                    210:        }
                    211: }
                    212: 
                    213: void
                    214: isc_heap_increased(isc_heap_t *heap, unsigned int index) {
                    215:        REQUIRE(index >= 1 && index <= heap->last);
                    216: 
                    217:        float_up(heap, index, heap->array[index]);
                    218: }
                    219: 
                    220: void
                    221: isc_heap_decreased(isc_heap_t *heap, unsigned int index) {
                    222:        REQUIRE(index >= 1 && index <= heap->last);
                    223: 
                    224:        sink_down(heap, index, heap->array[index]);
                    225: }
                    226: 
                    227: void *
                    228: isc_heap_element(isc_heap_t *heap, unsigned int index) {
                    229:        REQUIRE(index >= 1 && index <= heap->last);
                    230: 
                    231:        return (heap->array[index]);
                    232: }
                    233: 
                    234: void
                    235: isc_heap_foreach(isc_heap_t *heap, isc_heapaction_t action, void *uap) {
                    236:        unsigned int i;
                    237: 
                    238:        REQUIRE(action != NULL);
                    239: 
                    240:        for (i = 1 ; i <= heap->last ; i++)
                    241:                (action)(heap->array[i], uap);
                    242: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>