brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.6 KiB · 19eb255 Raw
102 lines · cpp
1//===-- Simple malloc and free for use with integration tests -------------===//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#include "hdr/stdint_proxy.h"9#include "src/__support/CPP/atomic.h"10#include "src/__support/common.h"11#include "src/__support/macros/config.h"12#include <stddef.h>13 14#ifdef LIBC_TARGET_ARCH_IS_AARCH6415#include "src/sys/auxv/getauxval.h"16#endif17 18// Integration tests rely on the following memory functions. This is because the19// compiler code generation can emit calls to them. We want to map the external20// entrypoint to the internal implementation of the function used for testing.21// This is done manually as not all targets support aliases.22 23namespace LIBC_NAMESPACE_DECL {24 25int bcmp(const void *lhs, const void *rhs, size_t count);26void bzero(void *ptr, size_t count);27int memcmp(const void *lhs, const void *rhs, size_t count);28void *memcpy(void *__restrict, const void *__restrict, size_t);29void *memmove(void *dst, const void *src, size_t count);30void *memset(void *ptr, int value, size_t count);31int atexit(void (*func)(void));32 33} // namespace LIBC_NAMESPACE_DECL34 35extern "C" {36 37int bcmp(const void *lhs, const void *rhs, size_t count) {38  return LIBC_NAMESPACE::bcmp(lhs, rhs, count);39}40void bzero(void *ptr, size_t count) { LIBC_NAMESPACE::bzero(ptr, count); }41int memcmp(const void *lhs, const void *rhs, size_t count) {42  return LIBC_NAMESPACE::memcmp(lhs, rhs, count);43}44void *memcpy(void *__restrict dst, const void *__restrict src, size_t count) {45  return LIBC_NAMESPACE::memcpy(dst, src, count);46}47void *memmove(void *dst, const void *src, size_t count) {48  return LIBC_NAMESPACE::memmove(dst, src, count);49}50void *memset(void *ptr, int value, size_t count) {51  return LIBC_NAMESPACE::memset(ptr, value, count);52}53 54// This is needed if the test was compiled with '-fno-use-cxa-atexit'.55int atexit(void (*func)(void)) { return LIBC_NAMESPACE::atexit(func); }56 57} // extern "C"58 59// Integration tests cannot use the SCUDO standalone allocator as SCUDO pulls60// various other parts of the libc. Since SCUDO development does not use61// LLVM libc build rules, it is very hard to keep track or pull all that SCUDO62// requires. Hence, as a work around for this problem, we use a simple allocator63// which just hands out continuous blocks from a statically allocated chunk of64// memory.65 66static constexpr uint64_t ALIGNMENT = alignof(double);67static constexpr uint64_t MEMORY_SIZE = 256 * 1024 /* 256 KiB */;68alignas(ALIGNMENT) static uint8_t memory[MEMORY_SIZE];69static size_t ptr = 0;70 71extern "C" {72 73void *malloc(size_t size) {74  LIBC_NAMESPACE::cpp::AtomicRef<size_t> ref(ptr);75  size = (size + ALIGNMENT - 1) & ~(ALIGNMENT - 1);76  size_t old_ptr =77      ref.fetch_add(size, LIBC_NAMESPACE::cpp::MemoryOrder::RELAXED);78  if (static_cast<size_t>(old_ptr + size) >= MEMORY_SIZE)79    return nullptr;80  return &memory[old_ptr];81}82 83void free(void *) {}84 85void *realloc(void *ptr, size_t s) {86  free(ptr);87  return malloc(s);88}89 90// Integration tests are linked with -nostdlib. BFD linker expects91// __dso_handle when -nostdlib is used.92void *__dso_handle = nullptr;93 94#ifdef LIBC_TARGET_ARCH_IS_AARCH6495// Due to historical reasons, libgcc on aarch64 may expect __getauxval to be96// defined. See also https://gcc.gnu.org/pipermail/gcc-cvs/2020-June/300635.html97unsigned long __getauxval(unsigned long id) {98  return LIBC_NAMESPACE::getauxval(id);99}100#endif101} // extern "C"102