1 /* ----------------------------------------------------------------------- *
3 * Copyright 2008 H. Peter Anvin - All Rights Reserved
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
8 * Boston MA 02110-1301, USA; either version 2 of the License, or
9 * (at your option) any later version; incorporated herein by reference.
11 * ----------------------------------------------------------------------- */
16 * Simple reference-counted strings
24 /* Allocate space for a refstring of len bytes, plus final null */
25 /* The final null is inserted in the string; the rest is uninitialized. */
26 char *refstr_alloc(size_t len
)
28 char *r
= malloc(sizeof(unsigned int) + len
+ 1);
31 *(unsigned int *)r
= 1;
32 r
+= sizeof(unsigned int);
37 const char *refstrndup(const char *str
, size_t len
)
44 len
= strnlen(str
, len
);
45 r
= refstr_alloc(len
);
51 const char *refstrdup(const char *str
)
60 r
= refstr_alloc(len
);
66 int vrsprintf(const char **bufp
, const char *fmt
, va_list ap
)
73 len
= vsnprintf(NULL
, 0, fmt
, ap1
);
76 *bufp
= p
= refstr_alloc(len
);
80 return vsnprintf(p
, len
+ 1, fmt
, ap
);
83 int rsprintf(const char **bufp
, const char *fmt
, ...)
89 rv
= vrsprintf(bufp
, fmt
, ap
);
95 void refstr_put(const char *r
)
100 ref
= (unsigned int *)r
- 1;