pokeemerald/gflib/malloc.h

59 lines
1.5 KiB
C
Raw Normal View History

#ifndef GUARD_ALLOC_H
#define GUARD_ALLOC_H
2017-09-01 16:53:06 +02:00
2017-11-13 05:16:51 +01:00
#define HEAP_SIZE 0x1C000
2017-09-16 04:12:45 +02:00
2017-11-02 18:19:49 +01:00
#define FREE_AND_SET_NULL(ptr) \
{ \
2022-09-17 01:36:44 +02:00
Free(ptr); \
2017-11-02 18:19:49 +01:00
ptr = NULL; \
}
2021-01-19 10:03:51 +01:00
#define TRY_FREE_AND_SET_NULL(ptr) if (ptr != NULL) FREE_AND_SET_NULL(ptr)
#define MALLOC_SYSTEM_ID 0xA3A3
struct MemBlock
{
// Whether this block is currently allocated.
u16 allocated:1;
u16 unused_00:4;
// High 11 bits of location pointer.
u16 locationHi:11;
// Magic number used for error checking. Should equal MALLOC_SYSTEM_ID.
u16 magic;
// Size of the block (not including this header struct).
u32 size:18;
// Low 14 bits of location pointer.
u32 locationLo:14;
// Previous block pointer. Equals sHeapStart if this is the first block.
struct MemBlock *prev;
// Next block pointer. Equals sHeapStart if this is the last block.
struct MemBlock *next;
// Data in the memory block. (Arrays of length 0 are a GNU extension.)
u8 data[0];
};
2017-09-20 03:00:24 +02:00
extern u8 gHeap[];
#define Alloc(size) Alloc_(size, __FILE__ ":" STR(__LINE__))
#define AllocZeroed(size) AllocZeroed_(size, __FILE__ ":" STR(__LINE__))
void *Alloc_(u32 size, const char *location);
void *AllocZeroed_(u32 size, const char *location);
2017-09-01 16:53:06 +02:00
void Free(void *pointer);
2017-09-16 04:12:45 +02:00
void InitHeap(void *pointer, u32 size);
2017-09-01 16:53:06 +02:00
const struct MemBlock *HeapHead(void);
const char *MemBlockLocation(const struct MemBlock *block);
#endif // GUARD_ALLOC_H