Files
PX4-Autopilot/platforms/common/module_params.cpp
Jacob Dahl ccd9eae185 refactor(platform): load DEFINE_PARAMETERS members from a table (#29554)
* fix(cmake): link the functional gtest libraries as a group

px4_platform, parameters and uorb_msgs all reference uORB, but uORB is
listed before them, so a test only linked when something earlier
happened to pull in px4_log.o. Inlined param_get() type checks did that
for any test using DEFINE_PARAMETERS; without them PerformanceModel and
StickYaw failed with undefined orb_advertise and orb_publish.

Assisted-by: Claude:claude-opus-5-5

* refactor(platform): load DEFINE_PARAMETERS members from a table

Every parameter cost a param_get_mark_used() call in its class
constructor and a param_get() call in updateParamsImpl(), about 12 bytes
each, for roughly 2000 parameters. Each Param now takes one
pointer-sized slot, so the members a DEFINE_PARAMETERS list declares
form an array. A ParamTable member declared right after them holds the
handles and loads them all with one call, in the constructor and on
update. External parameters keep a pointer instead of a reference so
the loader can follow it.

Assisted-by: Claude:claude-opus-5-5

* refactor(platform): use param_t for parameter table entries

Each entry is a parameter handle with the storage kind in its top bits,
so it takes the handle's type.

Assisted-by: Claude:claude-opus-5-5

* refactor(platform): keep each Param at its natural size

Padding every Param to a pointer-sized slot doubled float and int32
members on 64-bit targets and grew ParamBool from 1 to 4 bytes. The
macro now passes the first parameter's address to the table, and the
loader steps through the members at each kind's own size and alignment.
A table placed before the parameters would instead load through the
external parameters before their pointers are set.

Assisted-by: Claude:claude-opus-5-5

* refactor(platform): drop the parameter table end marker

APPLY_ALL leaves a comma after every entry, so the template argument
list needed a closing element, which every class then stored in its
entry array. The entries are now a static array of the class, whose
braced initializer accepts the trailing comma.

Assisted-by: Claude:claude-opus-5-5

* refactor(platform): check the parameter layout at compile time

The language leaves padding between members to the compiler, so the
table loader relies on the ABI placing each member at the next address
its alignment allows. Each class's updateParamsImpl() now checks every
parameter's offsetof against where the loader steps, so a compiler that
pads differently fails the build instead of loading into the wrong
members.

Assisted-by: Claude:claude-opus-5-5

* docs(platform): describe how DEFINE_PARAMETERS loads parameters

The table loader walks the members of a class from the first parameter's
address, which is not obvious from the macros or the loader alone. A
block above the macros now states the flash cost it removes, what the
macro adds to a class, how the loader steps, and which static_asserts
guard the layout it relies on.

The namespace comment said a Param does not depend on the macro. That
stopped being true when the constructors stopped loading.

Assisted-by: Claude:claude-fable-5-2

* docs(platform): drop the flash figure from the parameter table comment

A measured saving for one board goes stale as parameters are added. The
comment now states what the table replaces the per-parameter calls with.

Assisted-by: Claude:claude-fable-5-2

* fix(platform): build the parameter layout check on GCC before 9

GCC 7 and 8 ignore _Pragma diagnostics inside a macro expansion, so the
offsetof warning in the DEFINE_PARAMETERS layout check stays an error on
the VOXL 2 toolchain. Those compilers now build with
-Wno-invalid-offsetof. The static_assert still runs there.

Assisted-by: Claude:claude-opus-5-5
2026-11-24 17:04:42 -07:00

102 lines
3.2 KiB
C++

/****************************************************************************
*
* Copyright (c) 2026 PX4 Development Team. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name PX4 nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
#include <px4_platform_common/module_params.h>
#include <px4_platform_common/param.h>
#include <string.h>
namespace do_not_explicitly_use_this_namespace
{
void param_table_load(const param_t *entries, unsigned count, void *first, bool mark_used)
{
uintptr_t member = reinterpret_cast<uintptr_t>(first);
for (unsigned i = 0; i < count; i++) {
const param_t handle = entries[i] & param_table_handle_mask;
const param_t kind = entries[i] & ~param_table_handle_mask;
member = param_table_member_start(member, kind);
uint8_t *slot = reinterpret_cast<uint8_t *>(member);
member += param_table_member_size(kind);
void *dest = slot;
int32_t bool_value;
if (kind == param_table_external) {
memcpy(&dest, slot, sizeof(dest));
} else if (kind == param_table_bool) {
dest = &bool_value;
}
// (param_get) is the C function, not the type-checked C++ wrapper macro
const int ret = mark_used ? param_get_mark_used(handle, dest) : (param_get)(handle, dest);
if (kind == param_table_bool && ret == 0) {
*reinterpret_cast<bool *>(slot) = bool_value != 0;
}
}
}
} // namespace do_not_explicitly_use_this_namespace
ModuleParams::ModuleParams(ModuleParams *parent)
{
setParent(parent);
}
void ModuleParams::setParent(ModuleParams *parent)
{
if (parent) {
parent->_children.add(this);
}
_parent = parent;
}
void ModuleParams::updateParams()
{
for (const auto &child : _children) {
child->updateParams();
}
updateParamsImpl();
}
ModuleParams::~ModuleParams()
{
if (_parent) { _parent->_children.remove(this); }
}