4 This file is part of PulseAudio.
6 Copyright 2004-2006 Lennart Poettering
8 PulseAudio is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published
10 by the Free Software Foundation; either version 2 of the License,
11 or (at your option) any later version.
13 PulseAudio is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with PulseAudio; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
34 #include <pulsecore/core-util.h>
35 #include <pulsecore/gccmacro.h>
39 /* Make sure not to allocate more than this much memory. */
40 #define MAX_ALLOC_SIZE (1024*1024*20) /* 20MB */
48 static void oom(void) PA_GCC_NORETURN;
50 /** called in case of an OOM situation. Prints an error message and
52 static void oom(void) {
53 static const char e[] = "Not enough memory\n";
54 pa_loop_write(STDERR_FILENO, e, sizeof(e)-1, NULL);
61 void* pa_xmalloc(size_t size) {
64 assert(size < MAX_ALLOC_SIZE);
66 if (!(p = malloc(size)))
72 void* pa_xmalloc0(size_t size) {
75 assert(size < MAX_ALLOC_SIZE);
77 if (!(p = calloc(1, size)))
83 void *pa_xrealloc(void *ptr, size_t size) {
86 assert(size < MAX_ALLOC_SIZE);
88 if (!(p = realloc(ptr, size)))
93 void* pa_xmemdup(const void *p, size_t l) {
97 char *r = pa_xmalloc(l);
103 char *pa_xstrdup(const char *s) {
107 return pa_xmemdup(s, strlen(s)+1);
110 char *pa_xstrndup(const char *s, size_t l) {
116 if ((e = memchr(s, 0, l)))
117 return pa_xmemdup(s, e-s+1);
125 void pa_xfree(void *p) {