handmade.network » Forums » Alignment Arena Allocator
Draos
20 posts
#20834 Alignment Arena Allocator
1 month, 3 weeks ago Edited by Draos on April 2, 2019, 1:30 a.m. Reason: Initial post

Hi! I am reading a article by Ginger Bill
https://www.gingerbill.org/articl...memory-allocation-strategies-002/

in the article he describes how to implement a basic Arena Allocator. i thought the code was fairly straightforward, but I had a question about something he did.

1
2
3
4
5
6
7
8
#ifndef DEFAULT_ALIGNMENT
#define DEFAULT_ALIGNMENT (2*sizeof(void *))
#endif

// Because C doesn't have default parameters
void *arena_alloc(Arena *a, size_t size) {
	return arena_alloc_align(a, size, DEFAULT_ALIGNMENT);
}


as you can see he set DEFAULT_ALIGNMENT to 2 * sizeof(void *). I understand that Processors often operate best when things are aligned to their native Word Size, but why is it that GB chose to set the DEFAULT_ALIGNMENT to 2 * WORD_SIZE?
mmozeiko
Mārtiņš Možeiko
1923 posts / 1 project
#20835 Alignment Arena Allocator
1 month, 3 weeks ago

If he is writing this only for x64 then this probably is because of SSE types. SSE has special instructions that work faster on some CPUs when memory is aligned to 16 bytes. 2*sizeof(void*) is 16 bytes.
Draos
20 posts
#20836 Alignment Arena Allocator
1 month, 3 weeks ago

makes sense to me. thanks Mārtiņš