Home | History | Annotate | Download | only in pulse
      1 #ifndef foomemoryhfoo
      2 #define foomemoryhfoo
      3 
      4 /* $Id: xmalloc.h 1971 2007-10-28 19:13:50Z lennart $ */
      5 
      6 /***
      7   This file is part of PulseAudio.
      8 
      9   Copyright 2004-2006 Lennart Poettering
     10 
     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.
     15 
     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.
     20 
     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
     24   USA.
     25 ***/
     26 
     27 #include <sys/types.h>
     28 #include <stdlib.h>
     29 #include <limits.h>
     30 #include <assert.h>
     31 #include <pulse/cdecl.h>
     32 
     33 /** \file
     34  * Memory allocation functions.
     35  */
     36 
     37 PA_C_DECL_BEGIN
     38 
     39 /** Allocate the specified number of bytes, just like malloc() does. However, in case of OOM, terminate */
     40 void* pa_xmalloc(size_t l);
     41 
     42 /** Same as pa_xmalloc(), but initialize allocated memory to 0 */
     43 void *pa_xmalloc0(size_t l);
     44 
     45 /**  The combination of pa_xmalloc() and realloc() */
     46 void *pa_xrealloc(void *ptr, size_t size);
     47 
     48 /** Free allocated memory */
     49 void pa_xfree(void *p);
     50 
     51 /** Duplicate the specified string, allocating memory with pa_xmalloc() */
     52 char *pa_xstrdup(const char *s);
     53 
     54 /** Duplicate the specified string, but truncate after l characters */
     55 char *pa_xstrndup(const char *s, size_t l);
     56 
     57 /** Duplicate the specified memory block */
     58 void* pa_xmemdup(const void *p, size_t l);
     59 
     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);
     64 }
     65 
     66 /** Allocate n new structures of the specified type. */
     67 #define pa_xnew(type, n) ((type*) pa_xnew_internal((n), sizeof(type)))
     68 
     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);
     73 }
     74 
     75 /** Same as pa_xnew() but set the memory to zero */
     76 #define pa_xnew0(type, n) ((type*) pa_xnew0_internal((n), sizeof(type)))
     77 
     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);
     82 }
     83 
     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)))
     86 
     87 PA_C_DECL_END
     88 
     89 #endif
     90