Skip to content
Myr

xmalloc.h

This file

Path

xmalloc.h

62 lines · 1.6 KiB · C

Last changed by

Not known.

History of this file

Take it

The walk stopped after 200 commits: lines older than 6a4c416 point at it.

changelog: add missing refDaniel Eklöfbefore adoption6a4c416

lines 1–62why, one step back
1#pragma once
23#include <stdarg.h>4#include <stddef.h>5#include <string.h>6#include <wchar.h>7#include <uchar.h>89#include "char32.h"10#include "macros.h"1112void *xmalloc(size_t size) XMALLOC;13void *xcalloc(size_t nmemb, size_t size) XMALLOC;14void *xrealloc(void *ptr, size_t size);15void *xreallocarray(void *ptr, size_t nmemb, size_t size) RETURNS_NONNULL WARN_UNUSED_RESULT;16char *xstrdup(const char *str) XSTRDUP;17char *xstrndup(const char *str, size_t n) XSTRDUP;18char32_t *xc32dup(const char32_t *str) XSTRDUP;19char *xasprintf(const char *format, ...) PRINTF(1) XMALLOC;2021static inline void *22xmemdup(const void *ptr, size_t size)23{24 return memcpy(xmalloc(size), ptr, size);25}2627static inline char *28xstrjoin(const char *s1, const char *s2)29{30 size_t n1 = strlen(s1);31 size_t n2 = strlen(s2);32 char *joined = xmalloc(n1 + n2 + 1);33 memcpy(joined, s1, n1);34 memcpy(joined + n1, s2, n2 + 1);35 return joined;36}3738static inline char *39xstrjoin3(const char *s1, const char *s2, const char *s3)40{41 size_t n1 = strlen(s1);42 size_t n2 = strlen(s2);43 size_t n3 = strlen(s3);44 char *joined = xmalloc(n1 + n2 + n3 + 1);45 memcpy(joined, s1, n1);46 memcpy(joined + n1, s2, n2);47 memcpy(joined + n1 + n2, s3, n3 + 1);48 return joined;49}5051static inline char32_t *52xc32join3(const char32_t *s1, const char32_t *s2, const char32_t *s3)53{54 size_t n1 = c32len(s1);55 size_t n2 = c32len(s2);56 size_t n3 = c32len(s3);57 char32_t *joined = xmalloc((n1 + n2 + n3 + 1) * sizeof(char32_t));58 c32memcpy(joined, s1, n1);59 c32memcpy(joined + n1, s2, n2);60 c32memcpy(joined + n1 + n2, s3, n3 + 1);61 return joined;62}