1//===-- Implementation of hcreate -------------------------------*- 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/hcreate.h"
10#include "src/__support/HashTable/randomness.h"
11#include "src/__support/HashTable/table.h"
12#include "src/errno/libc_errno.h"
13#include "src/search/hsearch/global.h"
14
15namespace LIBC_NAMESPACE {
16LLVM_LIBC_FUNCTION(int, hcreate, (size_t capacity)) {
17 // We follow FreeBSD's implementation here. If the global_hash_table is
18 // already initialized, this function will do nothing and return 1.
19 // https://cgit.freebsd.org/src/tree/lib/libc/stdlib/hcreate.c
20 if (internal::global_hash_table != nullptr)
21 return 1;
22
23 uint64_t randomness = internal::randomness::next_random_seed();
24 internal::HashTable *table =
25 internal::HashTable::allocate(capacity, randomness);
26 if (table == nullptr) {
27 libc_errno = ENOMEM;
28 return 0;
29 }
30 internal::global_hash_table = table;
31 return 1;
32}
33
34} // namespace LIBC_NAMESPACE
35

source code of libc/src/search/hcreate.cpp