stack_alloc.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /* Copyright (C) 2002-2012 IETF Trust, Jean-Marc Valin, Xiph.Org Foundation.
  2. All rights reserved.*/
  3. /**
  4. @file stack_alloc.h
  5. @brief Temporary memory allocation on stack
  6. */
  7. /*
  8. This file is extracted from RFC6716. Please see that RFC for additional
  9. information.
  10. Redistribution and use in source and binary forms, with or without
  11. modification, are permitted provided that the following conditions
  12. are met:
  13. - Redistributions of source code must retain the above copyright
  14. notice, this list of conditions and the following disclaimer.
  15. - Redistributions in binary form must reproduce the above copyright
  16. notice, this list of conditions and the following disclaimer in the
  17. documentation and/or other materials provided with the distribution.
  18. - Neither the name of Internet Society, IETF or IETF Trust, nor the
  19. names of specific contributors, may be used to endorse or promote
  20. products derived from this software without specific prior written
  21. permission.
  22. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  23. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  24. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  25. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  26. OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  27. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  28. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  29. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  30. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  31. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  32. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #ifndef STACK_ALLOC_H
  35. #define STACK_ALLOC_H
  36. #ifdef USE_ALLOCA
  37. # ifdef WIN32
  38. # include <malloc.h>
  39. # else
  40. # ifdef HAVE_ALLOCA_H
  41. # include <alloca.h>
  42. # else
  43. # include <stdlib.h>
  44. # endif
  45. # endif
  46. #endif
  47. /**
  48. * @def ALIGN(stack, size)
  49. *
  50. * Aligns the stack to a 'size' boundary
  51. *
  52. * @param stack Stack
  53. * @param size New size boundary
  54. */
  55. /**
  56. * @def PUSH(stack, size, type)
  57. *
  58. * Allocates 'size' elements of type 'type' on the stack
  59. *
  60. * @param stack Stack
  61. * @param size Number of elements
  62. * @param type Type of element
  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. #if defined(VAR_ARRAYS)
  81. #define VARDECL(type, var)
  82. #define ALLOC(var, size, type) type var[size]
  83. #define SAVE_STACK
  84. #define RESTORE_STACK
  85. #define ALLOC_STACK
  86. #elif defined(USE_ALLOCA)
  87. #define VARDECL(type, var) type *var
  88. # ifdef WIN32
  89. # define ALLOC(var, size, type) var = ((type*)_alloca(sizeof(type)*(size)))
  90. # else
  91. # define ALLOC(var, size, type) var = ((type*)alloca(sizeof(type)*(size)))
  92. # endif
  93. #define SAVE_STACK
  94. #define RESTORE_STACK
  95. #define ALLOC_STACK
  96. #else
  97. #ifdef CELT_C
  98. char *global_stack=0;
  99. #else
  100. extern char *global_stack;
  101. #endif /* CELT_C */
  102. #ifdef ENABLE_VALGRIND
  103. #include <valgrind/memcheck.h>
  104. #ifdef CELT_C
  105. char *global_stack_top=0;
  106. #else
  107. extern char *global_stack_top;
  108. #endif /* CELT_C */
  109. #define ALIGN(stack, size) ((stack) += ((size) - (long)(stack)) & ((size) - 1))
  110. #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))))
  111. #define RESTORE_STACK ((global_stack = _saved_stack),VALGRIND_MAKE_MEM_NOACCESS(global_stack, global_stack_top-global_stack))
  112. #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;
  113. #else
  114. #define ALIGN(stack, size) ((stack) += ((size) - (long)(stack)) & ((size) - 1))
  115. #define PUSH(stack, size, type) (ALIGN((stack),sizeof(type)/sizeof(char)),(stack)+=(size)*(sizeof(type)/sizeof(char)),(type*)((stack)-(size)*(sizeof(type)/sizeof(char))))
  116. #define RESTORE_STACK (global_stack = _saved_stack)
  117. #define ALLOC_STACK char *_saved_stack; (global_stack = (global_stack==0) ? opus_alloc_scratch(GLOBAL_STACK_SIZE) : global_stack); _saved_stack = global_stack;
  118. #endif /* ENABLE_VALGRIND */
  119. #include "os_support.h"
  120. #define VARDECL(type, var) type *var
  121. #define ALLOC(var, size, type) var = PUSH(global_stack, size, type)
  122. #define SAVE_STACK char *_saved_stack = global_stack;
  123. #endif /* VAR_ARRAYS */
  124. #endif /* STACK_ALLOC_H */