You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

common_stackalloc.h 3.7 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*******************************************************************************
  2. Copyright (c) 2016, The OpenBLAS Project
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are
  6. met:
  7. 1. Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. 2. Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in
  11. the documentation and/or other materials provided with the
  12. distribution.
  13. 3. Neither the name of the OpenBLAS project nor the names of
  14. its contributors may be used to endorse or promote products
  15. derived from this software without specific prior written permission.
  16. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. ARE DISCLAIMED. IN NO EVENT SHALL THE OPENBLAS PROJECT OR CONTRIBUTORS BE
  20. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  24. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  25. USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. *******************************************************************************/
  27. #define STACK_ALLOC_PROTECT
  28. #ifdef STACK_ALLOC_PROTECT
  29. // Try to detect stack smashing
  30. #include <assert.h>
  31. #define STACK_ALLOC_PROTECT_SET volatile int stack_check = 0x7fc01234;
  32. #define STACK_ALLOC_PROTECT_CHECK assert(stack_check == 0x7fc01234);
  33. #else
  34. #define STACK_ALLOC_PROTECT_SET
  35. #define STACK_ALLOC_PROTECT_CHECK
  36. #endif
  37. #if defined(MAX_STACK_ALLOC) && MAX_STACK_ALLOC > 0
  38. /*
  39. * Allocate a buffer on the stack if the size is smaller than MAX_STACK_ALLOC.
  40. * Stack allocation is much faster than blas_memory_alloc or malloc, particularly
  41. * when OpenBLAS is used from a multi-threaded application.
  42. * SIZE must be carefully chosen to be:
  43. * - as small as possible to maximize the number of stack allocation
  44. * - large enough to support all architectures and kernel
  45. * Choosing a SIZE too small will lead to a stack smashing.
  46. */
  47. #define STACK_ALLOC(SIZE, TYPE, BUFFER) \
  48. /* make it volatile because some function (ex: dgemv_n.S) */ \
  49. /* do not restore all register */ \
  50. volatile int stack_alloc_size = SIZE; \
  51. if (stack_alloc_size > MAX_STACK_ALLOC / sizeof(TYPE)) stack_alloc_size = 0; \
  52. STACK_ALLOC_PROTECT_SET \
  53. /* Avoid declaring an array of length 0 */ \
  54. TYPE stack_buffer[stack_alloc_size ? stack_alloc_size : 1] \
  55. __attribute__((aligned(0x20))); \
  56. BUFFER = stack_alloc_size ? stack_buffer : (TYPE *)blas_memory_alloc(1);
  57. #else
  58. //Original OpenBLAS/GotoBLAS codes.
  59. #define STACK_ALLOC(SIZE, TYPE, BUFFER) BUFFER = (TYPE *)blas_memory_alloc(1)
  60. #endif
  61. #if defined(MAX_STACK_ALLOC) && MAX_STACK_ALLOC > 0
  62. #define STACK_FREE(BUFFER) \
  63. STACK_ALLOC_PROTECT_CHECK \
  64. if(!stack_alloc_size) \
  65. blas_memory_free(BUFFER);
  66. #else
  67. #define STACK_FREE(BUFFER) blas_memory_free(BUFFER)
  68. #endif