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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [rtems-20020807/] [c/] [src/] [libnetworking/] [rtems_webserver/] [h.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1026 ivang
/*
2
 * h.c -- Handle allocation module
3
 *
4
 * Copyright (c) GoAhead Software Inc., 1995-2000. 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
#if B_STATS
45
int HALLOC(B_ARGS_DEC, void ***map)
46
#else
47
int hAlloc(void ***map)
48
#endif
49
{
50
        int             *mp;
51
        int             handle, len, memsize, incr;
52
 
53
        a_assert(map);
54
 
55
        if (*map == NULL) {
56
                incr = H_INCR;
57
                memsize = (incr + H_OFFSET) * sizeof(void**);
58
#if B_STATS
59
                if ((mp = (int*) balloc(B_ARGS, memsize)) == NULL) {
60
#else
61
                if ((mp = (int*) balloc(B_L, memsize)) == NULL) {
62
#endif
63
                        return -1;
64
                }
65
                memset(mp, 0, memsize);
66
                mp[H_LEN] = incr;
67
                mp[H_USED] = 0;
68
                *map = (void**) &mp[H_OFFSET];
69
        } else {
70
                mp = &((*(int**)map)[-H_OFFSET]);
71
        }
72
 
73
        len = mp[H_LEN];
74
 
75
/*
76
 *      Find the first null handle
77
 */
78
        if (mp[H_USED] < mp[H_LEN]) {
79
                for (handle = 0; handle < len; handle++) {
80
                        if (mp[handle+H_OFFSET] == 0) {
81
                                mp[H_USED]++;
82
                                return handle;
83
                        }
84
                }
85
        } else {
86
                handle = len;
87
        }
88
 
89
/*
90
 *      No free handle so grow the handle list. Grow list in chunks of H_INCR.
91
 */
92
        len += H_INCR;
93
        memsize = (len + H_OFFSET) * sizeof(void**);
94
        if ((mp = (int*) brealloc(B_L, (void*) mp, memsize)) == NULL) {
95
                return -1;
96
        }
97
        *map = (void**) &mp[H_OFFSET];
98
        mp[H_LEN] = len;
99
        memset(&mp[H_OFFSET + len - H_INCR], 0, sizeof(int*) * H_INCR);
100
        mp[H_USED]++;
101
        return handle;
102
}
103
 
104
/******************************************************************************/
105
/*
106
 *      Free a handle.  This function returns the value of the largest
107
 *      handle in use plus 1, to be saved as a max value.
108
 */
109
 
110
int hFree(void ***map, int handle)
111
{
112
        int             *mp;
113
        int             len;
114
 
115
        a_assert(map);
116
        mp = &((*(int**)map)[-H_OFFSET]);
117
        a_assert(mp[H_LEN] >= H_INCR);
118
 
119
        a_assert(mp[handle + H_OFFSET]);
120
        a_assert(mp[H_USED]);
121
        mp[handle + H_OFFSET] = 0;
122
        if (--(mp[H_USED]) == 0) {
123
                bfree(B_L, (void*) mp);
124
                *map = NULL;
125
        }
126
 
127
/*
128
 *      Find the greatest handle number in use.
129
 */
130
        if (*map == NULL) {
131
                handle = -1;
132
        } else {
133
                len = mp[H_LEN];
134
                if (mp[H_USED] < mp[H_LEN]) {
135
                        for (handle = len - 1; handle >= 0; handle--) {
136
                                if (mp[handle + H_OFFSET])
137
                                        break;
138
                        }
139
                } else {
140
                        handle = len;
141
                }
142
        }
143
        return handle + 1;
144
}
145
 
146
/******************************************************************************/
147
/*
148
 *      Allocate an entry in the halloc array.
149
 */
150
 
151
#if B_STATS
152
int HALLOCENTRY(B_ARGS_DEC, void ***list, int *max, int size)
153
#else
154
int hAllocEntry(void ***list, int *max, int size)
155
#endif
156
{
157
        char_t  *cp;
158
        int             id;
159
 
160
        a_assert(list);
161
        a_assert(max);
162
 
163
#if B_STATS
164
        if ((id = HALLOC(B_ARGS, (void***) list)) < 0) {
165
#else
166
        if ((id = hAlloc((void***) list)) < 0) {
167
#endif
168
                return -1;
169
        }
170
 
171
        if (size > 0) {
172
#if B_STATS
173
                if ((cp = balloc(B_ARGS, size)) == NULL) {
174
#else
175
                if ((cp = balloc(B_L, size)) == NULL) {
176
#endif
177
                        hFree(list, id);
178
                        return -1;
179
                }
180
                a_assert(cp);
181
                memset(cp, 0, size);
182
 
183
                (*list)[id] = (void*) cp;
184
        }
185
 
186
        if (id >= *max) {
187
                *max = id + 1;
188
        }
189
        return id;
190
}
191
 
192
/******************************************************************************/
193
 

powered by: WebSVN 2.1.0

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