summaryrefslogtreecommitdiff
path: root/src/memory_mid.c
blob: 546820afa528b57ccc5fadef7eb487d1182d6aef (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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include "global.h"
#include "memory.h"

extern u32 gUnknown_80B7F14;
extern u32 gUnknown_80B7F88;
extern const char gLocateSetErrorMessage[];
extern struct HeapDescriptor gMainHeapDescriptor;
extern const char gLocalCreateErrorMessage[];
extern s32 MemorySearchFromBack(struct HeapDescriptor *heap, s32, s32);
extern s32 MemorySearchFromFront(struct HeapDescriptor *heap, s32, s32);
extern struct HeapDescriptor * _LocateSetBack(struct HeapDescriptor *, u32, u32, u32, u32);
extern struct HeapDescriptor * _LocateSetFront(struct HeapDescriptor *, u32, u32, u32, u32);
void DoFree(struct HeapDescriptor *, void *);
void *DoAlloc(struct HeapDescriptor *, s32, u32);
extern struct HeapDescriptor *DoCreateSubHeap(u32 *, u32);

void FatalError(u32 *, const char *, ...) __attribute__((noreturn));


s32 _LocateSet(struct HeapDescriptor *heap, s32 size, s32 group)
{
  s32 index;
  struct HeapDescriptor * uVar2;
  s32 atb;
  
  if (heap == NULL) {
    heap = &gMainHeapDescriptor;
  }

  // Set some sort flag/attr?
  atb = group >> 8 | 1;

  // Reset it?
  group = group & 0xff;

  if ((atb & 2) != 0) {
    index = MemorySearchFromFront(heap,atb,size);
    if (index < 0) goto error;
    uVar2 = _LocateSetFront(heap,index,atb,size,group);
    return uVar2->freeCount;
  }
  else {
    index = MemorySearchFromBack(heap,atb,size);
    if (index < 0) goto error;
    uVar2 = _LocateSetBack(heap,index,atb,size,group);
    return uVar2->freeCount;
  }

error:
    // LocateSet [%p] buffer %8x size can't locate
        // atb %02x grp %3d
   FatalError(&gUnknown_80B7F14,
                 gLocateSetErrorMessage,
                 heap,size,atb,group);
}

void *MemoryAlloc(s32 size, s32 b)
{
    DoAlloc(&gMainHeapDescriptor, size, b);
}

void MemoryFree(void *a)
{
    DoFree(&gMainHeapDescriptor, a);
}

struct HeapDescriptor *MemoryLocate_LocalCreate(struct HeapDescriptor *parentHeap,u32 size,u32 param_3,u32 group)
{
  int index;
  struct HeapDescriptor *iVar2;
  struct HeapDescriptor *iVar3;
  s32 local_1c[2];
  
  if (parentHeap == NULL) {
    parentHeap = &gMainHeapDescriptor;
  }

  index = MemorySearchFromBack(parentHeap,9,size);
  if (index < 0)
    // Memroy LocalCreate buffer %08x size can't locate
    FatalError(&gUnknown_80B7F88,gLocalCreateErrorMessage,size);

  iVar2 = _LocateSetBack(parentHeap,index,9,size,group);
  local_1c[0] = iVar2->freeCount;
  local_1c[1] = iVar2->freeListLength;

  iVar3 = DoCreateSubHeap(local_1c,param_3);
  iVar3->parentHeap = parentHeap;
  return iVar3;
}