[lib][libcpp] move c++ specific shims out of libc and heap

Consolidate into a single library. Also renames legacy new.h to
the more standard new.

Possible that some C++ code will need to get this added to their
MODULE_DEPS.
This commit is contained in:
Travis Geiselbrecht
2021-04-02 16:50:38 -07:00
parent 890504c922
commit ebdc1ea077
8 changed files with 99 additions and 27 deletions

View File

@@ -8,9 +8,6 @@ MODULE_SRCS += \
$(LOCAL_DIR)/heap_wrapper.c \
$(LOCAL_DIR)/page_alloc.c
MODULE_SRCS += \
$(LOCAL_DIR)/new.cpp
# pick a heap implementation
ifndef LK_HEAP_IMPLEMENTATION
LK_HEAP_IMPLEMENTATION=miniheap

View File

@@ -1,18 +0,0 @@
/*
* Copyright (c) 2008 Travis Geiselbrecht
*
* Use of this source code is governed by a MIT-style
* license that can be found in the LICENSE file or at
* https://opensource.org/licenses/MIT
*/
#pragma once
#include <sys/types.h>
void *operator new (size_t);
void *operator new (size_t, void *ptr);
void *operator new[](size_t);
void *operator new[](size_t, void *ptr);
void operator delete (void *p);
void operator delete[](void *p);

View File

@@ -21,9 +21,6 @@ MODULE_SRCS += \
$(LOCAL_DIR)/qsort.c \
$(LOCAL_DIR)/eabi.c
MODULE_SRCS += \
$(LOCAL_DIR)/pure_virtual.cpp
MODULE_COMPILEFLAGS += -fno-builtin
include $(LOCAL_DIR)/string/rules.mk

View File

@@ -0,0 +1,48 @@
// Copyright 2017 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// Provides std::initializer_list<T> on behalf of the Magenta toolchain
// when compiling without the C++ standard library.
//
#pragma once
// This implementation is known to be compatible with our GCC and Clang toolchains.
#if defined(__GNUC__) || defined(__clang__)
#include <stddef.h>
namespace std {
template <typename T>
class initializer_list {
public:
using value_type = T;
using reference = const T&;
using const_reference = const T&;
using size_type = size_t;
using iterator = const T*;
using const_iterator = const T*;
constexpr initializer_list()
: items_(nullptr), size_(0u) {}
constexpr size_t size() const { return size_; }
constexpr const T* begin() const { return items_; }
constexpr const T* end() const { return items_ + size_; }
private:
constexpr initializer_list(const T* items, size_t size)
: items_(items), size_(size) {}
const T* items_;
size_t size_;
};
} // namespace std
#else
#error "std::initializer_list<T> not supported by this toolchain"
#endif // defined(__GNUC__) || defined(__clang__)
// vim: syntax=cpp

27
lib/libcpp/include/new Normal file
View File

@@ -0,0 +1,27 @@
//
// Copyright (c) 2008 Travis Geiselbrecht
//
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT
#pragma once
#include <stddef.h>
// Fake std::nothrow_t without a standard C++ library.
namespace std {
struct nothrow_t {};
} // namespace std
void* operator new(size_t, const std::nothrow_t&) noexcept;
void* operator new[](size_t, const std::nothrow_t&) noexcept;
inline void* operator new(size_t, void *ptr) noexcept { return ptr; }
inline void* operator new[](size_t, void *ptr) noexcept { return ptr; }
void operator delete(void *p);
void operator delete[](void *p);
void operator delete(void *p, size_t);
void operator delete[](void *p, size_t);
// vim: syntax=cpp

View File

@@ -5,7 +5,7 @@
* license that can be found in the LICENSE file or at
* https://opensource.org/licenses/MIT
*/
#include <new.h>
#include <new>
#include <lk/debug.h>
#include <lib/heap.h>
@@ -17,8 +17,12 @@ void *operator new[](size_t s) {
return malloc(s);
}
void *operator new (size_t, void *p) {
return p;
void *operator new (size_t s, const std::nothrow_t &) noexcept {
return malloc(s);
}
void *operator new[](size_t s, const std::nothrow_t &) noexcept {
return malloc(s);
}
void operator delete (void *p) {
@@ -29,3 +33,10 @@ void operator delete[](void *p) {
return free(p);
}
void operator delete (void *p, size_t s) {
return free(p);
}
void operator delete[](void *p, size_t s) {
return free(p);
}

10
lib/libcpp/rules.mk Normal file
View File

@@ -0,0 +1,10 @@
LOCAL_DIR := $(GET_LOCAL_DIR)
MODULE := $(LOCAL_DIR)
MODULE_DEPS :=
MODULE_SRCS += $(LOCAL_DIR)/new.cpp
MODULE_SRCS += $(LOCAL_DIR)/pure_virtual.cpp
include make/module.mk