Add copyright notices to all relevant files. (based on svn log)
[profile/ivi/pulseaudio.git] / src / pulse / xmalloc.c
1 /* $Id$ */
2
3 /***
4   This file is part of PulseAudio.
5
6   Copyright 2004-2006 Lennart Poettering
7
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.
12
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.
17
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
21   USA.
22 ***/
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <stdlib.h>
29 #include <signal.h>
30 #include <assert.h>
31 #include <unistd.h>
32 #include <string.h>
33
34 #include <pulsecore/core-util.h>
35 #include <pulsecore/gccmacro.h>
36
37 #include "xmalloc.h"
38
39 /* Make sure not to allocate more than this much memory. */
40 #define MAX_ALLOC_SIZE (1024*1024*20) /* 20MB */
41
42 /* #undef malloc */
43 /* #undef free */
44 /* #undef realloc */
45 /* #undef strndup */
46 /* #undef strdup */
47
48 static void oom(void) PA_GCC_NORETURN;
49
50 /** called in case of an OOM situation. Prints an error message and
51  * exits */
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);
55 #ifdef SIGQUIT
56     raise(SIGQUIT);
57 #endif
58     _exit(1);
59 }
60
61 void* pa_xmalloc(size_t size) {
62     void *p;
63     assert(size > 0);
64     assert(size < MAX_ALLOC_SIZE);
65
66     if (!(p = malloc(size)))
67         oom();
68
69     return p;
70 }
71
72 void* pa_xmalloc0(size_t size) {
73     void *p;
74     assert(size > 0);
75     assert(size < MAX_ALLOC_SIZE);
76
77     if (!(p = calloc(1, size)))
78         oom();
79
80     return p;
81 }
82
83 void *pa_xrealloc(void *ptr, size_t size) {
84     void *p;
85     assert(size > 0);
86     assert(size < MAX_ALLOC_SIZE);
87
88     if (!(p = realloc(ptr, size)))
89         oom();
90     return p;
91 }
92
93 void* pa_xmemdup(const void *p, size_t l) {
94     if (!p)
95         return NULL;
96     else {
97         char *r = pa_xmalloc(l);
98         memcpy(r, p, l);
99         return r;
100     }
101 }
102
103 char *pa_xstrdup(const char *s) {
104     if (!s)
105         return NULL;
106
107     return pa_xmemdup(s, strlen(s)+1);
108 }
109
110 char *pa_xstrndup(const char *s, size_t l) {
111     char *e, *r;
112
113     if (!s)
114         return NULL;
115
116     if ((e = memchr(s, 0, l)))
117         return pa_xmemdup(s, e-s+1);
118
119     r = pa_xmalloc(l+1);
120     memcpy(r, s, l);
121     r[l] = 0;
122     return r;
123 }
124
125 void pa_xfree(void *p) {
126     if (!p)
127         return;
128
129     free(p);
130 }