stack_alloc.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. #if (!defined (VAR_ARRAYS) && !defined (USE_ALLOCA) && !defined (NONTHREADSAFE_PSEUDOSTACK))
  31. #error "Opus requires one of VAR_ARRAYS, USE_ALLOCA, or NONTHREADSAFE_PSEUDOSTACK be defined to select the temporary allocation mode."
  32. #endif
  33. #ifdef USE_ALLOCA
  34. # ifdef WIN32
  35. # include <malloc.h>
  36. # else
  37. # ifdef HAVE_ALLOCA_H
  38. # include <alloca.h>
  39. # else
  40. # include <stdlib.h>
  41. # endif
  42. # endif
  43. #endif
  44. /**
  45. * @def ALIGN(stack, size)
  46. *
  47. * Aligns the stack to a 'size' boundary
  48. *
  49. * @param stack Stack
  50. * @param size New size boundary
  51. */
  52. /**
  53. * @def PUSH(stack, size, type)
  54. *
  55. * Allocates 'size' elements of type 'type' on the stack
  56. *
  57. * @param stack Stack
  58. * @param size Number of elements
  59. * @param type Type of element
  60. */
  61. /**
  62. * @def VARDECL(var)
  63. *
  64. * Declare variable on stack
  65. *
  66. * @param var Variable to declare
  67. */
  68. /**
  69. * @def ALLOC(var, size, type)
  70. *
  71. * Allocate 'size' elements of 'type' on stack
  72. *
  73. * @param var Name of variable to allocate
  74. * @param size Number of elements
  75. * @param type Type of element
  76. */
  77. #if defined(VAR_ARRAYS)
  78. #define VARDECL(type, var)
  79. #define ALLOC(var, size, type) type var[size]
  80. #define SAVE_STACK
  81. #define RESTORE_STACK
  82. #define ALLOC_STACK
  83. #elif defined(USE_ALLOCA)
  84. #define VARDECL(type, var) type *var
  85. # ifdef WIN32
  86. # define ALLOC(var, size, type) var = ((type*)_alloca(sizeof(type)*(size)))
  87. # else
  88. # define ALLOC(var, size, type) var = ((type*)alloca(sizeof(type)*(size)))
  89. # endif
  90. #define SAVE_STACK
  91. #define RESTORE_STACK
  92. #define ALLOC_STACK
  93. #else
  94. #ifdef CELT_C
  95. char *global_stack=0;
  96. #else
  97. extern char *global_stack;
  98. #endif /* CELT_C */
  99. #ifdef ENABLE_VALGRIND
  100. #include <valgrind/memcheck.h>
  101. #ifdef CELT_C
  102. char *global_stack_top=0;
  103. #else
  104. extern char *global_stack_top;
  105. #endif /* CELT_C */
  106. #define ALIGN(stack, size) ((stack) += ((size) - (long)(stack)) & ((size) - 1))
  107. #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))))
  108. #define RESTORE_STACK ((global_stack = _saved_stack),VALGRIND_MAKE_MEM_NOACCESS(global_stack, global_stack_top-global_stack))
  109. #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;
  110. #else
  111. #define ALIGN(stack, size) ((stack) += ((size) - (long)(stack)) & ((size) - 1))
  112. #define PUSH(stack, size, type) (ALIGN((stack),sizeof(type)/sizeof(char)),(stack)+=(size)*(sizeof(type)/sizeof(char)),(type*)((stack)-(size)*(sizeof(type)/sizeof(char))))
  113. #define RESTORE_STACK (global_stack = _saved_stack)
  114. #define ALLOC_STACK char *_saved_stack; (global_stack = (global_stack==0) ? opus_alloc_scratch(GLOBAL_STACK_SIZE) : global_stack); _saved_stack = global_stack;
  115. #endif /* ENABLE_VALGRIND */
  116. #include "os_support.h"
  117. #define VARDECL(type, var) type *var
  118. #define ALLOC(var, size, type) var = PUSH(global_stack, size, type)
  119. #define SAVE_STACK char *_saved_stack = global_stack;
  120. #endif /* VAR_ARRAYS */
  121. #ifdef ENABLE_VALGRIND
  122. #include <valgrind/memcheck.h>
  123. #define OPUS_CHECK_ARRAY(ptr, len) VALGRIND_CHECK_MEM_IS_DEFINED(ptr, len*sizeof(*ptr))
  124. #define OPUS_CHECK_VALUE(value) VALGRIND_CHECK_VALUE_IS_DEFINED(value)
  125. #define OPUS_CHECK_ARRAY_COND(ptr, len) VALGRIND_CHECK_MEM_IS_DEFINED(ptr, len*sizeof(*ptr))
  126. #define OPUS_CHECK_VALUE_COND(value) VALGRIND_CHECK_VALUE_IS_DEFINED(value)
  127. #define OPUS_PRINT_INT(value) do {fprintf(stderr, #value " = %d at %s:%d\n", value, __FILE__, __LINE__);}while(0)
  128. #define OPUS_FPRINTF fprintf
  129. #else
  130. static inline int _opus_false(void) {return 0;}
  131. #define OPUS_CHECK_ARRAY(ptr, len) _opus_false()
  132. #define OPUS_CHECK_VALUE(value) _opus_false()
  133. #define OPUS_PRINT_INT(value) do{}while(0)
  134. #define OPUS_FPRINTF (void)
  135. #endif
  136. #endif /* STACK_ALLOC_H */