Annotation of embedaddon/dhcp/omapip/array.c, revision 1.1.1.1

1.1       misho       1: /* listener.c
                      2: 
                      3:    Subroutines that support the omapi extensible array type. */
                      4: 
                      5: /*
                      6:  * Copyright (c) 2004-2007,2009 by Internet Systems Consortium, Inc. ("ISC")
                      7:  * Copyright (c) 2001-2003 by Internet Software Consortium
                      8:  *
                      9:  * Permission to use, copy, modify, and distribute this software for any
                     10:  * purpose with or without fee is hereby granted, provided that the above
                     11:  * copyright notice and this permission notice appear in all copies.
                     12:  *
                     13:  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
                     14:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     15:  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
                     16:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     17:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     18:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
                     19:  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     20:  *
                     21:  *   Internet Systems Consortium, Inc.
                     22:  *   950 Charter Street
                     23:  *   Redwood City, CA 94063
                     24:  *   <info@isc.org>
                     25:  *   https://www.isc.org/
                     26:  *
                     27:  * This software has been written for Internet Systems Consortium
                     28:  * by Ted Lemon in cooperation with Vixie Enterprises and Nominum, Inc.
                     29:  * To learn more about Internet Systems Consortium, see
                     30:  * ``https://www.isc.org/''.  To learn more about Vixie Enterprises,
                     31:  * see ``http://www.vix.com''.   To learn more about Nominum, Inc., see
                     32:  * ``http://www.nominum.com''.
                     33:  */
                     34: 
                     35: #include "dhcpd.h"
                     36: 
                     37: #include <omapip/omapip_p.h>
                     38: 
                     39: /* Allocate a new extensible array. */
                     40: 
                     41: isc_result_t omapi_array_allocate (omapi_array_t **array,
                     42:                                   omapi_array_ref_t ref,
                     43:                                   omapi_array_deref_t deref,
                     44:                                   const char *file, int line)
                     45: {
                     46:        omapi_array_t *aptr;
                     47: 
                     48:        if (!array || *array)
                     49:                return ISC_R_INVALIDARG;
                     50:        aptr = dmalloc (sizeof (omapi_array_t),file, line);
                     51:        if (!aptr)
                     52:                return ISC_R_NOMEMORY;
                     53:        *array = aptr;
                     54:        aptr -> ref = ref;
                     55:        aptr -> deref = deref;
                     56:        return ISC_R_SUCCESS;
                     57: }
                     58: 
                     59: isc_result_t omapi_array_free (omapi_array_t **array,
                     60:                               const char *file, int line)
                     61: {
                     62:        omapi_array_t *aptr;
                     63:        int i;
                     64: 
                     65:        if (!array || !*array)
                     66:                return ISC_R_INVALIDARG;
                     67:        aptr = *array;
                     68:        for (i = 0; i < aptr -> count; i++)
                     69:                if (aptr -> data [i] && aptr -> deref)
                     70:                        (*aptr -> deref) (&aptr -> data [i], file, line);
                     71:        dfree (aptr -> data, MDL);
                     72:        dfree (aptr, MDL);
                     73:        *array = (omapi_array_t *)0;
                     74:        return ISC_R_SUCCESS;
                     75: }
                     76: 
                     77: /* Extend the size of the array by one entry (we may allocate more than that)
                     78:    and store the specified value in the new array element. */
                     79: 
                     80: isc_result_t omapi_array_extend (omapi_array_t *array, char *ptr,
                     81:                                 int *index, const char *file, int line)
                     82: {
                     83:        isc_result_t status;
                     84:        int new = array -> count;
                     85:        status = omapi_array_set (array, ptr, new, file, line);
                     86:        if (index && status == ISC_R_SUCCESS)
                     87:                *index = new;
                     88:        return status;
                     89: }
                     90: 
                     91: /* Set a value in the specified array, extending it if necessary. */
                     92: 
                     93: isc_result_t omapi_array_set (omapi_array_t *array, void *ptr, int index,
                     94:                              const char *file, int line)
                     95: {
                     96:        char **newbuf;
                     97:        int delta;
                     98:        isc_result_t status;
                     99: 
                    100:        if (!array)
                    101:                return ISC_R_INVALIDARG;
                    102:        if (!ptr)
                    103:                return ISC_R_INVALIDARG;
                    104:        if (index < 0)
                    105:                return ISC_R_INVALIDARG;
                    106: 
                    107:        /* If the proposed index is larger than the current available
                    108:           space in the array, make more space in the array. */
                    109:        if (array -> max <= index) {
                    110:                delta = index - array -> max + 10;
                    111:                newbuf = dmalloc ((array -> max + delta) * sizeof (char *),
                    112:                                  file, line);
                    113:                if (!newbuf)
                    114:                        return ISC_R_NOMEMORY;
                    115:                /* Zero the new elements. */
                    116:                memset (&newbuf [array -> max], 0, (sizeof (char *)) * delta);
                    117:                array -> max += delta;
                    118:                /* Copy the old array data into the new buffer. */
                    119:                if (array -> data) {
                    120:                    memcpy (newbuf,
                    121:                            array -> data, array -> count * sizeof (char *));
                    122:                    dfree (array -> data, file, line);
                    123:                }
                    124:                array -> data = newbuf;
                    125:        } else {
                    126:                /* If there's already data there, and this is an array
                    127:                   of references, dereference what's there. */
                    128:                if (array -> data [index]) {
                    129:                        status = ((*array -> deref) (&array -> data [index],
                    130:                                                     file, line));
                    131:                
                    132:                        if (status != ISC_R_SUCCESS)
                    133:                                return status;
                    134:                }
                    135:        }
                    136: 
                    137:        /* Store the pointer using the referencer function.  We have
                    138:           either just memset this to zero or dereferenced what was
                    139:           there previously, so there is no need to do anything if the
                    140:           pointer we have been asked to store is null. */
                    141:        if (ptr) {
                    142:                status = (*array -> ref) (&array -> data [index], ptr,
                    143:                                          file, line);
                    144:                if (status != ISC_R_SUCCESS)
                    145:                        return status;
                    146:        }
                    147:        if (index >= array -> count)
                    148:                array -> count = index + 1;
                    149:        return ISC_R_SUCCESS;
                    150: }
                    151: 
                    152: isc_result_t omapi_array_lookup (char **ptr, omapi_array_t *array, int index,
                    153:                                 const char *file, int line)
                    154: {
                    155:        if (!array || !ptr || *ptr || index < 0 || index >= array -> count)
                    156:                return ISC_R_INVALIDARG;
                    157:        if (array -> data [index])
                    158:                return (*array -> ref) (ptr,
                    159:                                        array -> data [index], file, line);
                    160:        return ISC_R_NOTFOUND;
                    161: }
                    162: 

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