36 lines · cpp
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-exception6//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/__support/libc_errno.h"13#include "src/__support/macros/config.h"14#include "src/search/hsearch/global.h"15 16namespace LIBC_NAMESPACE_DECL {17LLVM_LIBC_FUNCTION(int, hcreate, (size_t capacity)) {18 // We follow FreeBSD's implementation here. If the global_hash_table is19 // already initialized, this function will do nothing and return 1.20 // https://cgit.freebsd.org/src/tree/lib/libc/stdlib/hcreate.c21 if (internal::global_hash_table != nullptr)22 return 1;23 24 uint64_t randomness = internal::randomness::next_random_seed();25 internal::HashTable *table =26 internal::HashTable::allocate(capacity, randomness);27 if (table == nullptr) {28 libc_errno = ENOMEM;29 return 0;30 }31 internal::global_hash_table = table;32 return 1;33}34 35} // namespace LIBC_NAMESPACE_DECL36