Skip to content

Commit

Permalink
Improvement/gma alignment (SynthstromAudible#3220)
Browse files Browse the repository at this point in the history
* align gma to 16 bytes

* fix test  to account for alignment

* add missing header for this test

* restore accidentally deleted config file

* raise average allocation size in tests to reflect real usage
  • Loading branch information
m-m-adams authored Jan 6, 2025
1 parent baf20ed commit 2ad07a5
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 10 deletions.
1 change: 1 addition & 0 deletions src/OSLikeStuff/timers_interrupts/clock_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ extern "C" {
#endif

#include <math.h>
#include <stdint.h>
#define DELUGE_CLOCKS_PER 33330000
#define DELUGE_CLOCKS_PERf 33330000.
#define ONE_OVER_CLOCK (1 / DELUGE_CLOCKS_PERf)
Expand Down
13 changes: 8 additions & 5 deletions src/deluge/memory/memory_region.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ MemoryRegion::MemoryRegion() : emptySpaces(sizeof(EmptySpaceRecord)) {
void MemoryRegion::setup(void* emptySpacesMemory, int32_t emptySpacesMemorySize, uint32_t regionBegin,
uint32_t regionEnd) {
emptySpaces.setStaticMemory(emptySpacesMemory, emptySpacesMemorySize);
// bit of a hack - the allocations start with a 4 byte type+size header, this ensures the
// resulting allocations are still aligned to 16 bytes (which should generally be fine for anything?)
regionBegin = (regionBegin & 0xFFFFFFF0) + 16;

start = regionBegin;
// this is actually the location of the footer but that's better anyway
end = regionEnd - 8;
Expand All @@ -53,6 +57,8 @@ void MemoryRegion::setup(void* emptySpacesMemory, int32_t emptySpacesMemorySize,
}

uint32_t MemoryRegion::padSize(uint32_t requiredSize) {
requiredSize += 8; // dirty hack - we need the size with its headers to be aligned so we'll add it here then
// subtract 8 afterwards
if (requiredSize < minAlign) {
requiredSize = minAlign;
}
Expand All @@ -69,7 +75,7 @@ uint32_t MemoryRegion::padSize(uint32_t requiredSize) {
}
requiredSize += extraSize;
}
return requiredSize;
return requiredSize - 8;
}

bool seenYet = false;
Expand Down Expand Up @@ -426,7 +432,6 @@ void* MemoryRegion::alloc(uint32_t requiredSize, bool makeStealable, void* thing
// Returns new size
uint32_t MemoryRegion::shortenRight(void* address, uint32_t newSize) {
newSize = padSize(newSize);

uint32_t* __restrict__ header = (uint32_t*)((char*)address - 4);
uint32_t oldAllocatedSize = *header & SPACE_SIZE_MASK;
uint32_t allocationType = *header & SPACE_TYPE_MASK;
Expand Down Expand Up @@ -460,7 +465,6 @@ uint32_t MemoryRegion::shortenRight(void* address, uint32_t newSize) {

// Returns how much it was shortened by
uint32_t MemoryRegion::shortenLeft(void* address, uint32_t amountToShorten, uint32_t numBytesToMoveRightIfSuccessful) {

uint32_t* __restrict__ header = (uint32_t*)((char*)address - 4);
uint32_t oldAllocatedSize = *header & SPACE_SIZE_MASK;
uint32_t allocationType = *header & SPACE_TYPE_MASK;
Expand Down Expand Up @@ -515,7 +519,6 @@ void MemoryRegion::writeTempHeadersBeforeASteal(uint32_t newStartAddress, uint32
// Will grab one item of empty or stealable space to the right of the supplied allocation.
// Returns new size, or same size if couldn't extend.
uint32_t MemoryRegion::extendRightAsMuchAsEasilyPossible(void* address) {

uint32_t* __restrict__ header = (uint32_t*)(static_cast<char*>(address) - 4);
uint32_t spaceSize = (*header & SPACE_SIZE_MASK);
uint32_t currentSpaceType = *header & SPACE_TYPE_MASK;
Expand Down Expand Up @@ -742,7 +745,6 @@ gotEnoughMemory: {}
void MemoryRegion::extend(void* address, uint32_t minAmountToExtend, uint32_t idealAmountToExtend,
uint32_t* __restrict__ getAmountExtendedLeft, uint32_t* __restrict__ getAmountExtendedRight,
void* thingNotToStealFrom) {

minAmountToExtend = padSize(minAmountToExtend);
idealAmountToExtend = padSize(idealAmountToExtend);

Expand Down Expand Up @@ -823,6 +825,7 @@ void MemoryRegion::dealloc(void* address) {
// uint16_t startTime = *TCNT[TIMER_SYSTEM_FAST];

uint32_t* __restrict__ header = (uint32_t*)((uint32_t)address - 4);

uint32_t spaceSize = (*header & SPACE_SIZE_MASK);

#if ALPHA_OR_BETA_VERSION
Expand Down
1 change: 0 additions & 1 deletion src/deluge/memory/memory_region.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ struct NeighbouringMemoryGrabAttemptResult {
int32_t amountsExtended[2];
uint32_t longestRunFound; // Only valid if didn't return some space.
};

#define SPACE_HEADER_EMPTY 0
#define SPACE_HEADER_STEALABLE 0x40000000
#define SPACE_HEADER_ALLOCATED 0x80000000
Expand Down
11 changes: 7 additions & 4 deletions tests/32bit_unit_tests/memory_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,9 @@ TEST(MemoryAllocation, allocationSizes) {
for (int i = 0; i < expectedAllocations; i++) {
if (!testAllocations[i]) {
// this is to make a log distribution - probably the worst case for packing efficiency
int magnitude = rand() % 16;
int size = (rand() % 10) << magnitude;
// min allocation size is 8 bytes
int magnitude = rand() % 16 + 2;
int size = (rand() % 10 + 1) << magnitude;
void* testalloc = memreg.alloc(size, false, NULL);
if (testalloc) {
totalSize += size;
Expand All @@ -229,11 +230,13 @@ TEST(MemoryAllocation, allocationSizes) {

// we should have one empty space left, and it should be the size of the memory minus headers
CHECK(memreg.emptySpaces.getNumElements() == 1);
CHECK(memreg.emptySpaces.getKeyAtIndex(0) == mem_size - 16);
// we might have needed to align the region start to 16 after setting the headers
int sizeDiff = (mem_size - 16 - memreg.emptySpaces.getKeyAtIndex(0));
CHECK(sizeDiff <= 16);
}
// un modified GMA gets .999311
// current with extra padding gets .9939
// std::cout << "Packing factor: " << (average_packing_factor / numRepeats) << std::endl;
std::cout << "Packing factor: " << (average_packing_factor / numRepeats) << std::endl;
CHECK(average_packing_factor / numRepeats > 0.99);
};

Expand Down

0 comments on commit 2ad07a5

Please sign in to comment.