mimalloc-override.h 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
  2. /* ----------------------------------------------------------------------------
  3. Copyright (c) 2018-2020 Microsoft Research, Daan Leijen
  4. This is free software; you can redistribute it and/or modify it under the
  5. terms of the MIT license. A copy of the license can be found in the file
  6. "LICENSE" at the root of this distribution.
  7. -----------------------------------------------------------------------------*/
  8. #pragma once
  9. #ifndef MIMALLOC_OVERRIDE_H
  10. #define MIMALLOC_OVERRIDE_H
  11. /* ----------------------------------------------------------------------------
  12. This header can be used to statically redirect malloc/free and new/delete
  13. to the mimalloc variants. This can be useful if one can include this file on
  14. each source file in a project (but be careful when using external code to
  15. not accidentally mix pointers from different allocators).
  16. -----------------------------------------------------------------------------*/
  17. #include <mimalloc.h>
  18. // Standard C allocation
  19. #define malloc(n) mi_malloc(n)
  20. #define calloc(n,c) mi_calloc(n,c)
  21. #define realloc(p,n) mi_realloc(p,n)
  22. #define free(p) mi_free(p)
  23. #define strdup(s) mi_strdup(s)
  24. #define strndup(s,n) mi_strndup(s,n)
  25. #define realpath(f,n) mi_realpath(f,n)
  26. // Microsoft extensions
  27. #define _expand(p,n) mi_expand(p,n)
  28. #define _msize(p) mi_usable_size(p)
  29. #define _recalloc(p,n,c) mi_recalloc(p,n,c)
  30. #define _strdup(s) mi_strdup(s)
  31. #define _strndup(s,n) mi_strndup(s,n)
  32. #define _wcsdup(s) (wchar_t*)mi_wcsdup((const unsigned short*)(s))
  33. #define _mbsdup(s) mi_mbsdup(s)
  34. #define _dupenv_s(b,n,v) mi_dupenv_s(b,n,v)
  35. #define _wdupenv_s(b,n,v) mi_wdupenv_s((unsigned short*)(b),n,(const unsigned short*)(v))
  36. // Various Posix and Unix variants
  37. #define reallocf(p,n) mi_reallocf(p,n)
  38. #define malloc_size(p) mi_usable_size(p)
  39. #define malloc_usable_size(p) mi_usable_size(p)
  40. #define malloc_good_size(sz) mi_malloc_good_size(sz)
  41. #define cfree(p) mi_free(p)
  42. #define valloc(n) mi_valloc(n)
  43. #define pvalloc(n) mi_pvalloc(n)
  44. #define reallocarray(p,s,n) mi_reallocarray(p,s,n)
  45. #define reallocarr(p,s,n) mi_reallocarr(p,s,n)
  46. #define memalign(a,n) mi_memalign(a,n)
  47. #define aligned_alloc(a,n) mi_aligned_alloc(a,n)
  48. #define posix_memalign(p,a,n) mi_posix_memalign(p,a,n)
  49. #define _posix_memalign(p,a,n) mi_posix_memalign(p,a,n)
  50. // Microsoft aligned variants
  51. #define _aligned_malloc(n,a) mi_malloc_aligned(n,a)
  52. #define _aligned_realloc(p,n,a) mi_realloc_aligned(p,n,a)
  53. #define _aligned_recalloc(p,s,n,a) mi_aligned_recalloc(p,s,n,a)
  54. #define _aligned_msize(p,a,o) mi_usable_size(p)
  55. #define _aligned_free(p) mi_free(p)
  56. #define _aligned_offset_malloc(n,a,o) mi_malloc_aligned_at(n,a,o)
  57. #define _aligned_offset_realloc(p,n,a,o) mi_realloc_aligned_at(p,n,a,o)
  58. #define _aligned_offset_recalloc(p,s,n,a,o) mi_recalloc_aligned_at(p,s,n,a,o)
  59. #endif // MIMALLOC_OVERRIDE_H
  60. #else
  61. #error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
  62. #endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)