]> code.delx.au - pulseaudio/blob - src/pulse/xmalloc.h
big s/polyp/pulse/g
[pulseaudio] / src / pulse / xmalloc.h
1 #ifndef foomemoryhfoo
2 #define foomemoryhfoo
3
4 /* $Id$ */
5
6 /***
7 This file is part of PulseAudio.
8
9 PulseAudio 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 PulseAudio 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 PulseAudio; 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 #include <pulse/cdecl.h>
30
31 /** \file
32 * Memory allocation functions.
33 */
34
35 PA_C_DECL_BEGIN
36
37 /** Allocate the specified number of bytes, just like malloc() does. However, in case of OOM, terminate */
38 void* pa_xmalloc(size_t l);
39
40 /** Same as pa_xmalloc(), but initialize allocated memory to 0 */
41 void *pa_xmalloc0(size_t l);
42
43 /** The combination of pa_xmalloc() and realloc() */
44 void *pa_xrealloc(void *ptr, size_t size);
45
46 /** Free allocated memory */
47 void pa_xfree(void *p);
48
49 /** Duplicate the specified string, allocating memory with pa_xmalloc() */
50 char *pa_xstrdup(const char *s);
51
52 /** Duplicate the specified string, but truncate after l characters */
53 char *pa_xstrndup(const char *s, size_t l);
54
55 /** Duplicate the specified memory block */
56 void* pa_xmemdup(const void *p, size_t l);
57
58 /** Internal helper for pa_xnew() */
59 static inline void* pa_xnew_internal(unsigned n, size_t k) {
60 assert(n < INT_MAX/k);
61 return pa_xmalloc(n*k);
62 }
63
64 /** Allocate n new structures of the specified type. */
65 #define pa_xnew(type, n) ((type*) pa_xnew_internal((n), sizeof(type)))
66
67 /** Internal helper for pa_xnew0() */
68 static inline void* pa_xnew0_internal(unsigned n, size_t k) {
69 assert(n < INT_MAX/k);
70 return pa_xmalloc0(n*k);
71 }
72
73 /** Same as pa_xnew() but set the memory to zero */
74 #define pa_xnew0(type, n) ((type*) pa_xnew0_internal((n), sizeof(type)))
75
76 PA_C_DECL_END
77
78 #endif