42 static pthread_once_t lumiera_tmpbuf_tls_once = PTHREAD_ONCE_INIT;
43 static pthread_key_t lumiera_tmpbuf_tls_key;
49 lumiera_tmpbuf_destroy (
void* buf)
56 lumiera_tmpbuf_init (
void)
58 pthread_key_create (&lumiera_tmpbuf_tls_key, lumiera_tmpbuf_destroy);
66 pthread_once (&lumiera_tmpbuf_tls_once, lumiera_tmpbuf_init);
71 pthread_setspecific (lumiera_tmpbuf_tls_key, NULL);
82 pthread_once (&lumiera_tmpbuf_tls_once, lumiera_tmpbuf_init);
86 pthread_setspecific (lumiera_tmpbuf_tls_key,
89 buf->idx = (buf->idx + 1) & 0x3f;
91 if (buf->sizes[buf->idx] < size || buf->sizes[buf->idx] > 8*size)
94 buf->sizes[buf->idx] = (size+4*
sizeof(long)) & ~(4*
sizeof(long)-1);
97 return buf->buffers[buf->idx];
104 size_t len = strlen (src);
105 len = len > size ? size : len;
108 strncpy (buf, src, len);
120 va_start (args, fmt);
121 size_t len = vsnprintf (NULL, 0, fmt, args);
124 len = len > size ? size : len;
126 va_start (args, fmt);
127 vsnprintf (buf, len+1, fmt, args);
136 const char* str2,
size_t str2_len,
137 const char* str3,
size_t str3_len)
140 str1?str1_len:0, str1?str1:
"", str1?
".":
"",
141 str2?str2_len:0, str2?str2:
"",
142 str3?
".":
"", str3?str3_len:0, str3?str3:
"");
149 REQUIRE (strlen (from) == strlen (to),
"from and to character set must have equal length");
155 for (wpos = rpos = ret; *rpos; ++rpos, ++wpos)
157 char* found = strchr (from, *rpos);
159 *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.