stack_alloc.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 FOUNDATION OR
  20. 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. #ifdef USE_ALLOCA
  31. # ifdef WIN32
  32. # include <malloc.h>
  33. # else
  34. # ifdef HAVE_ALLOCA_H
  35. # include <alloca.h>
  36. # else
  37. # include <stdlib.h>
  38. # endif
  39. # endif
  40. #endif
  41. /**
  42. * @def ALIGN(stack, size)
  43. *
  44. * Aligns the stack to a 'size' boundary
  45. *
  46. * @param stack Stack
  47. * @param size New size boundary
  48. */
  49. /**
  50. * @def PUSH(stack, size, type)
  51. *
  52. * Allocates 'size' elements of type 'type' on the stack
  53. *
  54. * @param stack Stack
  55. * @param size Number of elements
  56. * @param type Type of element
  57. */
  58. /**
  59. * @def VARDECL(var)
  60. *
  61. * Declare variable on stack
  62. *
  63. * @param var Variable to declare
  64. */
  65. /**
  66. * @def ALLOC(var, size, type)
  67. *
  68. * Allocate 'size' elements of 'type' on stack
  69. *
  70. * @param var Name of variable to allocate
  71. * @param size Number of elements
  72. * @param type Type of element
  73. */
  74. #if defined(VAR_ARRAYS)
  75. #define VARDECL(type, var)
  76. #define ALLOC(var, size, type) type var[size]
  77. #define SAVE_STACK
  78. #define RESTORE_STACK
  79. #define ALLOC_STACK
  80. #elif defined(USE_ALLOCA)
  81. #define VARDECL(type, var) type *var
  82. # ifdef WIN32
  83. # define ALLOC(var, size, type) var = ((type*)_alloca(sizeof(type)*(size)))
  84. # else
  85. # define ALLOC(var, size, type) var = ((type*)alloca(sizeof(type)*(size)))
  86. # endif
  87. #define SAVE_STACK
  88. #define RESTORE_STACK
  89. #define ALLOC_STACK
  90. #else
  91. #ifdef CELT_C
  92. char *global_stack=0;
  93. #else
  94. extern char *global_stack;
  95. #endif /*CELT_C*/
  96. #ifdef ENABLE_VALGRIND
  97. #include <valgrind/memcheck.h>
  98. #ifdef CELT_C
  99. char *global_stack_top=0;
  100. #else
  101. extern char *global_stack_top;
  102. #endif /*CELT_C*/
  103. #define ALIGN(stack, size) ((stack) += ((size) - (long)(stack)) & ((size) - 1))
  104. #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))))
  105. #define RESTORE_STACK ((global_stack = _saved_stack),VALGRIND_MAKE_MEM_NOACCESS(global_stack, global_stack_top-global_stack))
  106. #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;
  107. #else
  108. #define ALIGN(stack, size) ((stack) += ((size) - (long)(stack)) & ((size) - 1))
  109. #define PUSH(stack, size, type) (ALIGN((stack),sizeof(type)/sizeof(char)),(stack)+=(size)*(sizeof(type)/sizeof(char)),(type*)((stack)-(size)*(sizeof(type)/sizeof(char))))
  110. #define RESTORE_STACK (global_stack = _saved_stack)
  111. #define ALLOC_STACK char *_saved_stack; (global_stack = (global_stack==0) ? opus_alloc_scratch(GLOBAL_STACK_SIZE) : global_stack); _saved_stack = global_stack;
  112. #endif /*ENABLE_VALGRIND*/
  113. #include "os_support.h"
  114. #define VARDECL(type, var) type *var
  115. #define ALLOC(var, size, type) var = PUSH(global_stack, size, type)
  116. #define SAVE_STACK char *_saved_stack = global_stack;
  117. #endif /*VAR_ARRAYS*/
  118. #endif /*STACK_ALLOC_H*/