rcx

library of miscellaneous bits of C code
git clone git://git.rr3.xyz/rcx
Log | Files | Refs | README | LICENSE

commit 5dc9179db6c0d84a2598e91063d5aee33f630e3b
parent b3a948898f49969297d2ce5ea5e712e2fc807ca0
Author: Robert Russell <robertrussell.72001@gmail.com>
Date:   Fri, 24 Mar 2023 18:40:50 -0700

buffer: fix header alignment issues

Diffstat:
Minc/buffer.h | 6++++--
Msrc/buffer.c | 18+++++++++---------
2 files changed, 13 insertions(+), 11 deletions(-)

diff --git a/inc/buffer.h b/inc/buffer.h @@ -1,5 +1,7 @@ #pragma once +#include <stdalign.h> + #include "alloc.h" #include "debug.h" #include "def.h" @@ -25,14 +27,14 @@ struct buf_hdr_ { usize cap; u32 refcnt; u8 flags; - u8 data[]; + // Followed padding and then data }; #define buf_hdr_const_(n) struct { \ usize cap; \ u32 refcnt; \ u8 flags; \ - u8 data[n]; \ + alignas(BufHdr_) u8 data[n]; \ } #define buf_stack(s, ...) ((Buf)((BufHdr_ *)&(buf_hdr_const_( \ diff --git a/src/buffer.c b/src/buffer.c @@ -9,7 +9,7 @@ buf_memory(void *mem, usize cap) { "buf_memory: bad alignment"); BufHdr_ *h = mem; *h = (BufHdr_){cap, 1, 0}; - return h->data; + return (Buf)(h + 1); } Buf @@ -17,7 +17,7 @@ buf_alloc(usize cap) { BufHdr_ *h = r_alloc(sizeof *h + cap); if (!h) return 0; *h = (BufHdr_){cap, 1, BUF_FLAG_FREE_}; - return h->data; + return (Buf)(h + 1); } Buf @@ -25,21 +25,21 @@ buf_allocz(usize cap) { BufHdr_ *h = r_allocz(sizeof *h + cap); if (!h) return 0; *h = (BufHdr_){cap, 1, BUF_FLAG_FREE_}; - return h->data; + return (Buf)(h + 1); } Buf buf_ealloc(usize cap) { BufHdr_ *h = r_ealloc(sizeof *h + cap); *h = (BufHdr_){cap, 1, BUF_FLAG_FREE_}; - return h->data; + return (Buf)(h + 1); } Buf buf_eallocz(usize cap) { BufHdr_ *h = r_eallocz(sizeof *h + cap); *h = (BufHdr_){cap, 1, BUF_FLAG_FREE_}; - return h->data; + return (Buf)(h + 1); } int @@ -47,7 +47,7 @@ buf_realloc(Buf *b, usize cap) { BufHdr_ *h = *b ? (BufHdr_ *)*b - 1 : 0; if (r_realloc(&h, sizeof *h + cap) < 0) return -1; if (!*b) *h = (BufHdr_){cap, 1, BUF_FLAG_FREE_}; - *b = h->data; + *b = (Buf)(h + 1); return 0; } @@ -56,7 +56,7 @@ buf_reallocz(Buf *b, usize cap) { BufHdr_ *h = *b ? (BufHdr_ *)*b - 1 : 0; if (r_reallocz(&h, sizeof *h + h->cap, sizeof *h + cap) < 0) return -1; if (!*b) *h = (BufHdr_){cap, 1, BUF_FLAG_FREE_}; - *b = h->data; + *b = (Buf)(h + 1); return 0; } @@ -65,7 +65,7 @@ buf_erealloc(Buf *b, usize cap) { BufHdr_ *h = *b ? (BufHdr_ *)*b - 1 : 0; r_erealloc(&h, sizeof *h + cap); if (!*b) *h = (BufHdr_){cap, 1, BUF_FLAG_FREE_}; - *b = h->data; + *b = (Buf)(h + 1); return 0; } @@ -74,7 +74,7 @@ buf_ereallocz(Buf *b, usize cap) { BufHdr_ *h = *b ? (BufHdr_ *)*b - 1 : 0; r_ereallocz(&h, sizeof *h + h->cap, sizeof *h + cap); if (!*b) *h = (BufHdr_){cap, 1, BUF_FLAG_FREE_}; - *b = h->data; + *b = (Buf)(h + 1); return 0; }