xref: /aosp_15_r20/external/llvm-libc/src/search/hcreate_r.cpp (revision 71db0c75aadcf003ffe3238005f61d7618a3fead)
1 //===-- Implementation of hcreate_r -----------------------------*- C++ -*-===//
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/search/hcreate_r.h"
10 #include "src/__support/HashTable/randomness.h"
11 #include "src/__support/HashTable/table.h"
12 #include "src/__support/macros/config.h"
13 #include "src/errno/libc_errno.h"
14 
15 namespace LIBC_NAMESPACE_DECL {
16 LLVM_LIBC_FUNCTION(int, hcreate_r,
17                    (size_t capacity, struct hsearch_data *htab)) {
18   if (htab == nullptr) {
19     libc_errno = EINVAL;
20     return 0;
21   }
22   uint64_t randomness = internal::randomness::next_random_seed();
23   internal::HashTable *table =
24       internal::HashTable::allocate(capacity, randomness);
25   if (table == nullptr) {
26     libc_errno = ENOMEM;
27     return 0;
28   }
29   htab->__opaque = table;
30   return 1;
31 }
32 
33 } // namespace LIBC_NAMESPACE_DECL
34