diff --git a/ostd/algorithm.hh b/ostd/algorithm.hh index 1e3df0c..6bcf556 100644 --- a/ostd/algorithm.hh +++ b/ostd/algorithm.hh @@ -6,8 +6,7 @@ #ifndef OSTD_ALGORITHM_HH #define OSTD_ALGORITHM_HH -#include - +#include #include #include #include @@ -148,10 +147,9 @@ namespace detail { template inline void introsort(R range, C &compare) { - detail::introloop( - range, compare, - static_cast>(2 * (log(range.size()) / log(2))) - ); + detail::introloop(range, compare, static_cast>( + 2 * (std::log(range.size()) / std::log(2)) + )); } } /* namespace detail */ diff --git a/ostd/concurrency.hh b/ostd/concurrency.hh index 4d440d7..418861a 100644 --- a/ostd/concurrency.hh +++ b/ostd/concurrency.hh @@ -46,6 +46,7 @@ #ifndef OSTD_CONCURRENCY_HH #define OSTD_CONCURRENCY_HH +#include #include #include #include @@ -162,7 +163,7 @@ public: /** @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; @@ -174,7 +175,7 @@ public: * * @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. * @@ -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) { std::lock_guard l{p_lock}; p_stacks.reserve(n); @@ -595,7 +596,7 @@ public: p_stacks.deallocate(st); } - void reserve_stacks(size_t n) { + void reserve_stacks(std::size_t n) { p_stacks.reserve(n); } @@ -720,7 +721,7 @@ public: * @param[in] sa The provided stack allocator. */ 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)) {} @@ -830,7 +831,7 @@ public: } } - void reserve_stacks(size_t n) { + void reserve_stacks(std::size_t n) { if constexpr(!SA::is_thread_safe) { std::lock_guard l{p_lock}; p_stacks.reserve(n); @@ -862,13 +863,13 @@ private: } void init() { - size_t size = p_threads; + std::size_t size = p_threads; std::vector thrs; 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(); }); } - for (size_t i = 0; i < size; ++i) { + for (std::size_t i = 0; i < size; ++i) { if (thrs[i].joinable()) { thrs[i].join(); } @@ -951,7 +952,7 @@ private: } } - size_t p_threads; + std::size_t p_threads; std::condition_variable p_cond; std::mutex p_lock; SA p_stacks; @@ -1019,7 +1020,7 @@ inline generator make_generator(F &&func) { return detail::current_scheduler->make_generator(std::forward(func)); } -inline void reserve_stacks(size_t n) { +inline void reserve_stacks(std::size_t n) { detail::current_scheduler->reserve_stacks(n); } diff --git a/ostd/context_stack.hh b/ostd/context_stack.hh index cfba4cd..859223b 100644 --- a/ostd/context_stack.hh +++ b/ostd/context_stack.hh @@ -18,6 +18,7 @@ #ifndef OSTD_CONTEXT_STACK_HH #define OSTD_CONTEXT_STACK_HH +#include #include #include @@ -48,7 +49,7 @@ namespace ostd { */ struct stack_context { 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 int valgrind_id = 0; #endif @@ -75,7 +76,7 @@ struct OSTD_EXPORT stack_traits { * some other value too. Stack sizes are typically a multiple of page * size. */ - static size_t page_size() noexcept; + static std::size_t page_size() noexcept; /** @brief Gets the minimum size a stack can have. * @@ -85,7 +86,7 @@ struct OSTD_EXPORT stack_traits { * * @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. * @@ -95,7 +96,7 @@ struct OSTD_EXPORT stack_traits { * * @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. * @@ -105,14 +106,14 @@ struct OSTD_EXPORT stack_traits { * * @see minimum_size(), maximum_size() */ - static size_t default_size() noexcept; + static std::size_t default_size() noexcept; }; namespace detail { - OSTD_EXPORT void *stack_alloc(size_t sz); - OSTD_EXPORT void stack_free(void *p, size_t sz) noexcept; - OSTD_EXPORT void stack_protect(void *p, size_t sz) noexcept; - OSTD_EXPORT size_t stack_main_size() noexcept; + OSTD_EXPORT void *stack_alloc(std::size_t sz); + OSTD_EXPORT void stack_free(void *p, std::size_t sz) noexcept; + OSTD_EXPORT void stack_protect(void *p, std::size_t sz) noexcept; + OSTD_EXPORT std::size_t stack_main_size() noexcept; } /** @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 * 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( Traits::is_unbounded() ? std::max(ss, Traits::minimum_size()) @@ -169,10 +170,10 @@ struct basic_fixedsize_stack { /** @brief Allocates a stack. */ stack_context allocate() { - size_t ss = p_size; + std::size_t ss = p_size; - size_t pgs = Traits::page_size(); - size_t asize = ss + pgs - 1 - (ss - 1) % pgs + (pgs * Protected); + std::size_t pgs = Traits::page_size(); + std::size_t asize = ss + pgs - 1 - (ss - 1) % pgs + (pgs * Protected); void *p = detail::stack_alloc(asize); if constexpr(Protected) { @@ -207,7 +208,7 @@ struct basic_fixedsize_stack { * stacks can be reserved ahead of time. This allocates only one stack * 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. * @@ -220,7 +221,7 @@ struct basic_fixedsize_stack { } private: - size_t p_size; + std::size_t p_size; }; /** @brief An unprotected fixed size stack using ostd::stack_traits. */ @@ -270,7 +271,7 @@ private: public: /** @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. */ using traits_type = Traits; @@ -303,11 +304,12 @@ public: * @param cs The number of stacks in each chunk. */ 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 */ - size_t pgs = Traits::page_size(); - size_t asize = ss + pgs - 1 - (ss - 1) % pgs + (pgs * Protected); + std::size_t pgs = Traits::page_size(); + std::size_t asize = ss + pgs - 1 - (ss - 1) % pgs + (pgs * Protected); p_stacksize = asize; p_chunksize = cs * asize; } @@ -347,8 +349,8 @@ public: /** @brief Destroys the stack pool and all memory managed by it. */ ~basic_stack_pool() { - size_t ss = p_stacksize; - size_t cs = p_chunksize; + std::size_t ss = p_stacksize; + std::size_t cs = p_chunksize; void *pc = p_chunk; while (pc) { void *p = pc; @@ -363,12 +365,12 @@ public: * to contain. If it already contains that number or more, this function * does nothing. Otherwise it potentially reserves some extra chunks. */ - void reserve(size_t n) { - size_t cap = p_capacity; + void reserve(std::size_t n) { + std::size_t cap = p_capacity; if (n <= cap) { 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); } @@ -380,7 +382,7 @@ public: */ stack_context allocate() { 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(nd) - ss; if constexpr(Protected) { detail::stack_protect(p, Traits::page_size()); @@ -436,15 +438,15 @@ private: stack_node *next; }; - stack_node *alloc_chunks(stack_node *un, size_t n) { - size_t ss = p_stacksize; - size_t cs = p_chunksize; - size_t cnum = cs / ss; + stack_node *alloc_chunks(stack_node *un, std::size_t n) { + std::size_t ss = p_stacksize; + std::size_t cs = p_chunksize; + 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); 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); nd->next_chunk = nullptr; nd->next = prevn; @@ -473,7 +475,7 @@ private: 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( static_cast(chunk) + (ssize * n) - sizeof(stack_node) ); @@ -482,9 +484,9 @@ private: void *p_chunk = nullptr; stack_node *p_unused = nullptr; - size_t p_chunksize; - size_t p_stacksize; - size_t p_capacity = 0; + std::size_t p_chunksize; + std::size_t p_stacksize; + std::size_t p_capacity = 0; }; /** @brief Swaps two stack pools. */ diff --git a/ostd/coroutine.hh b/ostd/coroutine.hh index da3334c..5c5a896 100644 --- a/ostd/coroutine.hh +++ b/ostd/coroutine.hh @@ -20,6 +20,7 @@ #ifndef OSTD_COROUTINE_HH #define OSTD_COROUTINE_HH +#include #include #include #include @@ -65,7 +66,7 @@ namespace detail { extern "C" OSTD_EXPORT 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 @@ -267,7 +268,7 @@ protected: p_stack = sa.allocate(); void *sp = get_stack_ptr(); - size_t asize = p_stack.size - ( + std::size_t asize = p_stack.size - ( static_cast(p_stack.ptr) - static_cast(sp) ); @@ -282,14 +283,14 @@ private: template void *get_stack_ptr() { /* 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 * 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(p_stack.ptr) - sasize - salign; - size_t space = sasize + salign; + std::size_t space = sasize + salign; sp = std::align(salign, sasize, sp, space); return sp; @@ -1203,8 +1204,8 @@ struct generator_range: input_range> { using range_category = input_range_tag; using value_type = T; using reference = T &; - using size_type = size_t; - using difference_type = ptrdiff_t; + using size_type = std::size_t; + using difference_type = std::ptrdiff_t; /** Generator ranges are not default constructible. */ generator_range() = delete; diff --git a/ostd/environ.hh b/ostd/environ.hh index 7fc9f43..1004e66 100644 --- a/ostd/environ.hh +++ b/ostd/environ.hh @@ -21,9 +21,8 @@ #include "ostd/platform.hh" #include "ostd/internal/win32.hh" -#include -#include - +#include +#include #include #include @@ -50,27 +49,11 @@ namespace ostd { inline std::optional env_get(string_range name) { char buf[256]; auto tbuf = to_temp_cstr(name, buf, sizeof(buf)); -#ifndef OSTD_PLATFORM_WIN32 - char const *ret = getenv(tbuf.get()); + char const *ret = std::getenv(tbuf.get()); if (!ret) { return std::nullopt; } return std::string{ret}; -#else - std::vector 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. diff --git a/ostd/event.hh b/ostd/event.hh index 04a8799..5a9af07 100644 --- a/ostd/event.hh +++ b/ostd/event.hh @@ -6,6 +6,7 @@ #ifndef OSTD_EVENT_HH #define OSTD_EVENT_HH +#include #include #include @@ -22,7 +23,7 @@ namespace detail { using func_t = std::function; auto *bufp = new unsigned char[sizeof(func_t) * p_nfuncs]; func_t *nbuf = reinterpret_cast(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]); p_funcs = nbuf; } @@ -39,7 +40,7 @@ namespace detail { p_nfuncs = ev.p_nfuncs; auto *bufp = new unsigned char[sizeof(func_t) * p_nfuncs]; func_t *nbuf = reinterpret_cast(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]); } p_funcs = nbuf; @@ -54,7 +55,7 @@ namespace detail { ~signal_base() { 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; p_funcs[i].~func(); } @@ -64,9 +65,9 @@ namespace detail { } template - size_t connect(F &&func) { + std::size_t connect(F &&func) { using func_t = std::function; - for (size_t i = 0; i < p_nfuncs; ++i) { + for (std::size_t i = 0; i < p_nfuncs; ++i) { if (!p_funcs[i]) { p_funcs[i] = std::forward(func); return i; @@ -74,7 +75,7 @@ namespace detail { } auto *bufp = new unsigned char[sizeof(func_t) * (p_nfuncs + 1)]; func_t *nbuf = reinterpret_cast(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])); p_funcs[i].~func_t(); } @@ -84,7 +85,7 @@ namespace detail { return p_nfuncs++; } - bool disconnect(size_t idx) { + bool disconnect(std::size_t idx) { if ((idx >= p_nfuncs) || !p_funcs[idx]) { return false; } @@ -97,7 +98,7 @@ namespace detail { if (!p_class) { return; } - for (size_t i = 0; i < p_nfuncs; ++i) { + for (std::size_t i = 0; i < p_nfuncs; ++i) { if (p_funcs[i]) { p_funcs[i](*p_class, args...); } @@ -124,7 +125,7 @@ namespace detail { private: C *p_class; std::function *p_funcs; - size_t p_nfuncs; + std::size_t p_nfuncs; }; } /* namespace detail */ @@ -151,9 +152,9 @@ public: void clear() { p_base.clear(); } template - size_t connect(F &&func) { return p_base.connect(std::forward(func)); } + std::size_t connect(F &&func) { return p_base.connect(std::forward(func)); } - bool disconnect(size_t idx) { return p_base.disconnect(idx); } + bool disconnect(std::size_t idx) { return p_base.disconnect(idx); } template void emit(Args &&...args) { p_base.emit(std::forward(args)...); } @@ -190,9 +191,9 @@ public: void clear() { p_base.clear(); } template - size_t connect(F &&func) { return p_base.connect(std::forward(func)); } + std::size_t connect(F &&func) { return p_base.connect(std::forward(func)); } - bool disconnect(size_t idx) { return p_base.disconnect(idx); } + bool disconnect(std::size_t idx) { return p_base.disconnect(idx); } template void emit(Args &&...args) const { p_base.emit(std::forward(args)...); } diff --git a/ostd/format.hh b/ostd/format.hh index 83cb6cb..2f762bf 100644 --- a/ostd/format.hh +++ b/ostd/format.hh @@ -17,11 +17,8 @@ #ifndef OSTD_FORMAT_HH #define OSTD_FORMAT_HH -#include -#include -#include -#include - +#include +#include #include #include #include @@ -111,8 +108,8 @@ namespace detail { return ret; } - inline size_t read_digits(string_range &fmt, char *buf) { - size_t ret = 0; + inline std::size_t read_digits(string_range &fmt, char *buf) { + std::size_t ret = 0; for (; !fmt.empty() && isdigit(fmt.front()); ++ret) { *buf++ = fmt.front(); fmt.pop_front(); @@ -176,7 +173,7 @@ namespace detail { inline char const *escape_fmt_char(char v, char 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) { return "\\x7F"; } @@ -185,7 +182,7 @@ namespace detail { /* retrieve width/precision */ template - 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 constexpr(!sizeof...(A)) { throw format_error{"not enough format args"}; @@ -414,8 +411,8 @@ namespace detail { * allowed. * * 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 - * to `size_t` and formatted with the specifier (might error depending on - * the specifier). + * to `std::size_t` and formatted with the specifier (might error depending + * on the specifier). * * Then integers are formatted. Using the `s` specifier is like using the * `d` specifier. * * Floats follow. Using `s` is like using `g`. @@ -623,7 +620,7 @@ struct format_spec { * @see set_width(), set_precision_arg(); */ template - 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_has_width = p_arg_width = true; } @@ -656,7 +653,7 @@ struct format_spec { * @see set_precision(), set_width_arg(); */ template - 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_has_precision = p_arg_precision = true; } @@ -861,7 +858,7 @@ private: } 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; p_index = 0; @@ -877,11 +874,11 @@ private: /* parse flags */ p_flags = p_gflags; - size_t skipd = 0; + std::size_t skipd = 0; if (havepos || !ndig) { p_flags |= detail::parse_fmt_flags(p_fmt, 0); } else { - for (size_t i = 0; i < ndig; ++i) { + for (std::size_t i = 0; i < ndig; ++i) { if (p_buf[i] != '0') { break; } @@ -944,7 +941,9 @@ private: } template - 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)) { return; } @@ -954,14 +953,14 @@ private: /* string base writer */ template 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()) { - n = std::min(n, size_t(precision())); + n = std::min(n, std::size_t(precision())); } write_spaces(writer, n, true); if (escape) { writer.put('"'); - for (size_t i = 0; i < n; ++i) { + for (std::size_t i = 0; i < n; ++i) { if (val.empty()) { break; } @@ -989,7 +988,7 @@ private: if (esc) { char buf[6]; buf[0] = '\''; - size_t elen = strlen(esc); + std::size_t elen = strlen(esc); memcpy(buf + 1, esc, elen); buf[elen + 1] = '\''; write_val(writer, false, ostd::string_range{ @@ -1013,7 +1012,7 @@ private: void write_int(R &writer, bool ptr, bool neg, T val) const { /* binary representation is the biggest, assume grouping */ char buf[sizeof(T) * CHAR_BIT * 2]; - size_t n = 0; + std::size_t n = 0; char isp = spec(); if (isp == 's') { @@ -1053,11 +1052,11 @@ private: T vb = val % base; buf[n++] = (vb + "70"[vb < 10]) | cmask; } - size_t tn = n; + std::size_t tn = n; if (has_precision()) { int prec = precision(); - if (size_t(prec) > tn) { - tn = size_t(prec); + if (std::size_t(prec) > tn) { + tn = std::size_t(prec); } else if (!prec && zval) { tn = 0; } @@ -1088,10 +1087,10 @@ private: write_spaces(writer, tn + (!!pfx * 2) + sign, true, '0'); } if (tn) { - for (size_t i = 0; i < (tn - n); ++i) { + for (std::size_t i = 0; i < (tn - n); ++i) { 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]); } } @@ -1209,7 +1208,7 @@ private: * char pointers are handled by the string case above */ if constexpr(std::is_pointer_v) { - write_int(writer, (spec() == 's'), false, size_t(val)); + write_int(writer, (spec() == 's'), false, std::size_t(val)); return; } /* integers */ @@ -1239,7 +1238,7 @@ private: /* actual writer */ template 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 { if (idx) { if constexpr(!sizeof...(A)) { @@ -1302,7 +1301,7 @@ private: /* range writer */ template 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 ) const { if (idx) { @@ -1323,7 +1322,7 @@ private: } } - template + template void write_tuple_val( R &writer, bool escape, string_range sep, T const &tup ) const { @@ -1337,7 +1336,7 @@ private: template 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 constexpr(!sizeof...(A)) { @@ -1358,9 +1357,9 @@ private: template void write_fmt(R &writer, A const &...args) { - size_t argidx = 1; + std::size_t argidx = 1; while (read_until_spec(writer)) { - size_t argpos = index(); + std::size_t argpos = index(); if (is_nested()) { if (!argpos) { argpos = argidx++; @@ -1398,7 +1397,7 @@ private: set_precision_arg(argpos - 2, args...); } if (arg_width()) { - if (argpos <= (size_t(argprec) + 1)) { + if (argpos <= (std::size_t(argprec) + 1)) { throw format_error{"argument width not given"}; } set_width_arg(argpos - 2 - argprec, args...); @@ -1440,7 +1439,9 @@ private: template struct fmt_num_put final: std::num_put> { - fmt_num_put(size_t refs = 0): std::num_put>(refs) {} + fmt_num_put(std::size_t refs = 0): + std::num_put>(refs) + {} ~fmt_num_put() {} }; diff --git a/ostd/generic_condvar.hh b/ostd/generic_condvar.hh index 5a5371e..5de40de 100644 --- a/ostd/generic_condvar.hh +++ b/ostd/generic_condvar.hh @@ -145,8 +145,8 @@ struct generic_condvar { } private: - static constexpr size_t cvars = sizeof(std::condition_variable); - static constexpr size_t icvars = + static constexpr auto cvars = sizeof(std::condition_variable); + static constexpr auto icvars = sizeof(detail::cond_impl); std::aligned_storage_t #include #include @@ -203,7 +204,7 @@ struct OSTD_EXPORT file_stream: stream { * * @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. * @@ -211,7 +212,7 @@ struct OSTD_EXPORT file_stream: stream { * * @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. * @@ -280,8 +281,8 @@ namespace detail { struct stdout_range: output_range { using value_type = char; using reference = char &; - using size_type = size_t; - using difference_type = ptrdiff_t; + using size_type = std::size_t; + using difference_type = std::ptrdiff_t; stdout_range() {} void put(char c) { diff --git a/ostd/platform.hh b/ostd/platform.hh index 43b9862..2b1e463 100644 --- a/ostd/platform.hh +++ b/ostd/platform.hh @@ -23,7 +23,7 @@ #ifndef OSTD_PLATFORM_HH #define OSTD_PLATFORM_HH -#include +#include #include #include @@ -278,7 +278,10 @@ inline std::uint64_t endian_swap64(std::uint64_t x) noexcept { /* endian swap */ -template> +template< + typename T, std::size_t N = sizeof(T), + bool IsNum = std::is_arithmetic_v +> struct endian_swap; template diff --git a/ostd/range.hh b/ostd/range.hh index 6f407d9..7bc2817 100644 --- a/ostd/range.hh +++ b/ostd/range.hh @@ -6,9 +6,7 @@ #ifndef OSTD_RANGE_HH #define OSTD_RANGE_HH -#include -#include - +#include #include #include #include @@ -273,7 +271,7 @@ inline range_size_t range_pop_front_n(IR &range, range_size_t n) { range = range.slice(n, rs); return n; } else { - size_t r = 0; + range_size_t r = 0; while (n-- && !range.empty()) { range.pop_front(); ++r; @@ -292,7 +290,7 @@ inline range_size_t range_pop_back_n(IR &range, range_size_t n) { range = range.slice(0, rs - n); return n; } else { - size_t r = 0; + range_size_t r = 0; while (n-- && !range.empty()) { range.pop_back(); ++r; @@ -404,8 +402,8 @@ template struct noop_output_range: output_range> { using value_type = T; using reference = T &; - using size_type = size_t; - using difference_type = ptrdiff_t; + using size_type = std::size_t; + using difference_type = std::ptrdiff_t; void put(T const &) {} }; @@ -694,8 +692,8 @@ struct number_range: input_range> { using range_category = forward_range_tag; using value_type = T; using reference = T; - using size_type = size_t; - using difference_type = ptrdiff_t; + using size_type = std::size_t; + using difference_type = std::ptrdiff_t; number_range() = delete; number_range(T a, T b, T step = T(1)): @@ -882,7 +880,7 @@ public: }; namespace detail { - template + template inline void join_range_pop(T &tup) { if constexpr(I != N) { if (!std::get(tup).empty()) { @@ -893,7 +891,7 @@ namespace detail { } } - template + template inline auto join_range_front(T &tup) { if constexpr(I != N) { if (!std::get(tup).empty()) { @@ -1051,8 +1049,8 @@ struct appender_range: output_range> { void clear() { p_data.clear(); } bool empty() const { return p_data.empty(); } - void reserve(typename T::size_type cap) { p_data.reserve(cap); } - void resize(typename T::size_type len) { p_data.resize(len); } + void reserve(size_type cap) { p_data.reserve(cap); } + void resize(size_type len) { p_data.resize(len); } size_type size() const { return p_data.size(); } size_type capacity() const { return p_data.capacity(); } @@ -1245,7 +1243,7 @@ iterator_range citer(std::initializer_list init) noexcept { return iterator_range(init.begin(), init.end()); } -template +template struct ranged_traits { using range = iterator_range; diff --git a/ostd/stream.hh b/ostd/stream.hh index ea5053b..714a871 100644 --- a/ostd/stream.hh +++ b/ostd/stream.hh @@ -38,6 +38,7 @@ #ifndef OSTD_STREAM_HH #define OSTD_STREAM_HH +#include #include #include #include @@ -223,7 +224,7 @@ struct stream { * * @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()}; } @@ -244,7 +245,7 @@ struct stream { * * @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()}; } @@ -429,7 +430,7 @@ struct stream { * @throws ostd::stream_error on write failure. */ template - void put(T const *v, size_t count) { + void put(T const *v, std::size_t count) { write_bytes(v, count * sizeof(T)); } @@ -456,7 +457,7 @@ struct stream { * @throws ostd::stream_error on read failure. */ template - 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 */ return read_bytes(v, count * sizeof(T)) / sizeof(T); } @@ -556,7 +557,7 @@ struct stream_range: input_range> { using range_category = input_range_tag; using value_type = T; using reference = T; - using size_type = size_t; + using size_type = std::size_t; using difference_type = stream_off_t; /** @brief Stream ranges are not default constructible. */ @@ -670,7 +671,7 @@ struct stream_line_range: input_range> { using range_category = input_range_tag; using value_type = TC; using reference = TC &; - using size_type = size_t; + using size_type = std::size_t; using difference_type = stream_off_t; /** @brief Stream line ranges are not default constructible. */ diff --git a/ostd/string.hh b/ostd/string.hh index 3a7bab9..40efc8c 100644 --- a/ostd/string.hh +++ b/ostd/string.hh @@ -6,10 +6,8 @@ #ifndef OSTD_STRING_HH #define OSTD_STRING_HH -#include -#include -#include - +#include +#include #include #include #include @@ -27,8 +25,8 @@ struct basic_char_range: input_range> { using range_category = contiguous_range_tag; using value_type = T; using reference = T &; - using size_type = size_t; - using difference_type = ptrdiff_t; + using size_type = std::size_t; + using difference_type = std::ptrdiff_t; private: struct nat {}; @@ -43,7 +41,7 @@ public: std::is_convertible_v, nat > = nat{}): p_beg(beg) { if constexpr(std::is_array_v>) { - size_t N = std::extent_v>; + std::size_t N = std::extent_v>; p_end = beg + N - (beg[N - 1] == '\0'); } else { p_end = beg + (beg ? TR::length(beg) : 0); @@ -95,16 +93,16 @@ public: 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); } - basic_char_range slice(size_t start) const { + basic_char_range slice(size_type start) const { 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) { if (p_beg == p_end) { @@ -118,7 +116,7 @@ public: /* non-range */ int compare(basic_char_range s) const { - size_t s1 = size(), s2 = s.size(); + size_type s1 = size(), s2 = s.size(); int ret; if (!s1 || !s2) { goto diffsize; @@ -131,9 +129,9 @@ diffsize: } int case_compare(basic_char_range s) const { - size_t s1 = size(), s2 = s.size(); - for (size_t i = 0, ms = std::min(s1, s2); i < ms; ++i) { - int d = toupper(p_beg[i]) - toupper(s[i]); + size_type s1 = size(), s2 = s.size(); + for (size_type i = 0, ms = std::min(s1, s2); i < ms; ++i) { + int d = std::toupper(p_beg[i]) - std::toupper(s[i]); if (d) { return d; } @@ -381,7 +379,7 @@ inline std::basic_string>, TR, A> make_string( inline namespace 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); } } @@ -396,12 +394,15 @@ private: public: temp_c_string() = 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_allocated = false; } - temp_c_string(R input, std::remove_cv_t> *sbuf, size_t bufsize) - : p_buf(nullptr), p_allocated(false) { + temp_c_string( + R input, std::remove_cv_t> *sbuf, std::size_t bufsize + ): p_buf(nullptr), p_allocated(false) { if (input.empty()) { return; } @@ -427,8 +428,12 @@ public: return *this; } - operator std::remove_cv_t> const *() const { return p_buf; } - std::remove_cv_t> const *get() const { return p_buf; } + operator std::remove_cv_t> const *() const { + return p_buf; + } + std::remove_cv_t> const *get() const { + return p_buf; + } void swap(temp_c_string &s) { using std::swap; @@ -444,7 +449,7 @@ inline void swap(temp_c_string &a, temp_c_string &b) { template inline temp_c_string to_temp_cstr( - R input, std::remove_cv_t> *buf, size_t bufsize + R input, std::remove_cv_t> *buf, std::size_t bufsize ) { return temp_c_string(input, buf, bufsize); } @@ -455,7 +460,7 @@ namespace std { template struct hash> { - size_t operator()(ostd::basic_char_range const &v) const { + std::size_t operator()(ostd::basic_char_range const &v) const { return hash, TR>>{}(v); } }; diff --git a/ostd/thread_pool.hh b/ostd/thread_pool.hh index ee3f3bd..666bc3d 100644 --- a/ostd/thread_pool.hh +++ b/ostd/thread_pool.hh @@ -15,6 +15,7 @@ #ifndef OSTD_THREAD_POOL_HH #define OSTD_THREAD_POOL_HH +#include #include #include #include @@ -115,12 +116,12 @@ struct thread_pool { * * @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; auto tf = [this]() { 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}); } } diff --git a/ostd/unordered_map.hh b/ostd/unordered_map.hh index 57627f3..4ad601d 100644 --- a/ostd/unordered_map.hh +++ b/ostd/unordered_map.hh @@ -14,6 +14,7 @@ #ifndef OSTD_UNORDERED_MAP_HH #define OSTD_UNORDERED_MAP_HH +#include #include #include @@ -61,7 +62,7 @@ template< typename A = std::allocator>, typename R > inline std::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{} ) { static_assert( @@ -119,7 +120,7 @@ inline std::unordered_map< std::tuple_element_t<0, range_value_t>, std::tuple_element_t<1, range_value_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{} ) { static_assert( diff --git a/ostd/vecmath.hh b/ostd/vecmath.hh index 6e67dec..1683c28 100644 --- a/ostd/vecmath.hh +++ b/ostd/vecmath.hh @@ -6,6 +6,8 @@ #ifndef OSTD_VECMATH_HH #define OSTD_VECMATH_HH +#include + namespace ostd { template @@ -20,8 +22,8 @@ struct vec2 { vec2(T v): x(v), y(v) {} vec2(T x, T y): x(x), y(y) {} - T &operator[](size_t idx) { return value[idx]; } - T operator[](size_t idx) const { return value[idx]; } + T &operator[](std::size_t idx) { return value[idx]; } + T operator[](std::size_t idx) const { return value[idx]; } vec2 &add(T v) { x += v; y += v; @@ -146,8 +148,8 @@ struct vec3 { vec3(T v): x(v), y(v), z(v) {} vec3(T x, T y, T z): x(x), y(y), z(z) {} - T &operator[](size_t idx) { return value[idx]; } - T operator[](size_t idx) const { return value[idx]; } + T &operator[](std::size_t idx) { return value[idx]; } + T operator[](std::size_t idx) const { return value[idx]; } vec3 &add(T 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 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[](size_t idx) const { return value[idx]; } + T &operator[](std::size_t idx) { return value[idx]; } + T operator[](std::size_t idx) const { return value[idx]; } vec4 &add(T v) { x += v; y += v; z += v; w += v; diff --git a/src/context_stack.cc b/src/context_stack.cc index 1f6f0d4..f128df1 100644 --- a/src/context_stack.cc +++ b/src/context_stack.cc @@ -3,6 +3,7 @@ * This file is part of libostd. See COPYING.md for futher information. */ +#include #include #include #include @@ -35,7 +36,7 @@ namespace detail { # endif #endif - OSTD_EXPORT void *stack_alloc(size_t sz) { + OSTD_EXPORT void *stack_alloc(std::size_t sz) { #if defined(OSTD_PLATFORM_WIN32) void *p = VirtualAlloc(0, sz, MEM_COMMIT, PAGE_READWRITE); if (!p) { @@ -61,7 +62,7 @@ namespace detail { #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) (void)sz; VirtualFree(p, 0, MEM_RELEASE); @@ -74,18 +75,18 @@ namespace detail { #endif } - OSTD_EXPORT size_t stack_main_size() noexcept { + OSTD_EXPORT std::size_t stack_main_size() noexcept { #if defined(OSTD_PLATFORM_WIN32) /* 4 MiB for windows... */ return 4 * 1024 * 1024; #else struct rlimit l; getrlimit(RLIMIT_STACK, &l); - return size_t(l.rlim_cur); + return std::size_t(l.rlim_cur); #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) DWORD oo; VirtualProtect(p, sz, PAGE_READWRITE | PAGE_GUARD, &oo); @@ -95,13 +96,13 @@ namespace detail { } /* 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) SYSTEM_INFO si; GetSystemInfo(&si); - *s = size_t(si.dwPageSize); + *s = std::size_t(si.dwPageSize); #elif defined(OSTD_PLATFORM_POSIX) - *s = size_t(sysconf(_SC_PAGESIZE)); + *s = std::size_t(sysconf(_SC_PAGESIZE)); #endif } @@ -127,14 +128,14 @@ bool stack_traits::is_unbounded() noexcept { #endif } -size_t stack_traits::page_size() noexcept { - static size_t size = 0; +std::size_t stack_traits::page_size() noexcept { + static std::size_t size = 0; static std::once_flag fl; std::call_once(fl, detail::ctx_pagesize, &size); return size; } -size_t stack_traits::minimum_size() noexcept { +std::size_t stack_traits::minimum_size() noexcept { #if defined(OSTD_PLATFORM_WIN32) /* no func on windows, sane default of 8 KiB */ return 8 * 1024; @@ -144,7 +145,7 @@ size_t stack_traits::minimum_size() noexcept { #endif } -size_t stack_traits::maximum_size() noexcept { +std::size_t stack_traits::maximum_size() noexcept { #if defined(OSTD_PLATFORM_WIN32) /* value is technically undefined when is_unbounded() is * 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; #elif defined(OSTD_PLATFORM_POSIX) /* 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 } -size_t stack_traits::default_size() noexcept { +std::size_t stack_traits::default_size() noexcept { #if defined(OSTD_PLATFORM_WIN32) /* no func on windows either, default to 64 KiB */ return 8 * 8 * 1024; #elif defined(OSTD_PLATFORM_POSIX) /* 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()) { return r; } - size_t m = maximum_size(); + std::size_t m = maximum_size(); if (r > m) { return m; } diff --git a/src/io.cc b/src/io.cc index d03d6bf..5d2f66c 100644 --- a/src/io.cc +++ b/src/io.cc @@ -3,6 +3,7 @@ * This file is part of libostd. See COPYING.md for futher information. */ +#include #include #include #include @@ -24,9 +25,9 @@ bool file_stream::open(string_range path, stream_mode mode) { buf[path.size()] = '\0'; p_owned = false; #ifndef OSTD_PLATFORM_WIN32 - p_f = std::fopen(buf, filemodes[size_t(mode)]); + p_f = std::fopen(buf, filemodes[std::size_t(mode)]); #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; } #endif @@ -84,8 +85,8 @@ void file_stream::flush() { } } -size_t file_stream::read_bytes(void *buf, size_t count) { - size_t readn = std::fread(buf, 1, count, p_f); +std::size_t file_stream::read_bytes(void *buf, std::size_t count) { + std::size_t readn = std::fread(buf, 1, count, p_f); if (readn != count) { if (std::feof(p_f) != 0) { return readn; @@ -95,7 +96,7 @@ size_t file_stream::read_bytes(void *buf, size_t count) { 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) { throw stream_error{EIO, std::generic_category()}; }