xref: /llvm-project/libc/src/stdlib/gpu/calloc.cpp (revision bf42a7860a208439a0a29f5dcc7a33949c484efb)
1*bf42a786SJoseph Huber //===-- GPU Implementation of calloc --------------------------------------===//
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/calloc.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_memset.h"
15*bf42a786SJoseph Huber 
16*bf42a786SJoseph Huber namespace LIBC_NAMESPACE_DECL {
17*bf42a786SJoseph Huber 
18*bf42a786SJoseph Huber LLVM_LIBC_FUNCTION(void *, calloc, (size_t num, size_t size)) {
19*bf42a786SJoseph Huber   size_t bytes = num * size;
20*bf42a786SJoseph Huber   if (bytes == 0)
21*bf42a786SJoseph Huber     return nullptr;
22*bf42a786SJoseph Huber 
23*bf42a786SJoseph Huber   void *ptr = gpu::allocate(bytes);
24*bf42a786SJoseph Huber   if (!ptr)
25*bf42a786SJoseph Huber     return nullptr;
26*bf42a786SJoseph Huber 
27*bf42a786SJoseph Huber   inline_memset(ptr, 0, bytes);
28*bf42a786SJoseph Huber   return ptr;
29*bf42a786SJoseph Huber }
30*bf42a786SJoseph Huber 
31*bf42a786SJoseph Huber } // namespace LIBC_NAMESPACE_DECL
32