1 //===-- Implementation of hsearch -------------------------------*- 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/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" 15 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); 24 } 25 26 // In rare cases, the global hashtable may still fail to allocate. We treat it 27 // as ESRCH or ENOMEM depending on the action. 28 switch (action) { 29 case FIND: 30 result = internal::global_hash_table 31 ? internal::global_hash_table->find(item.key) 32 : nullptr; 33 if (result == nullptr) { 34 libc_errno = ESRCH; 35 } 36 break; 37 case ENTER: 38 result = 39 internal::global_hash_table 40 ? internal::HashTable::insert(internal::global_hash_table, item) 41 : nullptr; 42 if (result == nullptr) { 43 libc_errno = ENOMEM; 44 } 45 break; 46 } 47 return result; 48 } 49 50 } // namespace LIBC_NAMESPACE_DECL 51