type/include cleanup

master
Daniel Kolesa 2017-04-09 16:44:45 +02:00
parent a913f7beca
commit 1525edf3d7
18 changed files with 211 additions and 210 deletions

View File

@ -6,8 +6,7 @@
#ifndef OSTD_ALGORITHM_HH #ifndef OSTD_ALGORITHM_HH
#define OSTD_ALGORITHM_HH #define OSTD_ALGORITHM_HH
#include <math.h> #include <cmath>
#include <utility> #include <utility>
#include <functional> #include <functional>
#include <type_traits> #include <type_traits>
@ -148,10 +147,9 @@ namespace detail {
template<typename R, typename C> template<typename R, typename C>
inline void introsort(R range, C &compare) { inline void introsort(R range, C &compare) {
detail::introloop( detail::introloop(range, compare, static_cast<range_size_t<R>>(
range, compare, 2 * (std::log(range.size()) / std::log(2))
static_cast<range_size_t<R>>(2 * (log(range.size()) / log(2))) ));
);
} }
} /* namespace detail */ } /* namespace detail */

View File

@ -46,6 +46,7 @@
#ifndef OSTD_CONCURRENCY_HH #ifndef OSTD_CONCURRENCY_HH
#define OSTD_CONCURRENCY_HH #define OSTD_CONCURRENCY_HH
#include <cstddef>
#include <vector> #include <vector>
#include <list> #include <list>
#include <thread> #include <thread>
@ -162,7 +163,7 @@ public:
/** @brief Deallocates a stack allocated with allocate_stack(). /** @brief Deallocates a stack allocated with allocate_stack().
* *
* @see allocate_stack(), reserve_stacks(size_t), get_stack_allocator() * @see allocate_stack(), reserve_stacks(), get_stack_allocator()
*/ */
virtual void deallocate_stack(stack_context &st) noexcept = 0; virtual void deallocate_stack(stack_context &st) noexcept = 0;
@ -174,7 +175,7 @@ public:
* *
* @see allocate_stack(), deallocate_stack(), get_stack_allocator() * @see allocate_stack(), deallocate_stack(), get_stack_allocator()
*/ */
virtual void reserve_stacks(size_t n) = 0; virtual void reserve_stacks(std::size_t n) = 0;
/** @brief Gets a stack allocator using the scheduler's stack allocation. /** @brief Gets a stack allocator using the scheduler's stack allocation.
* *
@ -355,7 +356,7 @@ struct basic_thread_scheduler: scheduler {
} }
} }
void reserve_stacks(size_t n) { void reserve_stacks(std::size_t n) {
if constexpr(!SA::is_thread_safe) { if constexpr(!SA::is_thread_safe) {
std::lock_guard<std::mutex> l{p_lock}; std::lock_guard<std::mutex> l{p_lock};
p_stacks.reserve(n); p_stacks.reserve(n);
@ -595,7 +596,7 @@ public:
p_stacks.deallocate(st); p_stacks.deallocate(st);
} }
void reserve_stacks(size_t n) { void reserve_stacks(std::size_t n) {
p_stacks.reserve(n); p_stacks.reserve(n);
} }
@ -720,7 +721,7 @@ public:
* @param[in] sa The provided stack allocator. * @param[in] sa The provided stack allocator.
*/ */
basic_coroutine_scheduler( basic_coroutine_scheduler(
size_t thrs = std::thread::hardware_concurrency(), SA &&sa = SA{} std::size_t thrs = std::thread::hardware_concurrency(), SA &&sa = SA{}
): ):
p_threads(thrs), p_stacks(std::move(sa)) p_threads(thrs), p_stacks(std::move(sa))
{} {}
@ -830,7 +831,7 @@ public:
} }
} }
void reserve_stacks(size_t n) { void reserve_stacks(std::size_t n) {
if constexpr(!SA::is_thread_safe) { if constexpr(!SA::is_thread_safe) {
std::lock_guard<std::mutex> l{p_lock}; std::lock_guard<std::mutex> l{p_lock};
p_stacks.reserve(n); p_stacks.reserve(n);
@ -862,13 +863,13 @@ private:
} }
void init() { void init() {
size_t size = p_threads; std::size_t size = p_threads;
std::vector<std::thread> thrs; std::vector<std::thread> thrs;
thrs.reserve(size); thrs.reserve(size);
for (size_t i = 0; i < size; ++i) { for (std::size_t i = 0; i < size; ++i) {
thrs.emplace_back([this]() { thread_run(); }); thrs.emplace_back([this]() { thread_run(); });
} }
for (size_t i = 0; i < size; ++i) { for (std::size_t i = 0; i < size; ++i) {
if (thrs[i].joinable()) { if (thrs[i].joinable()) {
thrs[i].join(); thrs[i].join();
} }
@ -951,7 +952,7 @@ private:
} }
} }
size_t p_threads; std::size_t p_threads;
std::condition_variable p_cond; std::condition_variable p_cond;
std::mutex p_lock; std::mutex p_lock;
SA p_stacks; SA p_stacks;
@ -1019,7 +1020,7 @@ inline generator<T> make_generator(F &&func) {
return detail::current_scheduler->make_generator<T>(std::forward<F>(func)); return detail::current_scheduler->make_generator<T>(std::forward<F>(func));
} }
inline void reserve_stacks(size_t n) { inline void reserve_stacks(std::size_t n) {
detail::current_scheduler->reserve_stacks(n); detail::current_scheduler->reserve_stacks(n);
} }

View File

@ -18,6 +18,7 @@
#ifndef OSTD_CONTEXT_STACK_HH #ifndef OSTD_CONTEXT_STACK_HH
#define OSTD_CONTEXT_STACK_HH #define OSTD_CONTEXT_STACK_HH
#include <cstddef>
#include <new> #include <new>
#include <algorithm> #include <algorithm>
@ -48,7 +49,7 @@ namespace ostd {
*/ */
struct stack_context { struct stack_context {
void *ptr = nullptr; ///< The stack pointer. void *ptr = nullptr; ///< The stack pointer.
size_t size = 0; ///< The stack size. std::size_t size = 0; ///< The stack size.
#ifdef OSTD_USE_VALGRIND #ifdef OSTD_USE_VALGRIND
int valgrind_id = 0; int valgrind_id = 0;
#endif #endif
@ -75,7 +76,7 @@ struct OSTD_EXPORT stack_traits {
* some other value too. Stack sizes are typically a multiple of page * some other value too. Stack sizes are typically a multiple of page
* size. * size.
*/ */
static size_t page_size() noexcept; static std::size_t page_size() noexcept;
/** @brief Gets the minimum size a stack can have. /** @brief Gets the minimum size a stack can have.
* *
@ -85,7 +86,7 @@ struct OSTD_EXPORT stack_traits {
* *
* @see maximum_size(), default_size() * @see maximum_size(), default_size()
*/ */
static size_t minimum_size() noexcept; static std::size_t minimum_size() noexcept;
/** @brief Gets the maximum size a stack can have. /** @brief Gets the maximum size a stack can have.
* *
@ -95,7 +96,7 @@ struct OSTD_EXPORT stack_traits {
* *
* @see minimum_size(), default_size() * @see minimum_size(), default_size()
*/ */
static size_t maximum_size() noexcept; static std::size_t maximum_size() noexcept;
/** @brief Gets the default size for stacks. /** @brief Gets the default size for stacks.
* *
@ -105,14 +106,14 @@ struct OSTD_EXPORT stack_traits {
* *
* @see minimum_size(), maximum_size() * @see minimum_size(), maximum_size()
*/ */
static size_t default_size() noexcept; static std::size_t default_size() noexcept;
}; };
namespace detail { namespace detail {
OSTD_EXPORT void *stack_alloc(size_t sz); OSTD_EXPORT void *stack_alloc(std::size_t sz);
OSTD_EXPORT void stack_free(void *p, size_t sz) noexcept; OSTD_EXPORT void stack_free(void *p, std::size_t sz) noexcept;
OSTD_EXPORT void stack_protect(void *p, size_t sz) noexcept; OSTD_EXPORT void stack_protect(void *p, std::size_t sz) noexcept;
OSTD_EXPORT size_t stack_main_size() noexcept; OSTD_EXPORT std::size_t stack_main_size() noexcept;
} }
/** @brief A fixed size stack. /** @brief A fixed size stack.
@ -159,7 +160,7 @@ struct basic_fixedsize_stack {
* The provided argument is the size used for the stacks. It defaults * The provided argument is the size used for the stacks. It defaults
* to the default size used for the stacks according to the traits. * to the default size used for the stacks according to the traits.
*/ */
basic_fixedsize_stack(size_t ss = Traits::default_size()) noexcept: basic_fixedsize_stack(std::size_t ss = Traits::default_size()) noexcept:
p_size( p_size(
Traits::is_unbounded() Traits::is_unbounded()
? std::max(ss, Traits::minimum_size()) ? std::max(ss, Traits::minimum_size())
@ -169,10 +170,10 @@ struct basic_fixedsize_stack {
/** @brief Allocates a stack. */ /** @brief Allocates a stack. */
stack_context allocate() { stack_context allocate() {
size_t ss = p_size; std::size_t ss = p_size;
size_t pgs = Traits::page_size(); std::size_t pgs = Traits::page_size();
size_t asize = ss + pgs - 1 - (ss - 1) % pgs + (pgs * Protected); std::size_t asize = ss + pgs - 1 - (ss - 1) % pgs + (pgs * Protected);
void *p = detail::stack_alloc(asize); void *p = detail::stack_alloc(asize);
if constexpr(Protected) { if constexpr(Protected) {
@ -207,7 +208,7 @@ struct basic_fixedsize_stack {
* stacks can be reserved ahead of time. This allocates only one stack * stacks can be reserved ahead of time. This allocates only one stack
* at a time, so this function does nothing. * at a time, so this function does nothing.
*/ */
void reserve(size_t) {} void reserve(std::size_t) {}
/** @brief Gets the allocator for stack pool uses. /** @brief Gets the allocator for stack pool uses.
* *
@ -220,7 +221,7 @@ struct basic_fixedsize_stack {
} }
private: private:
size_t p_size; std::size_t p_size;
}; };
/** @brief An unprotected fixed size stack using ostd::stack_traits. */ /** @brief An unprotected fixed size stack using ostd::stack_traits. */
@ -270,7 +271,7 @@ private:
public: public:
/** @brief The default number of stacks to store in each chunk. */ /** @brief The default number of stacks to store in each chunk. */
static constexpr size_t DEFAULT_CHUNK_SIZE = 32; static constexpr std::size_t DEFAULT_CHUNK_SIZE = 32;
/** @brief The traits type used for the stacks. */ /** @brief The traits type used for the stacks. */
using traits_type = Traits; using traits_type = Traits;
@ -303,11 +304,12 @@ public:
* @param cs The number of stacks in each chunk. * @param cs The number of stacks in each chunk.
*/ */
basic_stack_pool( basic_stack_pool(
size_t ss = Traits::default_size(), size_t cs = DEFAULT_CHUNK_SIZE std::size_t ss = Traits::default_size(),
std::size_t cs = DEFAULT_CHUNK_SIZE
) { ) {
/* precalculate the sizes */ /* precalculate the sizes */
size_t pgs = Traits::page_size(); std::size_t pgs = Traits::page_size();
size_t asize = ss + pgs - 1 - (ss - 1) % pgs + (pgs * Protected); std::size_t asize = ss + pgs - 1 - (ss - 1) % pgs + (pgs * Protected);
p_stacksize = asize; p_stacksize = asize;
p_chunksize = cs * asize; p_chunksize = cs * asize;
} }
@ -347,8 +349,8 @@ public:
/** @brief Destroys the stack pool and all memory managed by it. */ /** @brief Destroys the stack pool and all memory managed by it. */
~basic_stack_pool() { ~basic_stack_pool() {
size_t ss = p_stacksize; std::size_t ss = p_stacksize;
size_t cs = p_chunksize; std::size_t cs = p_chunksize;
void *pc = p_chunk; void *pc = p_chunk;
while (pc) { while (pc) {
void *p = pc; void *p = pc;
@ -363,12 +365,12 @@ public:
* to contain. If it already contains that number or more, this function * to contain. If it already contains that number or more, this function
* does nothing. Otherwise it potentially reserves some extra chunks. * does nothing. Otherwise it potentially reserves some extra chunks.
*/ */
void reserve(size_t n) { void reserve(std::size_t n) {
size_t cap = p_capacity; std::size_t cap = p_capacity;
if (n <= cap) { if (n <= cap) {
return; return;
} }
size_t cnum = p_chunksize / p_stacksize; std::size_t cnum = p_chunksize / p_stacksize;
p_unused = alloc_chunks(p_unused, (n - cap + cnum - 1) / cnum); p_unused = alloc_chunks(p_unused, (n - cap + cnum - 1) / cnum);
} }
@ -380,7 +382,7 @@ public:
*/ */
stack_context allocate() { stack_context allocate() {
stack_node *nd = request(); stack_node *nd = request();
size_t ss = p_stacksize - sizeof(stack_node); std::size_t ss = p_stacksize - sizeof(stack_node);
auto *p = reinterpret_cast<unsigned char *>(nd) - ss; auto *p = reinterpret_cast<unsigned char *>(nd) - ss;
if constexpr(Protected) { if constexpr(Protected) {
detail::stack_protect(p, Traits::page_size()); detail::stack_protect(p, Traits::page_size());
@ -436,15 +438,15 @@ private:
stack_node *next; stack_node *next;
}; };
stack_node *alloc_chunks(stack_node *un, size_t n) { stack_node *alloc_chunks(stack_node *un, std::size_t n) {
size_t ss = p_stacksize; std::size_t ss = p_stacksize;
size_t cs = p_chunksize; std::size_t cs = p_chunksize;
size_t cnum = cs / ss; std::size_t cnum = cs / ss;
for (size_t ci = 0; ci < n; ++ci) { for (std::size_t ci = 0; ci < n; ++ci) {
void *chunk = detail::stack_alloc(cs); void *chunk = detail::stack_alloc(cs);
stack_node *prevn = un; stack_node *prevn = un;
for (size_t i = cnum; i >= 2; --i) { for (std::size_t i = cnum; i >= 2; --i) {
auto nd = get_node(chunk, ss, i); auto nd = get_node(chunk, ss, i);
nd->next_chunk = nullptr; nd->next_chunk = nullptr;
nd->next = prevn; nd->next = prevn;
@ -473,7 +475,7 @@ private:
return r; return r;
} }
stack_node *get_node(void *chunk, size_t ssize, size_t n) { stack_node *get_node(void *chunk, std::size_t ssize, std::size_t n) {
return reinterpret_cast<stack_node *>( return reinterpret_cast<stack_node *>(
static_cast<unsigned char *>(chunk) + (ssize * n) - sizeof(stack_node) static_cast<unsigned char *>(chunk) + (ssize * n) - sizeof(stack_node)
); );
@ -482,9 +484,9 @@ private:
void *p_chunk = nullptr; void *p_chunk = nullptr;
stack_node *p_unused = nullptr; stack_node *p_unused = nullptr;
size_t p_chunksize; std::size_t p_chunksize;
size_t p_stacksize; std::size_t p_stacksize;
size_t p_capacity = 0; std::size_t p_capacity = 0;
}; };
/** @brief Swaps two stack pools. */ /** @brief Swaps two stack pools. */

View File

@ -20,6 +20,7 @@
#ifndef OSTD_COROUTINE_HH #ifndef OSTD_COROUTINE_HH
#define OSTD_COROUTINE_HH #define OSTD_COROUTINE_HH
#include <cstddef>
#include <exception> #include <exception>
#include <stdexcept> #include <stdexcept>
#include <typeinfo> #include <typeinfo>
@ -65,7 +66,7 @@ namespace detail {
extern "C" OSTD_EXPORT extern "C" OSTD_EXPORT
fcontext_t OSTD_CDECL ostd_make_fcontext( fcontext_t OSTD_CDECL ostd_make_fcontext(
void *sp, size_t size, void (*fn)(transfer_t) void *sp, std::size_t size, void (*fn)(transfer_t)
); );
extern "C" OSTD_EXPORT extern "C" OSTD_EXPORT
@ -267,7 +268,7 @@ protected:
p_stack = sa.allocate(); p_stack = sa.allocate();
void *sp = get_stack_ptr<SA>(); void *sp = get_stack_ptr<SA>();
size_t asize = p_stack.size - ( std::size_t asize = p_stack.size - (
static_cast<unsigned char *>(p_stack.ptr) - static_cast<unsigned char *>(p_stack.ptr) -
static_cast<unsigned char *>(sp) static_cast<unsigned char *>(sp)
); );
@ -282,14 +283,14 @@ private:
template<typename SA> template<typename SA>
void *get_stack_ptr() { void *get_stack_ptr() {
/* 16 byte stack pointer alignment */ /* 16 byte stack pointer alignment */
constexpr size_t salign = 16; constexpr std::size_t salign = 16;
/* makes enough space so that we can store the allocator at /* makes enough space so that we can store the allocator at
* stack pointer location (we'll need it for dealloc later) * stack pointer location (we'll need it for dealloc later)
*/ */
constexpr size_t sasize = sizeof(SA); constexpr std::size_t sasize = sizeof(SA);
void *sp = static_cast<unsigned char *>(p_stack.ptr) - sasize - salign; void *sp = static_cast<unsigned char *>(p_stack.ptr) - sasize - salign;
size_t space = sasize + salign; std::size_t space = sasize + salign;
sp = std::align(salign, sasize, sp, space); sp = std::align(salign, sasize, sp, space);
return sp; return sp;
@ -1203,8 +1204,8 @@ struct generator_range: input_range<generator_range<T>> {
using range_category = input_range_tag; using range_category = input_range_tag;
using value_type = T; using value_type = T;
using reference = T &; using reference = T &;
using size_type = size_t; using size_type = std::size_t;
using difference_type = ptrdiff_t; using difference_type = std::ptrdiff_t;
/** Generator ranges are not default constructible. */ /** Generator ranges are not default constructible. */
generator_range() = delete; generator_range() = delete;

View File

@ -21,9 +21,8 @@
#include "ostd/platform.hh" #include "ostd/platform.hh"
#include "ostd/internal/win32.hh" #include "ostd/internal/win32.hh"
#include <stdlib.h> #include <cstdlib>
#include <string.h> #include <cstring>
#include <vector> #include <vector>
#include <optional> #include <optional>
@ -50,27 +49,11 @@ namespace ostd {
inline std::optional<std::string> env_get(string_range name) { inline std::optional<std::string> env_get(string_range name) {
char buf[256]; char buf[256];
auto tbuf = to_temp_cstr(name, buf, sizeof(buf)); auto tbuf = to_temp_cstr(name, buf, sizeof(buf));
#ifndef OSTD_PLATFORM_WIN32 char const *ret = std::getenv(tbuf.get());
char const *ret = getenv(tbuf.get());
if (!ret) { if (!ret) {
return std::nullopt; return std::nullopt;
} }
return std::string{ret}; return std::string{ret};
#else
std::vector<char> rbuf;
DWORD sz;
for (;;) {
sz = GetEnvironmentVariable(tbuf.get(), rbuf.data(), rbuf.capacity());
if (!sz) {
return std::nullopt;
}
if (sz < rbuf.capacity()) {
break;
}
rbuf.reserve(sz);
}
return std::string{rbuf.data(), sz};
#endif
} }
/** @brief Sets an environment variable. /** @brief Sets an environment variable.

View File

@ -6,6 +6,7 @@
#ifndef OSTD_EVENT_HH #ifndef OSTD_EVENT_HH
#define OSTD_EVENT_HH #define OSTD_EVENT_HH
#include <cstddef>
#include <functional> #include <functional>
#include <utility> #include <utility>
@ -22,7 +23,7 @@ namespace detail {
using func_t = std::function<void(C &, A...)>; using func_t = std::function<void(C &, A...)>;
auto *bufp = new unsigned char[sizeof(func_t) * p_nfuncs]; auto *bufp = new unsigned char[sizeof(func_t) * p_nfuncs];
func_t *nbuf = reinterpret_cast<func_t *>(bufp); func_t *nbuf = reinterpret_cast<func_t *>(bufp);
for (size_t i = 0; i < p_nfuncs; ++i) for (std::size_t i = 0; i < p_nfuncs; ++i)
new (&nbuf[i]) func_t(ev.p_funcs[i]); new (&nbuf[i]) func_t(ev.p_funcs[i]);
p_funcs = nbuf; p_funcs = nbuf;
} }
@ -39,7 +40,7 @@ namespace detail {
p_nfuncs = ev.p_nfuncs; p_nfuncs = ev.p_nfuncs;
auto *bufp = new unsigned char[sizeof(func_t) * p_nfuncs]; auto *bufp = new unsigned char[sizeof(func_t) * p_nfuncs];
func_t *nbuf = reinterpret_cast<func_t *>(bufp); func_t *nbuf = reinterpret_cast<func_t *>(bufp);
for (size_t i = 0; i < p_nfuncs; ++i) { for (std::size_t i = 0; i < p_nfuncs; ++i) {
new (&nbuf[i]) func_t(ev.p_funcs[i]); new (&nbuf[i]) func_t(ev.p_funcs[i]);
} }
p_funcs = nbuf; p_funcs = nbuf;
@ -54,7 +55,7 @@ namespace detail {
~signal_base() { clear(); } ~signal_base() { clear(); }
void clear() { void clear() {
for (size_t i = 0; i < p_nfuncs; ++i) { for (std::size_t i = 0; i < p_nfuncs; ++i) {
using func = std::function<void(C &, A...)>; using func = std::function<void(C &, A...)>;
p_funcs[i].~func(); p_funcs[i].~func();
} }
@ -64,9 +65,9 @@ namespace detail {
} }
template<typename F> template<typename F>
size_t connect(F &&func) { std::size_t connect(F &&func) {
using func_t = std::function<void(C &, A...)>; using func_t = std::function<void(C &, A...)>;
for (size_t i = 0; i < p_nfuncs; ++i) { for (std::size_t i = 0; i < p_nfuncs; ++i) {
if (!p_funcs[i]) { if (!p_funcs[i]) {
p_funcs[i] = std::forward<F>(func); p_funcs[i] = std::forward<F>(func);
return i; return i;
@ -74,7 +75,7 @@ namespace detail {
} }
auto *bufp = new unsigned char[sizeof(func_t) * (p_nfuncs + 1)]; auto *bufp = new unsigned char[sizeof(func_t) * (p_nfuncs + 1)];
func_t *nbuf = reinterpret_cast<func_t *>(bufp); func_t *nbuf = reinterpret_cast<func_t *>(bufp);
for (size_t i = 0; i < p_nfuncs; ++i) { for (std::size_t i = 0; i < p_nfuncs; ++i) {
new (&nbuf[i]) func_t(std::move(p_funcs[i])); new (&nbuf[i]) func_t(std::move(p_funcs[i]));
p_funcs[i].~func_t(); p_funcs[i].~func_t();
} }
@ -84,7 +85,7 @@ namespace detail {
return p_nfuncs++; return p_nfuncs++;
} }
bool disconnect(size_t idx) { bool disconnect(std::size_t idx) {
if ((idx >= p_nfuncs) || !p_funcs[idx]) { if ((idx >= p_nfuncs) || !p_funcs[idx]) {
return false; return false;
} }
@ -97,7 +98,7 @@ namespace detail {
if (!p_class) { if (!p_class) {
return; return;
} }
for (size_t i = 0; i < p_nfuncs; ++i) { for (std::size_t i = 0; i < p_nfuncs; ++i) {
if (p_funcs[i]) { if (p_funcs[i]) {
p_funcs[i](*p_class, args...); p_funcs[i](*p_class, args...);
} }
@ -124,7 +125,7 @@ namespace detail {
private: private:
C *p_class; C *p_class;
std::function<void(C &, A...)> *p_funcs; std::function<void(C &, A...)> *p_funcs;
size_t p_nfuncs; std::size_t p_nfuncs;
}; };
} /* namespace detail */ } /* namespace detail */
@ -151,9 +152,9 @@ public:
void clear() { p_base.clear(); } void clear() { p_base.clear(); }
template<typename F> template<typename F>
size_t connect(F &&func) { return p_base.connect(std::forward<F>(func)); } std::size_t connect(F &&func) { return p_base.connect(std::forward<F>(func)); }
bool disconnect(size_t idx) { return p_base.disconnect(idx); } bool disconnect(std::size_t idx) { return p_base.disconnect(idx); }
template<typename ...Args> template<typename ...Args>
void emit(Args &&...args) { p_base.emit(std::forward<Args>(args)...); } void emit(Args &&...args) { p_base.emit(std::forward<Args>(args)...); }
@ -190,9 +191,9 @@ public:
void clear() { p_base.clear(); } void clear() { p_base.clear(); }
template<typename F> template<typename F>
size_t connect(F &&func) { return p_base.connect(std::forward<F>(func)); } std::size_t connect(F &&func) { return p_base.connect(std::forward<F>(func)); }
bool disconnect(size_t idx) { return p_base.disconnect(idx); } bool disconnect(std::size_t idx) { return p_base.disconnect(idx); }
template<typename ...Args> template<typename ...Args>
void emit(Args &&...args) const { p_base.emit(std::forward<Args>(args)...); } void emit(Args &&...args) const { p_base.emit(std::forward<Args>(args)...); }

View File

@ -17,11 +17,8 @@
#ifndef OSTD_FORMAT_HH #ifndef OSTD_FORMAT_HH
#define OSTD_FORMAT_HH #define OSTD_FORMAT_HH
#include <stdio.h> #include <cstddef>
#include <stdlib.h> #include <climits>
#include <ctype.h>
#include <cmath>
#include <utility> #include <utility>
#include <stdexcept> #include <stdexcept>
#include <locale> #include <locale>
@ -111,8 +108,8 @@ namespace detail {
return ret; return ret;
} }
inline size_t read_digits(string_range &fmt, char *buf) { inline std::size_t read_digits(string_range &fmt, char *buf) {
size_t ret = 0; std::size_t ret = 0;
for (; !fmt.empty() && isdigit(fmt.front()); ++ret) { for (; !fmt.empty() && isdigit(fmt.front()); ++ret) {
*buf++ = fmt.front(); *buf++ = fmt.front();
fmt.pop_front(); fmt.pop_front();
@ -176,7 +173,7 @@ namespace detail {
inline char const *escape_fmt_char(char v, char quote) { inline char const *escape_fmt_char(char v, char quote) {
if ((v >= 0 && v < 0x20) || (v == quote)) { if ((v >= 0 && v < 0x20) || (v == quote)) {
return fmt_escapes[size_t(v)]; return fmt_escapes[std::size_t(v)];
} else if (v == 0x7F) { } else if (v == 0x7F) {
return "\\x7F"; return "\\x7F";
} }
@ -185,7 +182,7 @@ namespace detail {
/* retrieve width/precision */ /* retrieve width/precision */
template<typename T, typename ...A> template<typename T, typename ...A>
inline int get_arg_param(size_t idx, T const &val, A const &...args) { inline int get_arg_param(std::size_t idx, T const &val, A const &...args) {
if (idx) { if (idx) {
if constexpr(!sizeof...(A)) { if constexpr(!sizeof...(A)) {
throw format_error{"not enough format args"}; throw format_error{"not enough format args"};
@ -414,8 +411,8 @@ namespace detail {
* allowed. * allowed.
* * Pointers are formatted then. If the `s` specifier is used, the pointer * * Pointers are formatted then. If the `s` specifier is used, the pointer
* will be formatted as hex with the `0x` prefix. Otherwise it's converted * will be formatted as hex with the `0x` prefix. Otherwise it's converted
* to `size_t` and formatted with the specifier (might error depending on * to `std::size_t` and formatted with the specifier (might error depending
* the specifier). * on the specifier).
* * Then integers are formatted. Using the `s` specifier is like using the * * Then integers are formatted. Using the `s` specifier is like using the
* `d` specifier. * `d` specifier.
* * Floats follow. Using `s` is like using `g`. * * Floats follow. Using `s` is like using `g`.
@ -623,7 +620,7 @@ struct format_spec {
* @see set_width(), set_precision_arg(); * @see set_width(), set_precision_arg();
*/ */
template<typename ...A> template<typename ...A>
void set_width_arg(size_t idx, A const &...args) { void set_width_arg(std::size_t idx, A const &...args) {
p_width = detail::get_arg_param(idx, args...); p_width = detail::get_arg_param(idx, args...);
p_has_width = p_arg_width = true; p_has_width = p_arg_width = true;
} }
@ -656,7 +653,7 @@ struct format_spec {
* @see set_precision(), set_width_arg(); * @see set_precision(), set_width_arg();
*/ */
template<typename ...A> template<typename ...A>
void set_precision_arg(size_t idx, A const &...args) { void set_precision_arg(std::size_t idx, A const &...args) {
p_precision = detail::get_arg_param(idx, args...); p_precision = detail::get_arg_param(idx, args...);
p_has_precision = p_arg_precision = true; p_has_precision = p_arg_precision = true;
} }
@ -861,7 +858,7 @@ private:
} }
bool read_spec() { bool read_spec() {
size_t ndig = detail::read_digits(p_fmt, p_buf); std::size_t ndig = detail::read_digits(p_fmt, p_buf);
bool havepos = false; bool havepos = false;
p_index = 0; p_index = 0;
@ -877,11 +874,11 @@ private:
/* parse flags */ /* parse flags */
p_flags = p_gflags; p_flags = p_gflags;
size_t skipd = 0; std::size_t skipd = 0;
if (havepos || !ndig) { if (havepos || !ndig) {
p_flags |= detail::parse_fmt_flags(p_fmt, 0); p_flags |= detail::parse_fmt_flags(p_fmt, 0);
} else { } else {
for (size_t i = 0; i < ndig; ++i) { for (std::size_t i = 0; i < ndig; ++i) {
if (p_buf[i] != '0') { if (p_buf[i] != '0') {
break; break;
} }
@ -944,7 +941,9 @@ private:
} }
template<typename R> template<typename R>
void write_spaces(R &writer, size_t n, bool left, char c = ' ') const { void write_spaces(
R &writer, std::size_t n, bool left, char c = ' '
) const {
if (left == bool(p_flags & FMT_FLAG_DASH)) { if (left == bool(p_flags & FMT_FLAG_DASH)) {
return; return;
} }
@ -954,14 +953,14 @@ private:
/* string base writer */ /* string base writer */
template<typename R> template<typename R>
void write_str(R &writer, bool escape, string_range val) const { void write_str(R &writer, bool escape, string_range val) const {
size_t n = val.size(); std::size_t n = val.size();
if (has_precision()) { if (has_precision()) {
n = std::min(n, size_t(precision())); n = std::min(n, std::size_t(precision()));
} }
write_spaces(writer, n, true); write_spaces(writer, n, true);
if (escape) { if (escape) {
writer.put('"'); writer.put('"');
for (size_t i = 0; i < n; ++i) { for (std::size_t i = 0; i < n; ++i) {
if (val.empty()) { if (val.empty()) {
break; break;
} }
@ -989,7 +988,7 @@ private:
if (esc) { if (esc) {
char buf[6]; char buf[6];
buf[0] = '\''; buf[0] = '\'';
size_t elen = strlen(esc); std::size_t elen = strlen(esc);
memcpy(buf + 1, esc, elen); memcpy(buf + 1, esc, elen);
buf[elen + 1] = '\''; buf[elen + 1] = '\'';
write_val(writer, false, ostd::string_range{ write_val(writer, false, ostd::string_range{
@ -1013,7 +1012,7 @@ private:
void write_int(R &writer, bool ptr, bool neg, T val) const { void write_int(R &writer, bool ptr, bool neg, T val) const {
/* binary representation is the biggest, assume grouping */ /* binary representation is the biggest, assume grouping */
char buf[sizeof(T) * CHAR_BIT * 2]; char buf[sizeof(T) * CHAR_BIT * 2];
size_t n = 0; std::size_t n = 0;
char isp = spec(); char isp = spec();
if (isp == 's') { if (isp == 's') {
@ -1053,11 +1052,11 @@ private:
T vb = val % base; T vb = val % base;
buf[n++] = (vb + "70"[vb < 10]) | cmask; buf[n++] = (vb + "70"[vb < 10]) | cmask;
} }
size_t tn = n; std::size_t tn = n;
if (has_precision()) { if (has_precision()) {
int prec = precision(); int prec = precision();
if (size_t(prec) > tn) { if (std::size_t(prec) > tn) {
tn = size_t(prec); tn = std::size_t(prec);
} else if (!prec && zval) { } else if (!prec && zval) {
tn = 0; tn = 0;
} }
@ -1088,10 +1087,10 @@ private:
write_spaces(writer, tn + (!!pfx * 2) + sign, true, '0'); write_spaces(writer, tn + (!!pfx * 2) + sign, true, '0');
} }
if (tn) { if (tn) {
for (size_t i = 0; i < (tn - n); ++i) { for (std::size_t i = 0; i < (tn - n); ++i) {
writer.put('0'); writer.put('0');
} }
for (size_t i = 0; i < n; ++i) { for (std::size_t i = 0; i < n; ++i) {
writer.put(buf[n - i - 1]); writer.put(buf[n - i - 1]);
} }
} }
@ -1209,7 +1208,7 @@ private:
* char pointers are handled by the string case above * char pointers are handled by the string case above
*/ */
if constexpr(std::is_pointer_v<T>) { if constexpr(std::is_pointer_v<T>) {
write_int(writer, (spec() == 's'), false, size_t(val)); write_int(writer, (spec() == 's'), false, std::size_t(val));
return; return;
} }
/* integers */ /* integers */
@ -1239,7 +1238,7 @@ private:
/* actual writer */ /* actual writer */
template<typename R, typename T, typename ...A> template<typename R, typename T, typename ...A>
void write_arg( void write_arg(
R &writer, size_t idx, T const &val, A const &...args R &writer, std::size_t idx, T const &val, A const &...args
) const { ) const {
if (idx) { if (idx) {
if constexpr(!sizeof...(A)) { if constexpr(!sizeof...(A)) {
@ -1302,7 +1301,7 @@ private:
/* range writer */ /* range writer */
template<typename R, typename T, typename ...A> template<typename R, typename T, typename ...A>
void write_range( void write_range(
R &writer, size_t idx, bool expandval, string_range sep, R &writer, std::size_t idx, bool expandval, string_range sep,
T const &val, A const &...args T const &val, A const &...args
) const { ) const {
if (idx) { if (idx) {
@ -1323,7 +1322,7 @@ private:
} }
} }
template<size_t I, size_t N, typename R, typename T> template<std::size_t I, std::size_t N, typename R, typename T>
void write_tuple_val( void write_tuple_val(
R &writer, bool escape, string_range sep, T const &tup R &writer, bool escape, string_range sep, T const &tup
) const { ) const {
@ -1337,7 +1336,7 @@ private:
template<typename R, typename T, typename ...A> template<typename R, typename T, typename ...A>
void write_tuple( void write_tuple(
R &writer, size_t idx, T const &val, A const &...args R &writer, std::size_t idx, T const &val, A const &...args
) { ) {
if (idx) { if (idx) {
if constexpr(!sizeof...(A)) { if constexpr(!sizeof...(A)) {
@ -1358,9 +1357,9 @@ private:
template<typename R, typename ...A> template<typename R, typename ...A>
void write_fmt(R &writer, A const &...args) { void write_fmt(R &writer, A const &...args) {
size_t argidx = 1; std::size_t argidx = 1;
while (read_until_spec(writer)) { while (read_until_spec(writer)) {
size_t argpos = index(); std::size_t argpos = index();
if (is_nested()) { if (is_nested()) {
if (!argpos) { if (!argpos) {
argpos = argidx++; argpos = argidx++;
@ -1398,7 +1397,7 @@ private:
set_precision_arg(argpos - 2, args...); set_precision_arg(argpos - 2, args...);
} }
if (arg_width()) { if (arg_width()) {
if (argpos <= (size_t(argprec) + 1)) { if (argpos <= (std::size_t(argprec) + 1)) {
throw format_error{"argument width not given"}; throw format_error{"argument width not given"};
} }
set_width_arg(argpos - 2 - argprec, args...); set_width_arg(argpos - 2 - argprec, args...);
@ -1440,7 +1439,9 @@ private:
template<typename R> template<typename R>
struct fmt_num_put final: std::num_put<char, fmt_out<R>> { struct fmt_num_put final: std::num_put<char, fmt_out<R>> {
fmt_num_put(size_t refs = 0): std::num_put<char, fmt_out<R>>(refs) {} fmt_num_put(std::size_t refs = 0):
std::num_put<char, fmt_out<R>>(refs)
{}
~fmt_num_put() {} ~fmt_num_put() {}
}; };

View File

@ -145,8 +145,8 @@ struct generic_condvar {
} }
private: private:
static constexpr size_t cvars = sizeof(std::condition_variable); static constexpr auto cvars = sizeof(std::condition_variable);
static constexpr size_t icvars = static constexpr auto icvars =
sizeof(detail::cond_impl<std::condition_variable>); sizeof(detail::cond_impl<std::condition_variable>);
std::aligned_storage_t<std::max( std::aligned_storage_t<std::max(
6 * sizeof(void *) + (icvars - cvars), icvars 6 * sizeof(void *) + (icvars - cvars), icvars

View File

@ -16,6 +16,7 @@
#ifndef OSTD_IO_HH #ifndef OSTD_IO_HH
#define OSTD_IO_HH #define OSTD_IO_HH
#include <cstddef>
#include <cstdio> #include <cstdio>
#include <cerrno> #include <cerrno>
@ -203,7 +204,7 @@ struct OSTD_EXPORT file_stream: stream {
* *
* @see write_bytes() * @see write_bytes()
*/ */
size_t read_bytes(void *buf, size_t count); std::size_t read_bytes(void *buf, std::size_t count);
/** @brief Writes `count` bytes into the stream. /** @brief Writes `count` bytes into the stream.
* *
@ -211,7 +212,7 @@ struct OSTD_EXPORT file_stream: stream {
* *
* @see read_bytes() * @see read_bytes()
*/ */
void write_bytes(void const *buf, size_t count); void write_bytes(void const *buf, std::size_t count);
/** @brief Reads a single character from the stream. /** @brief Reads a single character from the stream.
* *
@ -280,8 +281,8 @@ namespace detail {
struct stdout_range: output_range<stdout_range> { struct stdout_range: output_range<stdout_range> {
using value_type = char; using value_type = char;
using reference = char &; using reference = char &;
using size_type = size_t; using size_type = std::size_t;
using difference_type = ptrdiff_t; using difference_type = std::ptrdiff_t;
stdout_range() {} stdout_range() {}
void put(char c) { void put(char c) {

View File

@ -23,7 +23,7 @@
#ifndef OSTD_PLATFORM_HH #ifndef OSTD_PLATFORM_HH
#define OSTD_PLATFORM_HH #define OSTD_PLATFORM_HH
#include <stdlib.h> #include <cstddef>
#include <cstdint> #include <cstdint>
#include <type_traits> #include <type_traits>
@ -278,7 +278,10 @@ inline std::uint64_t endian_swap64(std::uint64_t x) noexcept {
/* endian swap */ /* endian swap */
template<typename T, size_t N = sizeof(T), bool IsNum = std::is_arithmetic_v<T>> template<
typename T, std::size_t N = sizeof(T),
bool IsNum = std::is_arithmetic_v<T>
>
struct endian_swap; struct endian_swap;
template<typename T> template<typename T>

View File

@ -6,9 +6,7 @@
#ifndef OSTD_RANGE_HH #ifndef OSTD_RANGE_HH
#define OSTD_RANGE_HH #define OSTD_RANGE_HH
#include <stddef.h> #include <cstddef>
#include <string.h>
#include <new> #include <new>
#include <tuple> #include <tuple>
#include <utility> #include <utility>
@ -273,7 +271,7 @@ inline range_size_t<IR> range_pop_front_n(IR &range, range_size_t<IR> n) {
range = range.slice(n, rs); range = range.slice(n, rs);
return n; return n;
} else { } else {
size_t r = 0; range_size_t<IR> r = 0;
while (n-- && !range.empty()) { while (n-- && !range.empty()) {
range.pop_front(); range.pop_front();
++r; ++r;
@ -292,7 +290,7 @@ inline range_size_t<IR> range_pop_back_n(IR &range, range_size_t<IR> n) {
range = range.slice(0, rs - n); range = range.slice(0, rs - n);
return n; return n;
} else { } else {
size_t r = 0; range_size_t<IR> r = 0;
while (n-- && !range.empty()) { while (n-- && !range.empty()) {
range.pop_back(); range.pop_back();
++r; ++r;
@ -404,8 +402,8 @@ template<typename T>
struct noop_output_range: output_range<noop_output_range<T>> { struct noop_output_range: output_range<noop_output_range<T>> {
using value_type = T; using value_type = T;
using reference = T &; using reference = T &;
using size_type = size_t; using size_type = std::size_t;
using difference_type = ptrdiff_t; using difference_type = std::ptrdiff_t;
void put(T const &) {} void put(T const &) {}
}; };
@ -694,8 +692,8 @@ struct number_range: input_range<number_range<T>> {
using range_category = forward_range_tag; using range_category = forward_range_tag;
using value_type = T; using value_type = T;
using reference = T; using reference = T;
using size_type = size_t; using size_type = std::size_t;
using difference_type = ptrdiff_t; using difference_type = std::ptrdiff_t;
number_range() = delete; number_range() = delete;
number_range(T a, T b, T step = T(1)): number_range(T a, T b, T step = T(1)):
@ -882,7 +880,7 @@ public:
}; };
namespace detail { namespace detail {
template<size_t I, size_t N, typename T> template<std::size_t I, std::size_t N, typename T>
inline void join_range_pop(T &tup) { inline void join_range_pop(T &tup) {
if constexpr(I != N) { if constexpr(I != N) {
if (!std::get<I>(tup).empty()) { if (!std::get<I>(tup).empty()) {
@ -893,7 +891,7 @@ namespace detail {
} }
} }
template<size_t I, size_t N, typename T> template<std::size_t I, std::size_t N, typename T>
inline auto join_range_front(T &tup) { inline auto join_range_front(T &tup) {
if constexpr(I != N) { if constexpr(I != N) {
if (!std::get<I>(tup).empty()) { if (!std::get<I>(tup).empty()) {
@ -1051,8 +1049,8 @@ struct appender_range: output_range<appender_range<T>> {
void clear() { p_data.clear(); } void clear() { p_data.clear(); }
bool empty() const { return p_data.empty(); } bool empty() const { return p_data.empty(); }
void reserve(typename T::size_type cap) { p_data.reserve(cap); } void reserve(size_type cap) { p_data.reserve(cap); }
void resize(typename T::size_type len) { p_data.resize(len); } void resize(size_type len) { p_data.resize(len); }
size_type size() const { return p_data.size(); } size_type size() const { return p_data.size(); }
size_type capacity() const { return p_data.capacity(); } size_type capacity() const { return p_data.capacity(); }
@ -1245,7 +1243,7 @@ iterator_range<T const *> citer(std::initializer_list<T> init) noexcept {
return iterator_range<T const *>(init.begin(), init.end()); return iterator_range<T const *>(init.begin(), init.end());
} }
template<typename T, size_t N> template<typename T, std::size_t N>
struct ranged_traits<T[N]> { struct ranged_traits<T[N]> {
using range = iterator_range<T *>; using range = iterator_range<T *>;

View File

@ -38,6 +38,7 @@
#ifndef OSTD_STREAM_HH #ifndef OSTD_STREAM_HH
#define OSTD_STREAM_HH #define OSTD_STREAM_HH
#include <cstddef>
#include <cerrno> #include <cerrno>
#include <cstdlib> #include <cstdlib>
#include <type_traits> #include <type_traits>
@ -223,7 +224,7 @@ struct stream {
* *
* @see write_bytes(), get_char(), get(), get_line() * @see write_bytes(), get_char(), get(), get_line()
*/ */
virtual size_t read_bytes(void *, size_t) { virtual std::size_t read_bytes(void *, std::size_t) {
throw stream_error{EINVAL, std::generic_category()}; throw stream_error{EINVAL, std::generic_category()};
} }
@ -244,7 +245,7 @@ struct stream {
* *
* @see read_bytes(), put_char(), put(), write() * @see read_bytes(), put_char(), put(), write()
*/ */
virtual void write_bytes(void const *, size_t) { virtual void write_bytes(void const *, std::size_t) {
throw stream_error{EINVAL, std::generic_category()}; throw stream_error{EINVAL, std::generic_category()};
} }
@ -429,7 +430,7 @@ struct stream {
* @throws ostd::stream_error on write failure. * @throws ostd::stream_error on write failure.
*/ */
template<typename T> template<typename T>
void put(T const *v, size_t count) { void put(T const *v, std::size_t count) {
write_bytes(v, count * sizeof(T)); write_bytes(v, count * sizeof(T));
} }
@ -456,7 +457,7 @@ struct stream {
* @throws ostd::stream_error on read failure. * @throws ostd::stream_error on read failure.
*/ */
template<typename T> template<typename T>
size_t get(T *v, size_t count) { std::size_t get(T *v, std::size_t count) {
/* if eof was reached, at least return how many values succeeded */ /* if eof was reached, at least return how many values succeeded */
return read_bytes(v, count * sizeof(T)) / sizeof(T); return read_bytes(v, count * sizeof(T)) / sizeof(T);
} }
@ -556,7 +557,7 @@ struct stream_range<T, true>: input_range<stream_range<T>> {
using range_category = input_range_tag; using range_category = input_range_tag;
using value_type = T; using value_type = T;
using reference = T; using reference = T;
using size_type = size_t; using size_type = std::size_t;
using difference_type = stream_off_t; using difference_type = stream_off_t;
/** @brief Stream ranges are not default constructible. */ /** @brief Stream ranges are not default constructible. */
@ -670,7 +671,7 @@ struct stream_line_range: input_range<stream_line_range<T, TC>> {
using range_category = input_range_tag; using range_category = input_range_tag;
using value_type = TC; using value_type = TC;
using reference = TC &; using reference = TC &;
using size_type = size_t; using size_type = std::size_t;
using difference_type = stream_off_t; using difference_type = stream_off_t;
/** @brief Stream line ranges are not default constructible. */ /** @brief Stream line ranges are not default constructible. */

View File

@ -6,10 +6,8 @@
#ifndef OSTD_STRING_HH #ifndef OSTD_STRING_HH
#define OSTD_STRING_HH #define OSTD_STRING_HH
#include <stdio.h> #include <cstddef>
#include <stddef.h> #include <cctype>
#include <ctype.h>
#include <string> #include <string>
#include <string_view> #include <string_view>
#include <type_traits> #include <type_traits>
@ -27,8 +25,8 @@ struct basic_char_range: input_range<basic_char_range<T>> {
using range_category = contiguous_range_tag; using range_category = contiguous_range_tag;
using value_type = T; using value_type = T;
using reference = T &; using reference = T &;
using size_type = size_t; using size_type = std::size_t;
using difference_type = ptrdiff_t; using difference_type = std::ptrdiff_t;
private: private:
struct nat {}; struct nat {};
@ -43,7 +41,7 @@ public:
std::is_convertible_v<U, T *>, nat std::is_convertible_v<U, T *>, nat
> = nat{}): p_beg(beg) { > = nat{}): p_beg(beg) {
if constexpr(std::is_array_v<std::remove_reference_t<U>>) { if constexpr(std::is_array_v<std::remove_reference_t<U>>) {
size_t N = std::extent_v<std::remove_reference_t<U>>; std::size_t N = std::extent_v<std::remove_reference_t<U>>;
p_end = beg + N - (beg[N - 1] == '\0'); p_end = beg + N - (beg[N - 1] == '\0');
} else { } else {
p_end = beg + (beg ? TR::length(beg) : 0); p_end = beg + (beg ? TR::length(beg) : 0);
@ -95,16 +93,16 @@ public:
T &back() const { return *(p_end - 1); } T &back() const { return *(p_end - 1); }
size_t size() const { return p_end - p_beg; } size_type size() const { return p_end - p_beg; }
basic_char_range slice(size_t start, size_t end) const { basic_char_range slice(size_type start, size_type end) const {
return basic_char_range(p_beg + start, p_beg + end); return basic_char_range(p_beg + start, p_beg + end);
} }
basic_char_range slice(size_t start) const { basic_char_range slice(size_type start) const {
return slice(start, size()); return slice(start, size());
} }
T &operator[](size_t i) const { return p_beg[i]; } T &operator[](size_type i) const { return p_beg[i]; }
void put(T v) { void put(T v) {
if (p_beg == p_end) { if (p_beg == p_end) {
@ -118,7 +116,7 @@ public:
/* non-range */ /* non-range */
int compare(basic_char_range<T const> s) const { int compare(basic_char_range<T const> s) const {
size_t s1 = size(), s2 = s.size(); size_type s1 = size(), s2 = s.size();
int ret; int ret;
if (!s1 || !s2) { if (!s1 || !s2) {
goto diffsize; goto diffsize;
@ -131,9 +129,9 @@ diffsize:
} }
int case_compare(basic_char_range<T const> s) const { int case_compare(basic_char_range<T const> s) const {
size_t s1 = size(), s2 = s.size(); size_type s1 = size(), s2 = s.size();
for (size_t i = 0, ms = std::min(s1, s2); i < ms; ++i) { for (size_type i = 0, ms = std::min(s1, s2); i < ms; ++i) {
int d = toupper(p_beg[i]) - toupper(s[i]); int d = std::toupper(p_beg[i]) - std::toupper(s[i]);
if (d) { if (d) {
return d; return d;
} }
@ -381,7 +379,7 @@ inline std::basic_string<std::remove_cv_t<range_value_t<R>>, TR, A> make_string(
inline namespace literals { inline namespace literals {
inline namespace string_literals { inline namespace string_literals {
inline string_range operator "" _sr(char const *str, size_t len) { inline string_range operator "" _sr(char const *str, std::size_t len) {
return string_range(str, str + len); return string_range(str, str + len);
} }
} }
@ -396,12 +394,15 @@ private:
public: public:
temp_c_string() = delete; temp_c_string() = delete;
temp_c_string(temp_c_string const &) = delete; temp_c_string(temp_c_string const &) = delete;
temp_c_string(temp_c_string &&s): p_buf(s.p_buf), p_allocated(s.p_allocated) { temp_c_string(temp_c_string &&s):
p_buf(s.p_buf), p_allocated(s.p_allocated)
{
s.p_buf = nullptr; s.p_buf = nullptr;
s.p_allocated = false; s.p_allocated = false;
} }
temp_c_string(R input, std::remove_cv_t<range_value_t<R>> *sbuf, size_t bufsize) temp_c_string(
: p_buf(nullptr), p_allocated(false) { R input, std::remove_cv_t<range_value_t<R>> *sbuf, std::size_t bufsize
): p_buf(nullptr), p_allocated(false) {
if (input.empty()) { if (input.empty()) {
return; return;
} }
@ -427,8 +428,12 @@ public:
return *this; return *this;
} }
operator std::remove_cv_t<range_value_t<R>> const *() const { return p_buf; } operator std::remove_cv_t<range_value_t<R>> const *() const {
std::remove_cv_t<range_value_t<R>> const *get() const { return p_buf; } return p_buf;
}
std::remove_cv_t<range_value_t<R>> const *get() const {
return p_buf;
}
void swap(temp_c_string &s) { void swap(temp_c_string &s) {
using std::swap; using std::swap;
@ -444,7 +449,7 @@ inline void swap(temp_c_string<R> &a, temp_c_string<R> &b) {
template<typename R> template<typename R>
inline temp_c_string<R> to_temp_cstr( inline temp_c_string<R> to_temp_cstr(
R input, std::remove_cv_t<range_value_t<R>> *buf, size_t bufsize R input, std::remove_cv_t<range_value_t<R>> *buf, std::size_t bufsize
) { ) {
return temp_c_string<R>(input, buf, bufsize); return temp_c_string<R>(input, buf, bufsize);
} }
@ -455,7 +460,7 @@ namespace std {
template<typename T, typename TR> template<typename T, typename TR>
struct hash<ostd::basic_char_range<T, TR>> { struct hash<ostd::basic_char_range<T, TR>> {
size_t operator()(ostd::basic_char_range<T, TR> const &v) const { std::size_t operator()(ostd::basic_char_range<T, TR> const &v) const {
return hash<std::basic_string_view<std::remove_const_t<T>, TR>>{}(v); return hash<std::basic_string_view<std::remove_const_t<T>, TR>>{}(v);
} }
}; };

View File

@ -15,6 +15,7 @@
#ifndef OSTD_THREAD_POOL_HH #ifndef OSTD_THREAD_POOL_HH
#define OSTD_THREAD_POOL_HH #define OSTD_THREAD_POOL_HH
#include <cstddef>
#include <type_traits> #include <type_traits>
#include <functional> #include <functional>
#include <utility> #include <utility>
@ -115,12 +116,12 @@ struct thread_pool {
* *
* @param[in] size The number of threads to use. * @param[in] size The number of threads to use.
*/ */
void start(size_t size = std::thread::hardware_concurrency()) { void start(std::size_t size = std::thread::hardware_concurrency()) {
p_running = true; p_running = true;
auto tf = [this]() { auto tf = [this]() {
thread_run(); thread_run();
}; };
for (size_t i = 0; i < size; ++i) { for (std::size_t i = 0; i < size; ++i) {
p_thrs.push_back(std::thread{tf}); p_thrs.push_back(std::thread{tf});
} }
} }

View File

@ -14,6 +14,7 @@
#ifndef OSTD_UNORDERED_MAP_HH #ifndef OSTD_UNORDERED_MAP_HH
#define OSTD_UNORDERED_MAP_HH #define OSTD_UNORDERED_MAP_HH
#include <cstddef>
#include <unordered_map> #include <unordered_map>
#include <type_traits> #include <type_traits>
@ -61,7 +62,7 @@ template<
typename A = std::allocator<std::pair<K const, T>>, typename R typename A = std::allocator<std::pair<K const, T>>, typename R
> >
inline std::unordered_map<K, T, H, E, A> make_unordered_map( inline std::unordered_map<K, T, H, E, A> make_unordered_map(
R range, size_t bcount = 1, H const &hash = H{}, R range, std::size_t bcount = 1, H const &hash = H{},
E const &kequal = E{}, A const &alloc = A{} E const &kequal = E{}, A const &alloc = A{}
) { ) {
static_assert( static_assert(
@ -119,7 +120,7 @@ inline std::unordered_map<
std::tuple_element_t<0, range_value_t<R>>, std::tuple_element_t<0, range_value_t<R>>,
std::tuple_element_t<1, range_value_t<R>>, H, E, A std::tuple_element_t<1, range_value_t<R>>, H, E, A
> make_unordered_map( > make_unordered_map(
R &&range, size_t bcount = 1, H const &hash = H{}, R &&range, std::size_t bcount = 1, H const &hash = H{},
E const &kequal = E{}, A const &alloc = A{} E const &kequal = E{}, A const &alloc = A{}
) { ) {
static_assert( static_assert(

View File

@ -6,6 +6,8 @@
#ifndef OSTD_VECMATH_HH #ifndef OSTD_VECMATH_HH
#define OSTD_VECMATH_HH #define OSTD_VECMATH_HH
#include <cstddef>
namespace ostd { namespace ostd {
template<typename T> template<typename T>
@ -20,8 +22,8 @@ struct vec2 {
vec2(T v): x(v), y(v) {} vec2(T v): x(v), y(v) {}
vec2(T x, T y): x(x), y(y) {} vec2(T x, T y): x(x), y(y) {}
T &operator[](size_t idx) { return value[idx]; } T &operator[](std::size_t idx) { return value[idx]; }
T operator[](size_t idx) const { return value[idx]; } T operator[](std::size_t idx) const { return value[idx]; }
vec2 &add(T v) { vec2 &add(T v) {
x += v; y += v; x += v; y += v;
@ -146,8 +148,8 @@ struct vec3 {
vec3(T v): x(v), y(v), z(v) {} vec3(T v): x(v), y(v), z(v) {}
vec3(T x, T y, T z): x(x), y(y), z(z) {} vec3(T x, T y, T z): x(x), y(y), z(z) {}
T &operator[](size_t idx) { return value[idx]; } T &operator[](std::size_t idx) { return value[idx]; }
T operator[](size_t idx) const { return value[idx]; } T operator[](std::size_t idx) const { return value[idx]; }
vec3 &add(T v) { vec3 &add(T v) {
x += v; y += v; z += v; x += v; y += v; z += v;
@ -272,8 +274,8 @@ struct vec4 {
vec4(T v): x(v), y(v), z(v), w(v) {} vec4(T v): x(v), y(v), z(v), w(v) {}
vec4(T x, T y, T z, T w): x(x), y(y), z(z), w(w) {} vec4(T x, T y, T z, T w): x(x), y(y), z(z), w(w) {}
T &operator[](size_t idx) { return value[idx]; } T &operator[](std::size_t idx) { return value[idx]; }
T operator[](size_t idx) const { return value[idx]; } T operator[](std::size_t idx) const { return value[idx]; }
vec4 &add(T v) { vec4 &add(T v) {
x += v; y += v; z += v; w += v; x += v; y += v; z += v; w += v;

View File

@ -3,6 +3,7 @@
* This file is part of libostd. See COPYING.md for futher information. * This file is part of libostd. See COPYING.md for futher information.
*/ */
#include <cstddef>
#include <cstdlib> #include <cstdlib>
#include <new> #include <new>
#include <mutex> #include <mutex>
@ -35,7 +36,7 @@ namespace detail {
# endif # endif
#endif #endif
OSTD_EXPORT void *stack_alloc(size_t sz) { OSTD_EXPORT void *stack_alloc(std::size_t sz) {
#if defined(OSTD_PLATFORM_WIN32) #if defined(OSTD_PLATFORM_WIN32)
void *p = VirtualAlloc(0, sz, MEM_COMMIT, PAGE_READWRITE); void *p = VirtualAlloc(0, sz, MEM_COMMIT, PAGE_READWRITE);
if (!p) { if (!p) {
@ -61,7 +62,7 @@ namespace detail {
#endif #endif
} }
OSTD_EXPORT void stack_free(void *p, size_t sz) noexcept { OSTD_EXPORT void stack_free(void *p, std::size_t sz) noexcept {
#if defined(OSTD_PLATFORM_WIN32) #if defined(OSTD_PLATFORM_WIN32)
(void)sz; (void)sz;
VirtualFree(p, 0, MEM_RELEASE); VirtualFree(p, 0, MEM_RELEASE);
@ -74,18 +75,18 @@ namespace detail {
#endif #endif
} }
OSTD_EXPORT size_t stack_main_size() noexcept { OSTD_EXPORT std::size_t stack_main_size() noexcept {
#if defined(OSTD_PLATFORM_WIN32) #if defined(OSTD_PLATFORM_WIN32)
/* 4 MiB for windows... */ /* 4 MiB for windows... */
return 4 * 1024 * 1024; return 4 * 1024 * 1024;
#else #else
struct rlimit l; struct rlimit l;
getrlimit(RLIMIT_STACK, &l); getrlimit(RLIMIT_STACK, &l);
return size_t(l.rlim_cur); return std::size_t(l.rlim_cur);
#endif #endif
} }
OSTD_EXPORT void stack_protect(void *p, size_t sz) noexcept { OSTD_EXPORT void stack_protect(void *p, std::size_t sz) noexcept {
#if defined(OSTD_PLATFORM_WIN32) #if defined(OSTD_PLATFORM_WIN32)
DWORD oo; DWORD oo;
VirtualProtect(p, sz, PAGE_READWRITE | PAGE_GUARD, &oo); VirtualProtect(p, sz, PAGE_READWRITE | PAGE_GUARD, &oo);
@ -95,13 +96,13 @@ namespace detail {
} }
/* used by stack traits */ /* used by stack traits */
inline void ctx_pagesize(size_t *s) noexcept { inline void ctx_pagesize(std::size_t *s) noexcept {
#if defined(OSTD_PLATFORM_WIN32) #if defined(OSTD_PLATFORM_WIN32)
SYSTEM_INFO si; SYSTEM_INFO si;
GetSystemInfo(&si); GetSystemInfo(&si);
*s = size_t(si.dwPageSize); *s = std::size_t(si.dwPageSize);
#elif defined(OSTD_PLATFORM_POSIX) #elif defined(OSTD_PLATFORM_POSIX)
*s = size_t(sysconf(_SC_PAGESIZE)); *s = std::size_t(sysconf(_SC_PAGESIZE));
#endif #endif
} }
@ -127,14 +128,14 @@ bool stack_traits::is_unbounded() noexcept {
#endif #endif
} }
size_t stack_traits::page_size() noexcept { std::size_t stack_traits::page_size() noexcept {
static size_t size = 0; static std::size_t size = 0;
static std::once_flag fl; static std::once_flag fl;
std::call_once(fl, detail::ctx_pagesize, &size); std::call_once(fl, detail::ctx_pagesize, &size);
return size; return size;
} }
size_t stack_traits::minimum_size() noexcept { std::size_t stack_traits::minimum_size() noexcept {
#if defined(OSTD_PLATFORM_WIN32) #if defined(OSTD_PLATFORM_WIN32)
/* no func on windows, sane default of 8 KiB */ /* no func on windows, sane default of 8 KiB */
return 8 * 1024; return 8 * 1024;
@ -144,7 +145,7 @@ size_t stack_traits::minimum_size() noexcept {
#endif #endif
} }
size_t stack_traits::maximum_size() noexcept { std::size_t stack_traits::maximum_size() noexcept {
#if defined(OSTD_PLATFORM_WIN32) #if defined(OSTD_PLATFORM_WIN32)
/* value is technically undefined when is_unbounded() is /* value is technically undefined when is_unbounded() is
* true, just default to 1 GiB so we actually return something * true, just default to 1 GiB so we actually return something
@ -152,21 +153,21 @@ size_t stack_traits::maximum_size() noexcept {
return 1024 * 1024 * 1024; return 1024 * 1024 * 1024;
#elif defined(OSTD_PLATFORM_POSIX) #elif defined(OSTD_PLATFORM_POSIX)
/* can be RLIM_INFINITY, but that's ok, see above */ /* can be RLIM_INFINITY, but that's ok, see above */
return size_t(detail::ctx_rlimit().rlim_max); return std::size_t(detail::ctx_rlimit().rlim_max);
#endif #endif
} }
size_t stack_traits::default_size() noexcept { std::size_t stack_traits::default_size() noexcept {
#if defined(OSTD_PLATFORM_WIN32) #if defined(OSTD_PLATFORM_WIN32)
/* no func on windows either, default to 64 KiB */ /* no func on windows either, default to 64 KiB */
return 8 * 8 * 1024; return 8 * 8 * 1024;
#elif defined(OSTD_PLATFORM_POSIX) #elif defined(OSTD_PLATFORM_POSIX)
/* default to at least 64 KiB (see minimum_size comment) */ /* default to at least 64 KiB (see minimum_size comment) */
constexpr size_t r = std::max(8 * 8 * 1024, SIGSTKSZ); constexpr std::size_t r = std::max(8 * 8 * 1024, SIGSTKSZ);
if (is_unbounded()) { if (is_unbounded()) {
return r; return r;
} }
size_t m = maximum_size(); std::size_t m = maximum_size();
if (r > m) { if (r > m) {
return m; return m;
} }

View File

@ -3,6 +3,7 @@
* This file is part of libostd. See COPYING.md for futher information. * This file is part of libostd. See COPYING.md for futher information.
*/ */
#include <cstddef>
#include <cstdio> #include <cstdio>
#include <cstdlib> #include <cstdlib>
#include <cerrno> #include <cerrno>
@ -24,9 +25,9 @@ bool file_stream::open(string_range path, stream_mode mode) {
buf[path.size()] = '\0'; buf[path.size()] = '\0';
p_owned = false; p_owned = false;
#ifndef OSTD_PLATFORM_WIN32 #ifndef OSTD_PLATFORM_WIN32
p_f = std::fopen(buf, filemodes[size_t(mode)]); p_f = std::fopen(buf, filemodes[std::size_t(mode)]);
#else #else
if (fopen_s(&p_f, buf, filemodes[size_t(mode)]) != 0) { if (fopen_s(&p_f, buf, filemodes[std::size_t(mode)]) != 0) {
return false; return false;
} }
#endif #endif
@ -84,8 +85,8 @@ void file_stream::flush() {
} }
} }
size_t file_stream::read_bytes(void *buf, size_t count) { std::size_t file_stream::read_bytes(void *buf, std::size_t count) {
size_t readn = std::fread(buf, 1, count, p_f); std::size_t readn = std::fread(buf, 1, count, p_f);
if (readn != count) { if (readn != count) {
if (std::feof(p_f) != 0) { if (std::feof(p_f) != 0) {
return readn; return readn;
@ -95,7 +96,7 @@ size_t file_stream::read_bytes(void *buf, size_t count) {
return readn; return readn;
} }
void file_stream::write_bytes(void const *buf, size_t count) { void file_stream::write_bytes(void const *buf, std::size_t count) {
if (std::fwrite(buf, 1, count, p_f) != count) { if (std::fwrite(buf, 1, count, p_f) != count) {
throw stream_error{EIO, std::generic_category()}; throw stream_error{EIO, std::generic_category()};
} }