51 static pthread_once_t lumiera_tmpbuf_tls_once = PTHREAD_ONCE_INIT;
52 static pthread_key_t lumiera_tmpbuf_tls_key;
58 lumiera_tmpbuf_destroy (
void* buf)
65 lumiera_tmpbuf_init (
void)
67 pthread_key_create (&lumiera_tmpbuf_tls_key, lumiera_tmpbuf_destroy);
75 pthread_once (&lumiera_tmpbuf_tls_once, lumiera_tmpbuf_init);
80 pthread_setspecific (lumiera_tmpbuf_tls_key, NULL);
91 pthread_once (&lumiera_tmpbuf_tls_once, lumiera_tmpbuf_init);
95 pthread_setspecific (lumiera_tmpbuf_tls_key,
98 buf->idx = (buf->idx + 1) & 0x3f;
100 if (buf->sizes[buf->idx] < size || buf->sizes[buf->idx] > 8*size)
103 buf->sizes[buf->idx] = (size+4*
sizeof(long)) & ~(4*
sizeof(long)-1);
106 return buf->buffers[buf->idx];
113 size_t len = strlen (src);
114 len = len > size ? size : len;
117 strncpy (buf, src, len);
129 va_start (args, fmt);
130 size_t len = vsnprintf (NULL, 0, fmt, args);
133 len = len > size ? size : len;
135 va_start (args, fmt);
136 vsnprintf (buf, len+1, fmt, args);
145 const char* str2,
size_t str2_len,
146 const char* str3,
size_t str3_len)
149 str1?str1_len:0, str1?str1:
"", str1?
".":
"",
150 str2?str2_len:0, str2?str2:
"",
151 str3?
".":
"", str3?str3_len:0, str3?str3:
"");
158 REQUIRE (strlen (from) == strlen (to),
"from and to character set must have equal length");
164 for (wpos = rpos = ret; *rpos; ++rpos, ++wpos)
166 char* found = strchr (from, *rpos);
168 *wpos = to[found-from];
Portable and safe wrappers around some C-Lib functions.
void * lumiera_calloc(size_t n, size_t size)
Allocate cleared memory for an array.
void * lumiera_tmpbuf_provide(size_t size)
Query a thread local tmpbuf.
char * lumiera_tmpbuf_strndup(const char *src, size_t size)
Duplicate string to a tmpbuf.
Round robin temporary buffers.
static void lumiera_free(void *mem)
Free previously allocated memory.
void lumiera_tmpbuf_freeall(void)
free all buffers associated with this thread.
void * lumiera_malloc(size_t size)
Allocate memory.
char * lumiera_tmpbuf_tr(const char *in, const char *from, const char *to, const char *def)
Translates characters in a string, similar to the shell 'tr' utility.
char * lumiera_tmpbuf_strcat3(const char *str1, size_t str1_len, const char *str2, size_t str2_len, const char *str3, size_t str3_len)
Concat up to 3 strings in a tmpbuf.
#define LUMIERA_TMPBUF_NUM
Number of buffers in the ring This also defines how many concurent buffers can be in use in one threa...
char * lumiera_tmpbuf_snprintf(size_t size, const char *fmt,...)
Construct a string in a tmpbuf.