Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework main pool to use alloc only pool logic #859

Open
wants to merge 1 commit into
base: develop/3.0.0
Choose a base branch
from

Conversation

aglab2
Copy link
Collaborator

@aglab2 aglab2 commented Jan 30, 2025

This is refactored logic from PR #737 that implements new main pool logic.

}
return addr;
// takes the at least first 'size' bytes from 'region' and return pointer aligned on 'alignment'
static void* main_pool_region_try_alloc_from_start_aligned(struct MainPoolRegion* region, u32 size, u32 alignment) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all these functions seem quite messy and repetitive. there must be a way to consolidate some of these into fewer functions, perhaps with macros or whatnot. also where is main_pool_alloc defined now, i may be blind but i cant find it

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

main_pool_alloc is static inline in the header file. It is incredibly fast compiling to roughly 4 instructions.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gotcha. Do you think the functions defined here could be cleaned up a little? The names and quantity of them are quite confusing

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will work on it

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah while I'd prefer long function names over poorly named ones, 9-word function names is a bit on the large side.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added one "big" function that makes allocation with FORCEINLINE to enable const propagations. I hope it is good enough although there is still a bit of "7-word" functioning here. I'd rather be explicit with names because reading memory code tends to be complicated.

@@ -352,10 +361,17 @@ void *load_segment(s32 segment, u8 *srcStart, u8 *srcEnd, u32 side, u8 *bssStart
void *load_to_fixed_pool_addr(u8 *destAddr, u8 *srcStart, u8 *srcEnd) {
void *dest = NULL;
u32 srcSize = ALIGN16(srcEnd - srcStart);
u32 destSize = ALIGN16((u8 *) sPoolListHeadR - destAddr);
u32 destSize = ALIGN16((u8 *) RAM_END - destAddr);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this assuming that main pool ends at end of ram? i think there might be a better variable to use here although not sure

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

load_to_fixed_pool_addr is loading at fixed address in the end of RAM.

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
@aglab2 aglab2 force-pushed the main-pool-rework-v2 branch from 14281d6 to 4d0e6a4 Compare February 6, 2025 14:29
@gheskett gheskett added enhancement New feature or request performance Issue or feature related to game performance labels Feb 14, 2025
@gheskett gheskett added this to the 3.0 milestone Feb 14, 2025
struct MainPoolRegion* region = &gMainPool.regions[region_idx];
char errmsg[70];
sprintf(errmsg, "%salloc failed for %d bytes in region %d\n%x-%x", freeable ? "freeable " : "", size, region_idx, region->start, region->end);
__n64Assert((char*) file, line, errmsg);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: use the error macro to simplify this (and to make replacing these with assert_args or similar easier to find once 2.4 gets merged into 3.0)

Nevermind, I see what's going on here. Add a TODO then for replacing errmsg with gAssertionStr once 2.4 is merged.

Comment on lines +171 to +173
void *main_pool_alloc_ex(int region, u32 size, u32 alignment) {
void* buf = region ? main_pool_region_alloc_from_start(region, size, alignment, MAIN_POOL_ALLOC_TRY) : NULL;
return buf ?: main_pool_region_alloc_from_start(0, size, alignment, MAIN_POOL_ALLOC_FORCE);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I feel like this is needlessly difficult to read

Comment on lines +96 to +101
Main pool initial state is a multiple regions of memory:
|-------| |----| |-------------------|
If alloc(sizeof(+++++)), first region is used and the state becomes:
|+++++--| |----| |-------------------|
^
returned pointer, no extra memory overhead
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The segmented portion was removed, but this comment still poses more questions than answers. Can I still allocate to the other memory regions? Are the other regions even supported in the first place?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it can if you explicitly call main_pool_region_alloc_from_start

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would add an example to this then to demonstrate that use case.

u8* end;
};

#define MAIN_POOL_REGIONS_COUNT 2
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If regions are still specifiable and involve more than one use in HackerSM64, then we should still be using enums to specify region IDs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request performance Issue or feature related to game performance
Projects
Status: In progress
Development

Successfully merging this pull request may close these issues.

None yet

3 participants