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/errno/libc_errno.h" 12 13 namespace LIBC_NAMESPACE { 14 LLVM_LIBC_FUNCTION(int, hsearch_r, 15 (ENTRY item, ACTION action, ENTRY **retval, 16 struct hsearch_data *htab)) { 17 if (htab == nullptr) { 18 libc_errno = EINVAL; 19 return 0; 20 } 21 internal::HashTable *table = 22 static_cast<internal::HashTable *>(htab->__opaque); 23 switch (action) { 24 case FIND: 25 *retval = table->find(item.key); 26 if (*retval == nullptr) { 27 libc_errno = ESRCH; 28 return 0; 29 } 30 break; 31 case ENTER: 32 *retval = internal::HashTable::insert(table, item); 33 htab->__opaque = table; 34 if (*retval == nullptr) { 35 libc_errno = ENOMEM; 36 return 0; 37 } 38 break; 39 } 40 return 1; 41 } 42 43 } // namespace LIBC_NAMESPACE 44