libluna: Add max() and min()
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
b63a8ff245
commit
159c05c064
@ -1,5 +1,6 @@
|
||||
#include <bits/errno-return.h>
|
||||
#include <fcntl.h>
|
||||
#include <luna/Common.h>
|
||||
#include <luna/Format.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
@ -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;
|
||||
|
@ -21,3 +21,39 @@ template <typename T> 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 <typename T, class... Args> 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 <typename T, class... Args> 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;
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
#pragma once
|
||||
#include <luna/CString.h>
|
||||
#include <luna/Common.h>
|
||||
#include <luna/StringView.h>
|
||||
#include <luna/Types.h>
|
||||
|
||||
@ -37,8 +38,7 @@ template <usize Size> 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;
|
||||
|
@ -367,7 +367,7 @@ Result<void*> 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); }
|
||||
|
Loading…
Reference in New Issue
Block a user