xref: /aosp_15_r20/external/llvm-libc/src/stdlib/gpu/aligned_alloc.cpp (revision 71db0c75aadcf003ffe3238005f61d7618a3fead)
1 //===-- GPU Implementation of aligned_alloc -------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "src/stdlib/aligned_alloc.h"
10 
11 #include "src/__support/GPU/allocator.h"
12 #include "src/__support/common.h"
13 #include "src/__support/macros/config.h"
14 
15 namespace LIBC_NAMESPACE_DECL {
16 
17 LLVM_LIBC_FUNCTION(void *, aligned_alloc, (size_t alignment, size_t size)) {
18   if ((alignment & -alignment) != alignment)
19     return nullptr;
20 
21   void *ptr = gpu::allocate(size);
22   if ((reinterpret_cast<uintptr_t>(ptr) & (alignment - 1)) != 0) {
23     gpu::deallocate(ptr);
24     return nullptr;
25   }
26   return ptr;
27 }
28 
29 } // namespace LIBC_NAMESPACE_DECL
30