[libc-commits] [libc] [libc] [search] implement hcreate(_r)/hsearch(_r)/hdestroy(_r) (PR #73469)

Nick Desaulniers via libc-commits libc-commits at lists.llvm.org
Wed Nov 29 08:50:21 PST 2023


================
@@ -0,0 +1,35 @@
+//===-- Implementation of hsearch -------------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/search/hsearch.h"
+#include "src/__support/HashTable/table.h"
+#include "src/errno/libc_errno.h"
+#include "src/search/hsearch/global.h"
+
+namespace LIBC_NAMESPACE {
+LLVM_LIBC_FUNCTION(ENTRY *, hsearch, (ENTRY item, ACTION action)) {
+  ENTRY *result;
+
+  switch (action) {
+  case FIND:
+    result = internal::global_hash_table->find(item.key);
+    if (result == nullptr) {
+      libc_errno = ESRCH;
+    }
+    break;
+  case ENTER:
+    result = internal::global_hash_table->insert(item);
----------------
nickdesaulniers wrote:

FWIW, I was studying bionic last night and found their implementations of these come from FreeBSD.

https://android.googlesource.com/platform/bionic/+/refs/heads/main/libc/upstream-freebsd/lib/libc/stdlib/hcreate.c

Looks like they use a `bool` to track whether the global hash table hash been initialized.  They also do the right thing if search has been called before the table was ever explicitly created.  I think we should, too.  Do you mind following up with such a change?

https://github.com/llvm/llvm-project/pull/73469


More information about the libc-commits mailing list