39
39
/** Allocate the specified number of bytes, just like malloc() does. However, in case of OOM, terminate */
40
void* pa_xmalloc(size_t l);
40
void* pa_xmalloc(size_t l) PA_GCC_MALLOC PA_GCC_ALLOC_SIZE(1);
42
42
/** Same as pa_xmalloc(), but initialize allocated memory to 0 */
43
void *pa_xmalloc0(size_t l);
43
void *pa_xmalloc0(size_t l) PA_GCC_MALLOC PA_GCC_ALLOC_SIZE(1);
45
45
/** The combination of pa_xmalloc() and realloc() */
46
void *pa_xrealloc(void *ptr, size_t size);
46
void *pa_xrealloc(void *ptr, size_t size) PA_GCC_ALLOC_SIZE(2);
48
48
/** Free allocated memory */
49
49
void pa_xfree(void *p);
51
51
/** Duplicate the specified string, allocating memory with pa_xmalloc() */
52
char *pa_xstrdup(const char *s);
52
char *pa_xstrdup(const char *s) PA_GCC_MALLOC;
54
54
/** Duplicate the specified string, but truncate after l characters */
55
char *pa_xstrndup(const char *s, size_t l);
55
char *pa_xstrndup(const char *s, size_t l) PA_GCC_MALLOC;
57
57
/** Duplicate the specified memory block */
58
void* pa_xmemdup(const void *p, size_t l);
58
void* pa_xmemdup(const void *p, size_t l) PA_GCC_MALLOC PA_GCC_ALLOC_SIZE(2);
60
60
/** Internal helper for pa_xnew() */
61
static inline void* pa_xnew_internal(unsigned n, size_t k) {
61
static void* _pa_xnew_internal(size_t n, size_t k) PA_GCC_MALLOC PA_GCC_ALLOC_SIZE2(1,2);
63
static inline void* _pa_xnew_internal(size_t n, size_t k) {
62
64
assert(n < INT_MAX/k);
63
65
return pa_xmalloc(n*k);
66
68
/** Allocate n new structures of the specified type. */
67
#define pa_xnew(type, n) ((type*) pa_xnew_internal((n), sizeof(type)))
69
#define pa_xnew(type, n) ((type*) _pa_xnew_internal((n), sizeof(type)))
69
71
/** Internal helper for pa_xnew0() */
70
static inline void* pa_xnew0_internal(unsigned n, size_t k) {
72
static void* _pa_xnew0_internal(size_t n, size_t k) PA_GCC_MALLOC PA_GCC_ALLOC_SIZE2(1,2);
74
static inline void* _pa_xnew0_internal(size_t n, size_t k) {
71
75
assert(n < INT_MAX/k);
72
76
return pa_xmalloc0(n*k);
75
79
/** Same as pa_xnew() but set the memory to zero */
76
#define pa_xnew0(type, n) ((type*) pa_xnew0_internal((n), sizeof(type)))
80
#define pa_xnew0(type, n) ((type*) _pa_xnew0_internal((n), sizeof(type)))
78
82
/** Internal helper for pa_xnew0() */
79
static inline void* pa_xnewdup_internal(const void *p, unsigned n, size_t k) {
83
static void* _pa_xnewdup_internal(const void *p, size_t n, size_t k) PA_GCC_MALLOC PA_GCC_ALLOC_SIZE2(2,3);
85
static inline void* _pa_xnewdup_internal(const void *p, size_t n, size_t k) {
80
86
assert(n < INT_MAX/k);
81
87
return pa_xmemdup(p, n*k);
84
90
/** Same as pa_xnew() but set the memory to zero */
85
#define pa_xnewdup(type, p, n) ((type*) pa_xnewdup_internal((p), (n), sizeof(type)))
91
#define pa_xnewdup(type, p, n) ((type*) _pa_xnewdup_internal((p), (n), sizeof(type)))