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