xmalloc.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #ifndef foomemoryhfoo
  2. #define foomemoryhfoo
  3. /***
  4. This file is part of PulseAudio.
  5. Copyright 2004-2006 Lennart Poettering
  6. PulseAudio is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU Lesser General Public License as published
  8. by the Free Software Foundation; either version 2.1 of the License,
  9. or (at your option) any later version.
  10. PulseAudio is distributed in the hope that it will be useful, but
  11. WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. General Public License for more details.
  14. You should have received a copy of the GNU Lesser General Public License
  15. along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
  16. ***/
  17. #include <sys/types.h>
  18. #include <stdlib.h>
  19. #include <limits.h>
  20. #include <assert.h>
  21. #include <pulse/cdecl.h>
  22. #include <pulse/gccmacro.h>
  23. #include <pulse/version.h>
  24. /** \file
  25. * Memory allocation functions.
  26. */
  27. PA_C_DECL_BEGIN
  28. /** Allocate the specified number of bytes, just like malloc() does. However, in case of OOM, terminate */
  29. void* pa_xmalloc(size_t l) PA_GCC_MALLOC PA_GCC_ALLOC_SIZE(1);
  30. /** Same as pa_xmalloc(), but initialize allocated memory to 0 */
  31. void *pa_xmalloc0(size_t l) PA_GCC_MALLOC PA_GCC_ALLOC_SIZE(1);
  32. /** The combination of pa_xmalloc() and realloc() */
  33. void *pa_xrealloc(void *ptr, size_t size) PA_GCC_ALLOC_SIZE(2);
  34. /** Free allocated memory */
  35. void pa_xfree(void *p);
  36. /** Duplicate the specified string, allocating memory with pa_xmalloc() */
  37. char *pa_xstrdup(const char *s) PA_GCC_MALLOC;
  38. /** Duplicate the specified string, but truncate after l characters */
  39. char *pa_xstrndup(const char *s, size_t l) PA_GCC_MALLOC;
  40. /** Duplicate the specified memory block */
  41. void* pa_xmemdup(const void *p, size_t l) PA_GCC_MALLOC PA_GCC_ALLOC_SIZE(2);
  42. /** Internal helper for pa_xnew() */
  43. static void* _pa_xnew_internal(size_t n, size_t k) PA_GCC_MALLOC PA_GCC_ALLOC_SIZE2(1,2);
  44. static inline void* _pa_xnew_internal(size_t n, size_t k) {
  45. assert(n < INT_MAX/k);
  46. return pa_xmalloc(n*k);
  47. }
  48. /** Allocate n new structures of the specified type. */
  49. #define pa_xnew(type, n) ((type*) _pa_xnew_internal((n), sizeof(type)))
  50. /** Internal helper for pa_xnew0() */
  51. static void* _pa_xnew0_internal(size_t n, size_t k) PA_GCC_MALLOC PA_GCC_ALLOC_SIZE2(1,2);
  52. static inline void* _pa_xnew0_internal(size_t n, size_t k) {
  53. assert(n < INT_MAX/k);
  54. return pa_xmalloc0(n*k);
  55. }
  56. /** Same as pa_xnew() but set the memory to zero */
  57. #define pa_xnew0(type, n) ((type*) _pa_xnew0_internal((n), sizeof(type)))
  58. /** Internal helper for pa_xnew0() */
  59. static void* _pa_xnewdup_internal(const void *p, size_t n, size_t k) PA_GCC_MALLOC PA_GCC_ALLOC_SIZE2(2,3);
  60. static inline void* _pa_xnewdup_internal(const void *p, size_t n, size_t k) {
  61. assert(n < INT_MAX/k);
  62. return pa_xmemdup(p, n*k);
  63. }
  64. /** Same as pa_xnew() but duplicate the specified data */
  65. #define pa_xnewdup(type, p, n) ((type*) _pa_xnewdup_internal((p), (n), sizeof(type)))
  66. /** Internal helper for pa_xrenew() */
  67. static void* _pa_xrenew_internal(void *p, size_t n, size_t k) PA_GCC_MALLOC PA_GCC_ALLOC_SIZE2(2,3);
  68. static inline void* _pa_xrenew_internal(void *p, size_t n, size_t k) {
  69. assert(n < INT_MAX/k);
  70. return pa_xrealloc(p, n*k);
  71. }
  72. /** Reallocate n new structures of the specified type. */
  73. #define pa_xrenew(type, p, n) ((type*) _pa_xrenew_internal(p, (n), sizeof(type)))
  74. PA_C_DECL_END
  75. #endif