| 182 | | char* |
| 183 | | i_malloc(len, tag) |
| 184 | | unsigned len; |
| 185 | | char *tag; |
| 186 | | { |
| 187 | | char *ptr; |
| 188 | | struct glob *ob; |
| 189 | | struct glob **he; |
| 190 | | int i; |
| 191 | | |
| 192 | | if (firsttime) { |
| 193 | | firsttime = 0; |
| 194 | | BZERO((char*)hashtbl, sizeof(hashtbl));
|
| 195 | | |
| 196 | | #if STATICGLOBS > 0
|
| 197 | | |
| 198 | | ob = 0; |
| 199 | | for (i = STATICGLOBS-1; i >= 0; i--) { |
| 200 | | globheap[i].next = ob; |
| 201 | | ob = &globheap[i]; |
| 202 | | } |
| 203 | | globfl = ob; |
| 204 | | #endif |
| 205 | | } |
| 206 | | |
| 207 | | |
| 208 | | |
| 209 | | #if LET0BE1 > 0
|
| 210 | | if (!len) |
| 211 | | len = 1; |
| 212 | | #endif |
| 213 | | if (len < 1 || len > lengthlimit) { |
| 214 | | (void)sprintf(msbuf, "i_malloc: bogus len=%d\n", len); |
| 215 | | (void)SWRITE(debfd, msbuf);
|
| 216 | | i_choke(); |
| 217 | | return (char*)0; |
| 218 | | } |
| 219 | | |
| 220 | | |
| 221 | | |
| 222 | | if (!(ptr = (char*)malloc(len + lopad + hipad))) { |
| 223 | | (void)sprintf(msbuf, "i_malloc: malloc failed len=%d\n", len); |
| 224 | | (void)SWRITE(debfd, msbuf);
|
| 225 | | i_choke(); |
| 226 | | return (char*)0; |
| 227 | | } |
| 228 | | |
| 229 | | |
| 230 | | |
| 231 | | #if STATICGLOBS > 0
|
| 232 | | if (ob = globfl) { |
| 233 | | globfl = globfl->next; |
| 234 | | globavail--; |
| 235 | | |
| 236 | | } else { |
| 237 | | (void)sprintf(msbuf, "i_malloc: glob allocate failed (max %d)\n", |
| 238 | | STATICGLOBS); |
| 239 | | (void)SWRITE(debfd, msbuf); |
| 240 | | i_choke(); |
| 241 | | return (char*)0; |
| 242 | | } |
| 243 | | #else |
| 244 | | if (!(ob = (struct glob*)malloc(sizeof(struct glob)))) { |
| 245 | | (void)sprintf(msbuf, "i_malloc: malloc failed for glob\n"); |
| 246 | | (void)SWRITE(debfd, msbuf);
|
| 247 | | i_choke(); |
| 248 | | return (char*)0; |
| 249 | | } |
| 250 | | #endif |
| 251 | | |
| 252 | | |
| 253 | | |
| 254 | | ob->flg = OBALLOC;
|
| 255 | | ob->id = ++globid; |
| 256 | | ob->tag[0] = 0; |
| 257 | | if (tag) |
| 258 | | strncpy(ob->tag, tag, 4); |
| 259 | | ob->len = len; |
| 260 | | ob->lop = lopad; |
| 261 | | ob->hip = hipad; |
| 262 | | ob->rst = rnstate; |
| 263 | | for (i = lopad; i-- > 0; *ptr++ = NEXTRN(rnstate));
|
| 264 | | ob->base = ptr; |
| 265 | | he = &hashtbl[HASH(ptr)];
|
| 266 | | for (i = hipad, ptr += len; i-- > 0; *ptr++ = NEXTRN(rnstate));
|
| 267 | | ob->next = *he; |
| 268 | | *he = ob; |
| 269 | | totlnbyts += len; |
| 270 | | return ob->base; |
| 271 | | } |