portability: cmsis: Avoid copying objects names into control block

Instead, just store the pointer to the string provided as part of the
RTOS object init attributes.

Signed-off-by: Utsav Munendra <utsavm@meta.com>
This commit is contained in:
Utsav Munendra 2025-04-13 00:36:14 -07:00 committed by Benjamin Cabé
commit fd8abcff37
7 changed files with 34 additions and 55 deletions

View file

@ -11,9 +11,6 @@
#include <zephyr/kernel.h> #include <zephyr/kernel.h>
#include <zephyr/portability/cmsis_os2.h> #include <zephyr/portability/cmsis_os2.h>
/** @brief Size for names of RTOS objects. */
#define CMSIS_OBJ_NAME_MAX_LEN 16
/** /**
* @brief Control block for a CMSIS-RTOSv2 thread. * @brief Control block for a CMSIS-RTOSv2 thread.
* *
@ -40,7 +37,7 @@ struct cmsis_rtos_timer_cb {
osTimerType_t type; osTimerType_t type;
uint32_t status; uint32_t status;
bool is_cb_dynamic_allocation; bool is_cb_dynamic_allocation;
char name[CMSIS_OBJ_NAME_MAX_LEN]; const char *name;
void (*callback_function)(void *argument); void (*callback_function)(void *argument);
void *arg; void *arg;
}; };
@ -54,7 +51,7 @@ struct cmsis_rtos_timer_cb {
struct cmsis_rtos_mutex_cb { struct cmsis_rtos_mutex_cb {
struct k_mutex z_mutex; struct k_mutex z_mutex;
bool is_cb_dynamic_allocation; bool is_cb_dynamic_allocation;
char name[CMSIS_OBJ_NAME_MAX_LEN]; const char *name;
uint32_t state; uint32_t state;
}; };
@ -67,7 +64,7 @@ struct cmsis_rtos_mutex_cb {
struct cmsis_rtos_semaphore_cb { struct cmsis_rtos_semaphore_cb {
struct k_sem z_semaphore; struct k_sem z_semaphore;
bool is_cb_dynamic_allocation; bool is_cb_dynamic_allocation;
char name[CMSIS_OBJ_NAME_MAX_LEN]; const char *name;
}; };
/** /**
@ -81,7 +78,7 @@ struct cmsis_rtos_mempool_cb {
void *pool; void *pool;
char is_dynamic_allocation; char is_dynamic_allocation;
bool is_cb_dynamic_allocation; bool is_cb_dynamic_allocation;
char name[CMSIS_OBJ_NAME_MAX_LEN]; const char *name;
}; };
/** /**
@ -95,7 +92,7 @@ struct cmsis_rtos_msgq_cb {
void *pool; void *pool;
char is_dynamic_allocation; char is_dynamic_allocation;
bool is_cb_dynamic_allocation; bool is_cb_dynamic_allocation;
char name[CMSIS_OBJ_NAME_MAX_LEN]; const char *name;
}; };
/** /**
@ -109,7 +106,7 @@ struct cmsis_rtos_event_cb {
struct k_poll_event poll_event; struct k_poll_event poll_event;
uint32_t signal_results; uint32_t signal_results;
bool is_cb_dynamic_allocation; bool is_cb_dynamic_allocation;
char name[CMSIS_OBJ_NAME_MAX_LEN]; const char *name;
}; };
#endif #endif

View file

@ -50,11 +50,7 @@ osEventFlagsId_t osEventFlagsNew(const osEventFlagsAttr_t *attr)
&events->poll_signal); &events->poll_signal);
events->signal_results = 0U; events->signal_results = 0U;
if (attr->name == NULL) { events->name = (attr->name == NULL) ? init_event_flags_attrs.name : attr->name;
strncpy(events->name, init_event_flags_attrs.name, sizeof(events->name) - 1);
} else {
strncpy(events->name, attr->name, sizeof(events->name) - 1);
}
return (osEventFlagsId_t)events; return (osEventFlagsId_t)events;
} }
@ -207,16 +203,16 @@ uint32_t osEventFlagsWait(osEventFlagsId_t ef_id, uint32_t flags, uint32_t optio
/** /**
* @brief Get name of an Event Flags object. * @brief Get name of an Event Flags object.
* This function may be called from Interrupt Service Routines.
*/ */
const char *osEventFlagsGetName(osEventFlagsId_t ef_id) const char *osEventFlagsGetName(osEventFlagsId_t ef_id)
{ {
struct cmsis_rtos_event_cb *events = (struct cmsis_rtos_event_cb *)ef_id; struct cmsis_rtos_event_cb *events = (struct cmsis_rtos_event_cb *)ef_id;
if (!k_is_in_isr() && (ef_id != NULL)) { if (events == NULL) {
return events->name;
} else {
return NULL; return NULL;
} }
return events->name;
} }
/** /**

View file

@ -84,11 +84,7 @@ osMemoryPoolId_t osMemoryPoolNew(uint32_t block_count, uint32_t block_size,
return NULL; return NULL;
} }
if (attr->name == NULL) { mslab->name = (attr->name == NULL) ? init_mslab_attrs.name : attr->name;
strncpy(mslab->name, init_mslab_attrs.name, sizeof(mslab->name) - 1);
} else {
strncpy(mslab->name, attr->name, sizeof(mslab->name) - 1);
}
return (osMemoryPoolId_t)mslab; return (osMemoryPoolId_t)mslab;
} }
@ -152,16 +148,16 @@ osStatus_t osMemoryPoolFree(osMemoryPoolId_t mp_id, void *block)
/** /**
* @brief Get name of a Memory Pool object. * @brief Get name of a Memory Pool object.
* This function may be called from Interrupt Service Routines.
*/ */
const char *osMemoryPoolGetName(osMemoryPoolId_t mp_id) const char *osMemoryPoolGetName(osMemoryPoolId_t mp_id)
{ {
struct cmsis_rtos_mempool_cb *mslab = (struct cmsis_rtos_mempool_cb *)mp_id; struct cmsis_rtos_mempool_cb *mslab = (struct cmsis_rtos_mempool_cb *)mp_id;
if (!k_is_in_isr() && (mslab != NULL)) { if (mslab == NULL) {
return mslab->name;
} else {
return NULL; return NULL;
} }
return mslab->name;
} }
/** /**

View file

@ -79,11 +79,7 @@ osMessageQueueId_t osMessageQueueNew(uint32_t msg_count, uint32_t msg_size,
k_msgq_init(&msgq->z_msgq, msgq->pool, msg_size, msg_count); k_msgq_init(&msgq->z_msgq, msgq->pool, msg_size, msg_count);
if (attr->name == NULL) { msgq->name = (attr->name == NULL) ? init_msgq_attrs.name : attr->name;
strncpy(msgq->name, init_msgq_attrs.name, sizeof(msgq->name) - 1);
} else {
strncpy(msgq->name, attr->name, sizeof(msgq->name) - 1);
}
return (osMessageQueueId_t)(msgq); return (osMessageQueueId_t)(msgq);
} }
@ -222,16 +218,16 @@ uint32_t osMessageQueueGetSpace(osMessageQueueId_t msgq_id)
/** /**
* @brief Get name of a Message Queue object. * @brief Get name of a Message Queue object.
* This function may be called from Interrupt Service Routines.
*/ */
const char *osMessageQueueGetName(osMessageQueueId_t msgq_id) const char *osMessageQueueGetName(osMessageQueueId_t msgq_id)
{ {
struct cmsis_rtos_msgq_cb *msgq = (struct cmsis_rtos_msgq_cb *)msgq_id; struct cmsis_rtos_msgq_cb *msgq = (struct cmsis_rtos_msgq_cb *)msgq_id;
if (!k_is_in_isr() && (msgq_id != NULL)) { if (msgq == NULL) {
return msgq->name;
} else {
return NULL; return NULL;
} }
return msgq->name;
} }
/** /**

View file

@ -51,11 +51,7 @@ osMutexId_t osMutexNew(const osMutexAttr_t *attr)
k_mutex_init(&mutex->z_mutex); k_mutex_init(&mutex->z_mutex);
mutex->state = attr->attr_bits; mutex->state = attr->attr_bits;
if (attr->name == NULL) { mutex->name = (attr->name == NULL) ? init_mutex_attrs.name : attr->name;
strncpy(mutex->name, init_mutex_attrs.name, sizeof(mutex->name) - 1);
} else {
strncpy(mutex->name, attr->name, sizeof(mutex->name) - 1);
}
return (osMutexId_t)mutex; return (osMutexId_t)mutex;
} }
@ -156,13 +152,16 @@ osThreadId_t osMutexGetOwner(osMutexId_t mutex_id)
return get_cmsis_thread_id(mutex->z_mutex.owner); return get_cmsis_thread_id(mutex->z_mutex.owner);
} }
/**
* @brief Get name of a mutex.
* This function may be called from Interrupt Service Routines.
*/
const char *osMutexGetName(osMutexId_t mutex_id) const char *osMutexGetName(osMutexId_t mutex_id)
{ {
struct cmsis_rtos_mutex_cb *mutex = (struct cmsis_rtos_mutex_cb *)mutex_id; struct cmsis_rtos_mutex_cb *mutex = (struct cmsis_rtos_mutex_cb *)mutex_id;
if (k_is_in_isr() || (mutex == NULL)) { if (mutex == NULL) {
return NULL; return NULL;
} }
return mutex->name; return mutex->name;
} }

View file

@ -47,11 +47,7 @@ osSemaphoreId_t osSemaphoreNew(uint32_t max_count, uint32_t initial_count,
k_sem_init(&semaphore->z_semaphore, initial_count, max_count); k_sem_init(&semaphore->z_semaphore, initial_count, max_count);
if (attr->name == NULL) { semaphore->name = (attr->name == NULL) ? init_sema_attrs.name : attr->name;
strncpy(semaphore->name, init_sema_attrs.name, sizeof(semaphore->name) - 1);
} else {
strncpy(semaphore->name, attr->name, sizeof(semaphore->name) - 1);
}
return (osSemaphoreId_t)semaphore; return (osSemaphoreId_t)semaphore;
} }
@ -148,13 +144,16 @@ osStatus_t osSemaphoreDelete(osSemaphoreId_t semaphore_id)
return osOK; return osOK;
} }
/**
* @brief Get name of a semaphore.
* This function may be called from Interrupt Service Routines.
*/
const char *osSemaphoreGetName(osSemaphoreId_t semaphore_id) const char *osSemaphoreGetName(osSemaphoreId_t semaphore_id)
{ {
struct cmsis_rtos_semaphore_cb *semaphore = (struct cmsis_rtos_semaphore_cb *)semaphore_id; struct cmsis_rtos_semaphore_cb *semaphore = (struct cmsis_rtos_semaphore_cb *)semaphore_id;
if (!k_is_in_isr() && (semaphore_id != NULL)) { if (semaphore == NULL) {
return semaphore->name;
} else {
return NULL; return NULL;
} }
return semaphore->name;
} }

View file

@ -67,11 +67,7 @@ osTimerId_t osTimerNew(osTimerFunc_t func, osTimerType_t type, void *argument,
k_timer_init(&timer->z_timer, zephyr_timer_wrapper, NULL); k_timer_init(&timer->z_timer, zephyr_timer_wrapper, NULL);
if (attr->name == NULL) { timer->name = (attr->name == NULL) ? init_timer_attrs.name : attr->name;
strncpy(timer->name, init_timer_attrs.name, sizeof(timer->name) - 1);
} else {
strncpy(timer->name, attr->name, sizeof(timer->name) - 1);
}
return (osTimerId_t)timer; return (osTimerId_t)timer;
} }
@ -153,15 +149,15 @@ osStatus_t osTimerDelete(osTimerId_t timer_id)
/** /**
* @brief Get name of a timer. * @brief Get name of a timer.
* This function may be called from Interrupt Service Routines.
*/ */
const char *osTimerGetName(osTimerId_t timer_id) const char *osTimerGetName(osTimerId_t timer_id)
{ {
struct cmsis_rtos_timer_cb *timer = (struct cmsis_rtos_timer_cb *)timer_id; struct cmsis_rtos_timer_cb *timer = (struct cmsis_rtos_timer_cb *)timer_id;
if (k_is_in_isr() || (timer == NULL)) { if (timer == NULL) {
return NULL; return NULL;
} }
return timer->name; return timer->name;
} }