diff --git a/libc/src/stdio.cpp b/libc/src/stdio.cpp index 3a9365e8..5d6e4a38 100644 --- a/libc/src/stdio.cpp +++ b/libc/src/stdio.cpp @@ -1,5 +1,6 @@ #include #include +#include #include #include #include @@ -107,7 +108,7 @@ static ssize_t write_into_buffer(FILE* stream, const u8* data, ssize_t size) } ssize_t size_remaining = stream->_buf.capacity - stream->_buf.size; - nwritten = size > size_remaining ? size_remaining : size; + nwritten = min(size_remaining, size); memcpy(stream->_buf.buffer + stream->_buf.size, data, nwritten); stream->_buf.status |= FileStatusFlags::LastWrite; @@ -120,7 +121,7 @@ static ssize_t write_into_buffer(FILE* stream, const u8* data, ssize_t size) } } else - nwritten = write(stream->_fd, data, size > BUFSIZ ? BUFSIZ : size); + nwritten = write(stream->_fd, data, min(size, BUFSIZ)); if (nwritten < 0) return nwritten; size -= nwritten; data += nwritten; @@ -160,13 +161,13 @@ static ssize_t read_from_buffer(FILE* stream, u8* data, ssize_t size) } ssize_t size_remaining = stream->_buf.size - stream->_buf.index; - nread = size > size_remaining ? size_remaining : size; + nread = min(size_remaining, size); memcpy(data, stream->_buf.buffer + stream->_buf.index, nread); stream->_buf.index += nread; } else - nread = read(stream->_fd, data, size > BUFSIZ ? BUFSIZ : size); + nread = read(stream->_fd, data, min(size, BUFSIZ)); if (nread < 0) return nread; if (nread == 0) return total_read; size -= nread; diff --git a/libluna/include/luna/Common.h b/libluna/include/luna/Common.h index 6ed20e0a..b6976823 100644 --- a/libluna/include/luna/Common.h +++ b/libluna/include/luna/Common.h @@ -21,3 +21,39 @@ template inline constexpr T ceil_div(T a, T b) { return (a + (b - 1)) / b; } + +/** + * @brief Return the minimum value out of a set of arguments. + * + * @tparam T The type of the arguments. + * @param a The first value of the set of arguments. + * @param args The rest of the set of arguments. + * @return constexpr T The minimum value. + */ +template inline constexpr T min(T a, Args... args) +{ + if constexpr (sizeof...(args) == 0) return a; + else + { + T b = min(args...); + return a > b ? b : a; + } +} + +/** + * @brief Return the maximum value out of a set of arguments. + * + * @tparam T The type of the arguments. + * @param a The first value of the set of arguments. + * @param args The rest of the set of arguments. + * @return constexpr T The maximum value. + */ +template inline constexpr T max(T a, Args... args) +{ + if constexpr (sizeof...(args) == 0) return a; + else + { + T b = max(args...); + return a > b ? a : b; + } +} diff --git a/libluna/include/luna/StaticString.h b/libluna/include/luna/StaticString.h index 19be34a6..23e5efcb 100644 --- a/libluna/include/luna/StaticString.h +++ b/libluna/include/luna/StaticString.h @@ -1,5 +1,6 @@ #pragma once #include +#include #include #include @@ -37,8 +38,7 @@ template class StaticString void adopt(StringView string) { - usize length = strlcpy(m_buffer, string.chars(), - string.length() > sizeof(m_buffer) ? sizeof(m_buffer) : string.length() + 1); + usize length = strlcpy(m_buffer, string.chars(), min(sizeof(m_buffer), string.length() + 1)); if (length > Size) { m_length = Size; } else m_length = length; diff --git a/libluna/src/Heap.cpp b/libluna/src/Heap.cpp index dbd34a1c..1d85d2a7 100644 --- a/libluna/src/Heap.cpp +++ b/libluna/src/Heap.cpp @@ -367,7 +367,7 @@ Result realloc_impl(void* ptr, usize size, bool may_realloc_again) lock.take_over().unlock(); void* const new_ptr = TRY(malloc_impl(size, may_realloc_again, false)); - memcpy(new_ptr, ptr, old_size > size ? size : old_size); + memcpy(new_ptr, ptr, min(size, old_size)); TRY(free_impl(ptr)); if (old_size < size) { memset(offset_ptr(new_ptr, old_size), MALLOC_SCRUB_BYTE, size - old_size); }