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