153a5a1b3Sopenharmony_ci#ifndef foomemoryhfoo 253a5a1b3Sopenharmony_ci#define foomemoryhfoo 353a5a1b3Sopenharmony_ci 453a5a1b3Sopenharmony_ci/*** 553a5a1b3Sopenharmony_ci This file is part of PulseAudio. 653a5a1b3Sopenharmony_ci 753a5a1b3Sopenharmony_ci Copyright 2004-2006 Lennart Poettering 853a5a1b3Sopenharmony_ci 953a5a1b3Sopenharmony_ci PulseAudio is free software; you can redistribute it and/or modify 1053a5a1b3Sopenharmony_ci it under the terms of the GNU Lesser General Public License as published 1153a5a1b3Sopenharmony_ci by the Free Software Foundation; either version 2.1 of the License, 1253a5a1b3Sopenharmony_ci or (at your option) any later version. 1353a5a1b3Sopenharmony_ci 1453a5a1b3Sopenharmony_ci PulseAudio is distributed in the hope that it will be useful, but 1553a5a1b3Sopenharmony_ci WITHOUT ANY WARRANTY; without even the implied warranty of 1653a5a1b3Sopenharmony_ci MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 1753a5a1b3Sopenharmony_ci General Public License for more details. 1853a5a1b3Sopenharmony_ci 1953a5a1b3Sopenharmony_ci You should have received a copy of the GNU Lesser General Public License 2053a5a1b3Sopenharmony_ci along with PulseAudio; if not, see <http://www.gnu.org/licenses/>. 2153a5a1b3Sopenharmony_ci***/ 2253a5a1b3Sopenharmony_ci 2353a5a1b3Sopenharmony_ci#include <sys/types.h> 2453a5a1b3Sopenharmony_ci#include <stdlib.h> 2553a5a1b3Sopenharmony_ci#include <limits.h> 2653a5a1b3Sopenharmony_ci#include <assert.h> 2753a5a1b3Sopenharmony_ci 2853a5a1b3Sopenharmony_ci#include <pulse/cdecl.h> 2953a5a1b3Sopenharmony_ci#include <pulse/gccmacro.h> 3053a5a1b3Sopenharmony_ci#include <pulse/version.h> 3153a5a1b3Sopenharmony_ci 3253a5a1b3Sopenharmony_ci/** \file 3353a5a1b3Sopenharmony_ci * Memory allocation functions. 3453a5a1b3Sopenharmony_ci */ 3553a5a1b3Sopenharmony_ci 3653a5a1b3Sopenharmony_ciPA_C_DECL_BEGIN 3753a5a1b3Sopenharmony_ci 3853a5a1b3Sopenharmony_ci/** Allocate the specified number of bytes, just like malloc() does. However, in case of OOM, terminate */ 3953a5a1b3Sopenharmony_civoid* pa_xmalloc(size_t l) PA_GCC_MALLOC PA_GCC_ALLOC_SIZE(1); 4053a5a1b3Sopenharmony_ci 4153a5a1b3Sopenharmony_ci/** Same as pa_xmalloc(), but initialize allocated memory to 0 */ 4253a5a1b3Sopenharmony_civoid *pa_xmalloc0(size_t l) PA_GCC_MALLOC PA_GCC_ALLOC_SIZE(1); 4353a5a1b3Sopenharmony_ci 4453a5a1b3Sopenharmony_ci/** The combination of pa_xmalloc() and realloc() */ 4553a5a1b3Sopenharmony_civoid *pa_xrealloc(void *ptr, size_t size) PA_GCC_ALLOC_SIZE(2); 4653a5a1b3Sopenharmony_ci 4753a5a1b3Sopenharmony_ci/** Free allocated memory */ 4853a5a1b3Sopenharmony_civoid pa_xfree(void *p); 4953a5a1b3Sopenharmony_ci 5053a5a1b3Sopenharmony_ci/** Duplicate the specified string, allocating memory with pa_xmalloc() */ 5153a5a1b3Sopenharmony_cichar *pa_xstrdup(const char *s) PA_GCC_MALLOC; 5253a5a1b3Sopenharmony_ci 5353a5a1b3Sopenharmony_ci/** Duplicate the specified string, but truncate after l characters */ 5453a5a1b3Sopenharmony_cichar *pa_xstrndup(const char *s, size_t l) PA_GCC_MALLOC; 5553a5a1b3Sopenharmony_ci 5653a5a1b3Sopenharmony_ci/** Duplicate the specified memory block */ 5753a5a1b3Sopenharmony_civoid* pa_xmemdup(const void *p, size_t l) PA_GCC_MALLOC PA_GCC_ALLOC_SIZE(2); 5853a5a1b3Sopenharmony_ci 5953a5a1b3Sopenharmony_ci/** Internal helper for pa_xnew() */ 6053a5a1b3Sopenharmony_cistatic void* _pa_xnew_internal(size_t n, size_t k) PA_GCC_MALLOC PA_GCC_ALLOC_SIZE2(1,2); 6153a5a1b3Sopenharmony_ci 6253a5a1b3Sopenharmony_cistatic inline void* _pa_xnew_internal(size_t n, size_t k) { 6353a5a1b3Sopenharmony_ci assert(n < INT_MAX/k); 6453a5a1b3Sopenharmony_ci return pa_xmalloc(n*k); 6553a5a1b3Sopenharmony_ci} 6653a5a1b3Sopenharmony_ci 6753a5a1b3Sopenharmony_ci/** Allocate n new structures of the specified type. */ 6853a5a1b3Sopenharmony_ci#define pa_xnew(type, n) ((type*) _pa_xnew_internal((n), sizeof(type))) 6953a5a1b3Sopenharmony_ci 7053a5a1b3Sopenharmony_ci/** Internal helper for pa_xnew0() */ 7153a5a1b3Sopenharmony_cistatic void* _pa_xnew0_internal(size_t n, size_t k) PA_GCC_MALLOC PA_GCC_ALLOC_SIZE2(1,2); 7253a5a1b3Sopenharmony_ci 7353a5a1b3Sopenharmony_cistatic inline void* _pa_xnew0_internal(size_t n, size_t k) { 7453a5a1b3Sopenharmony_ci assert(n < INT_MAX/k); 7553a5a1b3Sopenharmony_ci return pa_xmalloc0(n*k); 7653a5a1b3Sopenharmony_ci} 7753a5a1b3Sopenharmony_ci 7853a5a1b3Sopenharmony_ci/** Same as pa_xnew() but set the memory to zero */ 7953a5a1b3Sopenharmony_ci#define pa_xnew0(type, n) ((type*) _pa_xnew0_internal((n), sizeof(type))) 8053a5a1b3Sopenharmony_ci 8153a5a1b3Sopenharmony_ci/** Internal helper for pa_xnew0() */ 8253a5a1b3Sopenharmony_cistatic void* _pa_xnewdup_internal(const void *p, size_t n, size_t k) PA_GCC_MALLOC PA_GCC_ALLOC_SIZE2(2,3); 8353a5a1b3Sopenharmony_ci 8453a5a1b3Sopenharmony_cistatic inline void* _pa_xnewdup_internal(const void *p, size_t n, size_t k) { 8553a5a1b3Sopenharmony_ci assert(n < INT_MAX/k); 8653a5a1b3Sopenharmony_ci return pa_xmemdup(p, n*k); 8753a5a1b3Sopenharmony_ci} 8853a5a1b3Sopenharmony_ci 8953a5a1b3Sopenharmony_ci/** Same as pa_xnew() but duplicate the specified data */ 9053a5a1b3Sopenharmony_ci#define pa_xnewdup(type, p, n) ((type*) _pa_xnewdup_internal((p), (n), sizeof(type))) 9153a5a1b3Sopenharmony_ci 9253a5a1b3Sopenharmony_ci/** Internal helper for pa_xrenew() */ 9353a5a1b3Sopenharmony_cistatic void* _pa_xrenew_internal(void *p, size_t n, size_t k) PA_GCC_MALLOC PA_GCC_ALLOC_SIZE2(2,3); 9453a5a1b3Sopenharmony_ci 9553a5a1b3Sopenharmony_cistatic inline void* _pa_xrenew_internal(void *p, size_t n, size_t k) { 9653a5a1b3Sopenharmony_ci assert(n < INT_MAX/k); 9753a5a1b3Sopenharmony_ci return pa_xrealloc(p, n*k); 9853a5a1b3Sopenharmony_ci} 9953a5a1b3Sopenharmony_ci 10053a5a1b3Sopenharmony_ci/** Reallocate n new structures of the specified type. */ 10153a5a1b3Sopenharmony_ci#define pa_xrenew(type, p, n) ((type*) _pa_xrenew_internal(p, (n), sizeof(type))) 10253a5a1b3Sopenharmony_ci 10353a5a1b3Sopenharmony_ciPA_C_DECL_END 10453a5a1b3Sopenharmony_ci 10553a5a1b3Sopenharmony_ci#endif 106