insert debug settings (disabled)
[rrq/fuse_xattrs.git] / stringmem.c
1 #include <stdarg.h>
2 #include <stdlib.h>
3 #include <string.h>
4
5 #define BIGSTRING 1048576
6 #define STRINGSPACE ( BIGSTRING * 20 )
7
8 static struct {
9     char *base;
10     char *current;
11     char *end;
12 } heap;
13
14 char *strjoin(const char *first,...) {
15     va_list ap;
16     if ( heap.base == 0 ) {
17         heap.base = (char*) malloc( STRINGSPACE );
18         heap.current = heap.base;
19         heap.end = heap.base + STRINGSPACE;
20     }
21     char *start = heap.current;
22     const char *p = first;
23     size_t size = strlen( first ) + 1;
24     va_start( ap, first );
25     while ( ( p = va_arg( ap, const char* ) ) ) {
26         size += strlen( p );
27     }
28     va_end( ap );
29     if ( size > BIGSTRING ) {
30         start = (char*) malloc( size );
31     } else {
32         if ( heap.current + size > heap.end ) {
33             heap.current = heap.base;
34         }
35         start = heap.current;
36         heap.current += size;
37     }
38     char *current = start;
39     strcpy( current, first );
40     current += strlen( first );
41     va_start( ap, first );
42     while ( ( p = va_arg( ap, const char* ) ) ) {
43         strcpy( current, p );
44         current += strlen( p );
45     }
46     va_end( ap );
47     *current = 0;
48     return start;
49 }
50
51 void strfree(char *p) {
52     if ( p < heap.base || p >= heap.end ) {
53         free( p );
54     }
55 }