i_malloc.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*******************************************************************************
  2. * Copyright 2006-2022 Intel Corporation.
  3. *
  4. * This software and the related documents are Intel copyrighted materials, and
  5. * your use of them is governed by the express license under which they were
  6. * provided to you (License). Unless the License provides otherwise, you may not
  7. * use, modify, copy, publish, distribute, disclose or transmit this software or
  8. * the related documents without Intel's prior written permission.
  9. *
  10. * This software and the related documents are provided as is, with no express
  11. * or implied warranties, other than those that are expressly stated in the
  12. * License.
  13. *******************************************************************************/
  14. /**
  15. * This header file describes how memory allocation can be replaced
  16. * in the Intel(R) oneAPI Math Kernel Library (oneMKL). It contains
  17. * all declarations required by an application developer to replace
  18. * the memory allocation.
  19. *
  20. * oneMKL supporting this feature only use the following
  21. * functions to allocate or free memory:
  22. * - malloc
  23. * - calloc
  24. * - realloc
  25. * - free
  26. * and call those functions via globally visible function pointers:
  27. * - i_malloc
  28. * - i_calloc
  29. * - i_realloc
  30. * - i_free
  31. *
  32. * C++ new and delete operators are changed to also use these function
  33. * pointers, if they occur in a library at all. No library supporting memory
  34. * allocation replacement will allocate memory before it is invoked explicitly
  35. * by the application for the first time.
  36. *
  37. * Therefore an application can safely set these function pointers
  38. * at the very beginning of its execution to some other replacement
  39. * functions. The function pointers must remain valid while
  40. * oneMKL is in use.
  41. *
  42. * Setting these pointers is optional because the copies contained in
  43. * oneMKL point to the standard C library functions by default.
  44. *
  45. * On Windows(R) data exported by a DLL and data contained in a static
  46. * library are accessed differently. To support mixing static
  47. * libraries and DLLs, the function pointers exist in two sets with
  48. * different names so that the application can override both sets in
  49. * the same source file without running into name conflicts.
  50. *
  51. * Here is an example:
  52. \verbatim
  53. #include <i_malloc.h>
  54. #include <my_malloc.h>
  55. int main( int argc, int argv )
  56. {
  57. // override normal pointers
  58. i_malloc = my_malloc;
  59. i_calloc = my_calloc;
  60. i_realloc = my_realloc;
  61. i_free = my_free;
  62. #ifdef _WIN32
  63. // also override pointers used by DLLs
  64. i_malloc_dll = my_malloc;
  65. i_calloc_dll = my_calloc;
  66. i_realloc_dll = my_realloc;
  67. i_free_dll = my_free;
  68. #endif
  69. }
  70. \endverbatim
  71. */
  72. #ifndef _I_MALLOC_H_
  73. #define _I_MALLOC_H_
  74. #include <stdlib.h> /* for size_t */
  75. #ifdef __cplusplus
  76. extern "C" {
  77. #endif
  78. /* typedefs for all four function pointers */
  79. typedef void * (* i_malloc_t)(size_t size);
  80. typedef void * (* i_calloc_t)(size_t nmemb, size_t size);
  81. typedef void * (* i_realloc_t)(void *ptr, size_t size);
  82. typedef void (* i_free_t)(void *ptr);
  83. #ifdef _WIN32
  84. # ifdef INTEL_DLL_EXPORTS
  85. # define INTEL_API_DEF __declspec(dllexport)
  86. # else
  87. # define INTEL_API_DEF __declspec(dllimport)
  88. #endif
  89. /* function pointers as used and exported by a DLL */
  90. extern INTEL_API_DEF i_malloc_t i_malloc_dll;
  91. extern INTEL_API_DEF i_calloc_t i_calloc_dll;
  92. extern INTEL_API_DEF i_realloc_t i_realloc_dll;
  93. extern INTEL_API_DEF i_free_t i_free_dll;
  94. #else /* _WIN32 */
  95. # define INTEL_API_DEF
  96. #endif /* _WIN32 */
  97. /* normal function pointers for static libraries */
  98. extern i_malloc_t i_malloc;
  99. extern i_calloc_t i_calloc;
  100. extern i_realloc_t i_realloc;
  101. extern i_free_t i_free;
  102. #ifdef __cplusplus
  103. }
  104. #endif
  105. #endif /* _I_MALLOC_H_ */