summaryrefslogtreecommitdiff
path: root/arm9/lib/libnns/src/NNS_FND_heapcommon.c
blob: 7364603045eac76a5f23069c1064ec729fbd24e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include "nitro.h"
#include "NNS_FND_heapcommon.h"
#include "NNS_FND_list.h"

BOOL sRootListInitialized;
NNSFndList sRootList;

ARM_FUNC void *NNS_FndGetNextListObject(NNSFndList *, void *);

ARM_FUNC static NNSiFndHeapHead* FindContainHeap(NNSFndList * pList, const void * memBlock)
{
    NNSiFndHeapHead * pHead = NULL;

    while ((pHead = NNS_FndGetNextListObject(pList, pHead)) != NULL)
    {
        if (pHead->heapStart <= memBlock && memBlock < pHead->heapEnd)
        {
            NNSiFndHeapHead * ret = FindContainHeap(&pHead->childList, memBlock);
            if (ret == NULL)
                ret = pHead;
            return ret;
        }
    }
    return NULL;
}

ARM_FUNC static NNSFndList* FindListContainHeap(const void * memBlock)
{
    NNSFndList* ret = &sRootList;
    NNSiFndHeapHead* pHead = FindContainHeap(&sRootList, memBlock);
    if (pHead != NULL)
        ret = &pHead->childList;
    return ret;
}

ARM_FUNC void NNSi_FndInitHeapHead(NNSiFndHeapHead *pHead, u32 signature, void* heapStart, void* heapEnd, u16 optionFlag)
{
    pHead->signature = signature;
    pHead->heapStart = heapStart;
    pHead->heapEnd = heapEnd;
    pHead->attribute = 0;
    SetOptForHeap(pHead, optionFlag);
    NNS_FndInitList(&pHead->childList, 4);
    if (!sRootListInitialized)
    {
        NNS_FndInitList(&sRootList, 4);
        sRootListInitialized = TRUE;
    }
    NNS_FndAppendListObject(FindListContainHeap(pHead), pHead);
}

ARM_FUNC void NNSi_FndFinalizeHeap(NNSiFndHeapHead *pHead)
{
    NNS_FndRemoveListObject(FindListContainHeap(pHead), pHead);
}