xref: /aosp_15_r20/external/llvm-libc/src/search/hsearch_r.cpp (revision 71db0c75aadcf003ffe3238005f61d7618a3fead)
1 //===-- Implementation of hsearch_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/hsearch_r.h"
10 #include "src/__support/HashTable/table.h"
11 #include "src/__support/macros/config.h"
12 #include "src/errno/libc_errno.h"
13 
14 namespace LIBC_NAMESPACE_DECL {
15 LLVM_LIBC_FUNCTION(int, hsearch_r,
16                    (ENTRY item, ACTION action, ENTRY **retval,
17                     struct hsearch_data *htab)) {
18   if (htab == nullptr) {
19     libc_errno = EINVAL;
20     return 0;
21   }
22   internal::HashTable *table =
23       static_cast<internal::HashTable *>(htab->__opaque);
24   switch (action) {
25   case FIND:
26     *retval = table->find(item.key);
27     if (*retval == nullptr) {
28       libc_errno = ESRCH;
29       return 0;
30     }
31     break;
32   case ENTER:
33     *retval = internal::HashTable::insert(table, item);
34     htab->__opaque = table;
35     if (*retval == nullptr) {
36       libc_errno = ENOMEM;
37       return 0;
38     }
39     break;
40   }
41   return 1;
42 }
43 
44 } // namespace LIBC_NAMESPACE_DECL
45