myMPD
Internal API documentation
Loading...
Searching...
No Matches
mem.h
1/*
2 SPDX-License-Identifier: GPL-3.0-or-later
3 myMPD (c) 2018-2026 Juergen Mang <mail@jcgames.de>
4 https://github.com/jcorporation/mympd
5*/
6
7#ifndef MYMPD_MEM_H
8#define MYMPD_MEM_H
9
10#include "src/lib/log.h"
11
12#include <stdlib.h>
13
14char *my_strdup(const char *str, size_t len);
15
21__attribute__((malloc))
22static inline void *malloc_assert(size_t size) {
23 if (size == 0) {
24 MYMPD_LOG_EMERG(NULL, "Allocation zero bytes it not allowed");
25 abort();
26 }
27 void *p = malloc(size);
28 if (p == NULL) {
29 MYMPD_LOG_EMERG(NULL, "Failure allocating %lu bytes of memory", (unsigned long) size);
30 abort();
31 }
32 return p;
33}
34
41__attribute__((malloc))
42static inline void *malloc_assert_count(size_t count, size_t size) {
43 if (count == 0 ||
44 size == 0)
45 {
46 MYMPD_LOG_EMERG(NULL, "Allocation zero bytes it not allowed");
47 abort();
48 }
49 if (size > SIZE_MAX / count) {
50 MYMPD_LOG_EMERG(NULL, "Overflow in allocating call for %lu x %lu", (unsigned long) count, (unsigned long) size);
51 abort();
52 }
53 void *p = malloc(count * size);
54 if (p == NULL) {
55 MYMPD_LOG_EMERG(NULL, "Failure allocating %lu bytes of memory", (unsigned long) (count * size));
56 abort();
57 }
58 return p;
59}
60
67__attribute__((malloc))
68static inline void *calloc_assert(size_t count, size_t size) {
69 if (count == 0 ||
70 size == 0)
71 {
72 MYMPD_LOG_EMERG(NULL, "Allocation zero bytes it not allowed");
73 abort();
74 }
75 if (size > SIZE_MAX / count) {
76 MYMPD_LOG_EMERG(NULL, "Overflow in allocating call for %lu x %lu", (unsigned long) count, (unsigned long) size);
77 abort();
78 }
79 void *p = calloc(count, size);
80 if (p == NULL) {
81 MYMPD_LOG_EMERG(NULL, "Failure allocating %lu bytes of memory", (unsigned long) (count * size));
82 abort();
83 }
84 return p;
85}
86
93__attribute__((malloc))
94static inline void *realloc_assert(void *ptr, size_t size) {
95 if (size == 0) {
96 MYMPD_LOG_EMERG(NULL, "Allocation zero bytes it not allowed");
97 abort();
98 }
99 void *p = realloc(ptr, size);
100 if (p == NULL) {
101 MYMPD_LOG_EMERG(NULL, "Failure allocating %lu bytes of memory", (unsigned long) size);
102 abort();
103 }
104 return p;
105}
106
114__attribute__((malloc))
115static inline void *realloc_assert_count(void *ptr, size_t count, size_t size) {
116 if (count == 0 ||
117 size == 0)
118 {
119 MYMPD_LOG_EMERG(NULL, "Allocation zero bytes it not allowed");
120 abort();
121 }
122 if (size > SIZE_MAX / count) {
123 MYMPD_LOG_EMERG(NULL, "Overflow in re-allocating call for %lu x %lu", (unsigned long) count, (unsigned long) size);
124 abort();
125 }
126 void *p = realloc(ptr, count * size);
127 if (p == NULL) {
128 MYMPD_LOG_EMERG(NULL, "Failure allocating %lu bytes of memory", (unsigned long) (count * size));
129 abort();
130 }
131 return p;
132}
133
138#define FREE_PTR(PTR) do { \
139 if (PTR != NULL) \
140 free(PTR); \
141 PTR = NULL; \
142} while (0)
143
144#endif
char * my_strdup(const char *str, size_t len)
Definition mem.c:22