Changeset 3456 for trunk/src/kash/memalloc.c
- Timestamp:
- Sep 14, 2020, 2:03:45 PM (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/kash/memalloc.c
r3437 r3456 42 42 43 43 #include <stdlib.h> 44 #include <assert.h> 44 45 45 46 #include "shell.h" … … 50 51 #include "mystring.h" 51 52 #include "shinstance.h" 53 #include "nodes.h" 52 54 53 55 /* … … 310 312 } 311 313 314 315 /* 316 * Got better control having a dedicated function for this. 317 * 318 * Was: #define grabstackstr(psh, p) stalloc((psh), stackblocksize(psh) - (psh)->sstrnleft) 319 */ 320 char * 321 grabstackstr(shinstance *psh, char *end) 322 { 323 char * const pstart = stackblock(psh); 324 size_t nbytes = (size_t)(end - pstart); 325 326 assert((uintptr_t)end >= (uintptr_t)pstart); 327 /*assert(end[-1] == '\0'); - not if it's followed by ungrabstrackstr(), sigh. */ 328 assert(SHELL_ALIGN((uintptr_t)pstart) == (uintptr_t)pstart); 329 assert(stackblocksize(psh) - psh->sstrnleft >= nbytes); 330 331 nbytes = SHELL_ALIGN(nbytes); 332 psh->stacknxt += nbytes; 333 psh->stacknleft -= (int)nbytes; 334 assert(psh->stacknleft >= 0); 335 336 return pstart; 337 } 338 312 339 void 313 340 ungrabstackstr(shinstance *psh, char *s, char *p) 314 341 { 342 assert((size_t)(psh->stacknxt - p) <= SHELL_SIZE); 343 assert((uintptr_t)s >= (uintptr_t)&psh->stackp->space[0]); 344 assert((uintptr_t)p >= (uintptr_t)s); 345 315 346 psh->stacknleft += (int)(psh->stacknxt - s); 316 347 psh->stacknxt = s; … … 318 349 319 350 } 351 352 353 /* 354 * Parser stack allocator. 355 */ 356 void *pstalloc(struct shinstance *psh, size_t nbytes) 357 { 358 return stalloc(psh, nbytes); 359 } 360 361 union node *pstallocnode(struct shinstance *psh, size_t nbytes) 362 { 363 return (union node *)pstalloc(psh, nbytes); 364 } 365 366 struct nodelist *pstalloclist(struct shinstance *psh) 367 { 368 return (struct nodelist *)pstalloc(psh, sizeof(struct nodelist)); 369 } 370 371 char *pstsavestr(struct shinstance *psh, const char *str) 372 { 373 if (str) { 374 size_t nbytes = strlen(str) + 1; 375 return (char *)memcpy(pstalloc(psh, nbytes), str, nbytes); 376 } 377 return NULL; 378 } 379 380 char *pstmakestrspace(struct shinstance *psh, size_t minbytes, char *end) 381 { 382 size_t const len = end - stackblock(psh); 383 assert(stackblocksize(psh) - psh->sstrnleft == len); 384 TRACE2((psh, "pstmakestrspace: len=%u minbytes=%u (=> %u)\n", len, minbytes, len + minbytes)); 385 minbytes += len; 386 while (stackblocksize(psh) < minbytes) 387 growstackblock(psh); 388 psh->sstrnleft = (int)(stackblocksize(psh) - len); 389 return (char *)stackblock(psh) + len; 390 } 391 392 /* PSTPUTC helper */ 393 char *pstputcgrow(shinstance *psh, char *end, char c) 394 { 395 psh->sstrnleft++; /* PSTPUTC() already incremented it. */ 396 end = pstmakestrspace(psh, 1, end); 397 assert(psh->sstrnleft > 0); 398 psh->sstrnleft--; 399 *end++ = c; 400 return end; 401 } 402 403 404 char *pstgrabstr(struct shinstance *psh, char *end) 405 { 406 char * const pstart = stackblock(psh); 407 size_t nbytes = (size_t)(end - pstart); 408 409 assert((uintptr_t)end > (uintptr_t)pstart); 410 assert(end[-1] == '\0'); 411 assert(SHELL_ALIGN((uintptr_t)pstart) == (uintptr_t)pstart); 412 assert(stackblocksize(psh) - psh->sstrnleft >= nbytes); 413 414 nbytes = SHELL_ALIGN(nbytes); /** @todo don't align strings, align the other allocations. */ 415 psh->stacknxt += nbytes; 416 psh->stacknleft -= (int)nbytes; 417 418 return pstart; 419 } 420
Note:
See TracChangeset
for help on using the changeset viewer.