]> code.delx.au - pulseaudio/blob - polyp/xmalloc.c
add missing copyright headers
[pulseaudio] / polyp / xmalloc.c
1 /* $Id$ */
2
3 /***
4 This file is part of polypaudio.
5
6 polypaudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published
8 by the Free Software Foundation; either version 2 of the License,
9 or (at your option) any later version.
10
11 polypaudio 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 General Public License
17 along with polypaudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdlib.h>
27 #include <signal.h>
28 #include <assert.h>
29
30 #include "memory.h"
31 #include "util.h"
32
33 #define MAX_ALLOC_SIZE (1024*1024*20)
34
35 #undef malloc
36 #undef free
37 #undef realloc
38 #undef strndup
39 #undef strdup
40
41 static void oom(void) {
42 static const char e[] = "Not enough memory\n";
43 pa_loop_write(2, e, sizeof(e)-1);
44 raise(SIGQUIT);
45 exit(1);
46 }
47
48 void* pa_xmalloc(size_t size) {
49 void *p;
50 assert(size > 0);
51 assert(size < MAX_ALLOC_SIZE);
52
53 if (!(p = malloc(size)))
54 oom();
55
56 return p;
57 }
58
59 void* pa_xmalloc0(size_t size) {
60 void *p;
61 assert(size > 0);
62 assert(size < MAX_ALLOC_SIZE);
63
64 if (!(p = calloc(1, size)))
65 oom();
66
67 return p;
68 }
69
70 void *pa_xrealloc(void *ptr, size_t size) {
71 void *p;
72 assert(size > 0);
73 assert(size < MAX_ALLOC_SIZE);
74
75 if (!(p = realloc(ptr, size)))
76 oom();
77 return p;
78 }
79
80 void* pa_xmemdup(const void *p, size_t l) {
81 if (!p)
82 return NULL;
83 else {
84 char *r = pa_xmalloc(l);
85 memcpy(r, p, l);
86 return r;
87 }
88 }
89
90 char *pa_xstrdup(const char *s) {
91 if (!s)
92 return NULL;
93
94 return pa_xmemdup(s, strlen(s)+1);
95 }
96
97 char *pa_xstrndup(const char *s, size_t l) {
98 if (!s)
99 return NULL;
100 else {
101 char *r;
102 size_t t = strlen(s);
103
104 if (t > l)
105 t = l;
106
107 r = pa_xmemdup(s, t+1);
108 r[t] = 0;
109 return r;
110 }
111 }
112