haze: use alignment, invalidate cached use values in console

This commit is contained in:
Liam 2023-04-15 20:48:20 -04:00
parent 48a05273be
commit d7b3676939
3 changed files with 13 additions and 9 deletions

View file

@ -61,9 +61,13 @@ namespace haze {
Result Initialize(EventReactor *reactor, PtpObjectHeap *object_heap) {
/* Register event reactor and heap. */
m_reactor = reactor;
m_reactor = reactor;
m_object_heap = object_heap;
/* Set cached use amounts to invalid values. */
m_last_heap_used = 0xffffffffu;
m_last_heap_total = 0xffffffffu;
/* Get whether we are launched in applet mode. */
AppletType applet_type = appletGetAppletType();
m_is_applet_mode = applet_type != AppletType_Application && applet_type != AppletType_SystemApplication;
@ -104,9 +108,9 @@ namespace haze {
private:
void RedrawConsole() {
/* Get use amounts from the heap. */
u32 heap_used = m_object_heap->GetUsedSize();
u32 heap_used = m_object_heap->GetUsedSize();
u32 heap_total = m_object_heap->GetTotalSize();
u32 heap_pct = heap_total > 0 ? static_cast<u32>((heap_used * 100ul) / heap_total) : 0;
u32 heap_pct = heap_total > 0 ? static_cast<u32>((heap_used * 100ul) / heap_total) : 0;
if (heap_used == m_last_heap_used && heap_total == m_last_heap_total) {
/* If usage didn't change, skip redrawing the console. */
@ -115,7 +119,7 @@ namespace haze {
}
/* Update cached use amounts. */
m_last_heap_used = heap_used;
m_last_heap_used = heap_used;
m_last_heap_total = heap_total;
/* Determine units to use for printing to the console. */

View file

@ -87,13 +87,13 @@ namespace haze {
public:
template <typename T = void>
constexpr T *Allocate(size_t n) {
/* Check for overflow. */
if (!util::CanAddWithoutOverflow(n, 7ul)) {
/* Check for overflow in alignment. */
if (!util::CanAddWithoutOverflow(n, alignof(u64) - 1)) {
return nullptr;
}
/* Round up the amount to a multiple of 8. */
n = util::AlignUp(n, 8);
/* Align the amount to satisfy allocation for u64. */
n = util::AlignUp(n, alignof(u64));
/* Check if the allocation is possible. */
if (!this->AllocationIsPossible(n)) {

View file

@ -44,7 +44,7 @@ namespace haze {
const size_t alloc_len = parent_name_len + 1 + name_len;
/* Allocate memory for the node. */
ObjectNode * const node = reinterpret_cast<ObjectNode *>(m_object_heap->Allocate(sizeof(ObjectNode) + alloc_len));
ObjectNode * const node = m_object_heap->Allocate<ObjectNode>(sizeof(ObjectNode) + alloc_len);
R_UNLESS(node != nullptr, haze::ResultOutOfMemory());
{