Skip to content

Commit c57f2c4

Browse files
authored
Extract out entity location and address structs from page_iterator_t (speeduino#1411)
* Refactor: extract entity_page_location_t and entity_page_address_t from page_iterator_t * Remove unecessary #include
1 parent 6a6bbde commit c57f2c4

File tree

5 files changed

+93
-50
lines changed

5 files changed

+93
-50
lines changed

speeduino/comms_legacy.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -897,7 +897,7 @@ namespace {
897897

898898
void send_raw_entity(const page_iterator_t &entity)
899899
{
900-
primarySerial.write((byte *)entity.pData, entity.size);
900+
primarySerial.write((byte *)entity.pData, entity.address.size);
901901
}
902902

903903
inline void send_table_values(table_value_iterator it)

speeduino/page_crc.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
#include "globals.h"
1+
#include <FastCRC.h>
22
#include "page_crc.h"
33
#include "pages.h"
4-
#include <FastCRC.h>
54

65
// Abstract the FastCrC32 functions
76
// - they have have very slight differences in signatures, which causes the Arduino
@@ -17,7 +16,7 @@ using pCrcCalc = uint32_t (*)(FastCRC32 &, const uint8_t *, uint16_t);
1716

1817
static inline uint32_t compute_raw_crc(const page_iterator_t &entity, pCrcCalc calcFunc, FastCRC32 &crcCalc)
1918
{
20-
return calcFunc(crcCalc, (uint8_t*)entity.pData, entity.size);
19+
return calcFunc(crcCalc, (uint8_t*)entity.pData, entity.address.size);
2120
}
2221

2322
static inline uint32_t compute_row_crc(const table_row_iterator &row, pCrcCalc calcFunc, FastCRC32 &crcCalc)
@@ -83,7 +82,7 @@ static inline uint32_t compute_crc(const page_iterator_t &entity, pCrcCalc calcF
8382
break;
8483

8584
case NoEntity:
86-
return pad_crc(entity.size, 0U, crcCalc);
85+
return pad_crc(entity.address.size, 0U, crcCalc);
8786
break;
8887

8988
default:
@@ -105,5 +104,5 @@ uint32_t calculatePageCRC32(byte pageNum)
105104
crc = compute_crc(entity, &updateCrc /* Note that we are *updating* */, crcCalc);
106105
entity = advance(entity);
107106
}
108-
return pad_crc(getPageSize(pageNum) - entity.size, crc, crcCalc);
107+
return pad_crc(getPageSize(pageNum) - entity.address.size, crc, crcCalc);
109108
}

speeduino/pages.cpp

Lines changed: 24 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -147,20 +147,20 @@ class offset_to_table
147147

148148
// ========================= Offset to entity byte mapping =========================
149149

150-
inline byte& get_raw_location(page_iterator_t &entity, uint16_t offset)
150+
static inline byte& get_raw_location(const page_iterator_t &entity, uint16_t offset)
151151
{
152-
return *((byte*)entity.pData + (offset-entity.start));
152+
return *((byte*)entity.pData + (offset-entity.address.start));
153153
}
154154

155-
inline byte get_table_value(page_iterator_t &entity, uint16_t offset)
155+
static inline byte get_table_value(const page_iterator_t &entity, uint16_t offset)
156156
{
157157
#define CTA_GET_TABLE_VALUE(size, xDomain, yDomain, pTable, offset) \
158158
return *offset_to_table<TABLE3D_TYPENAME_BASE(size, xDomain, yDomain)>((TABLE3D_TYPENAME_BASE(size, xDomain, yDomain)*)pTable, offset);
159159
#define CTA_GET_TABLE_VALUE_DEFAULT ({ return 0U; })
160-
CONCRETE_TABLE_ACTION(entity.table_key, CTA_GET_TABLE_VALUE, CTA_GET_TABLE_VALUE_DEFAULT, entity.pData, (offset-entity.start));
160+
CONCRETE_TABLE_ACTION(entity.table_key, CTA_GET_TABLE_VALUE, CTA_GET_TABLE_VALUE_DEFAULT, entity.pData, (offset-entity.address.start));
161161
}
162162

163-
inline byte get_value(page_iterator_t &entity, uint16_t offset)
163+
static inline byte get_value(const page_iterator_t &entity, uint16_t offset)
164164
{
165165
if (Raw==entity.type)
166166
{
@@ -173,15 +173,15 @@ inline byte get_value(page_iterator_t &entity, uint16_t offset)
173173
return 0U;
174174
}
175175

176-
inline void set_table_value(page_iterator_t &entity, uint16_t offset, byte new_value)
176+
inline void set_table_value(const page_iterator_t &entity, uint16_t offset, byte new_value)
177177
{
178178
#define CTA_SET_TABLE_VALUE(size, xDomain, yDomain, pTable, offset, new_value) \
179179
offset_to_table<TABLE3D_TYPENAME_BASE(size, xDomain, yDomain)>((TABLE3D_TYPENAME_BASE(size, xDomain, yDomain)*)pTable, offset) = new_value; break;
180180
#define CTA_SET_TABLE_VALUE_DEFAULT ({ })
181-
CONCRETE_TABLE_ACTION(entity.table_key, CTA_SET_TABLE_VALUE, CTA_SET_TABLE_VALUE_DEFAULT, entity.pData, (offset-entity.start), new_value);
181+
CONCRETE_TABLE_ACTION(entity.table_key, CTA_SET_TABLE_VALUE, CTA_SET_TABLE_VALUE_DEFAULT, entity.pData, (offset-entity.address.start), new_value);
182182
}
183183

184-
inline void set_value(page_iterator_t &entity, byte value, uint16_t offset)
184+
inline void set_value(const page_iterator_t &entity, byte value, uint16_t offset)
185185
{
186186
if (Raw==entity.type)
187187
{
@@ -224,16 +224,11 @@ static inline void check_size() {
224224
//
225225
// Instead we use this (and other) intermediate factory function(s) - it provides a barrier that
226226
// forces GCC to construct the page_iterator_t instance at runtime.
227-
inline const page_iterator_t create_end_iterator(uint8_t pageNum, uint16_t start)
227+
static inline page_iterator_t create_end_iterator(uint8_t pageNum, uint16_t start)
228228
{
229-
return page_iterator_t {
230-
.pData = nullptr,
231-
.table_key = table_type_None,
232-
.page = pageNum,
233-
.start = start,
234-
.size = start,
235-
.type = End,
236-
};
229+
return page_iterator_t( End,
230+
entity_page_location_t(pageNum, UINT8_MAX),
231+
entity_page_address_t(start, 0U));
237232
}
238233

239234
// Signal the end of a page
@@ -243,47 +238,37 @@ inline const page_iterator_t create_end_iterator(uint8_t pageNum, uint16_t start
243238

244239
// ========================= Table processing ===================
245240

246-
inline const page_iterator_t create_table_iterator(void *pTable, table_type_t key, uint8_t pageNum, uint16_t start, uint16_t size)
241+
static inline page_iterator_t create_table_iterator(void *pTable, table_type_t key, uint8_t pageNum, uint8_t index, uint16_t start, uint16_t size)
247242
{
248-
return page_iterator_t {
249-
.pData = pTable,
250-
.table_key = key,
251-
.page = pageNum,
252-
.start = start,
253-
.size = size,
254-
.type = Table,
255-
};
243+
return page_iterator_t( pTable, key,
244+
entity_page_location_t(pageNum, index),
245+
entity_page_address_t(start, size));
256246
}
257247

258248
// If the offset is in range, create a Table entity_t
259249
#define CHECK_TABLE(pageNum, offset, pTable, entityNum) \
260250
if (offset < ENTITY_START_VAR(entityNum)+get_table_axisy_end(pTable)) \
261251
{ \
262252
return create_table_iterator(pTable, (pTable)->type_key, \
263-
pageNum, \
264-
ENTITY_START_VAR(entityNum), get_table_axisy_end(pTable)); \
253+
(pageNum), (entityNum), \
254+
ENTITY_START_VAR(entityNum), get_table_axisy_end((pTable))); \
265255
} \
266256
DECLARE_NEXT_ENTITY_START(entityNum, get_table_axisy_end(pTable))
267257

268258
// ========================= Raw memory block processing ===================
269259

270-
inline const page_iterator_t create_raw_iterator(void *pBuffer, uint8_t pageNum, uint16_t start, uint16_t size)
260+
static inline page_iterator_t create_raw_iterator(void *pBuffer, uint8_t pageNum, uint8_t index, uint16_t start, uint16_t size)
271261
{
272-
return page_iterator_t {
273-
.pData = pBuffer,
274-
.table_key = table_type_None,
275-
.page = pageNum,
276-
.start = start,
277-
.size = size,
278-
.type = Raw,
279-
};
262+
return page_iterator_t( pBuffer,
263+
entity_page_location_t(pageNum, index),
264+
entity_page_address_t(start, size));
280265
}
281266

282267
// If the offset is in range, create a Raw entity_t
283268
#define CHECK_RAW(pageNum, offset, pDataBlock, blockSize, entityNum) \
284269
if (offset < ENTITY_START_VAR(entityNum)+blockSize) \
285270
{ \
286-
return create_raw_iterator(pDataBlock, pageNum, ENTITY_START_VAR(entityNum), blockSize);\
271+
return create_raw_iterator((pDataBlock), (pageNum), (entityNum), ENTITY_START_VAR(entityNum), (blockSize));\
287272
} \
288273
DECLARE_NEXT_ENTITY_START(entityNum, blockSize)
289274

@@ -448,7 +433,7 @@ page_iterator_t page_begin(byte pageNum)
448433

449434
page_iterator_t advance(const page_iterator_t &it)
450435
{
451-
return map_page_offset_to_entity(it.page, it.start+it.size);
436+
return map_page_offset_to_entity(it.location.page, it.address.start+it.address.size);
452437
}
453438

454439
/**

speeduino/pages.h

Lines changed: 63 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,73 @@ enum entity_type {
5959
End // The offset was past any known entity for the page
6060
};
6161

62+
/** @brief The *unique* location of an entity within all pages */
63+
struct entity_page_location_t {
64+
uint8_t page; // The index of the page the entity belongs to
65+
uint8_t index; // The sub-index of the item within the page
66+
67+
constexpr entity_page_location_t(uint8_t pageNum, uint8_t pageSubIndex)
68+
: page(pageNum)
69+
, index(pageSubIndex)
70+
{
71+
}
72+
73+
bool operator==(const entity_page_location_t &other) const
74+
{
75+
return page==other.page
76+
&& index==other.index;
77+
}
78+
bool operator!=(const entity_page_location_t &other) const
79+
{
80+
return !operator==(other);
81+
}
82+
};
83+
84+
/** @brief Position and size of an entity within a page */
85+
struct entity_page_address_t {
86+
uint16_t start; // The start position of the entity, in bytes, from the start of the page
87+
uint16_t size; // Size of the entity in bytes
88+
89+
entity_page_address_t(uint16_t base, uint16_t length)
90+
: start(base)
91+
, size(length)
92+
{
93+
}
94+
};
95+
6296
// A entity on a logical page.
6397
struct page_iterator_t {
6498
void *pData;
65-
table_type_t table_key;
66-
uint8_t page; // The page the entity belongs to
67-
uint16_t start; // The start position of the entity, in bytes, from the start of the page
68-
uint16_t size; // Size of the entity in bytes
6999
entity_type type;
100+
table_type_t table_key;
101+
entity_page_location_t location;
102+
entity_page_address_t address;
103+
104+
page_iterator_t(entity_type theType, const entity_page_location_t &entityLocation, const entity_page_address_t &entityAddress)
105+
: pData(nullptr)
106+
, type(theType)
107+
, table_key(table_type_None)
108+
, location(entityLocation)
109+
, address(entityAddress)
110+
{
111+
}
112+
113+
page_iterator_t(void *pBuffer, const entity_page_location_t &entityLocation, const entity_page_address_t &entityAddress)
114+
: pData(pBuffer)
115+
, type(Raw)
116+
, table_key(table_type_None)
117+
, location(entityLocation)
118+
, address(entityAddress)
119+
{
120+
}
121+
page_iterator_t(void *pTable, table_type_t key, const entity_page_location_t &entityLocation, const entity_page_address_t &entityAddress)
122+
: pData(pTable)
123+
, type(Table)
124+
, table_key(key)
125+
, location(entityLocation)
126+
, address(entityAddress)
127+
{
128+
}
70129
};
71130

72131
/**

speeduino/storage.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ void resetConfigPages(void)
341341
{
342342
if (entity.type==Raw)
343343
{
344-
memset(entity.pData, 0, entity.size);
344+
memset(entity.pData, 0, entity.address.size);
345345
}
346346
entity = advance(entity);
347347
}

0 commit comments

Comments
 (0)