]>
code.delx.au - pulseaudio/blob - src/pulse/xmalloc.h
7 This file is part of PulseAudio.
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.
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.
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
25 #include <sys/types.h>
29 #include <pulse/cdecl.h>
32 * Memory allocation functions.
37 /** Allocate the specified number of bytes, just like malloc() does. However, in case of OOM, terminate */
38 void* pa_xmalloc(size_t l
);
40 /** Same as pa_xmalloc(), but initialize allocated memory to 0 */
41 void *pa_xmalloc0(size_t l
);
43 /** The combination of pa_xmalloc() and realloc() */
44 void *pa_xrealloc(void *ptr
, size_t size
);
46 /** Free allocated memory */
47 void pa_xfree(void *p
);
49 /** Duplicate the specified string, allocating memory with pa_xmalloc() */
50 char *pa_xstrdup(const char *s
);
52 /** Duplicate the specified string, but truncate after l characters */
53 char *pa_xstrndup(const char *s
, size_t l
);
55 /** Duplicate the specified memory block */
56 void* pa_xmemdup(const void *p
, size_t l
);
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
);
64 /** Allocate n new structures of the specified type. */
65 #define pa_xnew(type, n) ((type*) pa_xnew_internal((n), sizeof(type)))
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
);
73 /** Same as pa_xnew() but set the memory to zero */
74 #define pa_xnew0(type, n) ((type*) pa_xnew0_internal((n), sizeof(type)))