Done some renaming for consistency.

git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@10715 35acf78f-673a-0410-8e92-d51de3d6d3f4
This commit is contained in:
Giovanni Di Sirio 2017-09-26 08:42:24 +00:00
parent 9f30d457c0
commit 3864014e03
2 changed files with 89 additions and 78 deletions

View File

@ -136,8 +136,8 @@ typedef struct ch_dyn_object {
* @brief Physical objects. * @brief Physical objects.
* @note This requires C99. * @note This requires C99.
*/ */
uint8_t obj[]; uint8_t buffer[];
} dyn_object_t; } dyn_buffer_t;
#endif #endif
#if (CH_CFG_FACTORY_SEMAPHORES == TRUE) || defined(__DOXIGEN__) #if (CH_CFG_FACTORY_SEMAPHORES == TRUE) || defined(__DOXIGEN__)
@ -194,11 +194,12 @@ objects_factory_t ch_factory;
extern "C" { extern "C" {
#endif #endif
void _factory_init(void); void _factory_init(void);
// dyn_registered_object_t *chFactoryRegisterObject(const char *name,
// void *objp);
#if (CH_CFG_FACTORY_GENERIC == TRUE) || defined(__DOXIGEN__) #if (CH_CFG_FACTORY_GENERIC == TRUE) || defined(__DOXIGEN__)
dyn_object_t *chFactoryCreateObject(const char *name, size_t size); dyn_buffer_t *chFactoryCreateBuffer(const char *name, size_t size);
dyn_object_t *chFactoryFindObject(const char *name); dyn_buffer_t *chFactoryFindBuffer(const char *name);
void chFactoryReleaseObject(dyn_object_t *dop); void chFactoryReleaseBuffer(dyn_buffer_t *dbp);
size_t chFactoryGetObjectSize(dyn_object_t *dop);
#endif #endif
#if (CH_CFG_FACTORY_SEMAPHORES == TRUE) || defined(__DOXIGEN__) #if (CH_CFG_FACTORY_SEMAPHORES == TRUE) || defined(__DOXIGEN__)
dyn_semaphore_t *chFactoryCreateSemaphore(const char *name, cnt_t n); dyn_semaphore_t *chFactoryCreateSemaphore(const char *name, cnt_t n);
@ -215,6 +216,7 @@ extern "C" {
/** /**
* @brief Duplicates an object reference. * @brief Duplicates an object reference.
* @note This function can be used on any kind of dynamic object.
* *
* @param[in] dep pointer to the element field of the object * @param[in] dep pointer to the element field of the object
* *
@ -229,6 +231,20 @@ static inline dyn_element_t *chFactoryDuplicateReferenceI(dyn_element_t *dep) {
return dep; return dep;
} }
#if (CH_CFG_FACTORY_GENERIC == TRUE) || defined(__DOXIGEN__)
/**
* @brief Returns the size of a generic dynamic buffer object.
*
* @return The size of the buffer object in bytes.
*
* @api
*/
static inline size_t chFactoryGetBufferSize(dyn_buffer_t *dbp) {
return chHeapGetSize(dbp) - sizeof (dyn_element_t);
}
#endif
#endif /* CH_CFG_USE_FACTORY == TRUE */ #endif /* CH_CFG_USE_FACTORY == TRUE */
#endif /* CHFACTORY_H */ #endif /* CHFACTORY_H */

View File

@ -132,141 +132,136 @@ void _factory_init(void) {
#if (CH_CFG_FACTORY_GENERIC == TRUE) || defined(__DOXIGEN__) #if (CH_CFG_FACTORY_GENERIC == TRUE) || defined(__DOXIGEN__)
/** /**
* @brief Creates a generic dynamic object. * @brief Creates a generic dynamic buffer object.
* @post A reference to the object is returned and the reference counter * @post A reference to the buffer object is returned and the reference
* is initialized to one. * counter is initialized to one.
* @post The object is zero filled. * @post The buffer object is zero filled.
* *
* @param[in] name name to be assigned to the new object * @param[in] name name to be assigned to the new buffer object
* @param[in] size payload size of the object to be created * @param[in] size payload size of the buffer object to be created
* *
* @return The reference to the created object. * @return The reference to the created buffer object.
* @retval NULL if the object cannot be allocated or an object with * @retval NULL if the buffer object cannot be allocated or a buffer
* the same name exists. * object with the same name exists.
* *
* @api * @api
*/ */
dyn_object_t *chFactoryCreateObject(const char *name, size_t size) { dyn_buffer_t *chFactoryCreateBuffer(const char *name, size_t size) {
dyn_object_t *dop; dyn_buffer_t *dbp;
chDbgCheck(name != NULL); chDbgCheck(name != NULL);
chSysLock(); chSysLock();
/* Checking if an object with this name has already been created.*/ /* Checking if a buffer object with this name has already been created.*/
dop = (dyn_object_t *)dyn_list_find(&ch_factory.obj_list, name); dbp = (dyn_buffer_t *)dyn_list_find(&ch_factory.obj_list, name);
if (dop != NULL) { if (dbp != NULL) {
/* Object exists, error.*/ /* Object exists, error.*/
chSysUnlock(); chSysUnlock();
return NULL; return NULL;
} }
/* Allocating space for the new object.*/ /* Allocating space for the new buffer object.*/
dop = chHeapAlloc(NULL, size); dbp = chHeapAlloc(NULL, size);
if (dop == NULL) { if (dbp == NULL) {
chSysUnlock(); chSysUnlock();
return NULL; return NULL;
} }
/* Initializing object data and metadata.*/ /* Initializing buffer object data and metadata.*/
strncpy(dop->element.name, name, CH_CFG_FACTORY_MAX_NAMES_LENGHT); strncpy(dbp->element.name, name, CH_CFG_FACTORY_MAX_NAMES_LENGHT);
dop->element.refs = 1; dbp->element.refs = 1;
dop->element.next = ch_factory.obj_list.next; dbp->element.next = ch_factory.obj_list.next;
memset((void *)dop->obj, 0, size); memset((void *)dbp->buffer, 0, size);
/* Updating factory list.*/ /* Updating factory list.*/
ch_factory.obj_list.next = (dyn_element_t *)dop; ch_factory.obj_list.next = (dyn_element_t *)dbp;
chSysUnlock(); chSysUnlock();
return dop; return dbp;
} }
/** /**
* @brief Retrieves a generic dynamic object. * @brief Retrieves a generic dynamic buffer object.
* @post A reference to the object is returned with the reference counter * @post A reference to the buffer object is returned with the reference
* increased by one. * counter increased by one.
* *
* @param[in] name name to be assigned to the new object * @param[in] name name to be assigned to the new buffer object
* *
* @return The reference to the found object. * @return The reference to the found buffer object.
* @retval NULL if an object with the specified name name does * @retval NULL if a buffer object with the specified name name does
* not exist. * not exist.
* *
* @api * @api
*/ */
dyn_object_t *chFactoryFindObject(const char *name) { dyn_buffer_t *chFactoryFindBuffer(const char *name) {
dyn_object_t *dop; dyn_buffer_t *dbp;
chDbgCheck(name != NULL); chDbgCheck(name != NULL);
chSysLock(); chSysLock();
/* Checking if an object with this name has already been created.*/ /* Checking if a buffer object with this name has already been created.*/
dop = (dyn_object_t *)dyn_list_find(&ch_factory.obj_list, name); dbp = (dyn_buffer_t *)dyn_list_find(&ch_factory.obj_list, name);
if (dop == NULL) { if (dbp == NULL) {
/* Object does not exists, error.*/ /* The buffer object does not exists, error.*/
chSysUnlock(); chSysUnlock();
return NULL; return NULL;
} }
/* Increasing references counter.*/ /* Increasing references counter.*/
dop->element.refs += 1; dbp->element.refs += 1;
chSysUnlock(); chSysUnlock();
return dop; return dbp;
} }
/** /**
* @brief Releases a generic dynamic object. * @brief Releases a generic dynamic buffer object.
* @details The reference counter of the object is decreased by one, if * @details The reference counter of the buffer object is decreased by one, if
* reaches zero then the object memory is freed. * reaches zero then the buffer object memory is freed.
* *
* @param[in] dop generic object reference * @param[in] dbp generic buffer object reference
* *
* @api * @api
*/ */
void chFactoryReleaseObject(dyn_object_t *dop) { void chFactoryReleaseBuffer(dyn_buffer_t *dbp) {
chDbgCheck(dop != NULL); chDbgCheck(dbp != NULL);
chSysLock(); chSysLock();
chDbgAssert(dop->element.refs > 0U, "invalid references number"); chDbgAssert(dbp->element.refs > 0U, "invalid references number");
dop = (dyn_object_t *)dyn_list_unlink(&ch_factory.obj_list, dbp = (dyn_buffer_t *)dyn_list_unlink(&ch_factory.obj_list,
&dop->element); &dbp->element);
chDbgAssert(dop != NULL, "invalid reference passed"); chDbgAssert(dbp != NULL, "invalid reference passed");
dop->element.refs--; dbp->element.refs--;
if (dop->element.refs == 0) { if (dbp->element.refs == 0) {
chHeapFree((void *)dop); chHeapFree((void *)dbp);
} }
chSysUnlock(); chSysUnlock();
} }
size_t chFactoryGetObjectSize(dyn_object_t *dop) {
return chHeapGetSize((void *)dop);
}
#endif /* CH_CFG_FACTORY_GENERIC = TRUE */ #endif /* CH_CFG_FACTORY_GENERIC = TRUE */
#if (CH_CFG_FACTORY_SEMAPHORES == TRUE) || defined(__DOXIGEN__) #if (CH_CFG_FACTORY_SEMAPHORES == TRUE) || defined(__DOXIGEN__)
/** /**
* @brief Creates a dynamic semaphore object. * @brief Creates a dynamic semaphore object.
* @post A reference to the semaphore is returned and the reference counter * @post A reference to the semaphore object is returned and the reference
* is initialized to one. * counter is initialized to one.
* @post The semaphore object is initialized and ready to use. * @post The semaphore object is initialized and ready to use.
* *
* @param[in] name name to be assigned to the new semaphore object * @param[in] name name to be assigned to the new semaphore object
* @param[in] n semaphore counter initialization value * @param[in] n semaphore object counter initialization value
* *
* @return The reference to the created semaphore object. * @return The reference to the created semaphore object.
* @retval NULL if the semaphore cannot be allocated or an semaphore * @retval NULL if the semaphore object cannot be allocated or a
* with the same name exists. * semaphore with the same name exists.
* *
* @api * @api
*/ */
@ -277,22 +272,22 @@ dyn_semaphore_t *chFactoryCreateSemaphore(const char *name, cnt_t n) {
chSysLock(); chSysLock();
/* Checking if an object with this name has already been created.*/ /* Checking if a semaphore object with this name has already been created.*/
dsp = (dyn_semaphore_t *)dyn_list_find(&ch_factory.sem_list, name); dsp = (dyn_semaphore_t *)dyn_list_find(&ch_factory.sem_list, name);
if (dsp != NULL) { if (dsp != NULL) {
/* Semaphore exists, error.*/ /* The semaphore object exists, error.*/
chSysUnlock(); chSysUnlock();
return NULL; return NULL;
} }
/* Allocating space for the new object.*/ /* Allocating space for the new semaphore object.*/
dsp = chCoreAlloc(sizeof (dyn_semaphore_t)); dsp = chCoreAlloc(sizeof (dyn_semaphore_t));
if (dsp == NULL) { if (dsp == NULL) {
chSysUnlock(); chSysUnlock();
return NULL; return NULL;
} }
/* Initializing object data and metadata.*/ /* Initializing semaphore object data and metadata.*/
strncpy(dsp->element.name, name, CH_CFG_FACTORY_MAX_NAMES_LENGHT); strncpy(dsp->element.name, name, CH_CFG_FACTORY_MAX_NAMES_LENGHT);
dsp->element.refs = 1; dsp->element.refs = 1;
dsp->element.next = ch_factory.obj_list.next; dsp->element.next = ch_factory.obj_list.next;
@ -308,13 +303,13 @@ dyn_semaphore_t *chFactoryCreateSemaphore(const char *name, cnt_t n) {
/** /**
* @brief Retrieves a generic dynamic semaphore object. * @brief Retrieves a generic dynamic semaphore object.
* @post A reference to the semaphore is returned with the reference counter * @post A reference to the semaphore object is returned with the reference
* increased by one. * counter increased by one.
* *
* @param[in] name name to be assigned to the new semaphore object * @param[in] name name to be assigned to the new semaphore object
* *
* @return The reference to the found semaphore object. * @return The reference to the found semaphore object.
* @retval NULL if a semaphore with the specified name name does * @retval NULL if a semaphore object with the specified name name does
* not exist. * not exist.
* *
* @api * @api
@ -326,10 +321,10 @@ dyn_semaphore_t *chFactoryFindSemaphore(const char *name) {
chSysLock(); chSysLock();
/* Checking if an object with this name has already been created.*/ /* Checking if a semaphore object with this name has already been created.*/
dsp = (dyn_semaphore_t *)dyn_list_find(&ch_factory.obj_list, name); dsp = (dyn_semaphore_t *)dyn_list_find(&ch_factory.obj_list, name);
if (dsp == NULL) { if (dsp == NULL) {
/* Object does not exists, error.*/ /* The semaphore object does not exists, error.*/
chSysUnlock(); chSysUnlock();
return NULL; return NULL;
} }
@ -344,8 +339,8 @@ dyn_semaphore_t *chFactoryFindSemaphore(const char *name) {
/** /**
* @brief Releases a semaphore dynamic object. * @brief Releases a semaphore dynamic object.
* @details The reference counter of the semaphore is decreased by one, if * @details The reference counter of the semaphore object is decreased by one,
* reaches zero then the semaphore memory is freed. * if reaches zero then the semaphore object memory is freed.
* *
* @param[in] dsp semaphore object reference * @param[in] dsp semaphore object reference
* *