mirror of
https://github.com/microsoft/mimalloc.git
synced 2025-05-05 15:09:31 +03:00
use mi_is_power_of_two when possible (pr #118)
This commit is contained in:
parent
b104e434e4
commit
ce81af1119
2 changed files with 5 additions and 3 deletions
|
@ -167,10 +167,12 @@ static inline bool mi_mul_overflow(size_t count, size_t size, size_t* total) {
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
// Align upwards
|
// Is `x` a power of two?
|
||||||
static inline uintptr_t _mi_is_power_of_two(uintptr_t x) {
|
static inline bool _mi_is_power_of_two(uintptr_t x) {
|
||||||
return ((x & (x - 1)) == 0);
|
return ((x & (x - 1)) == 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Align upwards
|
||||||
static inline uintptr_t _mi_align_up(uintptr_t sz, size_t alignment) {
|
static inline uintptr_t _mi_align_up(uintptr_t sz, size_t alignment) {
|
||||||
uintptr_t mask = alignment - 1;
|
uintptr_t mask = alignment - 1;
|
||||||
if ((alignment & mask) == 0) { // power of two?
|
if ((alignment & mask) == 0) { // power of two?
|
||||||
|
|
|
@ -48,7 +48,7 @@ int mi_posix_memalign(void** p, size_t alignment, size_t size) mi_attr_noexcept
|
||||||
// <http://man7.org/linux/man-pages/man3/posix_memalign.3.html>
|
// <http://man7.org/linux/man-pages/man3/posix_memalign.3.html>
|
||||||
if (p == NULL) return EINVAL;
|
if (p == NULL) return EINVAL;
|
||||||
if (alignment % sizeof(void*) != 0) return EINVAL; // natural alignment
|
if (alignment % sizeof(void*) != 0) return EINVAL; // natural alignment
|
||||||
if ((alignment & (alignment - 1)) != 0) return EINVAL; // not a power of 2
|
if (!_mi_is_power_of_two(alignment)) return EINVAL; // not a power of 2
|
||||||
void* q = mi_malloc_aligned(size, alignment);
|
void* q = mi_malloc_aligned(size, alignment);
|
||||||
if (q==NULL && size != 0) return ENOMEM;
|
if (q==NULL && size != 0) return ENOMEM;
|
||||||
*p = q;
|
*p = q;
|
||||||
|
|
Loading…
Add table
Reference in a new issue