chore(radio): Reduce size of the data structures in FLASH storage (#3979)

* Reduce size of the YamlNode structure in FLASH storage.
Fix compiler warning.

* Further reduction of FLASH used by YamlNode structures.

* Reduce size of sensor definition arrays and audio tone volumes array.
This commit is contained in:
philmoz
2023-09-24 14:18:11 +10:00
committed by GitHub
parent bb7deefc4f
commit c502dbeaee
14 changed files with 520 additions and 503 deletions
@@ -2162,7 +2162,7 @@ static void r_serialMode(void* user, uint8_t* data, uint32_t bitoffs,
auto tw = reinterpret_cast<YamlTreeWalker*>(user);
auto node = tw->getAttr();
if (!node || node->tag_len < 4) return;
if (!node || node->tag_len() < 4) return;
uint8_t port_nr;
if (node->tag[3] == 'S')
+38 -38
View File
@@ -74,18 +74,15 @@ struct YamlNode
typedef bool (*cust_write_func)(void* user, uint8_t* data, uint32_t bitoffs,
yaml_writer_func wf, void* opaque);
uint8_t type;
uint32_t size; // bits
uint8_t tag_len;
uint16_t size; // bits
uint8_t type:4;
uint16_t elmts:12; // maximum number of array elements
const char* tag;
union {
struct {
const YamlNode* child;
union {
struct {
is_active_func is_active;
uint16_t elmts; // maximum number of elements
} _a;
is_active_func is_active;
select_member_func select_member;
} u;
} _array;
@@ -109,105 +106,108 @@ struct YamlNode
cust_write_func write;
} _cust_attr;
} u;
uint8_t tag_len() const { return tag ? strlen(tag) : 0; }
};
#if !defined(_MSC_VER)
#define YAML_TAG(str) \
.tag_len=(sizeof(str)-1), .tag=(str)
.tag=(str)
#define YAML_IDX \
{ .type=YDT_IDX, .size=0, YAML_TAG("idx") }
{ .size=0, .type=YDT_IDX, 0, YAML_TAG("idx") }
#define YAML_IDX_CUST(tag, f_read, f_write) \
{ .type=YDT_IDX, .size=0, YAML_TAG(tag), .u={._cust_idx={.read=(f_read), .write=(f_write) }} }
{ .size=0, .type=YDT_IDX, 0, YAML_TAG(tag), .u={._cust_idx={.read=(f_read), .write=(f_write) }} }
#define YAML_SIGNED(tag, bits) \
{ .type=YDT_SIGNED, .size=(bits), YAML_TAG(tag) }
{ .size=(bits), .type=YDT_SIGNED, 0, YAML_TAG(tag) }
#define YAML_UNSIGNED(tag, bits) \
{ .type=YDT_UNSIGNED, .size=(bits), YAML_TAG(tag) }
{ .size=(bits), .type=YDT_UNSIGNED, 0, YAML_TAG(tag) }
#define YAML_SIGNED_CUST(tag, bits, f_cust_to_uint, f_uint_to_cust) \
{ .type=YDT_SIGNED, .size=(bits), YAML_TAG(tag), .u={._cust={ .cust_to_uint=f_cust_to_uint, .uint_to_cust=f_uint_to_cust }} }
{ .size=(bits), .type=YDT_SIGNED, 0, YAML_TAG(tag), .u={._cust={ .cust_to_uint=f_cust_to_uint, .uint_to_cust=f_uint_to_cust }} }
#define YAML_UNSIGNED_CUST(tag, bits, f_cust_to_uint, f_uint_to_cust) \
{ .type=YDT_UNSIGNED, .size=(bits), YAML_TAG(tag), .u={._cust={ .cust_to_uint=f_cust_to_uint, .uint_to_cust=f_uint_to_cust }} }
{ .size=(bits), .type=YDT_UNSIGNED, 0, YAML_TAG(tag), .u={._cust={ .cust_to_uint=f_cust_to_uint, .uint_to_cust=f_uint_to_cust }} }
#define YAML_STRING(tag, max_len) \
{ .type=YDT_STRING, .size=((max_len)<<3), YAML_TAG(tag) }
{ .size=((max_len)<<3), .type=YDT_STRING, 0, YAML_TAG(tag) }
#define YAML_STRUCT(tag, bits, nodes, f_is_active) \
{ .type=YDT_ARRAY, .size=(bits), YAML_TAG(tag), .u={._array={ .child=(nodes), .u={ ._a={.is_active=(f_is_active), .elmts=1 }}}} }
{ .size=(bits), .type=YDT_ARRAY, .elmts=1, YAML_TAG(tag), .u={._array={ .child=(nodes), .u={.is_active=(f_is_active)}}} }
#define YAML_ARRAY(tag, bits, max_elmts, nodes, f_is_active) \
{ .type=YDT_ARRAY, .size=(bits), YAML_TAG(tag), .u={._array={ .child=(nodes), .u={ ._a={.is_active=(f_is_active), .elmts=(max_elmts) }}}} }
{ .size=(bits), .type=YDT_ARRAY, .elmts=(max_elmts), YAML_TAG(tag), .u={._array={ .child=(nodes), .u={.is_active=(f_is_active)}}} }
#define YAML_ENUM(tag, bits, id_strs) \
{ .type=YDT_ENUM, .size=(bits), YAML_TAG(tag), .u={._enum={ .choices=(id_strs) }} }
{ .size=(bits), .type=YDT_ENUM, 0, YAML_TAG(tag), .u={._enum={ .choices=(id_strs) }} }
#define YAML_UNION(tag, bits, nodes, f_sel_m) \
{ .type=YDT_UNION, .size=(bits), YAML_TAG(tag), .u={._array={ .child=(nodes), .u={.select_member=(f_sel_m) }}} }
{ .size=(bits), .type=YDT_UNION, 0, YAML_TAG(tag), .u={._array={ .child=(nodes), .u={.select_member=(f_sel_m) }}} }
#define YAML_PADDING(bits) \
{ .type=YDT_PADDING, .size=(bits) }
{ .size=(bits), .type=YDT_PADDING }
#define YAML_CUSTOM(tag, f_read, f_write) \
{ .type=YDT_CUSTOM, .size=0, YAML_TAG(tag), .u={._cust_attr={.read=(f_read), .write=(f_write) }} }
{ .size=0, .type=YDT_CUSTOM, 0, YAML_TAG(tag), .u={._cust_attr={.read=(f_read), .write=(f_write) }} }
#define YAML_END \
{ .type=YDT_NONE }
{ .size=0, .type=YDT_NONE }
#define YAML_ROOT(nodes) \
{ .type=YDT_ARRAY, .size=0, .tag_len=0, .tag=NULL, \
{ .size=0, .type=YDT_ARRAY, .elmts=1, .tag=NULL, \
.u={ \
._array={ .child=(nodes), \
.u={._a={.is_active=NULL, .elmts=1 }} \
.u={.is_active=NULL} \
}} \
}
#else // MSVC++ compat
#define YAML_TAG(str) \
(sizeof(str)-1), (str)
(str)
#define YAML_IDX \
{ YDT_IDX , 0, YAML_TAG("idx") }
{ 0, YDT_IDX, 0, YAML_TAG("idx") }
#define YAML_SIGNED(tag, bits) \
{ YDT_SIGNED, (bits), YAML_TAG(tag) }
{ (bits), YDT_SIGNED, 0, YAML_TAG(tag) }
#define YAML_UNSIGNED(tag, bits) \
{ YDT_UNSIGNED, (bits), YAML_TAG(tag) }
{ (bits), YDT_UNSIGNED, 0, YAML_TAG(tag) }
#define YAML_SIGNED_CUST(tag, bits, f_cust_to_uint, f_uint_to_cust) \
{ YDT_SIGNED, (bits), YAML_TAG(tag), {{ (const YamlNode*)f_cust_to_uint, {{ (YamlNode::is_active_func)f_uint_to_cust, 0 }}}} }
{ (bits), YDT_SIGNED, 0, YAML_TAG(tag), {{ (const YamlNode*)f_cust_to_uint, {{ (YamlNode::is_active_func)f_uint_to_cust, 0 }}}} }
#define YAML_UNSIGNED_CUST(tag, bits, f_cust_to_uint, f_uint_to_cust) \
{ YDT_UNSIGNED, (bits), YAML_TAG(tag), {{ (const YamlNode*)f_cust_to_uint, {{ (YamlNode::is_active_func)f_uint_to_cust, 0}}}} }
{ (bits), YDT_UNSIGNED, 0, YAML_TAG(tag), {{ (const YamlNode*)f_cust_to_uint, {{ (YamlNode::is_active_func)f_uint_to_cust, 0}}}} }
#define YAML_STRING(tag, max_len) \
{ YDT_STRING, ((max_len)<<3), YAML_TAG(tag) }
{ ((max_len)<<3), YDT_STRING, 0, YAML_TAG(tag) }
#define YAML_STRUCT(tag, bits, nodes, f_is_active) \
{ YDT_ARRAY, (bits), YAML_TAG(tag), {{ (nodes), {{ (f_is_active), 1 }}}} }
{ (bits), YDT_ARRAY, 1, YAML_TAG(tag), {{ (nodes), {{ (f_is_active) }}}} }
#define YAML_ARRAY(tag, bits, max_elmts, nodes, f_is_active) \
{ YDT_ARRAY, (bits), YAML_TAG(tag), {{ (nodes), {{ (f_is_active), (max_elmts) }}}} }
{ (bits), YDT_ARRAY, (max_elmts), YAML_TAG(tag), {{ (nodes), {{ (f_is_active) }}}} }
#define YAML_ENUM(tag, bits, id_strs) \
{ YDT_ENUM, (bits), YAML_TAG(tag), {{ (const YamlNode*)(id_strs) }} }
{ (bits), YDT_ENUM, 0, YAML_TAG(tag), {{ (const YamlNode*)(id_strs) }} }
#define YAML_UNION(tag, bits, nodes, f_sel_m) \
{ YDT_UNION, (bits), YAML_TAG(tag), {{ (nodes), {{ (YamlNode::is_active_func)(f_sel_m), 0 }}}} }
{ (bits), YDT_UNION, 0, YAML_TAG(tag), {{ (nodes), {{ (YamlNode::is_active_func)(f_sel_m) }}}} }
#define YAML_PADDING(bits) \
{ YDT_PADDING, (bits) }
{ (bits), YDT_PADDING }
#define YAML_END \
{ YDT_NONE }
{ 0, YDT_NONE }
#define YAML_ROOT(nodes) \
{ YDT_ARRAY, 0, 0, NULL, {{ (nodes), {{ NULL, 1 }}}} }
{ 0, YDT_ARRAY, 1, NULL, {{ (nodes), {{ NULL }}}} }
#endif
+12 -12
View File
@@ -19,14 +19,14 @@
* GNU General Public License for more details.
*/
#include <string.h>
#include "debug.h"
#include "yaml_node.h"
#include "yaml_bits.h"
#include "yaml_tree_walker.h"
#include "yaml_parser.h"
#include <string.h>
#define MIN(a,b) (a < b ? a : b)
static void copy_string(char* dst, uint16_t dst_len, const char* src,
@@ -146,7 +146,7 @@ static bool yaml_output_attr(void* user, uint8_t* ptr, uint32_t bit_ofs,
if (node->type == YDT_CUSTOM && !node->u._cust_attr.write) return true;
// output tag
if (!wf(opaque, node->tag, node->tag_len)) return false;
if (!wf(opaque, node->tag, node->tag_len())) return false;
if (!wf(opaque, ": ", 2)) return false;
@@ -268,7 +268,7 @@ bool YamlTreeWalker::findNode(const char* tag, uint8_t tag_len)
while(attr && attr->type != YDT_NONE) {
if ((tag_len == attr->tag_len)
if ((tag_len == attr->tag_len())
&& !strncmp(tag, attr->tag, tag_len)) {
return true; // attribute found!
}
@@ -312,7 +312,7 @@ bool YamlTreeWalker::toChild()
bool is_array = false;
if (attr->type == YDT_ARRAY
&& attr->u._array.u._a.elmts > 1) {
&& attr->elmts > 1) {
is_array = true;
}
@@ -333,7 +333,7 @@ bool YamlTreeWalker::toChild()
if (!attr)
return false;
if ((attr->type == YDT_UNION) && (attr->tag_len == 0)) {
if ((attr->type == YDT_UNION) && (attr->tag_len() == 0)) {
toChild();
anon_union++;
}
@@ -359,7 +359,7 @@ bool YamlTreeWalker::toNextElmt()
setElmts(0);
}
if (getElmts() < node->u._array.u._a.elmts - 1) {
if (getElmts() < node->elmts - 1) {
incElmts();
rewind();
} else {
@@ -388,8 +388,8 @@ bool YamlTreeWalker::isElmtEmpty(uint8_t* data)
+ getLevelOfs();
// assume structs aligned on 8bit boundaries
if (node->u._array.u._a.is_active)
return !node->u._array.u._a.is_active(this, data, bit_ofs);
if (node->u._array.u.is_active)
return !node->u._array.u.is_active(this, data, bit_ofs);
return yaml_is_zero(data, bit_ofs, node->size);
}
@@ -417,7 +417,7 @@ void YamlTreeWalker::toNextAttr()
uint32_t attr_bit_ofs = getAttrOfs();
if (attr->type == YDT_ARRAY)
attr_bit_ofs += ((uint32_t)attr->u._array.u._a.elmts * attr->size);
attr_bit_ofs += ((uint32_t)attr->elmts * attr->size);
else
attr_bit_ofs += attr->size;
@@ -428,7 +428,7 @@ void YamlTreeWalker::toNextAttr()
// anonymous union handling
attr = getAttr();
if ((attr->type == YDT_UNION) && (attr->tag_len == 0)) {
if ((attr->type == YDT_UNION) && (strlen(attr->tag) == 0)) {
toChild();
anon_union++;
}
@@ -457,7 +457,7 @@ void YamlTreeWalker::setAttrValue(char* buf, uint16_t len)
i = yaml_str2uint(buf, len);
const YamlNode* node = getNode();
if (i < node->u._array.u._a.elmts) {
if (i < node->elmts) {
setElmts(i);
rewind();
} else {