opj_malloc.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * Copyright (c) 2005, Hervé Drolon, FreeImage Team
  3. * Copyright (c) 2007, Callum Lerwick <[email protected]>
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  19. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  20. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  21. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  22. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  23. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  24. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  25. * POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #ifndef __OPJ_MALLOC_H
  28. #define __OPJ_MALLOC_H
  29. /**
  30. @file opj_malloc.h
  31. @brief Internal functions
  32. The functions in opj_malloc.h are internal utilities used for memory management.
  33. */
  34. /** @defgroup MISC MISC - Miscellaneous internal functions */
  35. /*@{*/
  36. /** @name Exported functions */
  37. /*@{*/
  38. /* ----------------------------------------------------------------------- */
  39. /**
  40. Allocate an uninitialized memory block
  41. @param size Bytes to allocate
  42. @return Returns a void pointer to the allocated space, or NULL if there is insufficient memory available
  43. */
  44. #ifdef ALLOC_PERF_OPT
  45. void * OPJ_CALLCONV opj_malloc(size_t size);
  46. #else
  47. #define opj_malloc(size) malloc(size)
  48. #endif
  49. /**
  50. Allocate a memory block with elements initialized to 0
  51. @param num Blocks to allocate
  52. @param size Bytes per block to allocate
  53. @return Returns a void pointer to the allocated space, or NULL if there is insufficient memory available
  54. */
  55. #ifdef ALLOC_PERF_OPT
  56. void * OPJ_CALLCONV opj_calloc(size_t _NumOfElements, size_t _SizeOfElements);
  57. #else
  58. #define opj_calloc(num, size) calloc(num, size)
  59. #endif
  60. /**
  61. Allocate memory aligned to a 16 byte boundry
  62. @param size Bytes to allocate
  63. @return Returns a void pointer to the allocated space, or NULL if there is insufficient memory available
  64. */
  65. /* FIXME: These should be set with cmake tests, but we're currently not requiring use of cmake */
  66. #ifdef WIN32
  67. /* Someone should tell the mingw people that their malloc.h ought to provide _mm_malloc() */
  68. #ifdef __GNUC__
  69. #include <mm_malloc.h>
  70. #define HAVE_MM_MALLOC
  71. #else /* MSVC, Intel C++ */
  72. #include <malloc.h>
  73. #ifdef _mm_malloc
  74. #define HAVE_MM_MALLOC
  75. #endif
  76. #endif
  77. #else /* Not WIN32 */
  78. #if defined(__sun)
  79. #define HAVE_MEMALIGN
  80. /* Linux x86_64 and OSX always align allocations to 16 bytes */
  81. #elif !defined(__amd64__) && !defined(__APPLE__)
  82. #define HAVE_MEMALIGN
  83. #include <malloc.h>
  84. #endif
  85. #endif
  86. #define opj_aligned_malloc(size) malloc(size)
  87. #define opj_aligned_free(m) free(m)
  88. #ifdef HAVE_MM_MALLOC
  89. #undef opj_aligned_malloc
  90. #define opj_aligned_malloc(size) _mm_malloc(size, 16)
  91. #undef opj_aligned_free
  92. #define opj_aligned_free(m) _mm_free(m)
  93. #endif
  94. #ifdef HAVE_MEMALIGN
  95. extern void* memalign(size_t, size_t);
  96. #undef opj_aligned_malloc
  97. #define opj_aligned_malloc(size) memalign(16, (size))
  98. #undef opj_aligned_free
  99. #define opj_aligned_free(m) free(m)
  100. #endif
  101. #ifdef HAVE_POSIX_MEMALIGN
  102. #undef opj_aligned_malloc
  103. extern int posix_memalign(void**, size_t, size_t);
  104. static INLINE void* __attribute__ ((malloc)) opj_aligned_malloc(size_t size){
  105. void* mem = NULL;
  106. posix_memalign(&mem, 16, size);
  107. return mem;
  108. }
  109. #undef opj_aligned_free
  110. #define opj_aligned_free(m) free(m)
  111. #endif
  112. #ifdef ALLOC_PERF_OPT
  113. #undef opj_aligned_malloc
  114. #define opj_aligned_malloc(size) opj_malloc(size)
  115. #undef opj_aligned_free
  116. #define opj_aligned_free(m) opj_free(m)
  117. #endif
  118. /**
  119. Reallocate memory blocks.
  120. @param memblock Pointer to previously allocated memory block
  121. @param size New size in bytes
  122. @return Returns a void pointer to the reallocated (and possibly moved) memory block
  123. */
  124. #ifdef ALLOC_PERF_OPT
  125. void * OPJ_CALLCONV opj_realloc(void * _Memory, size_t NewSize);
  126. #else
  127. #define opj_realloc(m, s) realloc(m, s)
  128. #endif
  129. /**
  130. Deallocates or frees a memory block.
  131. @param memblock Previously allocated memory block to be freed
  132. */
  133. #ifdef ALLOC_PERF_OPT
  134. void OPJ_CALLCONV opj_free(void * _Memory);
  135. #else
  136. #define opj_free(m) free(m)
  137. #endif
  138. #ifdef __GNUC__
  139. #pragma GCC poison malloc calloc realloc free
  140. #endif
  141. /* ----------------------------------------------------------------------- */
  142. /*@}*/
  143. /*@}*/
  144. #endif /* __OPJ_MALLOC_H */