[libc] Deprecate LLVM_ENABLE_PROJECTS in favor of LLVM_ENABLE_RUNTIMES. (#117265)
[llvm-project.git] / libc / src / search / hcreate_r.cpp
blob17acd808c19a60eae2767bc941bfd8815bf95ad9
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 //===----------------------------------------------------------------------===//
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"
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;
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;
29 htab->__opaque = table;
30 return 1;
33 } // namespace LIBC_NAMESPACE_DECL