From: Ralph Ronnquist Date: Thu, 11 Nov 2021 08:08:18 +0000 (+1100) Subject: new function stralloc X-Git-Url: https://git.rrq.au/?a=commitdiff_plain;h=37f43d2aa3f135a7fff1ca03552a4468b07f3390;p=rrq%2Ffuse_xattrs.git new function stralloc --- diff --git a/stringmem.c b/stringmem.c index 303add7..1a6bf00 100644 --- a/stringmem.c +++ b/stringmem.c @@ -11,13 +11,27 @@ static struct { char *end; } heap; -char *strjoin(const char *first,...) { - va_list ap; +static inline void strinitialize() { if ( heap.base == 0 ) { heap.base = (char*) malloc( STRINGSPACE ); heap.current = heap.base; heap.end = heap.base + STRINGSPACE; } +} + +extern char *stralloc(int size) { + strinitialize(); + if ( heap.current + size >= heap.end ) { + heap.current = heap.base; + } + char *start = heap.current; + heap.current += size; + return start; +} + +char *strjoin(const char *first,...) { + va_list ap; + strinitialize(); char *start = heap.current; const char *p = first; size_t size = strlen( first ) + 1; @@ -29,7 +43,7 @@ char *strjoin(const char *first,...) { if ( size > BIGSTRING ) { start = (char*) malloc( size ); } else { - if ( heap.current + size > heap.end ) { + if ( heap.current + size >= heap.end ) { heap.current = heap.base; } start = heap.current; diff --git a/stringmem.h b/stringmem.h index c25218f..1a3f695 100644 --- a/stringmem.h +++ b/stringmem.h @@ -1,6 +1,9 @@ -// Concatenate strigns onto the heap if possible, or malloc new. +// Concatenate strings onto the heap if possible, or malloc new. extern char *strjoin(const char *first,...); +// Allocate buffer space from the heap +extern char *stralloc(int size); + // Dispose memory possibly within the string heap extern void strfree(char *p);