68
68
* and 100% lockless, and uses absolutely no writable static memory.
69
69
* That makes it also great for kernel and embedded development.
70
70
*
71
- * - We made it go 3x faster. It's almost as fast as libcxxabi now. The
71
+ * - We made it go 2x faster. It's almost as fast as libcxxabi now. The
72
72
* lightweight Dennis Ritchie style demangle_malloc() implementation
73
73
* helped. What also helped is introducing stack_str and strlcpy().
74
74
*
75
- * - We made it use 4x less memory. This came with the tradeoff of
75
+ * - We made it use 3x less memory. This came with the tradeoff of
76
76
* imposing limitations similar to embedded software. Rather than
77
77
* using pointers, we use 16-bit indexes into a heap that can grow no
78
78
* larger than 64kb. Please note that a buffer size of 20kb is more
@@ -383,7 +383,7 @@ static privileged returnspointerwithnoaliases returnsnonnull void *
383
383
demangle_malloc (struct demangle_data * h , int a , int n )
384
384
{
385
385
uintptr_t ptr ;
386
- int next , next2 ;
386
+ int rem , next , next2 ;
387
387
index_t * link , * link2 ;
388
388
int b = sizeof (index_t );
389
389
@@ -397,12 +397,20 @@ demangle_malloc(struct demangle_data *h, int a, int n)
397
397
next = h -> free ;
398
398
link = & h -> free ;
399
399
while (next ) {
400
- next2 = * (index_t * )(h -> heap + next );
401
400
link2 = (index_t * )(h -> heap + next );
402
- if (!(next & (a - 1 )) &&
403
- n <= ((index_t * )(h -> heap + next ))[-1 ]) {
404
- * link = next2 ;
405
- return (void * )(h -> heap + next );
401
+ next2 = * link2 ;
402
+ if (!(next & (a - 1 )) && (rem = link2 [-1 ] - n ) >= 0 ) {
403
+ if (rem < (b << 1 )) {
404
+ * link = next2 ;
405
+ } else {
406
+ /* Split chunk. */
407
+ link2 [-1 ] = n ;
408
+ * link = next + n + b ;
409
+ link = (index_t * )(h -> heap + next + n + b );
410
+ link [-1 ] = rem - b ;
411
+ link [0 ] = next2 ;
412
+ }
413
+ return link2 ;
406
414
}
407
415
next = next2 ;
408
416
link = link2 ;
0 commit comments