2022-11-13 23:29:50 +00:00
|
|
|
/**
|
|
|
|
* @file TwoHeadArena.c
|
|
|
|
*
|
|
|
|
* This file implements a simple general purpose double-ended stack allocator.
|
|
|
|
*
|
|
|
|
* A double-ended stack allocator accepts allocations at either the "head" or "tail" of its allotted memory region.
|
|
|
|
* While in general this type of allocator could accept deallocations on the most recently allocated block at either
|
|
|
|
* end, this implementation does not support any individual deallocations; the only provided way to deallocate anything
|
|
|
|
* is to reset the entire arena, deallocating everything. This scheme is most applicable to allocating similar data
|
|
|
|
* with identical lifetime.
|
|
|
|
*/
|
2020-10-03 15:22:44 +00:00
|
|
|
#include "global.h"
|
2020-03-17 04:31:30 +00:00
|
|
|
|
2020-03-22 21:19:43 +00:00
|
|
|
void* THA_GetHead(TwoHeadArena* tha) {
|
2020-03-17 04:31:30 +00:00
|
|
|
return tha->head;
|
|
|
|
}
|
|
|
|
|
2022-11-13 23:29:50 +00:00
|
|
|
void THA_SetHead(TwoHeadArena* tha, void* newHead) {
|
|
|
|
tha->head = newHead;
|
2020-03-17 04:31:30 +00:00
|
|
|
}
|
|
|
|
|
2020-03-22 21:19:43 +00:00
|
|
|
void* THA_GetTail(TwoHeadArena* tha) {
|
2020-03-17 04:31:30 +00:00
|
|
|
return tha->tail;
|
|
|
|
}
|
|
|
|
|
2022-11-13 23:29:50 +00:00
|
|
|
/**
|
|
|
|
* Allocates to the head of the Two Head Arena. The allocation will not have any alignment guarantees.
|
|
|
|
*/
|
|
|
|
void* THA_AllocHead(TwoHeadArena* tha, size_t size) {
|
2020-03-17 04:31:30 +00:00
|
|
|
void* start = tha->head;
|
2021-02-14 00:49:40 +00:00
|
|
|
|
2022-11-13 23:29:50 +00:00
|
|
|
tha->head = (u8*)tha->head + size;
|
2020-03-17 04:31:30 +00:00
|
|
|
return start;
|
|
|
|
}
|
|
|
|
|
2022-11-13 23:29:50 +00:00
|
|
|
void* THA_AllocHeadByte(TwoHeadArena* tha) {
|
|
|
|
return THA_AllocHead(tha, 1);
|
2020-03-17 04:31:30 +00:00
|
|
|
}
|
|
|
|
|
2022-11-13 23:29:50 +00:00
|
|
|
/**
|
|
|
|
* Allocates to the tail end of the Two Head Arena. The allocation will be aligned based on the size of the allocation.
|
|
|
|
* All allocations of 16 bytes or more will be aligned to 16-bytes. Otherwise, the alignment will be the largest power
|
|
|
|
* of 2 for which the size is a multiple, in order to accommodate the alignment requirements of any data types that can
|
|
|
|
* fit within the allocation.
|
|
|
|
*/
|
|
|
|
void* THA_AllocTail(TwoHeadArena* tha, size_t size) {
|
|
|
|
uintptr_t mask;
|
2020-03-17 04:31:30 +00:00
|
|
|
|
2020-03-22 21:19:43 +00:00
|
|
|
if (size == 8) {
|
2022-11-13 23:29:50 +00:00
|
|
|
// Align 8 for multiples of 8
|
|
|
|
mask = ALIGN_MASK(8);
|
2020-03-22 21:19:43 +00:00
|
|
|
} else if (size == 4 || size == 12) {
|
2022-11-13 23:29:50 +00:00
|
|
|
// Align 4 for multiples of 4
|
|
|
|
mask = ALIGN_MASK(4);
|
2020-03-22 21:19:43 +00:00
|
|
|
} else if (size == 2 || size == 6 || size == 10 || size == 12 || size == 14) {
|
2022-11-13 23:29:50 +00:00
|
|
|
// Align 2 for multiples of 2
|
|
|
|
mask = ALIGN_MASK(2);
|
|
|
|
} else if (size >= 0x10) {
|
|
|
|
// Align 0x10 for allocations greater than 0x10
|
|
|
|
mask = ALIGN_MASK(0x10);
|
2020-03-22 21:19:43 +00:00
|
|
|
} else {
|
2022-11-13 23:29:50 +00:00
|
|
|
//! @bug if size is less than 16 and odd the computation below will give NULL. The mask for this case would be
|
|
|
|
//! more sensible as ~0, for no extra alignment
|
|
|
|
mask = 0;
|
2020-03-22 21:19:43 +00:00
|
|
|
}
|
2020-03-17 04:31:30 +00:00
|
|
|
|
2022-11-13 23:29:50 +00:00
|
|
|
tha->tail = (void*)((((uintptr_t)tha->tail & mask) - size) & mask);
|
2021-01-15 22:18:15 +00:00
|
|
|
return tha->tail;
|
2020-03-17 04:31:30 +00:00
|
|
|
}
|
|
|
|
|
2022-11-13 23:29:50 +00:00
|
|
|
/**
|
|
|
|
* Allocates to the tail end of the Two Head Arena with guaranteed 16-byte alignment.
|
|
|
|
*/
|
|
|
|
void* THA_AllocTailAlign16(TwoHeadArena* tha, size_t size) {
|
|
|
|
uintptr_t mask = ALIGN_MASK(0x10);
|
2021-01-15 22:18:15 +00:00
|
|
|
|
2022-11-13 23:29:50 +00:00
|
|
|
tha->tail = (void*)((((uintptr_t)tha->tail & mask) - size) & (uintptr_t)(u64)mask);
|
2021-01-15 22:18:15 +00:00
|
|
|
return tha->tail;
|
2020-03-17 04:31:30 +00:00
|
|
|
}
|
|
|
|
|
2022-11-13 23:29:50 +00:00
|
|
|
/**
|
|
|
|
* Allocates to the tail end of the Two Head Arena using the provided mask to align the allocated region.
|
|
|
|
*
|
|
|
|
* @param tha Arena to allocate to
|
|
|
|
* @param size Size of the allocation
|
|
|
|
* @param mask Mask to use to align the allocated region. To align to n-bytes where n is a power of 2, use the
|
|
|
|
* ALIGN_MASK(n) macro
|
|
|
|
*
|
|
|
|
* @return Pointer to the start of the allocated block
|
|
|
|
*/
|
|
|
|
void* THA_AllocTailAlign(TwoHeadArena* tha, size_t size, uintptr_t mask) {
|
|
|
|
tha->tail = (void*)((((uintptr_t)tha->tail & mask) - size) & mask);
|
2021-01-15 22:18:15 +00:00
|
|
|
return tha->tail;
|
2020-03-17 04:31:30 +00:00
|
|
|
}
|
|
|
|
|
2022-11-13 23:29:50 +00:00
|
|
|
/**
|
|
|
|
* Gets the remaining size of the Two Head Arena
|
|
|
|
*
|
|
|
|
* @return Remaining size. A negative number indicates an overflow.
|
|
|
|
*/
|
|
|
|
s32 THA_GetRemaining(TwoHeadArena* tha) {
|
|
|
|
return (s32)((u8*)tha->tail - (u8*)tha->head);
|
2020-03-17 04:31:30 +00:00
|
|
|
}
|
|
|
|
|
2022-11-13 23:29:50 +00:00
|
|
|
/**
|
|
|
|
* @return true if the Two Head Arena has overflowed, false otherwise
|
|
|
|
*/
|
2020-03-22 21:19:43 +00:00
|
|
|
u32 THA_IsCrash(TwoHeadArena* tha) {
|
2022-11-13 23:29:50 +00:00
|
|
|
return THA_GetRemaining(tha) < 0;
|
2020-03-17 04:31:30 +00:00
|
|
|
}
|
|
|
|
|
2022-11-13 23:29:50 +00:00
|
|
|
/**
|
|
|
|
* Resets the head and tail positions of the Two Head Arena, all prior allocations are effectively considered free
|
|
|
|
* as any new allocations will begin to overwrite them.
|
|
|
|
*/
|
|
|
|
void THA_Reset(TwoHeadArena* tha) {
|
|
|
|
tha->head = tha->start;
|
|
|
|
tha->tail = (u8*)tha->start + tha->size;
|
2020-03-17 04:31:30 +00:00
|
|
|
}
|
|
|
|
|
2022-11-13 23:29:50 +00:00
|
|
|
/**
|
|
|
|
* Creates a new Two Head Arena at `start` with available size `size`
|
|
|
|
*/
|
|
|
|
void THA_Init(TwoHeadArena* tha, void* start, size_t size) {
|
|
|
|
tha->start = start;
|
2020-03-17 04:31:30 +00:00
|
|
|
tha->size = size;
|
2022-11-13 23:29:50 +00:00
|
|
|
THA_Reset(tha);
|
2020-03-17 04:31:30 +00:00
|
|
|
}
|
|
|
|
|
2022-11-13 23:29:50 +00:00
|
|
|
/**
|
|
|
|
* Destroys the Two Head Arena, no further allocations are possible
|
|
|
|
*/
|
|
|
|
void THA_Destroy(TwoHeadArena* tha) {
|
2020-03-17 04:31:30 +00:00
|
|
|
bzero(tha, sizeof(TwoHeadArena));
|
|
|
|
}
|