alloc-posix.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /* ----------------------------------------------------------------------------
  2. Copyright (c) 2018-2021, Microsoft Research, Daan Leijen
  3. This is free software; you can redistribute it and/or modify it under the
  4. terms of the MIT license. A copy of the license can be found in the file
  5. "LICENSE" at the root of this distribution.
  6. -----------------------------------------------------------------------------*/
  7. // ------------------------------------------------------------------------
  8. // mi prefixed publi definitions of various Posix, Unix, and C++ functions
  9. // for convenience and used when overriding these functions.
  10. // ------------------------------------------------------------------------
  11. #include "mimalloc.h"
  12. #include "mimalloc-internal.h"
  13. // ------------------------------------------------------
  14. // Posix & Unix functions definitions
  15. // ------------------------------------------------------
  16. #include <errno.h>
  17. #include <string.h> // memset
  18. #include <stdlib.h> // getenv
  19. #ifdef _MSC_VER
  20. #pragma warning(disable:4996) // getenv _wgetenv
  21. #endif
  22. #ifndef EINVAL
  23. #define EINVAL 22
  24. #endif
  25. #ifndef ENOMEM
  26. #define ENOMEM 12
  27. #endif
  28. size_t mi_malloc_size(const void* p) mi_attr_noexcept {
  29. return mi_usable_size(p);
  30. }
  31. size_t mi_malloc_usable_size(const void *p) mi_attr_noexcept {
  32. return mi_usable_size(p);
  33. }
  34. void mi_cfree(void* p) mi_attr_noexcept {
  35. if (mi_is_in_heap_region(p)) {
  36. mi_free(p);
  37. }
  38. }
  39. int mi_posix_memalign(void** p, size_t alignment, size_t size) mi_attr_noexcept {
  40. // Note: The spec dictates we should not modify `*p` on an error. (issue#27)
  41. // <http://man7.org/linux/man-pages/man3/posix_memalign.3.html>
  42. if (p == NULL) return EINVAL;
  43. if (alignment % sizeof(void*) != 0) return EINVAL; // natural alignment
  44. if (!_mi_is_power_of_two(alignment)) return EINVAL; // not a power of 2
  45. void* q = (mi_malloc_satisfies_alignment(alignment, size) ? mi_malloc(size) : mi_malloc_aligned(size, alignment));
  46. if (q==NULL && size != 0) return ENOMEM;
  47. mi_assert_internal(((uintptr_t)q % alignment) == 0);
  48. *p = q;
  49. return 0;
  50. }
  51. mi_decl_restrict void* mi_memalign(size_t alignment, size_t size) mi_attr_noexcept {
  52. void* p = (mi_malloc_satisfies_alignment(alignment,size) ? mi_malloc(size) : mi_malloc_aligned(size, alignment));
  53. mi_assert_internal(((uintptr_t)p % alignment) == 0);
  54. return p;
  55. }
  56. mi_decl_restrict void* mi_valloc(size_t size) mi_attr_noexcept {
  57. return mi_memalign( _mi_os_page_size(), size );
  58. }
  59. mi_decl_restrict void* mi_pvalloc(size_t size) mi_attr_noexcept {
  60. size_t psize = _mi_os_page_size();
  61. if (size >= SIZE_MAX - psize) return NULL; // overflow
  62. size_t asize = _mi_align_up(size, psize);
  63. return mi_malloc_aligned(asize, psize);
  64. }
  65. mi_decl_restrict void* mi_aligned_alloc(size_t alignment, size_t size) mi_attr_noexcept {
  66. if (alignment==0 || !_mi_is_power_of_two(alignment)) return NULL;
  67. if ((size&(alignment-1)) != 0) return NULL; // C11 requires integral multiple, see <https://en.cppreference.com/w/c/memory/aligned_alloc>
  68. void* p = (mi_malloc_satisfies_alignment(alignment, size) ? mi_malloc(size) : mi_malloc_aligned(size, alignment));
  69. mi_assert_internal(((uintptr_t)p % alignment) == 0);
  70. return p;
  71. }
  72. void* mi_reallocarray( void* p, size_t count, size_t size ) mi_attr_noexcept { // BSD
  73. void* newp = mi_reallocn(p,count,size);
  74. if (newp==NULL) errno = ENOMEM;
  75. return newp;
  76. }
  77. void* mi__expand(void* p, size_t newsize) mi_attr_noexcept { // Microsoft
  78. void* res = mi_expand(p, newsize);
  79. if (res == NULL) errno = ENOMEM;
  80. return res;
  81. }
  82. mi_decl_restrict unsigned short* mi_wcsdup(const unsigned short* s) mi_attr_noexcept {
  83. if (s==NULL) return NULL;
  84. size_t len;
  85. for(len = 0; s[len] != 0; len++) { }
  86. size_t size = (len+1)*sizeof(unsigned short);
  87. unsigned short* p = (unsigned short*)mi_malloc(size);
  88. if (p != NULL) {
  89. _mi_memcpy(p,s,size);
  90. }
  91. return p;
  92. }
  93. mi_decl_restrict unsigned char* mi_mbsdup(const unsigned char* s) mi_attr_noexcept {
  94. return (unsigned char*)mi_strdup((const char*)s);
  95. }
  96. int mi_dupenv_s(char** buf, size_t* size, const char* name) mi_attr_noexcept {
  97. if (buf==NULL || name==NULL) return EINVAL;
  98. if (size != NULL) *size = 0;
  99. char* p = getenv(name); // mscver warning 4996
  100. if (p==NULL) {
  101. *buf = NULL;
  102. }
  103. else {
  104. *buf = mi_strdup(p);
  105. if (*buf==NULL) return ENOMEM;
  106. if (size != NULL) *size = strlen(p);
  107. }
  108. return 0;
  109. }
  110. int mi_wdupenv_s(unsigned short** buf, size_t* size, const unsigned short* name) mi_attr_noexcept {
  111. if (buf==NULL || name==NULL) return EINVAL;
  112. if (size != NULL) *size = 0;
  113. #if !defined(_WIN32) || (defined(WINAPI_FAMILY) && (WINAPI_FAMILY != WINAPI_FAMILY_DESKTOP_APP))
  114. // not supported
  115. *buf = NULL;
  116. return EINVAL;
  117. #else
  118. unsigned short* p = (unsigned short*)_wgetenv((const wchar_t*)name); // msvc warning 4996
  119. if (p==NULL) {
  120. *buf = NULL;
  121. }
  122. else {
  123. *buf = mi_wcsdup(p);
  124. if (*buf==NULL) return ENOMEM;
  125. if (size != NULL) *size = wcslen((const wchar_t*)p);
  126. }
  127. return 0;
  128. #endif
  129. }
  130. void* mi_aligned_offset_recalloc(void* p, size_t newcount, size_t size, size_t alignment, size_t offset) mi_attr_noexcept { // Microsoft
  131. return mi_recalloc_aligned_at(p, newcount, size, alignment, offset);
  132. }
  133. void* mi_aligned_recalloc(void* p, size_t newcount, size_t size, size_t alignment) mi_attr_noexcept { // Microsoft
  134. return mi_recalloc_aligned(p, newcount, size, alignment);
  135. }