blob: 228b162fbf6339c71678e16d1cab612966c7fbe3 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001#ifdef UEMF
2 #include "uemf.h"
3#else
4 #include "basic/basicInternal.h"
5#endif
6
7
8#define H_LEN 0
9#define H_USED 1
10#define H_OFFSET 2
11#define H_INCR 16
12
13
14int hFree(void ***map, int handle)
15{
16 int len;
17 int *mp;
18
19 a_assert(map);
20 mp = &((*(int**)map)[-H_OFFSET]);
21 a_assert(mp[H_LEN] >= H_INCR);
22
23 a_assert(mp[handle + H_OFFSET]);
24 a_assert(mp[H_USED]);
25 mp[handle + H_OFFSET] = 0;
26 if (--(mp[H_USED]) == 0) {
27 bfree(B_L, (void*) mp);
28 *map = NULL;
29 }
30
31
32 if (*map == NULL) {
33 handle = -1;
34 } else {
35 len = mp[H_LEN];
36 if (mp[H_USED] < mp[H_LEN]) {
37 for (handle = len - 1; handle >= 0; handle--) {
38 if (mp[handle + H_OFFSET])
39 break;
40 }
41 } else {
42 handle = len;
43 }
44 }
45 return handle + 1;
46}
47
48
49#ifdef B_STATS
50int HALLOC(B_ARGS_DEC, void ***map)
51#else
52int hAlloc(void ***map)
53#endif
54{
55 int handle, len, memsize, incr;
56 int *mp;
57
58 a_assert(map);
59
60 if (*map == NULL) {
61 incr = H_INCR;
62 memsize = (incr + H_OFFSET) * sizeof(void**);
63#ifdef B_STATS
64 if ((mp = (int*) balloc(B_ARGS, memsize)) == NULL) {
65#else
66 if ((mp = (int*) balloc(B_L, memsize)) == NULL) {
67#endif
68 return -1;
69 }
70 memset(mp, 0, memsize);
71 mp[H_LEN] = incr;
72 mp[H_USED] = 0;
73 *map = (void**) &mp[H_OFFSET];
74 } else {
75 mp = &((*(int**)map)[-H_OFFSET]);
76 }
77
78 len = mp[H_LEN];
79
80
81 if (mp[H_USED] < mp[H_LEN]) {
82 for (handle = 0; handle < len; handle++) {
83 if (mp[handle+H_OFFSET] == 0) {
84 mp[H_USED]++;
85 return handle;
86 }
87 }
88 } else {
89 handle = len;
90 }
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
101 mp[H_USED]++;
102 return handle;
103}
104
105
106#ifdef B_STATS
107int HALLOCENTRY(B_ARGS_DEC, void ***list, int *max, int size)
108#else
109int hAllocEntry(void ***list, int *max, int size)
110#endif
111{
112 int id;
113 char_t *cp;
114
115 a_assert(list);
116 a_assert(max);
117
118#ifdef B_STATS
119 if ((id = HALLOC(B_ARGS, (void***) list)) < 0) {
120#else
121 if ((id = hAlloc((void***) list)) < 0) {
122#endif
123 return -1;
124 }
125
126 if (size > 0) {
127#ifdef B_STATS
128 if ((cp = balloc(B_ARGS, size)) == NULL) {
129#else
130 if ((cp = balloc(B_L, size)) == NULL) {
131#endif
132 hFree(list, id);
133 return -1;
134 }
135 a_assert(cp);
136 memset(cp, 0, size);
137
138 (*list)[id] = (void*) cp;
139 }
140
141 if (id >= *max) {
142 *max = id + 1;
143 }
144 return id;
145}
146