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

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [rtos/] [rtems/] [c/] [src/] [libnetworking/] [rtems_webserver/] [h.c] - Blame information for rev 373

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 30 unneback
/*
2
 * h.c -- Handle allocation module
3
 *
4
 * Copyright (c) GoAhead Software Inc., 1995-1999. All Rights Reserved.
5
 * See the file "license.txt" for usage and redistribution license requirements
6
 */
7
 
8
/******************************** Description *********************************/
9
 
10
/*
11
 *      This module provides a simple API to allocate and free handles
12
 *      It maintains a dynamic array of pointers. These usually point to
13
 *      per-handle structures.
14
 */
15
 
16
/********************************* Includes ***********************************/
17
 
18
#if UEMF
19
        #include        "uemf.h"
20
#else
21
        #include        "basic/basicInternal.h"
22
#endif
23
 
24
/********************************** Defines ***********************************/
25
/*
26
 *      The handle list stores the length of the list and the number of used
27
 *      handles in the first two words.  These are hidden from the caller by
28
 *      returning a pointer to the third word to the caller
29
 */
30
 
31
#define H_LEN           0                /* First entry holds length of list */
32
#define H_USED          1               /* Second entry holds number of used */
33
#define H_OFFSET        2               /* Offset to real start of list */
34
 
35
#define H_INCR          16              /* Grow handle list in chunks this size */
36
 
37
/*********************************** Code *************************************/
38
/*
39
 *      Allocate a new file handle.  On the first call, the caller must set the
40
 *      handle map to be a pointer to a null pointer.  *map points to the second
41
 *      element in the handle array.
42
 */
43
 
44
int hAlloc(void ***map)
45
{
46
        int             *mp;
47
        int             handle, len, memsize, incr;
48
 
49
        a_assert(map);
50
 
51
        if (*map == NULL) {
52
                incr = H_INCR;
53
                memsize = (incr + H_OFFSET) * sizeof(void**);
54
                if ((mp = (int*) balloc(B_L, memsize)) == NULL) {
55
                        return -1;
56
                }
57
                memset(mp, 0, memsize);
58
                mp[H_LEN] = incr;
59
                mp[H_USED] = 0;
60
                *map = (void**) &mp[H_OFFSET];
61
        } else {
62
                mp = &((*(int**)map)[-H_OFFSET]);
63
        }
64
 
65
        len = mp[H_LEN];
66
 
67
/*
68
 *      Find the first null handle
69
 */
70
        if (mp[H_USED] < mp[H_LEN]) {
71
                for (handle = 0; handle < len; handle++)
72
                        if (mp[handle+H_OFFSET] == 0) {
73
                                mp[H_USED]++;
74
                                return handle;
75
                        }
76
        } else {
77
                handle = len;
78
        }
79
 
80
/*
81
 *      No free handle so grow the handle list. Grow list in chunks of H_INCR.
82
 */
83
        len += H_INCR;
84
        memsize = (len + H_OFFSET) * sizeof(void**);
85
        if ((mp = (int*) brealloc(B_L, (void*) mp, memsize)) == NULL) {
86
                return -1;
87
        }
88
        *map = (void**) &mp[H_OFFSET];
89
        mp[H_LEN] = len;
90
        memset(&mp[H_OFFSET + len - H_INCR], 0, sizeof(int*) * H_INCR);
91
        mp[H_USED]++;
92
        return handle;
93
}
94
 
95
/******************************************************************************/
96
/*
97
 *      Free a handle.  This function returns the value of the largest
98
 *      handle in use plus 1, to be saved as a max value.
99
 */
100
 
101
int hFree(void ***map, int handle)
102
{
103
        int             *mp;
104
        int             len;
105
 
106
        a_assert(map);
107
        mp = &((*(int**)map)[-H_OFFSET]);
108
        a_assert(mp[H_LEN] >= H_INCR);
109
 
110
        a_assert(mp[handle + H_OFFSET]);
111
        a_assert(mp[H_USED]);
112
        mp[handle + H_OFFSET] = 0;
113
        if (--(mp[H_USED]) == 0) {
114
                bfree(B_L, (void*) mp);
115
                *map = NULL;
116
        }
117
 
118
/*
119
 *      Find the greatest handle number in use.
120
 */
121
        if (*map == NULL) {
122
                handle = -1;
123
        } else {
124
                len = mp[H_LEN];
125
                if (mp[H_USED] < mp[H_LEN]) {
126
                        for (handle = len - 1; handle >= 0; handle--) {
127
                                if (mp[handle + H_OFFSET])
128
                                        break;
129
                        }
130
                } else {
131
                        handle = len;
132
                }
133
        }
134
        return handle + 1;
135
}
136
 
137
/******************************************************************************/
138
/*
139
 *      Allocate an entry in the halloc array.
140
 */
141
 
142
int hAllocEntry(void ***list, int *max, int size)
143
{
144
        char_t  *cp;
145
        int             id;
146
 
147
        a_assert(list);
148
        a_assert(max);
149
 
150
        if ((id = hAlloc((void***) list)) < 0) {
151
                return -1;
152
        }
153
 
154
        if (size > 0) {
155
                if ((cp = balloc(B_L, size)) == NULL) {
156
                        hFree(list, id);
157
                        return -1;
158
                }
159
                a_assert(cp);
160
                memset(cp, 0, size);
161
 
162
                (*list)[id] = (void*) cp;
163
        }
164
 
165
        if (id >= *max) {
166
                *max = id + 1;
167
        }
168
        return id;
169
}
170
 
171
/******************************************************************************/

powered by: WebSVN 2.1.0

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