Prefer tag/size outside of union where possible

If we have control of the struct, such as in lfsr_data_t and lfsr_cat_t,
moving the common tag outside of the union avoids naming ambiguities.

Counter-example: This doesn't work for lfsr_bshrub_t, since the contents
of that union are also used as separate types elsewhere. Fortunately the
common initial sequence union rules kick in here.

No code changes, which is good:

           code          stack
  before: 33652           2624
  after:  33652 (+0.0%)   2624 (+0.0%)
This commit is contained in:
Christopher Haster
2024-05-10 01:58:54 -05:00
parent 643bf5b3e0
commit 0fd955edb7
2 changed files with 43 additions and 53 deletions
+41 -47
View File
@@ -1172,31 +1172,31 @@ static lfs_ssize_t lfsr_bd_progtag(lfs_t *lfs,
#define LFSR_DATA_NULL() \ #define LFSR_DATA_NULL() \
((lfsr_data_t){ \ ((lfsr_data_t){ \
.u.buf.size=0, \ .size=0, \
.u.buf.buffer=NULL}) .u.buffer=NULL})
#define LFSR_DATA_DISK(_block, _off, _size) \ #define LFSR_DATA_DISK(_block, _off, _size) \
((lfsr_data_t){ \ ((lfsr_data_t){ \
.u.disk.size=LFSR_DATA_ONDISK | (_size), \ .size=LFSR_DATA_ONDISK | (_size), \
.u.disk.block=_block, \ .u.disk.block=_block, \
.u.disk.off=_off}) .u.disk.off=_off})
#define LFSR_DATA_BUF(_buffer, _size) \ #define LFSR_DATA_BUF(_buffer, _size) \
((lfsr_data_t){ \ ((lfsr_data_t){ \
.u.buf.size=_size, \ .size=_size, \
.u.buf.buffer=(const void*)(_buffer)}) .u.buffer=(const void*)(_buffer)})
// data helpers // data helpers
static inline bool lfsr_data_ondisk(lfsr_data_t data) { static inline bool lfsr_data_ondisk(lfsr_data_t data) {
return data.u.size & LFSR_DATA_ONDISK; return data.size & LFSR_DATA_ONDISK;
} }
static inline bool lfsr_data_isbuf(lfsr_data_t data) { static inline bool lfsr_data_isbuf(lfsr_data_t data) {
return !(data.u.size & LFSR_DATA_ONDISK); return !(data.size & LFSR_DATA_ONDISK);
} }
static inline lfs_size_t lfsr_data_size(lfsr_data_t data) { static inline lfs_size_t lfsr_data_size(lfsr_data_t data) {
return data.u.size & ~LFSR_DATA_ONDISK; return data.size & ~LFSR_DATA_ONDISK;
} }
static lfsr_data_t lfsr_data_slice(lfsr_data_t data, static lfsr_data_t lfsr_data_slice(lfsr_data_t data,
@@ -1213,12 +1213,12 @@ static lfsr_data_t lfsr_data_slice(lfsr_data_t data,
// on-disk? // on-disk?
if (lfsr_data_ondisk(data)) { if (lfsr_data_ondisk(data)) {
data.u.disk.off += off_; data.u.disk.off += off_;
data.u.disk.size = LFSR_DATA_ONDISK | size_; data.size = LFSR_DATA_ONDISK | size_;
// buffer? // buffer?
} else { } else {
data.u.buf.buffer += off_; data.u.buffer += off_;
data.u.buf.size = size_; data.size = size_;
} }
return data; return data;
@@ -1260,7 +1260,7 @@ static lfs_ssize_t lfsr_data_read(lfs_t *lfs, lfsr_data_t *data,
// buffer? // buffer?
} else { } else {
memcpy(buffer, data->u.buf.buffer, d); memcpy(buffer, data->u.buffer, d);
} }
*data = lfsr_data_slice(*data, d, -1); *data = lfsr_data_slice(*data, d, -1);
@@ -1345,7 +1345,7 @@ static lfs_scmp_t lfsr_data_cmp(lfs_t *lfs, lfsr_data_t data,
// buffer? // buffer?
} else { } else {
int cmp = memcmp(data.u.buf.buffer, buffer, d); int cmp = memcmp(data.u.buffer, buffer, d);
if (cmp < 0) { if (cmp < 0) {
return LFS_CMP_LT; return LFS_CMP_LT;
} else if (cmp > 0) { } else if (cmp > 0) {
@@ -1399,7 +1399,7 @@ static int lfsr_bd_progdata(lfs_t *lfs,
// buffer? // buffer?
} else { } else {
int err = lfsr_bd_prog(lfs, block, off, int err = lfsr_bd_prog(lfs, block, off,
data.u.buf.buffer, data.u.buf.size, data.u.buffer, data.size,
cksum_); cksum_);
if (err) { if (err) {
return err; return err;
@@ -1415,16 +1415,10 @@ static int lfsr_bd_progdata(lfs_t *lfs,
typedef struct lfsr_cat { typedef struct lfsr_cat {
// sign(size)=0 => single in-RAM buffer // sign(size)=0 => single in-RAM buffer
// sign(size)=1 => multiple concatenated datas // sign(size)=1 => multiple concatenated datas
uint16_t size;
union { union {
uint16_t size; const uint8_t *buffer;
struct { const lfsr_data_t *datas;
uint16_t size;
const uint8_t *buffer;
} buf;
struct {
uint16_t size;
const lfsr_data_t *datas;
} cat;
} u; } u;
} lfsr_cat_t; } lfsr_cat_t;
@@ -1432,26 +1426,26 @@ typedef struct lfsr_cat {
#define LFSR_CAT_NULL() \ #define LFSR_CAT_NULL() \
((lfsr_cat_t){ \ ((lfsr_cat_t){ \
.u.buf.size=0, \ .size=0, \
.u.buf.buffer=NULL}) .u.buffer=NULL})
#define LFSR_CAT_BUF(_buffer, _size) \ #define LFSR_CAT_BUF(_buffer, _size) \
((lfsr_cat_t){ \ ((lfsr_cat_t){ \
.u.buf.size=_size, \ .size=_size, \
.u.buf.buffer=(const void*)(_buffer)}) .u.buffer=(const void*)(_buffer)})
#define LFSR_CAT_DATA_(_data) \ #define LFSR_CAT_DATA_(_data) \
((lfsr_cat_t){ \ ((lfsr_cat_t){ \
.u.cat.size=LFSR_CAT_ISCAT | 1, \ .size=LFSR_CAT_ISCAT | 1, \
.u.cat.datas=_data}) .u.datas=_data})
#define LFSR_CAT_DATA(_data) \ #define LFSR_CAT_DATA(_data) \
LFSR_CAT_DATA_((const lfsr_data_t[1]){_data}) LFSR_CAT_DATA_((const lfsr_data_t[1]){_data})
#define LFSR_CAT_DATAS_(_datas, _count) \ #define LFSR_CAT_DATAS_(_datas, _count) \
((lfsr_cat_t){ \ ((lfsr_cat_t){ \
.u.cat.size=LFSR_CAT_ISCAT | (_count), \ .size=LFSR_CAT_ISCAT | (_count), \
.u.cat.datas=_datas}) .u.datas=_datas})
#define LFSR_CAT_DATAS(...) \ #define LFSR_CAT_DATAS(...) \
LFSR_CAT_DATAS_( \ LFSR_CAT_DATAS_( \
@@ -1464,33 +1458,33 @@ static inline lfsr_cat_t lfsr_data_cat(lfsr_data_t data) {
LFS_ASSERT(lfsr_data_isbuf(data)); LFS_ASSERT(lfsr_data_isbuf(data));
LFS_ASSERT(lfsr_data_size(data) <= 0x7fff); LFS_ASSERT(lfsr_data_size(data) <= 0x7fff);
return (lfsr_cat_t){ return (lfsr_cat_t){
.u.buf.size=data.u.buf.size, .size=data.size,
.u.buf.buffer=data.u.buf.buffer}; .u.buffer=data.u.buffer};
} }
// cat helpers // cat helpers
static inline bool lfsr_cat_isbuf(lfsr_cat_t cat) { static inline bool lfsr_cat_isbuf(lfsr_cat_t cat) {
return !(cat.u.size & LFSR_CAT_ISCAT); return !(cat.size & LFSR_CAT_ISCAT);
} }
static inline bool lfsr_cat_iscat(lfsr_cat_t cat) { static inline bool lfsr_cat_iscat(lfsr_cat_t cat) {
return cat.u.size & LFSR_CAT_ISCAT; return cat.size & LFSR_CAT_ISCAT;
} }
static inline lfs_size_t lfsr_cat_count(lfsr_cat_t cat) { static inline lfs_size_t lfsr_cat_count(lfsr_cat_t cat) {
LFS_ASSERT(lfsr_cat_iscat(cat)); LFS_ASSERT(lfsr_cat_iscat(cat));
return cat.u.size & ~LFSR_CAT_ISCAT; return cat.size & ~LFSR_CAT_ISCAT;
} }
static inline lfs_size_t lfsr_cat_size(lfsr_cat_t cat) { static inline lfs_size_t lfsr_cat_size(lfsr_cat_t cat) {
// this gets a bit complicated for concatenated data // this gets a bit complicated for concatenated data
if (lfsr_cat_isbuf(cat)) { if (lfsr_cat_isbuf(cat)) {
return cat.u.size; return cat.size;
} else { } else {
lfs_size_t count = lfsr_cat_count(cat); lfs_size_t count = lfsr_cat_count(cat);
lfs_size_t size = 0; lfs_size_t size = 0;
for (lfs_size_t i = 0; i < count; i++) { for (lfs_size_t i = 0; i < count; i++) {
size += lfsr_data_size(cat.u.cat.datas[i]); size += lfsr_data_size(cat.u.datas[i]);
} }
return size; return size;
} }
@@ -1555,20 +1549,20 @@ static int lfsr_bd_progcat(lfs_t *lfs,
uint32_t *cksum_) { uint32_t *cksum_) {
// direct buffer? // direct buffer?
if (lfsr_cat_isbuf(cat)) { if (lfsr_cat_isbuf(cat)) {
return lfsr_bd_prog(lfs, block, off, cat.u.buf.buffer, cat.u.buf.size, return lfsr_bd_prog(lfs, block, off, cat.u.buffer, cat.size,
cksum_); cksum_);
// indirect concatenated data? // indirect concatenated data?
} else { } else {
lfs_size_t count = lfsr_cat_count(cat); lfs_size_t count = lfsr_cat_count(cat);
for (lfs_size_t i = 0; i < count; i++) { for (lfs_size_t i = 0; i < count; i++) {
int err = lfsr_bd_progdata(lfs, block, off, cat.u.cat.datas[i], int err = lfsr_bd_progdata(lfs, block, off, cat.u.datas[i],
cksum_); cksum_);
if (err) { if (err) {
return err; return err;
} }
off += lfsr_data_size(cat.u.cat.datas[i]); off += lfsr_data_size(cat.u.datas[i]);
} }
return 0; return 0;
} }
@@ -1596,9 +1590,9 @@ static inline lfsr_attr_t lfsr_attr(
lfsr_tag_t tag, lfsr_srid_t delta, lfsr_cat_t cat) { lfsr_tag_t tag, lfsr_srid_t delta, lfsr_cat_t cat) {
return (lfsr_attr_t){ return (lfsr_attr_t){
.tag=tag, .tag=tag,
.size=cat.u.cat.size, .size=cat.size,
.delta=delta, .delta=delta,
.u.datas=cat.u.cat.datas}; .u.datas=cat.u.datas};
} }
#define LFSR_ATTR_NOOP() \ #define LFSR_ATTR_NOOP() \
@@ -1656,8 +1650,8 @@ static inline bool lfsr_attr_isinsert(lfsr_attr_t attr) {
static inline lfsr_cat_t lfsr_attr_cat(lfsr_attr_t attr) { static inline lfsr_cat_t lfsr_attr_cat(lfsr_attr_t attr) {
return (lfsr_cat_t){ return (lfsr_cat_t){
.u.cat.size=attr.size, .size=attr.size,
.u.cat.datas=attr.u.datas}; .u.datas=attr.u.datas};
} }
static inline lfs_size_t lfsr_attr_size(lfsr_attr_t attr) { static inline lfs_size_t lfsr_attr_size(lfsr_attr_t attr) {
@@ -1926,7 +1920,7 @@ static lfsr_data_t lfsr_data_frombptr(const lfsr_bptr_t *bptr,
static int lfsr_data_readbptr(lfs_t *lfs, lfsr_data_t *data, static int lfsr_data_readbptr(lfs_t *lfs, lfsr_data_t *data,
lfsr_bptr_t *bptr) { lfsr_bptr_t *bptr) {
// read the block, offset, size // read the block, offset, size
int err = lfsr_data_readlleb128(lfs, data, &bptr->data.u.disk.size); int err = lfsr_data_readlleb128(lfs, data, &bptr->data.size);
if (err) { if (err) {
return err; return err;
} }
@@ -1954,7 +1948,7 @@ static int lfsr_data_readbptr(lfs_t *lfs, lfsr_data_t *data,
// all bptrs have this flag set, this is used to differentiate // all bptrs have this flag set, this is used to differentiate
// bptrs from btrees in files // bptrs from btrees in files
bptr->data.u.disk.size |= LFSR_DATA_ONDISK; bptr->data.size |= LFSR_DATA_ONDISK;
return 0; return 0;
} }
+2 -6
View File
@@ -397,17 +397,13 @@ typedef struct lfs_mdir {
typedef struct lfsr_data { typedef struct lfsr_data {
// sign(size)=0 => in-RAM buffer // sign(size)=0 => in-RAM buffer
// sign(size)=1 => on-disk reference // sign(size)=1 => on-disk reference
lfs_size_t size;
union { union {
lfs_size_t size;
struct { struct {
lfs_size_t size;
lfs_block_t block; lfs_block_t block;
lfs_size_t off; lfs_size_t off;
} disk; } disk;
struct { const uint8_t *buffer;
lfs_size_t size;
const uint8_t *buffer;
} buf;
} u; } u;
} lfsr_data_t; } lfsr_data_t;