7 This file is part of PulseAudio.
9 Copyright 2004-2006 Lennart Poettering
11 PulseAudio is free software; you can redistribute it and/or modify
12 it under the terms of the GNU Lesser General Public License as published
13 by the Free Software Foundation; either version 2 of the License,
14 or (at your option) any later version.
16 PulseAudio is distributed in the hope that it will be useful, but
17 WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 General Public License for more details.
21 You should have received a copy of the GNU Lesser General Public License
22 along with PulseAudio; if not, write to the Free Software
23 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
27 #include <sys/types.h>
31 #include <pulse/cdecl.h>
34 * Memory allocation functions.
39 /** Allocate the specified number of bytes, just like malloc() does. However, in case of OOM, terminate */
40 void* pa_xmalloc(size_t l);
42 /** Same as pa_xmalloc(), but initialize allocated memory to 0 */
43 void *pa_xmalloc0(size_t l);
45 /** The combination of pa_xmalloc() and realloc() */
46 void *pa_xrealloc(void *ptr, size_t size);
48 /** Free allocated memory */
49 void pa_xfree(void *p);
51 /** Duplicate the specified string, allocating memory with pa_xmalloc() */
52 char *pa_xstrdup(const char *s);
54 /** Duplicate the specified string, but truncate after l characters */
55 char *pa_xstrndup(const char *s, size_t l);
57 /** Duplicate the specified memory block */
58 void* pa_xmemdup(const void *p, size_t l);
60 /** Internal helper for pa_xnew() */
61 static inline void* pa_xnew_internal(unsigned n, size_t k) {
62 assert(n < INT_MAX/k);
63 return pa_xmalloc(n*k);
66 /** Allocate n new structures of the specified type. */
67 #define pa_xnew(type, n) ((type*) pa_xnew_internal((n), sizeof(type)))
69 /** Internal helper for pa_xnew0() */
70 static inline void* pa_xnew0_internal(unsigned n, size_t k) {
71 assert(n < INT_MAX/k);
72 return pa_xmalloc0(n*k);
75 /** Same as pa_xnew() but set the memory to zero */
76 #define pa_xnew0(type, n) ((type*) pa_xnew0_internal((n), sizeof(type)))
78 /** Internal helper for pa_xnew0() */
79 static inline void* pa_xnewdup_internal(const void *p, unsigned n, size_t k) {
80 assert(n < INT_MAX/k);
81 return pa_xmemdup(p, n*k);
84 /** Same as pa_xnew() but set the memory to zero */
85 #define pa_xnewdup(type, p, n) ((type*) pa_xnewdup_internal((p), (n), sizeof(type)))