|
7 | 7 |
|
8 | 8 | #define BLOCK_GROWTH_SIZE (1024 * 1024 - sizeof(struct mp_block))
|
9 | 9 |
|
| 10 | +/* |
| 11 | + * The inner union is an approximation for C11's max_align_t, and the |
| 12 | + * struct + offsetof computes _Alignof. This can all just be replaced |
| 13 | + * with _Alignof(max_align_t) if/when C11 is part of the baseline. |
| 14 | + * Note that _Alignof(X) need not be the same as sizeof(X); it's only |
| 15 | + * required to be a (possibly trivial) factor. They are the same for |
| 16 | + * most architectures, but m68k for example has only 2-byte alignment |
| 17 | + * for its 4-byte and 8-byte types, so using sizeof would waste space. |
| 18 | + * |
| 19 | + * Add more types to the union if the current set is insufficient. |
| 20 | + */ |
| 21 | +struct git_max_alignment { |
| 22 | + char unalign; |
| 23 | + union { |
| 24 | + uintmax_t max_align_uintmax; |
| 25 | + void *max_align_pointer; |
| 26 | + } aligned; |
| 27 | +}; |
| 28 | +#define GIT_MAX_ALIGNMENT offsetof(struct git_max_alignment, aligned) |
| 29 | + |
10 | 30 | /*
|
11 | 31 | * Allocate a new mp_block and insert it after the block specified in
|
12 | 32 | * `insert_after`. If `insert_after` is NULL, then insert block at the
|
@@ -69,9 +89,9 @@ void *mem_pool_alloc(struct mem_pool *pool, size_t len)
|
69 | 89 | struct mp_block *p = NULL;
|
70 | 90 | void *r;
|
71 | 91 |
|
72 |
| - /* round up to a 'uintmax_t' alignment */ |
73 |
| - if (len & (sizeof(uintmax_t) - 1)) |
74 |
| - len += sizeof(uintmax_t) - (len & (sizeof(uintmax_t) - 1)); |
| 92 | + /* round up to a 'GIT_MAX_ALIGNMENT' alignment */ |
| 93 | + if (len & (GIT_MAX_ALIGNMENT - 1)) |
| 94 | + len += GIT_MAX_ALIGNMENT - (len & (GIT_MAX_ALIGNMENT - 1)); |
75 | 95 |
|
76 | 96 | if (pool->mp_block &&
|
77 | 97 | pool->mp_block->end - pool->mp_block->next_free >= len)
|
|
0 commit comments