netlink: Align allocations on __max_align_t, not uint64_t.
uint64_t is not sufficient alignment for allocators on all platforms. On a CHERI platform pointers require 16 byte alignment, but also if a type contained a uint128_t or long double it would not be aligned correctly either. C11 added max_align_t precisely to provide a portable type for allocators to use. Reviewed by: melifaro Obtained from: CheriBSD Sponsored by: DARPA Differential Revision: https://reviews.freebsd.org/D41301
This commit is contained in:
parent
2bd446d7f1
commit
9795f14ec4
@ -41,12 +41,12 @@ struct linear_buffer {
|
||||
char *base; /* Base allocated memory pointer */
|
||||
uint32_t offset; /* Currently used offset */
|
||||
uint32_t size; /* Total buffer size */
|
||||
};
|
||||
} __aligned(_Alignof(__max_align_t));
|
||||
|
||||
static inline void *
|
||||
lb_alloc(struct linear_buffer *lb, int len)
|
||||
{
|
||||
len = roundup2(len, sizeof(uint64_t));
|
||||
len = roundup2(len, _Alignof(__max_align_t));
|
||||
if (lb->offset + len > lb->size)
|
||||
return (NULL);
|
||||
void *data = (void *)(lb->base + lb->offset);
|
||||
|
@ -33,6 +33,7 @@
|
||||
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <stdalign.h>
|
||||
#include <stddef.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
@ -74,7 +75,7 @@ struct linear_buffer {
|
||||
uint32_t offset; /* Currently used offset */
|
||||
uint32_t size; /* Total buffer size */
|
||||
struct linear_buffer *next; /* Buffer chaining */
|
||||
};
|
||||
} __aligned(alignof(__max_align_t));
|
||||
|
||||
static inline struct linear_buffer *
|
||||
lb_init(uint32_t size)
|
||||
@ -98,7 +99,7 @@ lb_free(struct linear_buffer *lb)
|
||||
static inline char *
|
||||
lb_allocz(struct linear_buffer *lb, int len)
|
||||
{
|
||||
len = roundup2(len, sizeof(uint64_t));
|
||||
len = roundup2(len, alignof(__max_align_t));
|
||||
if (lb->offset + len > lb->size)
|
||||
return (NULL);
|
||||
void *data = (void *)(lb->base + lb->offset);
|
||||
|
Loading…
Reference in New Issue
Block a user