]> code.delx.au - pulseaudio/blob - polyp/xmalloc.c
forgot some files
[pulseaudio] / polyp / xmalloc.c
1 #include <stdlib.h>
2 #include <signal.h>
3 #include <assert.h>
4
5 #include "memory.h"
6 #include "util.h"
7
8 #define MAX_ALLOC_SIZE (1024*1024*20)
9
10 #undef malloc
11 #undef free
12 #undef realloc
13 #undef strndup
14 #undef strdup
15
16 static void oom(void) {
17 static const char e[] = "Not enough memory\n";
18 pa_loop_write(2, e, sizeof(e)-1);
19 raise(SIGQUIT);
20 exit(1);
21 }
22
23 void* pa_xmalloc(size_t size) {
24 void *p;
25 assert(size > 0);
26 assert(size < MAX_ALLOC_SIZE);
27
28 if (!(p = malloc(size)))
29 oom();
30
31 return p;
32 }
33
34 void* pa_xmalloc0(size_t size) {
35 void *p;
36 assert(size > 0);
37 assert(size < MAX_ALLOC_SIZE);
38
39 if (!(p = calloc(1, size)))
40 oom();
41
42 return p;
43 }
44
45 void *pa_xrealloc(void *ptr, size_t size) {
46 void *p;
47 assert(size > 0);
48 assert(size < MAX_ALLOC_SIZE);
49
50 if (!(p = realloc(ptr, size)))
51 oom();
52 return p;
53 }
54
55 char *pa_xstrdup(const char *s) {
56 if (!s)
57 return NULL;
58 else {
59 char *r = strdup(s);
60 if (!r)
61 oom();
62
63 return r;
64 }
65 }
66
67 char *pa_xstrndup(const char *s, size_t l) {
68 if (!s)
69 return NULL;
70 else {
71 char *r = strndup(s, l);
72 if (!r)
73 oom();
74 return r;
75 }
76 }