Path: blob/master/thirdparty/linuxbsd_headers/pulse/xmalloc.h
9905 views
#ifndef foomemoryhfoo1#define foomemoryhfoo23/***4This file is part of PulseAudio.56Copyright 2004-2006 Lennart Poettering78PulseAudio is free software; you can redistribute it and/or modify9it under the terms of the GNU Lesser General Public License as published10by the Free Software Foundation; either version 2.1 of the License,11or (at your option) any later version.1213PulseAudio is distributed in the hope that it will be useful, but14WITHOUT ANY WARRANTY; without even the implied warranty of15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU16General Public License for more details.1718You should have received a copy of the GNU Lesser General Public License19along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.20***/2122#include <sys/types.h>23#include <stdlib.h>24#include <limits.h>25#include <assert.h>2627#include <pulse/cdecl.h>28#include <pulse/gccmacro.h>29#include <pulse/version.h>3031/** \file32* Memory allocation functions.33*/3435PA_C_DECL_BEGIN3637/** Allocate the specified number of bytes, just like malloc() does. However, in case of OOM, terminate */38void* pa_xmalloc(size_t l) PA_GCC_MALLOC PA_GCC_ALLOC_SIZE(1);3940/** Same as pa_xmalloc(), but initialize allocated memory to 0 */41void *pa_xmalloc0(size_t l) PA_GCC_MALLOC PA_GCC_ALLOC_SIZE(1);4243/** The combination of pa_xmalloc() and realloc() */44void *pa_xrealloc(void *ptr, size_t size) PA_GCC_ALLOC_SIZE(2);4546/** Free allocated memory */47void pa_xfree(void *p);4849/** Duplicate the specified string, allocating memory with pa_xmalloc() */50char *pa_xstrdup(const char *s) PA_GCC_MALLOC;5152/** Duplicate the specified string, but truncate after l characters */53char *pa_xstrndup(const char *s, size_t l) PA_GCC_MALLOC;5455/** Duplicate the specified memory block */56void* pa_xmemdup(const void *p, size_t l) PA_GCC_MALLOC PA_GCC_ALLOC_SIZE(2);5758/** Internal helper for pa_xnew() */59static void* _pa_xnew_internal(size_t n, size_t k) PA_GCC_MALLOC PA_GCC_ALLOC_SIZE2(1,2);6061static inline void* _pa_xnew_internal(size_t n, size_t k) {62assert(n < INT_MAX/k);63return pa_xmalloc(n*k);64}6566/** Allocate n new structures of the specified type. */67#define pa_xnew(type, n) ((type*) _pa_xnew_internal((n), sizeof(type)))6869/** Internal helper for pa_xnew0() */70static void* _pa_xnew0_internal(size_t n, size_t k) PA_GCC_MALLOC PA_GCC_ALLOC_SIZE2(1,2);7172static inline void* _pa_xnew0_internal(size_t n, size_t k) {73assert(n < INT_MAX/k);74return pa_xmalloc0(n*k);75}7677/** Same as pa_xnew() but set the memory to zero */78#define pa_xnew0(type, n) ((type*) _pa_xnew0_internal((n), sizeof(type)))7980/** Internal helper for pa_xnew0() */81static void* _pa_xnewdup_internal(const void *p, size_t n, size_t k) PA_GCC_MALLOC PA_GCC_ALLOC_SIZE2(2,3);8283static inline void* _pa_xnewdup_internal(const void *p, size_t n, size_t k) {84assert(n < INT_MAX/k);85return pa_xmemdup(p, n*k);86}8788/** Same as pa_xnew() but duplicate the specified data */89#define pa_xnewdup(type, p, n) ((type*) _pa_xnewdup_internal((p), (n), sizeof(type)))9091/** Internal helper for pa_xrenew() */92static void* _pa_xrenew_internal(void *p, size_t n, size_t k) PA_GCC_MALLOC PA_GCC_ALLOC_SIZE2(2,3);9394static inline void* _pa_xrenew_internal(void *p, size_t n, size_t k) {95assert(n < INT_MAX/k);96return pa_xrealloc(p, n*k);97}9899/** Reallocate n new structures of the specified type. */100#define pa_xrenew(type, p, n) ((type*) _pa_xrenew_internal(p, (n), sizeof(type)))101102PA_C_DECL_END103104#endif105106107