opj_malloc.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * Copyright (c) 2005, Herve Drolon, FreeImage Team
  3. * Copyright (c) 2007, Callum Lerwick <seg@haxxed.com>
  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. #elif defined(__FreeBSD__)
  81. #define HAVE_POSIX_MEMALIGN
  82. /* Linux x86_64 and OSX always align allocations to 16 bytes */
  83. #elif !defined(__amd64__) && !defined(__APPLE__) && !defined(_AIX)
  84. #define HAVE_MEMALIGN
  85. #include <malloc.h>
  86. #endif
  87. #endif
  88. #define opj_aligned_malloc(size) malloc(size)
  89. #define opj_aligned_free(m) free(m)
  90. #ifdef HAVE_MM_MALLOC
  91. #undef opj_aligned_malloc
  92. #define opj_aligned_malloc(size) _mm_malloc(size, 16)
  93. #undef opj_aligned_free
  94. #define opj_aligned_free(m) _mm_free(m)
  95. #endif
  96. #ifdef HAVE_MEMALIGN
  97. extern void* memalign(size_t, size_t);
  98. #undef opj_aligned_malloc
  99. #define opj_aligned_malloc(size) memalign(16, (size))
  100. #undef opj_aligned_free
  101. #define opj_aligned_free(m) free(m)
  102. #endif
  103. #ifdef HAVE_POSIX_MEMALIGN
  104. #undef opj_aligned_malloc
  105. extern int posix_memalign(void**, size_t, size_t);
  106. static INLINE void* __attribute__ ((malloc)) opj_aligned_malloc(size_t size){
  107. void* mem = NULL;
  108. posix_memalign(&mem, 16, size);
  109. return mem;
  110. }
  111. #undef opj_aligned_free
  112. #define opj_aligned_free(m) free(m)
  113. #endif
  114. #ifdef ALLOC_PERF_OPT
  115. #undef opj_aligned_malloc
  116. #define opj_aligned_malloc(size) opj_malloc(size)
  117. #undef opj_aligned_free
  118. #define opj_aligned_free(m) opj_free(m)
  119. #endif
  120. /**
  121. Reallocate memory blocks.
  122. @param m Pointer to previously allocated memory block
  123. @param s New size in bytes
  124. @return Returns a void pointer to the reallocated (and possibly moved) memory block
  125. */
  126. #ifdef ALLOC_PERF_OPT
  127. void * OPJ_CALLCONV opj_realloc(void * m, size_t s);
  128. #else
  129. #define opj_realloc(m, s) realloc(m, s)
  130. #endif
  131. /**
  132. Deallocates or frees a memory block.
  133. @param m Previously allocated memory block to be freed
  134. */
  135. #ifdef ALLOC_PERF_OPT
  136. void OPJ_CALLCONV opj_free(void * m);
  137. #else
  138. #define opj_free(m) free(m)
  139. #endif
  140. #ifdef __GNUC__
  141. #pragma GCC poison malloc calloc realloc free
  142. #endif
  143. /* ----------------------------------------------------------------------- */
  144. /*@}*/
  145. /*@}*/
  146. #endif /* __OPJ_MALLOC_H */