stack_alloc.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. #include <alloca.h>
  37. #endif
  38. #endif
  39. /**
  40. * @def ALIGN(stack, size)
  41. *
  42. * Aligns the stack to a 'size' boundary
  43. *
  44. * @param stack Stack
  45. * @param size New size boundary
  46. */
  47. /**
  48. * @def PUSH(stack, size, type)
  49. *
  50. * Allocates 'size' elements of type 'type' on the stack
  51. *
  52. * @param stack Stack
  53. * @param size Number of elements
  54. * @param type Type of element
  55. */
  56. /**
  57. * @def PUSHS(stack, type)
  58. *
  59. * Allocates a struct stack
  60. *
  61. * @param stack Stack
  62. * @param type Struct type
  63. */
  64. /**
  65. * @def VARDECL(var)
  66. *
  67. * Declare variable on stack
  68. *
  69. * @param var Variable to declare
  70. */
  71. /**
  72. * @def ALLOC(var, size, type)
  73. *
  74. * Allocate 'size' elements of 'type' on stack
  75. *
  76. * @param var Name of variable to allocate
  77. * @param size Number of elements
  78. * @param type Type of element
  79. */
  80. #ifdef ENABLE_VALGRIND
  81. #include <valgrind/memcheck.h>
  82. #define ALIGN(stack, size) ((stack) += ((size) - (long)(stack)) & ((size) - 1))
  83. #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))))
  84. #define PUSHS(stack, type) (VALGRIND_MAKE_NOACCESS(stack, 1000),ALIGN((stack),sizeof(long)),VALGRIND_MAKE_WRITABLE(stack, (sizeof(type))),(stack)+=(sizeof(type)),(type*)((stack)-(sizeof(type))))
  85. #else
  86. #define ALIGN(stack, size) ((stack) += ((size) - (long)(stack)) & ((size) - 1))
  87. #define PUSH(stack, size, type) (ALIGN((stack),sizeof(type)),(stack)+=((size)*sizeof(type)),(type*)((stack)-((size)*sizeof(type))))
  88. #define PUSHS(stack, type) (ALIGN((stack),sizeof(long)),(stack)+=(sizeof(type)),(type*)((stack)-(sizeof(type))))
  89. #endif
  90. #if defined(VAR_ARRAYS)
  91. #define VARDECL(var)
  92. #define ALLOC(var, size, type) type var[size]
  93. #elif defined(USE_ALLOCA)
  94. #define VARDECL(var) var
  95. #define ALLOC(var, size, type) var = alloca(sizeof(type)*size)
  96. #else
  97. #define VARDECL(var) var
  98. #define ALLOC(var, size, type) var = PUSH(stack, size, type)
  99. #endif
  100. #endif
粤ICP备19079148号