stack_alloc.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /* Copyright (C) 2002 Jean-Marc Valin */
  2. /**
  3. @file stack_alloc.h
  4. @brief Temporary memory allocation on stack
  5. */
  6. /*
  7. Redistribution and use in source and binary forms, with or without
  8. modification, are permitted provided that the following conditions
  9. are met:
  10. - Redistributions of source code must retain the above copyright
  11. notice, this list of conditions and the following disclaimer.
  12. - Redistributions in binary form must reproduce the above copyright
  13. notice, this list of conditions and the following disclaimer in the
  14. documentation and/or other materials provided with the distribution.
  15. - Neither the name of the Xiph.org Foundation nor the names of its
  16. contributors may be used to endorse or promote products derived from
  17. this software without specific prior written permission.
  18. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
  22. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  23. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  24. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  25. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  26. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  27. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #ifndef STACK_ALLOC_H
  31. #define STACK_ALLOC_H
  32. #ifdef USE_ALLOCA
  33. # ifdef _WIN32
  34. # include <malloc.h>
  35. # else
  36. # ifdef HAVE_ALLOCA_H
  37. # include <alloca.h>
  38. # else
  39. # include <stdlib.h>
  40. # endif
  41. # endif
  42. #endif
  43. /**
  44. * @def ALIGN(stack, size)
  45. *
  46. * Aligns the stack to a 'size' boundary
  47. *
  48. * @param stack Stack
  49. * @param size New size boundary
  50. */
  51. /**
  52. * @def PUSH(stack, size, type)
  53. *
  54. * Allocates 'size' elements of type 'type' on the stack
  55. *
  56. * @param stack Stack
  57. * @param size Number of elements
  58. * @param type Type of element
  59. */
  60. /**
  61. * @def VARDECL(var)
  62. *
  63. * Declare variable on stack
  64. *
  65. * @param var Variable to declare
  66. */
  67. /**
  68. * @def ALLOC(var, size, type)
  69. *
  70. * Allocate 'size' elements of 'type' on stack
  71. *
  72. * @param var Name of variable to allocate
  73. * @param size Number of elements
  74. * @param type Type of element
  75. */
  76. #ifdef ENABLE_VALGRIND
  77. #include <valgrind/memcheck.h>
  78. #define ALIGN(stack, size) ((stack) += ((size) - (long)(stack)) & ((size) - 1))
  79. #define PUSH(stack, size, type) (VALGRIND_MAKE_NOACCESS(stack, 1000),ALIGN((stack),sizeof(type)),VALGRIND_MAKE_WRITABLE(stack, ((size)*sizeof(type))),(stack)+=((size)*sizeof(type)),(type*)((stack)-((size)*sizeof(type))))
  80. #else
  81. #define ALIGN(stack, size) ((stack) += ((size) - (long)(stack)) & ((size) - 1))
  82. #define PUSH(stack, size, type) (ALIGN((stack),sizeof(type)),(stack)+=((size)*sizeof(type)),(type*)((stack)-((size)*sizeof(type))))
  83. #endif
  84. #if defined(VAR_ARRAYS)
  85. #define VARDECL(var)
  86. #define ALLOC(var, size, type) type var[size]
  87. #elif defined(USE_ALLOCA)
  88. #define VARDECL(var) var
  89. #define ALLOC(var, size, type) var = alloca(sizeof(type)*(size))
  90. #else
  91. #define VARDECL(var) var
  92. #define ALLOC(var, size, type) var = PUSH(stack, size, type)
  93. #endif
  94. #endif