5#ifndef V8_BASE_PLATFORM_MEMORY_H_
6#define V8_BASE_PLATFORM_MEMORY_H_
17#include "starboard/memory.h"
21#include <malloc/malloc.h>
23#include <sys/malloc.h>
30#if (V8_OS_POSIX && !V8_OS_AIX && !V8_OS_SOLARIS && !V8_OS_ZOS && !V8_OS_OPENBSD) || V8_OS_WIN
31#define V8_HAS_MALLOC_USABLE_SIZE 1
38 return SbMemoryAllocate(size);
39#elif V8_OS_AIX && _LINUX_SOURCE_COMPAT
42 return __linux_malloc(size);
48inline void*
Realloc(
void* memory,
size_t size) {
53 return SbMemoryReallocate(memory, size);
54#elif V8_OS_AIX && _LINUX_SOURCE_COMPAT
57 return __linux_realloc(memory, size);
59 return realloc(memory, size);
63inline void Free(
void* memory) {
65 return SbMemoryDeallocate(memory);
71inline void*
Calloc(
size_t count,
size_t size) {
73 return SbMemoryCalloc(count, size);
74#elif V8_OS_AIX && _LINUX_SOURCE_COMPAT
76 return __linux_calloc(count, size);
78 return calloc(count, size);
88 return _aligned_malloc(size, alignment);
92 return memalign(alignment, size);
94 return __aligned_malloc(size, alignment);
97 if (posix_memalign(&ptr, alignment, size)) ptr =
nullptr;
114#if V8_HAS_MALLOC_USABLE_SIZE
119inline size_t MallocUsableSize(
void* ptr) {
125 return malloc_size(ptr);
127 return malloc_usable_size(ptr);
134template <
class Po
inter>
145 const size_t min_wanted_size = n *
sizeof(
T);
146 auto* memory =
static_cast<T*
>(
Malloc(min_wanted_size));
147#if !V8_HAS_MALLOC_USABLE_SIZE
148 return {memory, min_wanted_size};
150 const size_t usable_size = MallocUsableSize(memory);
151#if V8_USE_UNDEFINED_BEHAVIOR_SANITIZER
152 if (memory ==
nullptr)
158 if (usable_size != min_wanted_size) {
159 memory =
static_cast<T*
>(
Realloc(memory, usable_size));
162 return {memory, usable_size};
168#undef V8_HAS_MALLOC_USABLE_SIZE
constexpr bool IsPowerOfTwo(T value)
V8_NODISCARD AllocationResult< T * > AllocateAtLeast(size_t n)
void * Realloc(void *memory, size_t size)
void * Calloc(size_t count, size_t size)
void * Malloc(size_t size)
void AlignedFree(void *ptr)
void * AlignedAlloc(size_t size, size_t alignment)
#define DCHECK_LE(v1, v2)
#define CHECK_NE(lhs, rhs)
#define DCHECK(condition)