xref: /llvm-project/libc/src/stdlib/gpu/realloc.cpp (revision bf42a7860a208439a0a29f5dcc7a33949c484efb)
1*bf42a786SJoseph Huber //===-- GPU Implementation of realloc -------------------------------------===//
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/realloc.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 #include "src/string/memory_utils/inline_memcpy.h"
15*bf42a786SJoseph Huber 
16*bf42a786SJoseph Huber namespace LIBC_NAMESPACE_DECL {
17*bf42a786SJoseph Huber 
18*bf42a786SJoseph Huber LLVM_LIBC_FUNCTION(void *, realloc, (void *ptr, size_t size)) {
19*bf42a786SJoseph Huber   if (ptr == nullptr)
20*bf42a786SJoseph Huber     return gpu::allocate(size);
21*bf42a786SJoseph Huber 
22*bf42a786SJoseph Huber   void *newmem = gpu::allocate(size);
23*bf42a786SJoseph Huber   if (newmem == nullptr)
24*bf42a786SJoseph Huber     return nullptr;
25*bf42a786SJoseph Huber 
26*bf42a786SJoseph Huber   // This will copy garbage if it goes beyond the old allocation size.
27*bf42a786SJoseph Huber   inline_memcpy(newmem, ptr, size);
28*bf42a786SJoseph Huber   gpu::deallocate(ptr);
29*bf42a786SJoseph Huber   return newmem;
30*bf42a786SJoseph Huber }
31*bf42a786SJoseph Huber 
32*bf42a786SJoseph Huber } // namespace LIBC_NAMESPACE_DECL
33