Path: blob/main_old/src/common/aligned_memory.cpp
1693 views
//1// Copyright 2017 The ANGLE Project Authors. All rights reserved.2// Use of this source code is governed by a BSD-style license that can be3// found in the LICENSE file.4//5// aligned_memory: An aligned memory allocator. Based on Chrome's base/memory/aligned_memory.6//78#include "common/aligned_memory.h"910#include "common/debug.h"11#include "common/platform.h"1213#if defined(COMPILER_MSVC)14# include <malloc.h>15#else16# include <stdlib.h>17#endif1819namespace angle20{2122void *AlignedAlloc(size_t size, size_t alignment)23{24ASSERT(size > 0);25ASSERT((alignment & (alignment - 1)) == 0);26ASSERT((alignment % sizeof(void *)) == 0);27void *ptr = nullptr;28#if defined(ANGLE_PLATFORM_WINDOWS)29ptr = _aligned_malloc(size, alignment);30// Android technically supports posix_memalign(), but does not expose it in31// the current version of the library headers used by Chrome. Luckily,32// memalign() on Android returns pointers which can safely be used with33// free(), so we can use it instead. Issue filed to document this:34// http://code.google.com/p/android/issues/detail?id=3539135#elif defined(ANGLE_PLATFORM_ANDROID)36ptr = memalign(alignment, size);37#else38if (posix_memalign(&ptr, alignment, size))39ptr = nullptr;40#endif41// Since aligned allocations may fail for non-memory related reasons, force a42// crash if we encounter a failed allocation.43if (!ptr)44{45ERR() << "If you crashed here, your aligned allocation is incorrect: "46<< "size=" << size << ", alignment=" << alignment;47ASSERT(false);48}49// Confidence check alignment just to be safe.50ASSERT((reinterpret_cast<uintptr_t>(ptr) & (alignment - 1)) == 0);51return ptr;52}5354void AlignedFree(void *ptr)55{56#if defined(ANGLE_PLATFORM_WINDOWS)57_aligned_free(ptr);58#else59free(ptr);60#endif61}6263} // namespace angle646566