/src/cpython/Objects/mimalloc/alloc-posix.c
Line | Count | Source (jump to first uncovered line) |
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 | | // ------------------------------------------------------------------------ |
9 | | // mi prefixed publi definitions of various Posix, Unix, and C++ functions |
10 | | // for convenience and used when overriding these functions. |
11 | | // ------------------------------------------------------------------------ |
12 | | #include "mimalloc.h" |
13 | | #include "mimalloc/internal.h" |
14 | | |
15 | | // ------------------------------------------------------ |
16 | | // Posix & Unix functions definitions |
17 | | // ------------------------------------------------------ |
18 | | |
19 | | #include <errno.h> |
20 | | #include <string.h> // memset |
21 | | #include <stdlib.h> // getenv |
22 | | |
23 | | #ifdef _MSC_VER |
24 | | #pragma warning(disable:4996) // getenv _wgetenv |
25 | | #endif |
26 | | |
27 | | #ifndef EINVAL |
28 | | #define EINVAL 22 |
29 | | #endif |
30 | | #ifndef ENOMEM |
31 | | #define ENOMEM 12 |
32 | | #endif |
33 | | |
34 | | |
35 | 0 | mi_decl_nodiscard size_t mi_malloc_size(const void* p) mi_attr_noexcept { |
36 | | // if (!mi_is_in_heap_region(p)) return 0; |
37 | 0 | return mi_usable_size(p); |
38 | 0 | } |
39 | | |
40 | 0 | mi_decl_nodiscard size_t mi_malloc_usable_size(const void *p) mi_attr_noexcept { |
41 | | // if (!mi_is_in_heap_region(p)) return 0; |
42 | 0 | return mi_usable_size(p); |
43 | 0 | } |
44 | | |
45 | 0 | mi_decl_nodiscard size_t mi_malloc_good_size(size_t size) mi_attr_noexcept { |
46 | 0 | return mi_good_size(size); |
47 | 0 | } |
48 | | |
49 | 0 | void mi_cfree(void* p) mi_attr_noexcept { |
50 | 0 | if (mi_is_in_heap_region(p)) { |
51 | 0 | mi_free(p); |
52 | 0 | } |
53 | 0 | } |
54 | | |
55 | 0 | int mi_posix_memalign(void** p, size_t alignment, size_t size) mi_attr_noexcept { |
56 | | // Note: The spec dictates we should not modify `*p` on an error. (issue#27) |
57 | | // <http://man7.org/linux/man-pages/man3/posix_memalign.3.html> |
58 | 0 | if (p == NULL) return EINVAL; |
59 | 0 | if ((alignment % sizeof(void*)) != 0) return EINVAL; // natural alignment |
60 | | // it is also required that alignment is a power of 2 and > 0; this is checked in `mi_malloc_aligned` |
61 | 0 | if (alignment==0 || !_mi_is_power_of_two(alignment)) return EINVAL; // not a power of 2 |
62 | 0 | void* q = mi_malloc_aligned(size, alignment); |
63 | 0 | if (q==NULL && size != 0) return ENOMEM; |
64 | 0 | mi_assert_internal(((uintptr_t)q % alignment) == 0); |
65 | 0 | *p = q; |
66 | 0 | return 0; |
67 | 0 | } |
68 | | |
69 | 0 | mi_decl_nodiscard mi_decl_restrict void* mi_memalign(size_t alignment, size_t size) mi_attr_noexcept { |
70 | 0 | void* p = mi_malloc_aligned(size, alignment); |
71 | 0 | mi_assert_internal(((uintptr_t)p % alignment) == 0); |
72 | 0 | return p; |
73 | 0 | } |
74 | | |
75 | 0 | mi_decl_nodiscard mi_decl_restrict void* mi_valloc(size_t size) mi_attr_noexcept { |
76 | 0 | return mi_memalign( _mi_os_page_size(), size ); |
77 | 0 | } |
78 | | |
79 | 0 | mi_decl_nodiscard mi_decl_restrict void* mi_pvalloc(size_t size) mi_attr_noexcept { |
80 | 0 | size_t psize = _mi_os_page_size(); |
81 | 0 | if (size >= SIZE_MAX - psize) return NULL; // overflow |
82 | 0 | size_t asize = _mi_align_up(size, psize); |
83 | 0 | return mi_malloc_aligned(asize, psize); |
84 | 0 | } |
85 | | |
86 | 0 | mi_decl_nodiscard mi_decl_restrict void* mi_aligned_alloc(size_t alignment, size_t size) mi_attr_noexcept { |
87 | | // C11 requires the size to be an integral multiple of the alignment, see <https://en.cppreference.com/w/c/memory/aligned_alloc>. |
88 | | // unfortunately, it turns out quite some programs pass a size that is not an integral multiple so skip this check.. |
89 | | /* if mi_unlikely((size & (alignment - 1)) != 0) { // C11 requires alignment>0 && integral multiple, see <https://en.cppreference.com/w/c/memory/aligned_alloc> |
90 | | #if MI_DEBUG > 0 |
91 | | _mi_error_message(EOVERFLOW, "(mi_)aligned_alloc requires the size to be an integral multiple of the alignment (size %zu, alignment %zu)\n", size, alignment); |
92 | | #endif |
93 | | return NULL; |
94 | | } |
95 | | */ |
96 | | // C11 also requires alignment to be a power-of-two (and > 0) which is checked in mi_malloc_aligned |
97 | 0 | void* p = mi_malloc_aligned(size, alignment); |
98 | 0 | mi_assert_internal(((uintptr_t)p % alignment) == 0); |
99 | 0 | return p; |
100 | 0 | } |
101 | | |
102 | 0 | mi_decl_nodiscard void* mi_reallocarray( void* p, size_t count, size_t size ) mi_attr_noexcept { // BSD |
103 | 0 | void* newp = mi_reallocn(p,count,size); |
104 | 0 | if (newp==NULL) { errno = ENOMEM; } |
105 | 0 | return newp; |
106 | 0 | } |
107 | | |
108 | 0 | mi_decl_nodiscard int mi_reallocarr( void* p, size_t count, size_t size ) mi_attr_noexcept { // NetBSD |
109 | 0 | mi_assert(p != NULL); |
110 | 0 | if (p == NULL) { |
111 | 0 | errno = EINVAL; |
112 | 0 | return EINVAL; |
113 | 0 | } |
114 | 0 | void** op = (void**)p; |
115 | 0 | void* newp = mi_reallocarray(*op, count, size); |
116 | 0 | if mi_unlikely(newp == NULL) { return errno; } |
117 | 0 | *op = newp; |
118 | 0 | return 0; |
119 | 0 | } |
120 | | |
121 | 0 | void* mi__expand(void* p, size_t newsize) mi_attr_noexcept { // Microsoft |
122 | 0 | void* res = mi_expand(p, newsize); |
123 | 0 | if (res == NULL) { errno = ENOMEM; } |
124 | 0 | return res; |
125 | 0 | } |
126 | | |
127 | 0 | mi_decl_nodiscard mi_decl_restrict unsigned short* mi_wcsdup(const unsigned short* s) mi_attr_noexcept { |
128 | 0 | if (s==NULL) return NULL; |
129 | 0 | size_t len; |
130 | 0 | for(len = 0; s[len] != 0; len++) { } |
131 | 0 | size_t size = (len+1)*sizeof(unsigned short); |
132 | 0 | unsigned short* p = (unsigned short*)mi_malloc(size); |
133 | 0 | if (p != NULL) { |
134 | 0 | _mi_memcpy(p,s,size); |
135 | 0 | } |
136 | 0 | return p; |
137 | 0 | } |
138 | | |
139 | 0 | mi_decl_nodiscard mi_decl_restrict unsigned char* mi_mbsdup(const unsigned char* s) mi_attr_noexcept { |
140 | 0 | return (unsigned char*)mi_strdup((const char*)s); |
141 | 0 | } |
142 | | |
143 | 0 | int mi_dupenv_s(char** buf, size_t* size, const char* name) mi_attr_noexcept { |
144 | 0 | if (buf==NULL || name==NULL) return EINVAL; |
145 | 0 | if (size != NULL) *size = 0; |
146 | 0 | char* p = getenv(name); // mscver warning 4996 |
147 | 0 | if (p==NULL) { |
148 | 0 | *buf = NULL; |
149 | 0 | } |
150 | 0 | else { |
151 | 0 | *buf = mi_strdup(p); |
152 | 0 | if (*buf==NULL) return ENOMEM; |
153 | 0 | if (size != NULL) *size = _mi_strlen(p); |
154 | 0 | } |
155 | 0 | return 0; |
156 | 0 | } |
157 | | |
158 | 0 | int mi_wdupenv_s(unsigned short** buf, size_t* size, const unsigned short* name) mi_attr_noexcept { |
159 | 0 | if (buf==NULL || name==NULL) return EINVAL; |
160 | 0 | if (size != NULL) *size = 0; |
161 | 0 | #if !defined(_WIN32) || (defined(WINAPI_FAMILY) && (WINAPI_FAMILY != WINAPI_FAMILY_DESKTOP_APP)) |
162 | | // not supported |
163 | 0 | *buf = NULL; |
164 | 0 | return EINVAL; |
165 | | #else |
166 | | unsigned short* p = (unsigned short*)_wgetenv((const wchar_t*)name); // msvc warning 4996 |
167 | | if (p==NULL) { |
168 | | *buf = NULL; |
169 | | } |
170 | | else { |
171 | | *buf = mi_wcsdup(p); |
172 | | if (*buf==NULL) return ENOMEM; |
173 | | if (size != NULL) *size = wcslen((const wchar_t*)p); |
174 | | } |
175 | | return 0; |
176 | | #endif |
177 | 0 | } |
178 | | |
179 | 0 | mi_decl_nodiscard void* mi_aligned_offset_recalloc(void* p, size_t newcount, size_t size, size_t alignment, size_t offset) mi_attr_noexcept { // Microsoft |
180 | 0 | return mi_recalloc_aligned_at(p, newcount, size, alignment, offset); |
181 | 0 | } |
182 | | |
183 | 0 | mi_decl_nodiscard void* mi_aligned_recalloc(void* p, size_t newcount, size_t size, size_t alignment) mi_attr_noexcept { // Microsoft |
184 | 0 | return mi_recalloc_aligned(p, newcount, size, alignment); |
185 | 0 | } |