NSMBW-Decomp
A decompilation of New Super Mario Bros. Wii
Loading...
Searching...
No Matches
mem_heapCommon.h
1#ifndef RVL_SDK_MEM_HEAP_COMMON_H
2#define RVL_SDK_MEM_HEAP_COMMON_H
3#include <types.h>
4
5#include <revolution/MEM/mem_list.h>
6
7#include <revolution/OS.h>
8
9#include <string.h>
10#ifdef __cplusplus
11extern "C" {
12#endif
13
14typedef enum {
15 MEM_HEAP_OPT_CLEAR_ALLOC = (1 << 0),
16 MEM_HEAP_OPT_DEBUG_FILL = (1 << 1),
17 MEM_HEAP_OPT_CAN_LOCK = (1 << 2)
18} MEMHeapOpt;
19
20typedef struct MEMiHeapHead {
21 u32 magic; // at 0x0
22 MEMLink link; // at 0x4
23 MEMList list; // at 0xC
24 u8* start; // at 0x18
25 u8* end; // at 0x1C
26 OSMutex mutex; // at 0x20
27
28 union {
29 u32 attribute;
30 struct {
31 u32 attribute_0_24 : 24;
32 u32 opt : 8;
33 };
34 }; // at 0x38
36
37void MEMiInitHeapHead(MEMiHeapHead* heap, u32 magic, void* start, void* end,
38 u16 opt);
39void MEMiFinalizeHeap(MEMiHeapHead* heap);
40MEMiHeapHead* MEMFindContainHeap(const void* memBlock);
41
42static uintptr_t GetUIntPtr(const void* p) {
43 return (uintptr_t)p;
44}
45
46static void* AddU32ToPtr(const void* p, u32 ofs) {
47 return (void*)(GetUIntPtr(p) + ofs);
48}
49
50static void* SubU32ToPtr(const void* p, u32 ofs) {
51 return (void*)(GetUIntPtr(p) - ofs);
52}
53
54static const void* AddU32ToCPtr(const void* p, u32 ofs) {
55 return (const void*)(GetUIntPtr(p) + ofs);
56}
57
58static const void* SubU32ToCPtr(const void* p, u32 ofs) {
59 return (const void*)(GetUIntPtr(p) - ofs);
60}
61
62static s32 GetOffsetFromPtr(const void* start, const void* end) {
63 return GetUIntPtr(end) - GetUIntPtr(start);
64}
65
66static u16 GetOptForHeap(const MEMiHeapHead* heap) {
67 return heap->opt;
68}
69
70static void SetOptForHeap(MEMiHeapHead* heap, u16 opt) {
71 heap->opt = (u8)opt;
72}
73
74static void LockHeap(MEMiHeapHead* heap) {
75 if (GetOptForHeap(heap) & MEM_HEAP_OPT_CAN_LOCK) {
76 OSLockMutex(&heap->mutex);
77 }
78}
79
80static void UnlockHeap(MEMiHeapHead* heap) {
81 if (GetOptForHeap(heap) & MEM_HEAP_OPT_CAN_LOCK) {
82 OSUnlockMutex(&heap->mutex);
83 }
84}
85
86static void FillAllocMemory(MEMiHeapHead* heap, void* memBlock, u32 size) {
87 if (GetOptForHeap(heap) & MEM_HEAP_OPT_CLEAR_ALLOC) {
88 memset(memBlock, 0, size);
89 }
90}
91
92static s32 MEMGetHeapTotalSize(const MEMiHeapHead* heap) {
93 return GetOffsetFromPtr(heap, heap->end);
94}
95
96static void* MEMGetHeapEndAddress(const MEMiHeapHead* heap) {
97 return heap->end;
98}
99
100#ifdef __cplusplus
101}
102#endif
103#endif