2021-10-10 14:39:17 +08:00
|
|
|
#pragma once
|
|
|
|
|
2021-11-04 20:15:58 +08:00
|
|
|
#include <stddef.h>
|
2021-10-10 14:39:17 +08:00
|
|
|
#include <new>
|
|
|
|
|
|
|
|
#include "memory.h"
|
|
|
|
|
|
|
|
|
|
|
|
namespace helos {
|
|
|
|
|
|
|
|
|
|
|
|
class bad_alloc: public std::exception {
|
|
|
|
public:
|
|
|
|
bad_alloc() noexcept {}
|
|
|
|
bad_alloc(const bad_alloc &other) noexcept {}
|
|
|
|
bad_alloc &operator=(const bad_alloc &other) noexcept { return *this; }
|
|
|
|
|
|
|
|
virtual const char *what() const noexcept override { return "helos::bad_alloc"; }
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// kAllocator is a class wrapper for kMalloc/Free satisfying the named requirement Allocator.
|
|
|
|
template<typename Type>
|
|
|
|
class kAllocator {
|
|
|
|
public:
|
|
|
|
typedef Type value_type;
|
|
|
|
|
|
|
|
kAllocator() = default;
|
|
|
|
template<typename Other>
|
|
|
|
constexpr kAllocator(const kAllocator<Other> &) {}
|
|
|
|
|
2021-11-04 20:15:58 +08:00
|
|
|
Type *allocate(size_t n) {
|
2021-10-10 14:39:17 +08:00
|
|
|
return kMalloc(n * sizeof(Type));
|
|
|
|
}
|
|
|
|
|
2021-11-04 20:15:58 +08:00
|
|
|
void deallocate(Type *p, size_t n) {
|
2021-10-10 14:39:17 +08:00
|
|
|
kFree(p);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class T, class U>
|
|
|
|
bool operator==(const kAllocator<T> &, const kAllocator<U> &) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
template<class T, class U>
|
|
|
|
bool operator!=(const kAllocator<T> &, const kAllocator<U> &) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace helos
|
|
|
|
|
|
|
|
|
2021-11-04 18:05:19 +08:00
|
|
|
// overload new/delete only in the real kernel, not in testing
|
|
|
|
#ifdef HELOS
|
|
|
|
|
2021-10-10 14:39:17 +08:00
|
|
|
// globally overload the new and delete operators
|
|
|
|
// so keep this header at the top of every source file
|
|
|
|
//
|
|
|
|
// operators new and delete only call kMalloc/kFree, so C++ code
|
|
|
|
// must stay after paging setup
|
2021-11-04 20:15:58 +08:00
|
|
|
void *operator new(size_t size);
|
|
|
|
void *operator new[](size_t size);
|
2021-10-23 18:36:31 +08:00
|
|
|
|
|
|
|
#if __cplusplus >= 201703L
|
2021-11-04 20:15:58 +08:00
|
|
|
void *operator new(size_t size, std::align_val_t align);
|
|
|
|
void *operator new[](size_t size, std::align_val_t align);
|
2021-10-23 18:36:31 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
void operator delete(void *ptr) noexcept;
|
|
|
|
void operator delete[](void *ptr) noexcept;
|
|
|
|
#if __cplusplus >= 201402L
|
2021-11-04 20:15:58 +08:00
|
|
|
void operator delete(void *ptr, size_t size) noexcept;
|
|
|
|
void operator delete[](void *ptr, size_t size) noexcept;
|
2021-10-23 18:36:31 +08:00
|
|
|
#endif
|
|
|
|
#if __cplusplus >= 201703L
|
|
|
|
void operator delete(void *ptr, std::align_val_t align) noexcept;
|
|
|
|
void operator delete[](void *ptr, std::align_val_t align) noexcept;
|
2021-11-04 20:15:58 +08:00
|
|
|
void operator delete(void *ptr, size_t size, std::align_val_t align) noexcept;
|
|
|
|
void operator delete[](void *ptr, size_t size, std::align_val_t align) noexcept;
|
2021-10-23 18:36:31 +08:00
|
|
|
#endif
|
2021-11-04 18:05:19 +08:00
|
|
|
|
|
|
|
#endif // HELOS
|