1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2004-2006 Lennart Poettering
5
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2.1 of the License,
9 or (at your option) any later version.
10
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 #include <stdlib.h>
25 #include <signal.h>
26 #include <unistd.h>
27 #include <string.h>
28 #include <errno.h>
29
30 #include <pulse/gccmacro.h>
31 #include <pulsecore/core-util.h>
32 #include <pulsecore/macro.h>
33
34 #include "xmalloc.h"
35
36 /* Make sure not to allocate more than this much memory. */
37 #define MAX_ALLOC_SIZE (1024*1024*96) /* 96MB */
38
39 /* #undef malloc */
40 /* #undef free */
41 /* #undef realloc */
42 /* #undef strndup */
43 /* #undef strdup */
44
45 static void oom(void) PA_GCC_NORETURN;
46
47 /* called in case of an OOM situation. Prints an error message and
48 * exits */
oom(void)49 static void oom(void) {
50 static const char e[] = "Not enough memory\n";
51 pa_loop_write(STDERR_FILENO, e, sizeof(e)-1, NULL);
52 #ifdef SIGQUIT
53 raise(SIGQUIT);
54 #endif
55 _exit(1);
56 }
57
pa_xmalloc(size_t size)58 void* pa_xmalloc(size_t size) {
59 void *p;
60 pa_assert(size > 0);
61 pa_assert(size < MAX_ALLOC_SIZE);
62
63 if (!(p = malloc(size)))
64 oom();
65
66 return p;
67 }
68
pa_xmalloc0(size_t size)69 void* pa_xmalloc0(size_t size) {
70 void *p;
71 pa_assert(size > 0);
72 pa_assert(size < MAX_ALLOC_SIZE);
73
74 if (!(p = calloc(1, size)))
75 oom();
76
77 return p;
78 }
79
pa_xrealloc(void * ptr,size_t size)80 void *pa_xrealloc(void *ptr, size_t size) {
81 void *p;
82 pa_assert(size > 0);
83 pa_assert(size < MAX_ALLOC_SIZE);
84
85 if (!(p = realloc(ptr, size)))
86 oom();
87 return p;
88 }
89
pa_xmemdup(const void * p,size_t l)90 void* pa_xmemdup(const void *p, size_t l) {
91 if (!p)
92 return NULL;
93 else {
94 char *r = pa_xmalloc(l);
95 memcpy(r, p, l);
96 return r;
97 }
98 }
99
pa_xstrdup(const char * s)100 char *pa_xstrdup(const char *s) {
101 if (!s)
102 return NULL;
103
104 return pa_xmemdup(s, strlen(s)+1);
105 }
106
pa_xstrndup(const char * s,size_t l)107 char *pa_xstrndup(const char *s, size_t l) {
108 char *e, *r;
109
110 if (!s)
111 return NULL;
112
113 if ((e = memchr(s, 0, l)))
114 return pa_xmemdup(s, (size_t) (e-s+1));
115
116 r = pa_xmalloc(l+1);
117 memcpy(r, s, l);
118 r[l] = 0;
119 return r;
120 }
121
pa_xfree(void * p)122 void pa_xfree(void *p) {
123 int saved_errno;
124
125 if (!p)
126 return;
127
128 saved_errno = errno;
129 free(p);
130 errno = saved_errno;
131 }
132