[libc] Deprecate LLVM_ENABLE_PROJECTS in favor of LLVM_ENABLE_RUNTIMES. (#117265)
[llvm-project.git] / libc / src / search / hsearch_r.cpp
blobf93e608a190b10d09fed4a635419986b151266aa
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 //===----------------------------------------------------------------------===//
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"
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;
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;
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;
39 break;
41 return 1;
44 } // namespace LIBC_NAMESPACE_DECL