2019-07-07 23:44:33 +03:00
|
|
|
/* ----------------------------------------------------------------------------
|
|
|
|
Copyright (c) 2018,2019, Microsoft Research, Daan Leijen
|
|
|
|
This is free software; you can redistribute it and/or modify it under the
|
|
|
|
terms of the MIT license. A copy of the license can be found in the file
|
|
|
|
"LICENSE" at the root of this distribution.
|
|
|
|
-----------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------
|
|
|
|
// mi prefixed publi definitions of various Posix, Unix, and C++ functions
|
|
|
|
// for convenience and used when overriding these functions.
|
|
|
|
// ------------------------------------------------------------------------
|
|
|
|
#include "mimalloc.h"
|
|
|
|
#include "mimalloc-internal.h"
|
|
|
|
|
|
|
|
// ------------------------------------------------------
|
|
|
|
// Posix & Unix functions definitions
|
|
|
|
// ------------------------------------------------------
|
|
|
|
|
|
|
|
#include <errno.h>
|
2019-07-19 07:12:40 +03:00
|
|
|
#include <string.h> // memcpy
|
2019-07-22 20:10:45 +03:00
|
|
|
#include <stdlib.h> // getenv
|
2019-07-07 23:44:33 +03:00
|
|
|
|
2020-09-04 23:06:18 +03:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning(disable:4996) // getenv _wgetenv
|
|
|
|
#endif
|
|
|
|
|
2019-07-07 23:44:33 +03:00
|
|
|
#ifndef EINVAL
|
|
|
|
#define EINVAL 22
|
|
|
|
#endif
|
|
|
|
#ifndef ENOMEM
|
|
|
|
#define ENOMEM 12
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2019-07-08 22:00:59 +03:00
|
|
|
size_t mi_malloc_size(const void* p) mi_attr_noexcept {
|
2019-07-07 23:44:33 +03:00
|
|
|
return mi_usable_size(p);
|
|
|
|
}
|
|
|
|
|
2019-07-08 22:00:59 +03:00
|
|
|
size_t mi_malloc_usable_size(const void *p) mi_attr_noexcept {
|
2019-07-07 23:44:33 +03:00
|
|
|
return mi_usable_size(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
void mi_cfree(void* p) mi_attr_noexcept {
|
2019-07-24 01:54:47 +03:00
|
|
|
if (mi_is_in_heap_region(p)) {
|
|
|
|
mi_free(p);
|
|
|
|
}
|
2019-07-07 23:44:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int mi_posix_memalign(void** p, size_t alignment, size_t size) mi_attr_noexcept {
|
|
|
|
// Note: The spec dictates we should not modify `*p` on an error. (issue#27)
|
|
|
|
// <http://man7.org/linux/man-pages/man3/posix_memalign.3.html>
|
|
|
|
if (p == NULL) return EINVAL;
|
2020-02-01 00:20:02 +03:00
|
|
|
if (alignment % sizeof(void*) != 0) return EINVAL; // natural alignment
|
2019-09-09 18:12:50 +03:00
|
|
|
if (!_mi_is_power_of_two(alignment)) return EINVAL; // not a power of 2
|
2020-02-17 20:59:11 +03:00
|
|
|
void* q = (mi_malloc_satisfies_alignment(alignment, size) ? mi_malloc(size) : mi_malloc_aligned(size, alignment));
|
2019-07-07 23:44:33 +03:00
|
|
|
if (q==NULL && size != 0) return ENOMEM;
|
2020-02-01 00:20:02 +03:00
|
|
|
mi_assert_internal(((uintptr_t)q % alignment) == 0);
|
2019-07-07 23:44:33 +03:00
|
|
|
*p = q;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-02-13 21:36:39 +03:00
|
|
|
mi_decl_restrict void* mi_memalign(size_t alignment, size_t size) mi_attr_noexcept {
|
2020-02-17 20:59:11 +03:00
|
|
|
void* p = (mi_malloc_satisfies_alignment(alignment,size) ? mi_malloc(size) : mi_malloc_aligned(size, alignment));
|
2020-02-01 00:20:02 +03:00
|
|
|
mi_assert_internal(((uintptr_t)p % alignment) == 0);
|
|
|
|
return p;
|
2019-07-07 23:44:33 +03:00
|
|
|
}
|
|
|
|
|
2020-02-13 21:36:39 +03:00
|
|
|
mi_decl_restrict void* mi_valloc(size_t size) mi_attr_noexcept {
|
2020-02-17 20:59:11 +03:00
|
|
|
return mi_memalign( _mi_os_page_size(), size );
|
2019-07-07 23:44:33 +03:00
|
|
|
}
|
|
|
|
|
2020-02-13 21:36:39 +03:00
|
|
|
mi_decl_restrict void* mi_pvalloc(size_t size) mi_attr_noexcept {
|
2019-07-07 23:44:33 +03:00
|
|
|
size_t psize = _mi_os_page_size();
|
|
|
|
if (size >= SIZE_MAX - psize) return NULL; // overflow
|
2020-02-17 20:59:11 +03:00
|
|
|
size_t asize = _mi_align_up(size, psize);
|
2019-07-07 23:44:33 +03:00
|
|
|
return mi_malloc_aligned(asize, psize);
|
|
|
|
}
|
|
|
|
|
2020-02-13 21:36:39 +03:00
|
|
|
mi_decl_restrict void* mi_aligned_alloc(size_t alignment, size_t size) mi_attr_noexcept {
|
2019-09-12 03:49:28 +03:00
|
|
|
if (alignment==0 || !_mi_is_power_of_two(alignment)) return NULL;
|
|
|
|
if ((size&(alignment-1)) != 0) return NULL; // C11 requires integral multiple, see <https://en.cppreference.com/w/c/memory/aligned_alloc>
|
2020-02-17 20:59:11 +03:00
|
|
|
void* p = (mi_malloc_satisfies_alignment(alignment, size) ? mi_malloc(size) : mi_malloc_aligned(size, alignment));
|
2020-02-01 00:20:02 +03:00
|
|
|
mi_assert_internal(((uintptr_t)p % alignment) == 0);
|
|
|
|
return p;
|
2019-07-07 23:44:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void* mi_reallocarray( void* p, size_t count, size_t size ) mi_attr_noexcept { // BSD
|
|
|
|
void* newp = mi_reallocn(p,count,size);
|
|
|
|
if (newp==NULL) errno = ENOMEM;
|
|
|
|
return newp;
|
|
|
|
}
|
|
|
|
|
2019-07-19 04:59:32 +03:00
|
|
|
void* mi__expand(void* p, size_t newsize) mi_attr_noexcept { // Microsoft
|
|
|
|
void* res = mi_expand(p, newsize);
|
|
|
|
if (res == NULL) errno = ENOMEM;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2020-02-13 21:36:39 +03:00
|
|
|
mi_decl_restrict unsigned short* mi_wcsdup(const unsigned short* s) mi_attr_noexcept {
|
2019-07-19 07:12:40 +03:00
|
|
|
if (s==NULL) return NULL;
|
|
|
|
size_t len;
|
|
|
|
for(len = 0; s[len] != 0; len++) { }
|
|
|
|
size_t size = (len+1)*sizeof(unsigned short);
|
|
|
|
unsigned short* p = (unsigned short*)mi_malloc(size);
|
|
|
|
if (p != NULL) {
|
2020-05-27 02:04:28 +03:00
|
|
|
_mi_memcpy(p,s,size);
|
2019-07-19 07:12:40 +03:00
|
|
|
}
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2020-02-13 21:36:39 +03:00
|
|
|
mi_decl_restrict unsigned char* mi_mbsdup(const unsigned char* s) mi_attr_noexcept {
|
2019-07-19 07:12:40 +03:00
|
|
|
return (unsigned char*)mi_strdup((const char*)s);
|
|
|
|
}
|
|
|
|
|
|
|
|
int mi_dupenv_s(char** buf, size_t* size, const char* name) mi_attr_noexcept {
|
|
|
|
if (buf==NULL || name==NULL) return EINVAL;
|
|
|
|
if (size != NULL) *size = 0;
|
2020-09-04 23:06:18 +03:00
|
|
|
char* p = getenv(name); // mscver warning 4996
|
2019-07-19 07:12:40 +03:00
|
|
|
if (p==NULL) {
|
2019-07-22 20:10:45 +03:00
|
|
|
*buf = NULL;
|
2019-07-19 07:12:40 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
*buf = mi_strdup(p);
|
|
|
|
if (*buf==NULL) return ENOMEM;
|
|
|
|
if (size != NULL) *size = strlen(p);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int mi_wdupenv_s(unsigned short** buf, size_t* size, const unsigned short* name) mi_attr_noexcept {
|
|
|
|
if (buf==NULL || name==NULL) return EINVAL;
|
|
|
|
if (size != NULL) *size = 0;
|
|
|
|
#if !defined(_WIN32) || (defined(WINAPI_FAMILY) && (WINAPI_FAMILY != WINAPI_FAMILY_DESKTOP_APP))
|
|
|
|
// not supported
|
|
|
|
*buf = NULL;
|
|
|
|
return EINVAL;
|
|
|
|
#else
|
2020-09-04 23:06:18 +03:00
|
|
|
unsigned short* p = (unsigned short*)_wgetenv((const wchar_t*)name); // msvc warning 4996
|
2019-07-19 07:12:40 +03:00
|
|
|
if (p==NULL) {
|
|
|
|
*buf = NULL;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
*buf = mi_wcsdup(p);
|
|
|
|
if (*buf==NULL) return ENOMEM;
|
|
|
|
if (size != NULL) *size = wcslen((const wchar_t*)p);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
}
|
2019-09-03 20:57:39 +03:00
|
|
|
|
|
|
|
void* mi_aligned_offset_recalloc(void* p, size_t newcount, size_t size, size_t alignment, size_t offset) mi_attr_noexcept { // Microsoft
|
|
|
|
return mi_recalloc_aligned_at(p, newcount, size, alignment, offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
void* mi_aligned_recalloc(void* p, size_t newcount, size_t size, size_t alignment) mi_attr_noexcept { // Microsoft
|
|
|
|
return mi_recalloc_aligned(p, newcount, size, alignment);
|
|
|
|
}
|