diff options
| author | Amlal El Mahrouss <amlal@nekernel.org> | 2026-03-27 01:43:44 +0100 |
|---|---|---|
| committer | Amlal El Mahrouss <amlal@nekernel.org> | 2026-03-27 01:43:44 +0100 |
| commit | 2174548c1cd0695399275dc59f1e4e3f726b4a6f (patch) | |
| tree | f4f3ae198e311686be8d13ba7c9e6d28c49177f5 /include/CoreRuntime/cplusplus/core | |
| parent | 81c082643568ef4507c92c6f8c4d7f2778274aa6 (diff) | |
[FEAT] Refactor CoreRuntimeKit to CoreRuntime and its design.
Signed-off-by: Amlal El Mahrouss <amlal@nekernel.org>
Diffstat (limited to 'include/CoreRuntime/cplusplus/core')
| -rw-r--r-- | include/CoreRuntime/cplusplus/core/base_alloc.hpp | 44 | ||||
| -rw-r--r-- | include/CoreRuntime/cplusplus/core/base_exception.hpp | 36 | ||||
| -rw-r--r-- | include/CoreRuntime/cplusplus/core/base_math.hpp | 88 | ||||
| -rw-r--r-- | include/CoreRuntime/cplusplus/core/base_process.hpp | 44 |
4 files changed, 212 insertions, 0 deletions
diff --git a/include/CoreRuntime/cplusplus/core/base_alloc.hpp b/include/CoreRuntime/cplusplus/core/base_alloc.hpp new file mode 100644 index 0000000..3b79706 --- /dev/null +++ b/include/CoreRuntime/cplusplus/core/base_alloc.hpp @@ -0,0 +1,44 @@ +// SPDX-License-Identifier: Apache-2.0 +// Copyright 2024-2026, Amlal El Mahrouss (amlal@nekernel.org) +// Licensed under the Apache License, Version 2.0 (see LICENSE file) +// Official repository: https://github.com/ne-foss-org/nectar + +#pragma once + +#include <CoreRuntime/cplusplus/defines.hpp> + +namespace std::base_alloc { + +/// @brief allocate a new class. +/// @tparam KindClass the class type to allocate. +template <class KindClass, typename... Args> +inline KindClass* allocate(Args&&... args) { + return new KindClass(forward(args)...); +} + +/// @brief allocate a new class. +/// @note aborts on error. +/// @tparam KindClass the class type to allocate. +template <class KindClass, typename... Args> +inline KindClass* allocate_nothrow(Args&&... args) noexcept { + return allocate(forward(args)...); +} + +/// @brief free a class. +/// @tparam KindClass the class type to allocate. +template <class KindClass> +inline void release(KindClass ptr) { + if (!ptr) return; + + delete ptr; +} + +/// @brief destroy and free a class. +/// @note aborts on error. +/// @tparam KindClass the class type to allocate. +template <class KindClass> +inline void release_nothrow(KindClass ptr) noexcept { + release(ptr); +} + +} // namespace std::base_alloc diff --git a/include/CoreRuntime/cplusplus/core/base_exception.hpp b/include/CoreRuntime/cplusplus/core/base_exception.hpp new file mode 100644 index 0000000..7316dfa --- /dev/null +++ b/include/CoreRuntime/cplusplus/core/base_exception.hpp @@ -0,0 +1,36 @@ +// SPDX-License-Identifier: Apache-2.0 +// Copyright 2024-2026, Amlal El Mahrouss (amlal@nekernel.org) +// Licensed under the Apache License, Version 2.0 (see LICENSE file) +// Official repository: https://github.com/ne-foss-org/nectar + +#pragma once + +#include <CoreRuntime/cplusplus/abi/abi.hpp> +#include <CoreRuntime/cplusplus/core/base_process.hpp> +#include <CoreRuntime/cplusplus/defines.hpp> +#include <iostream> + +/// @author Amlal El Mahrouss (amlal@nekernel.org) + +namespace std::base_exception::abi { +inline constexpr int __terminate_id = 33; + +/// @note This function is internal, don't call it. +extern void __unwind_object_list(); + +inline void __throw_general(const char* what) { + std::cout << "LibC++: Unwinding exception of kind: " << what << ", aborting here..." << std::endl; + __unwind_object_list(); + base_process::exit(__terminate_id); +} + +inline void __throw_domain_error(const char* what) { + __throw_general(what); + __builtin_unreachable(); // prevent from continuing. +} + +inline void __throw_bad_array_new_length(const char* what) { + __throw_general(what); + __builtin_unreachable(); // prevent from continuing. +} +} // namespace std::base_exception::abi diff --git a/include/CoreRuntime/cplusplus/core/base_math.hpp b/include/CoreRuntime/cplusplus/core/base_math.hpp new file mode 100644 index 0000000..103a1eb --- /dev/null +++ b/include/CoreRuntime/cplusplus/core/base_math.hpp @@ -0,0 +1,88 @@ +// SPDX-License-Identifier: Apache-2.0 +// Copyright 2024-2026, Amlal El Mahrouss (amlal@nekernel.org) +// Licensed under the Apache License, Version 2.0 (see LICENSE file) +// Official repository: https://github.com/ne-foss-org/nectar + +#pragma once + +#include <CoreRuntime/cplusplus/defines.hpp> + +#ifndef NAN +#define NAN (__builtin_nanf("")) +#endif // !NAN + +/// @file Math +/// @brief Math functions. + +#ifdef __LIBCXX_USE_DOUBLE__ +typedef double real_type; +#else +typedef float real_type; +#endif + +namespace std::base_math { +inline constexpr static auto not_a_number = NAN; + +/// =========================================================== /// +/// @brief Power function, with Repeat argument. +/// =========================================================== /// +template <size_t Exponent> +inline real_type pow(real_type in) { + if (Exponent == 0) return 1; // Any number to the power of 0 is 1. + + if (Exponent == 1) return in; // Any number to the power of 1 is itself. + + size_t cnt = Exponent; + + real_type result = 1; + + for (auto i = 0; i < cnt; ++i) result *= in; + + return result; +} + +/// =========================================================== /// +/// @brief Square root function. +/// =========================================================== /// +inline real_type sqrt(real_type in) { + if (in == 0) return 0; + if (in == not_a_number) return not_a_number; + + auto constexpr static kRootBase = 2; + auto constexpr static kNumberBase = 10; + + auto x = in / kRootBase; + + for (int i = 0; i < kNumberBase; ++i) { + x = (x + in / x) / kRootBase; + } + + return x; +} + +/// =========================================================== /// +/// @brief Square of function, with Base template argument. +/// @param of Base argument to find the square of. +/// =========================================================== /// +template <size_t Base> +inline real_type surd(real_type in) { + if (in == 0) return 0; + if (in == 1) return 1; + + if (Base == 1) return in; + if (Base == 2) return sqrt(in); + + return not_a_number; +} + +/// =========================================================== /// +/// @brief Linear interpolation equation solver. +/// @param from where? +/// @param to to? +/// @param Updated diff value according to difference. +/// =========================================================== /// +inline real_type lerp(real_type to, real_type from, real_type stat) { + real_type diff = (to - from); + return from + (diff * stat); +} +} // namespace std::base_math diff --git a/include/CoreRuntime/cplusplus/core/base_process.hpp b/include/CoreRuntime/cplusplus/core/base_process.hpp new file mode 100644 index 0000000..7673577 --- /dev/null +++ b/include/CoreRuntime/cplusplus/core/base_process.hpp @@ -0,0 +1,44 @@ +// SPDX-License-Identifier: Apache-2.0 +// Copyright 2024-2026, Amlal El Mahrouss (amlal@nekernel.org) +// Licensed under the Apache License, Version 2.0 (see LICENSE file) +// Official repository: https://github.com/ne-foss-org/nectar + +#pragma once + +#include <CoreRuntime/cplusplus/defines.hpp> + +__init_decl() + +/// @brief CRT exit, with exit code (!!! exits all threads. !!!) +/// @param code the exit code. +/// @return the return > 0 for non successful. +extern int exit_(int code); + +/// @brief CRT signal handler. +/// @param code the signal code. +extern void signal_(int code); + +extern void (*__atexit_cdecl_ptr)(void); +extern void (**__atexit_lst_ptr)(void); +extern size_t __atexit_lst_cnt; + +__fini_decl() + + /// @brief Standard C++ namespace + namespace std::base_process { + inline int signal(int code) { + signal_(code); + return -1; + } + + inline int32_t exit(const int32_t& code) { + for (auto idx = 0UL; idx < __atexit_lst_cnt; ++idx) { + __atexit_lst_ptr[idx](); + } + + if (__atexit_cdecl_ptr) __atexit_cdecl_ptr(); + + exit_(code); + return -1; + } +} // namespace std::base_process |
