1 //===-- Implementation of hcreate -------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #include "src/search/hcreate.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"
16 namespace LIBC_NAMESPACE_DECL
{
17 LLVM_LIBC_FUNCTION(int, hcreate
, (size_t capacity
)) {
18 // We follow FreeBSD's implementation here. If the global_hash_table is
19 // already initialized, this function will do nothing and return 1.
20 // https://cgit.freebsd.org/src/tree/lib/libc/stdlib/hcreate.c
21 if (internal::global_hash_table
!= nullptr)
24 uint64_t randomness
= internal::randomness::next_random_seed();
25 internal::HashTable
*table
=
26 internal::HashTable::allocate(capacity
, randomness
);
27 if (table
== nullptr) {
31 internal::global_hash_table
= table
;
35 } // namespace LIBC_NAMESPACE_DECL