1 //===-- Implementation of hsearch -------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #include "src/search/hsearch.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 #include "src/search/hsearch/global.h"
16 namespace LIBC_NAMESPACE_DECL
{
17 LLVM_LIBC_FUNCTION(ENTRY
*, hsearch
, (ENTRY item
, ACTION action
)) {
18 ENTRY
*result
= nullptr;
19 if (internal::global_hash_table
== nullptr) {
20 // If global_hash_table is null, we create a new hash table with a minimal
21 // capacity. Such hashtable will be expanded as needed.
22 uint64_t randomness
= internal::randomness::next_random_seed();
23 internal::global_hash_table
= internal::HashTable::allocate(0, randomness
);
26 // In rare cases, the global hashtable may still fail to allocate. We treat it
27 // as ESRCH or ENOMEM depending on the action.
30 result
= internal::global_hash_table
31 ? internal::global_hash_table
->find(item
.key
)
33 if (result
== nullptr) {
39 internal::global_hash_table
40 ? internal::HashTable::insert(internal::global_hash_table
, item
)
42 if (result
== nullptr) {
50 } // namespace LIBC_NAMESPACE_DECL