mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-01-31 11:37:35 +00:00
38921dc46b
This change introduces accumulate, addressof, advance, all_of, distance, array, enable_if, allocator_traits, back_inserter, bad_alloc, is_signed, any_of, copy, exception, fill, fill_n, is_same, is_same_v, out_of_range, lexicographical_compare, is_integral, uninitialized_fill_n, is_unsigned, numeric_limits, uninitialized_fill, iterator_traits, move_backward, min, max, iterator_tag, move_iterator, reverse_iterator, uninitialized_move_n This change experiments with rewriting the ctl::vector class to make the CTL design more similar to the STL. So far it has not slowed things down to have 42 #include lines rather than 2, since it's still almost nothing compared to LLVM's code. In fact the closer we can flirt with being just like libcxx, the better chance we might have of discovering exactly what makes it so slow to compile. It would be an enormous discovery if we can find one simple trick to solving the issue there instead. This also fixes a bug in `ctl::string(const string &s)` when `s` is big.
94 lines
2.1 KiB
C++
94 lines
2.1 KiB
C++
// -*-mode:c++;indent-tabs-mode:nil;c-basic-offset:4;tab-width:8;coding:utf-8-*-
|
|
// vi: set et ft=cpp ts=4 sts=4 sw=4 fenc=utf-8 :vi
|
|
#ifndef CTL_ALLOCATOR_H_
|
|
#define CTL_ALLOCATOR_H_
|
|
#include "bad_alloc.h"
|
|
#include "new.h"
|
|
#include "type_traits.h"
|
|
#include "utility.h"
|
|
|
|
namespace ctl {
|
|
|
|
template<typename T>
|
|
class allocator
|
|
{
|
|
public:
|
|
using value_type = T;
|
|
using size_type = size_t;
|
|
using difference_type = ptrdiff_t;
|
|
using propagate_on_container_move_assignment = ctl::true_type;
|
|
using is_always_equal = ctl::true_type;
|
|
using pointer = T*;
|
|
using const_pointer = const T*;
|
|
using reference = T&;
|
|
using const_reference = const T&;
|
|
|
|
constexpr allocator() noexcept = default;
|
|
|
|
constexpr allocator(const allocator&) noexcept = default;
|
|
|
|
template<class U>
|
|
constexpr allocator(const allocator<U>&) noexcept
|
|
{
|
|
}
|
|
|
|
constexpr ~allocator() = default;
|
|
|
|
[[nodiscard]] T* allocate(size_type n)
|
|
{
|
|
if (n > __SIZE_MAX__ / sizeof(T))
|
|
throw ctl::bad_alloc();
|
|
if (auto p = static_cast<T*>(::operator new(
|
|
n * sizeof(T), ctl::align_val_t(alignof(T)), ctl::nothrow)))
|
|
return p;
|
|
throw ctl::bad_alloc();
|
|
}
|
|
|
|
void deallocate(T* p, size_type n) noexcept
|
|
{
|
|
::operator delete(p, n * sizeof(T), ctl::align_val_t(alignof(T)));
|
|
}
|
|
|
|
template<typename U, typename... Args>
|
|
void construct(U* p, Args&&... args)
|
|
{
|
|
::new (static_cast<void*>(p)) U(ctl::forward<Args>(args)...);
|
|
}
|
|
|
|
template<typename U>
|
|
void destroy(U* p)
|
|
{
|
|
p->~U();
|
|
}
|
|
|
|
size_type max_size() const noexcept
|
|
{
|
|
return __SIZE_MAX__ / sizeof(T);
|
|
}
|
|
|
|
allocator& operator=(const allocator&) = default;
|
|
|
|
template<typename U>
|
|
struct rebind
|
|
{
|
|
using other = allocator<U>;
|
|
};
|
|
};
|
|
|
|
template<class T, class U>
|
|
bool
|
|
operator==(const allocator<T>&, const allocator<U>&) noexcept
|
|
{
|
|
return true;
|
|
}
|
|
|
|
template<class T, class U>
|
|
bool
|
|
operator!=(const allocator<T>&, const allocator<U>&) noexcept
|
|
{
|
|
return false;
|
|
}
|
|
|
|
} // namespace ctl
|
|
|
|
#endif // CTL_ALLOCATOR_H_
|