Replies: 3 comments 1 reply
-
This can be done in both normal and embedded ( For normal mode you need to realloc both the metadata (buddy_buf) and arena (data_buf) and then call resize. This will work, however if realloc changed the base arena pointer all your allocations, while still valid from the point of the buddy, would have been shifted to a new location. For embedded mode this works the same and there is a new function added to get the new address of the metadata after resize. The same caveat for existing allocations is valid - if the new base is different then all allocations would also be different! In general, to avoid issues with moving pointers it is best to initialize the largest arena that you will need. In all modern operating systems just doing malloc will not cause memory use - only when you write to some page it will be actually allocated in physical memory and used. Then, the buddy by default will try to allocate while minimizing fragmentation and bias to the left - so the actual used space will stay small if you allocations stay small. You can use the |
Beta Was this translation helpful? Give feedback.
-
Thank you! |
Beta Was this translation helpful? Give feedback.
-
I thought I had understood everything upon reading your answer, now I'm confused about something. The original question was about how to resize the buddy allocator beyond the reserve. So I realloc my arena and my metadata. I understand that if // Create with initial size of 4096.
unsigned char *buddy_buf = malloc(buddy_sizeof(4096));
unsigned char *data_buf = malloc(4096);
struct buddy *buddy = buddy_init(buddy_buf, data_buf, 4096);
// I want to grow to 8192: "you need to realloc both the metadata (buddy_buf) and arena (data_buf) and then call resize"
buddy_buf = realloc(buddy_buf, buddy_sizeof(8192)); // this could be a new pointer that the buddy is not aware of
data_buf = realloc(data_buf, data_buf, 8192); // this could also be a new pointer that the buddy is not aware of
// What will the buddy resize into if it doesn't know the new location of the realloced buffers?
buddy_resize(buddy, 8192);
// Destroy.
free(buddy_buf);
free(data_buf); |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
If this is how to resize up within reserve:
I want to resize up beyond the limit of the reserve, of which I will realloc the reserve appropriately. Something along the lines of:
Can this be done?
Beta Was this translation helpful? Give feedback.
All reactions