foot/xmalloc.h

51 lines
1.3 KiB
C
Raw Normal View History

#pragma once
2021-05-07 16:41:02 +02:00
#include <stdarg.h>
#include <stddef.h>
#include <string.h>
2020-10-09 19:43:04 +02:00
#include <wchar.h>
#include <uchar.h>
#include "char32.h"
#include "macros.h"
void *xmalloc(size_t size) XMALLOC;
void *xcalloc(size_t nmemb, size_t size) XMALLOC;
void *xrealloc(void *ptr, size_t size);
2025-01-30 09:06:24 +01:00
void *xreallocarray(void *ptr, size_t n, size_t size);
char *xstrdup(const char *str) XSTRDUP;
char *xstrndup(const char *str, size_t n) XSTRDUP;
char *xasprintf(const char *format, ...) PRINTF(1) XMALLOC;
2021-05-07 16:41:02 +02:00
char *xvasprintf(const char *format, va_list va) VPRINTF(1) XMALLOC;
char32_t *xc32dup(const char32_t *str) XSTRDUP;
static inline void *
xmemdup(const void *ptr, size_t size)
{
return memcpy(xmalloc(size), ptr, size);
}
static inline char *
xstrjoin(const char *s1, const char *s2)
{
size_t n1 = strlen(s1);
size_t n2 = strlen(s2);
char *joined = xmalloc(n1 + n2 + 1);
memcpy(joined, s1, n1);
memcpy(joined + n1, s2, n2 + 1);
return joined;
}
static inline char *
xstrjoin3(const char *s1, const char *s2, const char *s3)
{
size_t n1 = strlen(s1);
size_t n2 = strlen(s2);
size_t n3 = strlen(s3);
char *joined = xmalloc(n1 + n2 + n3 + 1);
memcpy(joined, s1, n1);
memcpy(joined + n1, s2, n2);
memcpy(joined + n1 + n2, s3, n3 + 1);
return joined;
}