OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [rtems/] [c/] [src/] [libnetworking/] [rtems_webserver/] [h.c] - Diff between revs 30 and 173

Go to most recent revision | Only display areas with differences | Details | Blame | View Log

Rev 30 Rev 173
/*
/*
 * h.c -- Handle allocation module
 * h.c -- Handle allocation module
 *
 *
 * Copyright (c) GoAhead Software Inc., 1995-1999. All Rights Reserved.
 * Copyright (c) GoAhead Software Inc., 1995-1999. All Rights Reserved.
 * See the file "license.txt" for usage and redistribution license requirements
 * See the file "license.txt" for usage and redistribution license requirements
 */
 */
 
 
/******************************** Description *********************************/
/******************************** Description *********************************/
 
 
/*
/*
 *      This module provides a simple API to allocate and free handles
 *      This module provides a simple API to allocate and free handles
 *      It maintains a dynamic array of pointers. These usually point to
 *      It maintains a dynamic array of pointers. These usually point to
 *      per-handle structures.
 *      per-handle structures.
 */
 */
 
 
/********************************* Includes ***********************************/
/********************************* Includes ***********************************/
 
 
#if UEMF
#if UEMF
        #include        "uemf.h"
        #include        "uemf.h"
#else
#else
        #include        "basic/basicInternal.h"
        #include        "basic/basicInternal.h"
#endif
#endif
 
 
/********************************** Defines ***********************************/
/********************************** Defines ***********************************/
/*
/*
 *      The handle list stores the length of the list and the number of used
 *      The handle list stores the length of the list and the number of used
 *      handles in the first two words.  These are hidden from the caller by
 *      handles in the first two words.  These are hidden from the caller by
 *      returning a pointer to the third word to the caller
 *      returning a pointer to the third word to the caller
 */
 */
 
 
#define H_LEN           0                /* First entry holds length of list */
#define H_LEN           0                /* First entry holds length of list */
#define H_USED          1               /* Second entry holds number of used */
#define H_USED          1               /* Second entry holds number of used */
#define H_OFFSET        2               /* Offset to real start of list */
#define H_OFFSET        2               /* Offset to real start of list */
 
 
#define H_INCR          16              /* Grow handle list in chunks this size */
#define H_INCR          16              /* Grow handle list in chunks this size */
 
 
/*********************************** Code *************************************/
/*********************************** Code *************************************/
/*
/*
 *      Allocate a new file handle.  On the first call, the caller must set the
 *      Allocate a new file handle.  On the first call, the caller must set the
 *      handle map to be a pointer to a null pointer.  *map points to the second
 *      handle map to be a pointer to a null pointer.  *map points to the second
 *      element in the handle array.
 *      element in the handle array.
 */
 */
 
 
int hAlloc(void ***map)
int hAlloc(void ***map)
{
{
        int             *mp;
        int             *mp;
        int             handle, len, memsize, incr;
        int             handle, len, memsize, incr;
 
 
        a_assert(map);
        a_assert(map);
 
 
        if (*map == NULL) {
        if (*map == NULL) {
                incr = H_INCR;
                incr = H_INCR;
                memsize = (incr + H_OFFSET) * sizeof(void**);
                memsize = (incr + H_OFFSET) * sizeof(void**);
                if ((mp = (int*) balloc(B_L, memsize)) == NULL) {
                if ((mp = (int*) balloc(B_L, memsize)) == NULL) {
                        return -1;
                        return -1;
                }
                }
                memset(mp, 0, memsize);
                memset(mp, 0, memsize);
                mp[H_LEN] = incr;
                mp[H_LEN] = incr;
                mp[H_USED] = 0;
                mp[H_USED] = 0;
                *map = (void**) &mp[H_OFFSET];
                *map = (void**) &mp[H_OFFSET];
        } else {
        } else {
                mp = &((*(int**)map)[-H_OFFSET]);
                mp = &((*(int**)map)[-H_OFFSET]);
        }
        }
 
 
        len = mp[H_LEN];
        len = mp[H_LEN];
 
 
/*
/*
 *      Find the first null handle
 *      Find the first null handle
 */
 */
        if (mp[H_USED] < mp[H_LEN]) {
        if (mp[H_USED] < mp[H_LEN]) {
                for (handle = 0; handle < len; handle++)
                for (handle = 0; handle < len; handle++)
                        if (mp[handle+H_OFFSET] == 0) {
                        if (mp[handle+H_OFFSET] == 0) {
                                mp[H_USED]++;
                                mp[H_USED]++;
                                return handle;
                                return handle;
                        }
                        }
        } else {
        } else {
                handle = len;
                handle = len;
        }
        }
 
 
/*
/*
 *      No free handle so grow the handle list. Grow list in chunks of H_INCR.
 *      No free handle so grow the handle list. Grow list in chunks of H_INCR.
 */
 */
        len += H_INCR;
        len += H_INCR;
        memsize = (len + H_OFFSET) * sizeof(void**);
        memsize = (len + H_OFFSET) * sizeof(void**);
        if ((mp = (int*) brealloc(B_L, (void*) mp, memsize)) == NULL) {
        if ((mp = (int*) brealloc(B_L, (void*) mp, memsize)) == NULL) {
                return -1;
                return -1;
        }
        }
        *map = (void**) &mp[H_OFFSET];
        *map = (void**) &mp[H_OFFSET];
        mp[H_LEN] = len;
        mp[H_LEN] = len;
        memset(&mp[H_OFFSET + len - H_INCR], 0, sizeof(int*) * H_INCR);
        memset(&mp[H_OFFSET + len - H_INCR], 0, sizeof(int*) * H_INCR);
        mp[H_USED]++;
        mp[H_USED]++;
        return handle;
        return handle;
}
}
 
 
/******************************************************************************/
/******************************************************************************/
/*
/*
 *      Free a handle.  This function returns the value of the largest
 *      Free a handle.  This function returns the value of the largest
 *      handle in use plus 1, to be saved as a max value.
 *      handle in use plus 1, to be saved as a max value.
 */
 */
 
 
int hFree(void ***map, int handle)
int hFree(void ***map, int handle)
{
{
        int             *mp;
        int             *mp;
        int             len;
        int             len;
 
 
        a_assert(map);
        a_assert(map);
        mp = &((*(int**)map)[-H_OFFSET]);
        mp = &((*(int**)map)[-H_OFFSET]);
        a_assert(mp[H_LEN] >= H_INCR);
        a_assert(mp[H_LEN] >= H_INCR);
 
 
        a_assert(mp[handle + H_OFFSET]);
        a_assert(mp[handle + H_OFFSET]);
        a_assert(mp[H_USED]);
        a_assert(mp[H_USED]);
        mp[handle + H_OFFSET] = 0;
        mp[handle + H_OFFSET] = 0;
        if (--(mp[H_USED]) == 0) {
        if (--(mp[H_USED]) == 0) {
                bfree(B_L, (void*) mp);
                bfree(B_L, (void*) mp);
                *map = NULL;
                *map = NULL;
        }
        }
 
 
/*
/*
 *      Find the greatest handle number in use.
 *      Find the greatest handle number in use.
 */
 */
        if (*map == NULL) {
        if (*map == NULL) {
                handle = -1;
                handle = -1;
        } else {
        } else {
                len = mp[H_LEN];
                len = mp[H_LEN];
                if (mp[H_USED] < mp[H_LEN]) {
                if (mp[H_USED] < mp[H_LEN]) {
                        for (handle = len - 1; handle >= 0; handle--) {
                        for (handle = len - 1; handle >= 0; handle--) {
                                if (mp[handle + H_OFFSET])
                                if (mp[handle + H_OFFSET])
                                        break;
                                        break;
                        }
                        }
                } else {
                } else {
                        handle = len;
                        handle = len;
                }
                }
        }
        }
        return handle + 1;
        return handle + 1;
}
}
 
 
/******************************************************************************/
/******************************************************************************/
/*
/*
 *      Allocate an entry in the halloc array.
 *      Allocate an entry in the halloc array.
 */
 */
 
 
int hAllocEntry(void ***list, int *max, int size)
int hAllocEntry(void ***list, int *max, int size)
{
{
        char_t  *cp;
        char_t  *cp;
        int             id;
        int             id;
 
 
        a_assert(list);
        a_assert(list);
        a_assert(max);
        a_assert(max);
 
 
        if ((id = hAlloc((void***) list)) < 0) {
        if ((id = hAlloc((void***) list)) < 0) {
                return -1;
                return -1;
        }
        }
 
 
        if (size > 0) {
        if (size > 0) {
                if ((cp = balloc(B_L, size)) == NULL) {
                if ((cp = balloc(B_L, size)) == NULL) {
                        hFree(list, id);
                        hFree(list, id);
                        return -1;
                        return -1;
                }
                }
                a_assert(cp);
                a_assert(cp);
                memset(cp, 0, size);
                memset(cp, 0, size);
 
 
                (*list)[id] = (void*) cp;
                (*list)[id] = (void*) cp;
        }
        }
 
 
        if (id >= *max) {
        if (id >= *max) {
                *max = id + 1;
                *max = id + 1;
        }
        }
        return id;
        return id;
}
}
 
 
/******************************************************************************/
/******************************************************************************/
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.