PulseAudio  9.0
xmalloc.h
Go to the documentation of this file.
00001 #ifndef foomemoryhfoo
00002 #define foomemoryhfoo
00003 
00004 /***
00005   This file is part of PulseAudio.
00006 
00007   Copyright 2004-2006 Lennart Poettering
00008 
00009   PulseAudio is free software; you can redistribute it and/or modify
00010   it under the terms of the GNU Lesser General Public License as published
00011   by the Free Software Foundation; either version 2.1 of the License,
00012   or (at your option) any later version.
00013 
00014   PulseAudio is distributed in the hope that it will be useful, but
00015   WITHOUT ANY WARRANTY; without even the implied warranty of
00016   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00017   General Public License for more details.
00018 
00019   You should have received a copy of the GNU Lesser General Public License
00020   along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
00021 ***/
00022 
00023 #include <sys/types.h>
00024 #include <stdlib.h>
00025 #include <limits.h>
00026 #include <assert.h>
00027 
00028 #include <pulse/cdecl.h>
00029 #include <pulse/gccmacro.h>
00030 #include <pulse/version.h>
00031 
00036 PA_C_DECL_BEGIN
00037 
00039 void* pa_xmalloc(size_t l) PA_GCC_MALLOC PA_GCC_ALLOC_SIZE(1);
00040 
00042 void *pa_xmalloc0(size_t l) PA_GCC_MALLOC PA_GCC_ALLOC_SIZE(1);
00043 
00045 void *pa_xrealloc(void *ptr, size_t size) PA_GCC_ALLOC_SIZE(2);
00046 
00048 void pa_xfree(void *p);
00049 
00051 char *pa_xstrdup(const char *s) PA_GCC_MALLOC;
00052 
00054 char *pa_xstrndup(const char *s, size_t l) PA_GCC_MALLOC;
00055 
00057 void* pa_xmemdup(const void *p, size_t l) PA_GCC_MALLOC PA_GCC_ALLOC_SIZE(2);
00058 
00060 static void* _pa_xnew_internal(size_t n, size_t k) PA_GCC_MALLOC PA_GCC_ALLOC_SIZE2(1,2);
00061 
00062 static inline void* _pa_xnew_internal(size_t n, size_t k) {
00063     assert(n < INT_MAX/k);
00064     return pa_xmalloc(n*k);
00065 }
00066 
00068 #define pa_xnew(type, n) ((type*) _pa_xnew_internal((n), sizeof(type)))
00069 
00071 static void* _pa_xnew0_internal(size_t n, size_t k) PA_GCC_MALLOC PA_GCC_ALLOC_SIZE2(1,2);
00072 
00073 static inline void* _pa_xnew0_internal(size_t n, size_t k) {
00074     assert(n < INT_MAX/k);
00075     return pa_xmalloc0(n*k);
00076 }
00077 
00079 #define pa_xnew0(type, n) ((type*) _pa_xnew0_internal((n), sizeof(type)))
00080 
00082 static void* _pa_xnewdup_internal(const void *p, size_t n, size_t k) PA_GCC_MALLOC PA_GCC_ALLOC_SIZE2(2,3);
00083 
00084 static inline void* _pa_xnewdup_internal(const void *p, size_t n, size_t k) {
00085     assert(n < INT_MAX/k);
00086     return pa_xmemdup(p, n*k);
00087 }
00088 
00090 #define pa_xnewdup(type, p, n) ((type*) _pa_xnewdup_internal((p), (n), sizeof(type)))
00091 
00093 static void* _pa_xrenew_internal(void *p, size_t n, size_t k) PA_GCC_MALLOC PA_GCC_ALLOC_SIZE2(2,3);
00094 
00095 static inline void* _pa_xrenew_internal(void *p, size_t n, size_t k) {
00096     assert(n < INT_MAX/k);
00097     return pa_xrealloc(p, n*k);
00098 }
00099 
00101 #define pa_xrenew(type, p, n) ((type*) _pa_xrenew_internal(p, (n), sizeof(type)))
00102 
00103 PA_C_DECL_END
00104 
00105 #endif