Skip to content
Permalink
Browse files

linker generated list: introduce Z_STRUCT_SECTION_ITERABLE()

This convenience macro wraps Z_DECL_ALIGN() and __in_section() to
simplify static definitions of structure instances gathered in dedicated
sections. Most of the time those go together, and the section name is
already closely related to the struct type, so abstracting things behind
a simpler interface reduces probability of mistakes and makes the code
clearer. A few input section names have been adjusted accordingly.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
  • Loading branch information...
Nicolas Pitre authored and andrewboie committed Jun 3, 2019
1 parent 8bb1f2a commit b1d3742ce21ecd8c88bfc983bb47c6b593638ddf
Showing with 35 additions and 39 deletions.
  1. +20 −34 include/kernel.h
  2. +3 −1 include/linker/common-ram.ld
  3. +2 −2 include/linker/common-rom.ld
  4. +9 −0 include/toolchain/common.h
  5. +1 −2 subsys/bluetooth/host/settings.h
@@ -186,9 +186,8 @@ struct _k_object_assignment {
#define K_THREAD_ACCESS_GRANT(name_, ...) \
static void * const _CONCAT(_object_list_, name_)[] = \
{ __VA_ARGS__, NULL }; \
static __used __in_section_unique(object_access) \
const Z_DECL_ALIGN(struct _k_object_assignment) \
_CONCAT(_object_access_, name_) = \
static const Z_STRUCT_SECTION_ITERABLE(_k_object_assignment, \
_CONCAT(_object_access_, name_)) = \
{ (&_k_thread_obj_ ## name_), \
(_CONCAT(_object_list_, name_)) }

@@ -968,9 +967,8 @@ struct _static_thread_data {
entry, p1, p2, p3, \
prio, options, delay) \
K_THREAD_STACK_DEFINE(_k_thread_stack_##name, stack_size); \
struct k_thread _k_thread_obj_##name; \
Z_DECL_ALIGN(struct _static_thread_data) _k_thread_data_##name \
__in_section(_static_thread_data, static, name) = \
struct k_thread _k_thread_obj_##name; \
Z_STRUCT_SECTION_ITERABLE(_static_thread_data, _k_thread_data_##name) =\
_THREAD_INITIALIZER(&_k_thread_obj_##name, \
_k_thread_stack_##name, stack_size, \
entry, p1, p2, p3, prio, options, delay, \
@@ -1468,8 +1466,7 @@ typedef void (*k_timer_stop_t)(struct k_timer *timer);
* @param stop_fn Function to invoke if the timer is stopped while running.
*/
#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Z_DECL_ALIGN(struct k_timer) name \
__in_section(_k_timer, static, name) = \
Z_STRUCT_SECTION_ITERABLE(k_timer, name) = \
Z_TIMER_INITIALIZER(name, expiry_fn, stop_fn)

/**
@@ -2050,8 +2047,7 @@ static inline void *z_impl_k_queue_peek_tail(struct k_queue *queue)
* @param name Name of the queue.
*/
#define K_QUEUE_DEFINE(name) \
Z_DECL_ALIGN(struct k_queue) name \
__in_section(_k_queue, static, name) = \
Z_STRUCT_SECTION_ITERABLE(k_queue, name) = \
_K_QUEUE_INITIALIZER(name)

/** @} */
@@ -2266,8 +2262,7 @@ struct k_fifo {
* @req K-FIFO-002
*/
#define K_FIFO_DEFINE(name) \
Z_DECL_ALIGN(struct k_fifo) name \
__in_section(_k_queue, static, name) = \
Z_STRUCT_SECTION_ITERABLE(k_fifo, name) = \
Z_FIFO_INITIALIZER(name)

/** @} */
@@ -2378,8 +2373,7 @@ struct k_lifo {
* @req K-LIFO-002
*/
#define K_LIFO_DEFINE(name) \
Z_DECL_ALIGN(struct k_lifo) name \
__in_section(_k_queue, static, name) = \
Z_STRUCT_SECTION_ITERABLE(k_lifo, name) = \
_K_LIFO_INITIALIZER(name)

/** @} */
@@ -2514,8 +2508,7 @@ __syscall int k_stack_pop(struct k_stack *stack, u32_t *data, s32_t timeout);
#define K_STACK_DEFINE(name, stack_num_entries) \
u32_t __noinit \
_k_stack_buf_##name[stack_num_entries]; \
Z_DECL_ALIGN(struct k_stack) name \
__in_section(_k_stack, static, name) = \
Z_STRUCT_SECTION_ITERABLE(k_stack, name) = \
_K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
stack_num_entries)

@@ -2949,8 +2942,7 @@ struct k_mutex {
* @req K-MUTEX-001
*/
#define K_MUTEX_DEFINE(name) \
Z_DECL_ALIGN(struct k_mutex) name \
__in_section(_k_mutex, static, name) = \
Z_STRUCT_SECTION_ITERABLE(k_mutex, name) = \
_K_MUTEX_INITIALIZER(name)

/**
@@ -3150,8 +3142,7 @@ static inline unsigned int z_impl_k_sem_count_get(struct k_sem *sem)
* @req K-SEM-002
*/
#define K_SEM_DEFINE(name, initial_count, count_limit) \
Z_DECL_ALIGN(struct k_sem) name \
__in_section(_k_sem, static, name) = \
Z_STRUCT_SECTION_ITERABLE(k_sem, name) = \
Z_SEM_INITIALIZER(name, initial_count, count_limit); \
BUILD_ASSERT(((count_limit) != 0) && \
((initial_count) <= (count_limit)));
@@ -3237,12 +3228,11 @@ struct k_msgq_attrs {
*
* @req K-MSGQ-001
*/
#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
static char __noinit __aligned(q_align) \
_k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Z_DECL_ALIGN(struct k_msgq) q_name \
__in_section(_k_msgq, static, q_name) = \
_K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
static char __noinit __aligned(q_align) \
_k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Z_STRUCT_SECTION_ITERABLE(k_msgq, q_name) = \
_K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
q_msg_size, q_max_msgs)

/**
@@ -3504,8 +3494,7 @@ struct k_mbox {
* @req K-MBOX-001
*/
#define K_MBOX_DEFINE(name) \
Z_DECL_ALIGN(struct k_mbox) name \
__in_section(_k_mbox, static, name) = \
Z_STRUCT_SECTION_ITERABLE(k_mbox, name) = \
_K_MBOX_INITIALIZER(name) \

/**
@@ -3707,8 +3696,7 @@ struct k_pipe {
#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
static unsigned char __noinit __aligned(pipe_align) \
_k_pipe_buf_##name[pipe_buffer_size]; \
Z_DECL_ALIGN(struct k_pipe) name \
__in_section(_k_pipe, static, name) = \
Z_STRUCT_SECTION_ITERABLE(k_pipe, name) = \
_K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)

/**
@@ -3888,8 +3876,7 @@ struct k_mem_slab {
#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
char __noinit __aligned(slab_align) \
_k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
Z_DECL_ALIGN(struct k_mem_slab) name \
__in_section(_k_mem_slab, static, name) = \
Z_STRUCT_SECTION_ITERABLE(k_mem_slab, name) = \
_K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
slab_block_size, slab_num_blocks)

@@ -4025,8 +4012,7 @@ struct k_mem_pool {
char __aligned(align) _mpool_buf_##name[_ALIGN4(maxsz * nmax) \
+ _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \
struct sys_mem_pool_lvl _mpool_lvls_##name[Z_MPOOL_LVLS(maxsz, minsz)]; \
Z_DECL_ALIGN(struct k_mem_pool) name \
__in_section(_k_mem_pool, static, name) = { \
Z_STRUCT_SECTION_ITERABLE(k_mem_pool, name) = { \
.base = { \
.buf = _mpool_buf_##name, \
.max_sz = maxsz, \
@@ -34,7 +34,7 @@
SECTION_DATA_PROLOGUE(_static_thread_area,,SUBALIGN(4))
{
_static_thread_data_list_start = .;
KEEP(*(SORT_BY_NAME("._static_thread_data.static.*")))
KEEP(*(SORT_BY_NAME(".__static_thread_data.static.*")))
_static_thread_data_list_end = .;
} GROUP_DATA_LINK_IN(RAMABLE_REGION, ROMABLE_REGION)

@@ -88,6 +88,8 @@
{
_k_queue_list_start = .;
KEEP(*("._k_queue.static.*"))
KEEP(*("._k_fifo.static.*"))
KEEP(*("._k_lifo.static.*"))
_k_queue_list_end = .;
} GROUP_DATA_LINK_IN(RAMABLE_REGION, ROMABLE_REGION)

@@ -39,7 +39,7 @@
SECTION_PROLOGUE(object_access,,)
{
__object_access_start = .;
KEEP(*(".object_access.*"))
KEEP(*(".__k_object_assignment.*"))
__object_access_end = .;
} GROUP_LINK_IN(ROMABLE_REGION)
#endif
@@ -104,7 +104,7 @@
SECTION_DATA_PROLOGUE(_bt_settings_area,,SUBALIGN(4))
{
_bt_settings_start = .;
KEEP(*(SORT_BY_NAME("._bt_settings.static.*")))
KEEP(*(SORT_BY_NAME("._bt_settings_handler.static.*")))
_bt_settings_end = .;
} GROUP_LINK_IN(ROMABLE_REGION)
#endif
@@ -163,4 +163,13 @@
*/
#define Z_DECL_ALIGN(type) __aligned(__alignof(type)) type

/*
* Convenience helper combining __in_section() and Z_DECL_ALIGN().
* The section name is the struct type prepended with an underscore.
* The subsection is "static" and the subsubsection is the variable name.
*/
#define Z_STRUCT_SECTION_ITERABLE(struct_type, name) \
Z_DECL_ALIGN(struct struct_type) name \
__in_section(_##struct_type, static, name) __used

#endif /* ZEPHYR_INCLUDE_TOOLCHAIN_COMMON_H_ */
@@ -14,8 +14,7 @@ struct bt_settings_handler {
};

#define BT_SETTINGS_DEFINE(_name, _set, _commit, _export) \
const Z_DECL_ALIGN(struct bt_settings_handler) _name \
__in_section(_bt_settings, static, _name) = { \
const Z_STRUCT_SECTION_ITERABLE(bt_settings_handler, _name) = { \
.name = STRINGIFY(_name), \
.set = _set, \
.commit = _commit, \

0 comments on commit b1d3742

Please sign in to comment.
You can’t perform that action at this time.