stack_alloc.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /* Copyright (C) 2002-2003 Jean-Marc Valin
  2. Copyright (C) 2007-2009 Xiph.Org Foundation */
  3. /**
  4. @file stack_alloc.h
  5. @brief Temporary memory allocation on stack
  6. */
  7. /*
  8. Redistribution and use in source and binary forms, with or without
  9. modification, are permitted provided that the following conditions
  10. are met:
  11. - Redistributions of source code must retain the above copyright
  12. notice, this list of conditions and the following disclaimer.
  13. - Redistributions in binary form must reproduce the above copyright
  14. notice, this list of conditions and the following disclaimer in the
  15. documentation and/or other materials provided with the distribution.
  16. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  17. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  18. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  19. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  20. OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  21. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  22. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  23. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  24. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  25. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  26. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. #ifndef STACK_ALLOC_H
  29. #define STACK_ALLOC_H
  30. #include "opus_types.h"
  31. #include "opus_defines.h"
  32. #if (!defined (VAR_ARRAYS) && !defined (USE_ALLOCA) && !defined (NONTHREADSAFE_PSEUDOSTACK))
  33. #error "Opus requires one of VAR_ARRAYS, USE_ALLOCA, or NONTHREADSAFE_PSEUDOSTACK be defined to select the temporary allocation mode."
  34. #endif
  35. #ifdef USE_ALLOCA
  36. # ifdef WIN32
  37. # include <malloc.h>
  38. # else
  39. # ifdef HAVE_ALLOCA_H
  40. # include <alloca.h>
  41. # else
  42. # include <stdlib.h>
  43. # endif
  44. # endif
  45. #endif
  46. /**
  47. * @def ALIGN(stack, size)
  48. *
  49. * Aligns the stack to a 'size' boundary
  50. *
  51. * @param stack Stack
  52. * @param size New size boundary
  53. */
  54. /**
  55. * @def PUSH(stack, size, type)
  56. *
  57. * Allocates 'size' elements of type 'type' on the stack
  58. *
  59. * @param stack Stack
  60. * @param size Number of elements
  61. * @param type Type of element
  62. */
  63. /**
  64. * @def VARDECL(var)
  65. *
  66. * Declare variable on stack
  67. *
  68. * @param var Variable to declare
  69. */
  70. /**
  71. * @def ALLOC(var, size, type)
  72. *
  73. * Allocate 'size' elements of 'type' on stack
  74. *
  75. * @param var Name of variable to allocate
  76. * @param size Number of elements
  77. * @param type Type of element
  78. */
  79. #if defined(VAR_ARRAYS)
  80. #define VARDECL(type, var)
  81. #define ALLOC(var, size, type) type var[size]
  82. #define SAVE_STACK
  83. #define RESTORE_STACK
  84. #define ALLOC_STACK
  85. /* C99 does not allow VLAs of size zero */
  86. #define ALLOC_NONE 1
  87. #elif defined(USE_ALLOCA)
  88. #define VARDECL(type, var) type *var
  89. # ifdef WIN32
  90. # define ALLOC(var, size, type) var = ((type*)_alloca(sizeof(type)*(size)))
  91. # else
  92. # define ALLOC(var, size, type) var = ((type*)alloca(sizeof(type)*(size)))
  93. # endif
  94. #define SAVE_STACK
  95. #define RESTORE_STACK
  96. #define ALLOC_STACK
  97. #define ALLOC_NONE 0
  98. #else
  99. #ifdef CELT_C
  100. char *scratch_ptr=0;
  101. char *global_stack=0;
  102. #else
  103. extern char *global_stack;
  104. extern char *scratch_ptr;
  105. #endif /* CELT_C */
  106. #ifdef ENABLE_VALGRIND
  107. #include <valgrind/memcheck.h>
  108. #ifdef CELT_C
  109. char *global_stack_top=0;
  110. #else
  111. extern char *global_stack_top;
  112. #endif /* CELT_C */
  113. #define ALIGN(stack, size) ((stack) += ((size) - (long)(stack)) & ((size) - 1))
  114. #define PUSH(stack, size, type) (VALGRIND_MAKE_MEM_NOACCESS(stack, global_stack_top-stack),ALIGN((stack),sizeof(type)/sizeof(char)),VALGRIND_MAKE_MEM_UNDEFINED(stack, ((size)*sizeof(type)/sizeof(char))),(stack)+=(2*(size)*sizeof(type)/sizeof(char)),(type*)((stack)-(2*(size)*sizeof(type)/sizeof(char))))
  115. #define RESTORE_STACK ((global_stack = _saved_stack),VALGRIND_MAKE_MEM_NOACCESS(global_stack, global_stack_top-global_stack))
  116. #define ALLOC_STACK char *_saved_stack; ((global_stack = (global_stack==0) ? ((global_stack_top=opus_alloc_scratch(GLOBAL_STACK_SIZE*2)+(GLOBAL_STACK_SIZE*2))-(GLOBAL_STACK_SIZE*2)) : global_stack),VALGRIND_MAKE_MEM_NOACCESS(global_stack, global_stack_top-global_stack)); _saved_stack = global_stack;
  117. #else
  118. #define ALIGN(stack, size) ((stack) += ((size) - (long)(stack)) & ((size) - 1))
  119. #define PUSH(stack, size, type) (ALIGN((stack),sizeof(type)/sizeof(char)),(stack)+=(size)*(sizeof(type)/sizeof(char)),(type*)((stack)-(size)*(sizeof(type)/sizeof(char))))
  120. #if 0 /* Set this to 1 to instrument pseudostack usage */
  121. #define RESTORE_STACK (printf("%ld %s:%d\n", global_stack-scratch_ptr, __FILE__, __LINE__),global_stack = _saved_stack)
  122. #else
  123. #define RESTORE_STACK (global_stack = _saved_stack)
  124. #endif
  125. #define ALLOC_STACK char *_saved_stack; (global_stack = (global_stack==0) ? (scratch_ptr=opus_alloc_scratch(GLOBAL_STACK_SIZE)) : global_stack); _saved_stack = global_stack;
  126. #endif /* ENABLE_VALGRIND */
  127. #include "os_support.h"
  128. #define VARDECL(type, var) type *var
  129. #define ALLOC(var, size, type) var = PUSH(global_stack, size, type)
  130. #define SAVE_STACK char *_saved_stack = global_stack;
  131. #define ALLOC_NONE 0
  132. #endif /* VAR_ARRAYS */
  133. #ifdef ENABLE_VALGRIND
  134. #include <valgrind/memcheck.h>
  135. #define OPUS_CHECK_ARRAY(ptr, len) VALGRIND_CHECK_MEM_IS_DEFINED(ptr, len*sizeof(*ptr))
  136. #define OPUS_CHECK_VALUE(value) VALGRIND_CHECK_VALUE_IS_DEFINED(value)
  137. #define OPUS_CHECK_ARRAY_COND(ptr, len) VALGRIND_CHECK_MEM_IS_DEFINED(ptr, len*sizeof(*ptr))
  138. #define OPUS_CHECK_VALUE_COND(value) VALGRIND_CHECK_VALUE_IS_DEFINED(value)
  139. #define OPUS_PRINT_INT(value) do {fprintf(stderr, #value " = %d at %s:%d\n", value, __FILE__, __LINE__);}while(0)
  140. #define OPUS_FPRINTF fprintf
  141. #else
  142. static OPUS_INLINE int _opus_false(void) {return 0;}
  143. #define OPUS_CHECK_ARRAY(ptr, len) _opus_false()
  144. #define OPUS_CHECK_VALUE(value) _opus_false()
  145. #define OPUS_PRINT_INT(value) do{}while(0)
  146. #define OPUS_FPRINTF (void)
  147. #endif
  148. #endif /* STACK_ALLOC_H */