]> code.delx.au - pulseaudio/blob - polyp/xmalloc.h
2946011aff1490ae05829f0e9a01210729a84ad5
[pulseaudio] / polyp / xmalloc.h
1 #ifndef foomemoryhfoo
2 #define foomemoryhfoo
3
4 /* $Id$ */
5
6 /***
7 This file is part of polypaudio.
8
9 polypaudio is free software; you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as published
11 by the Free Software Foundation; either version 2 of the License,
12 or (at your option) any later version.
13
14 polypaudio is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public License
20 along with polypaudio; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 USA.
23 ***/
24
25 #include <sys/types.h>
26 #include <stdlib.h>
27 #include <limits.h>
28 #include <assert.h>
29
30 void* pa_xmalloc(size_t l);
31 void *pa_xmalloc0(size_t l);
32 void *pa_xrealloc(void *ptr, size_t size);
33 #define pa_xfree free
34
35 char *pa_xstrdup(const char *s);
36 char *pa_xstrndup(const char *s, size_t l);
37
38 void* pa_xmemdup(const void *p, size_t l);
39
40 /** Internal helper for pa_xnew() */
41 static inline void* pa_xnew_internal(unsigned n, size_t k) {
42 assert(n < INT_MAX/k);
43 return pa_xmalloc(n*k);
44 }
45
46 /** Allocate n new structures of the specified type. */
47 #define pa_xnew(type, n) ((type*) pa_xnew_internal((n), sizeof(type)))
48
49 /** Internal helper for pa_xnew0() */
50 static inline void* pa_xnew0_internal(unsigned n, size_t k) {
51 assert(n < INT_MAX/k);
52 return pa_xmalloc0(n*k);
53 }
54
55 /** Same as pa_xnew() but set the memory to zero */
56 #define pa_xnew0(type, n) ((type*) pa_xnew0_internal((n), sizeof(type)))
57
58 #endif