Skip to content

Commit 67c0f88

Browse files
authored
Merge pull request #595 from jphickey/fix-589-objid-to-arrayindex
Fix #589, promote OS_ObjectIdToArrayIndex to public API
2 parents c3b1398 + 7fd8933 commit 67c0f88

File tree

3 files changed

+90
-61
lines changed

3 files changed

+90
-61
lines changed

src/os/inc/osapi-os-core.h

+39
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,12 @@ uint32 OS_IdentifyObject (osal_id_t object_id);
428428
* @note This does NOT verify the validity of the ID, that is left to the caller.
429429
* This is only the conversion logic.
430430
*
431+
* This routine accepts any object type, and returns a value based on the
432+
* maximum number of objects for that type. This is equivalent to invoking
433+
* OS_ObjectIdToArrayIndex() with the idtype set to OS_OBJECT_TYPE_UNDEFINED.
434+
*
435+
* @sa OS_ObjectIdToArrayIndex
436+
*
431437
* @param[in] object_id The object ID to operate on
432438
* @param[out] *ArrayIndex The Index to return
433439
*
@@ -437,6 +443,39 @@ uint32 OS_IdentifyObject (osal_id_t object_id);
437443
*/
438444
int32 OS_ConvertToArrayIndex (osal_id_t object_id, uint32 *ArrayIndex);
439445

446+
/*-------------------------------------------------------------------------------------*/
447+
/**
448+
* @brief Converts an abstract ID into a number suitable for use as an array index.
449+
*
450+
* This will return a unique zero-based integer number in the range of [0,MAX) for
451+
* any valid object ID. This may be used by application code as an array index
452+
* for indexing into local tables.
453+
*
454+
* This routine operates on a specific object type, and returns a value based on the
455+
* maximum number of objects for that type.
456+
*
457+
* If the idtype is passed as #OS_OBJECT_TYPE_UNDEFINED, then object type verification
458+
* is skipped and any object ID will be accepted and converted to an index. In this
459+
* mode, the range of the output depends on the actual passed-in object type.
460+
*
461+
* If the idtype is passed as any other value, the passed-in ID value is first
462+
* confirmed to be the correct type. This check will guarantee that the output
463+
* is within an expected range; for instance, if the type is passed as
464+
* #OS_OBJECT_TYPE_OS_TASK, then the output index is guaranteed to be between 0 and
465+
* #OS_MAX_TASKS-1 after successful conversion.
466+
*
467+
* @param[in] idtype The object type to convert
468+
* @param[in] object_id The object ID to operate on
469+
* @param[out] *ArrayIndex The Index to return
470+
*
471+
* @return Execution status, see @ref OSReturnCodes
472+
* @retval #OS_SUCCESS @copybrief OS_SUCCESS
473+
* @retval #OS_ERR_INCORRECT_OBJ_TYPE @copybrief OS_ERR_INCORRECT_OBJ_TYPE
474+
* */
475+
int32 OS_ObjectIdToArrayIndex(uint32 idtype, osal_id_t id, uint32 *ArrayIndex);
476+
477+
478+
440479
/*-------------------------------------------------------------------------------------*/
441480
/**
442481
* @brief call the supplied callback function for all valid object IDs

src/os/shared/inc/os-shared-idmap.h

-9
Original file line numberDiff line numberDiff line change
@@ -216,15 +216,6 @@ uint32 OS_GetBaseForObjectType(uint32 idtype);
216216
------------------------------------------------------------------*/
217217
int32 OS_ObjectIdFindByName (uint32 idtype, const char *name, osal_id_t *object_id);
218218

219-
/*----------------------------------------------------------------
220-
Function: OS_ObjectIdToArrayIndex
221-
222-
Purpose: Convert a 32-bit OSAL ID into a zero-based array index
223-
224-
Returns: OS_SUCCESS on success, or relevant error code
225-
------------------------------------------------------------------*/
226-
int32 OS_ObjectIdToArrayIndex(uint32 idtype, osal_id_t id, uint32 *ArrayIndex);
227-
228219
/*----------------------------------------------------------------
229220
Function: OS_ObjectIdGetBySearch
230221

src/os/shared/src/osapi-idmap.c

+51-52
Original file line numberDiff line numberDiff line change
@@ -663,58 +663,6 @@ void OS_Unlock_Global(uint32 idtype)
663663
}
664664
}
665665

666-
667-
/*----------------------------------------------------------------
668-
*
669-
* Function: OS_ObjectIdToArrayIndex
670-
*
671-
* Purpose: Local helper routine, not part of OSAL API.
672-
* Convert an object ID (which must be of the given type) to a number suitable
673-
* for use as an array index. The array index will be in the range of:
674-
* 0 <= ArrayIndex < OS_MAX_<OBJTYPE>
675-
*
676-
* If the passed-in ID type is OS_OBJECT_TYPE_UNDEFINED, then any type
677-
* is allowed.
678-
*
679-
* returns: If the passed-in ID is not of the proper type, OS_ERROR is returned
680-
* Otherwise OS_SUCCESS is returned.
681-
*
682-
*-----------------------------------------------------------------*/
683-
int32 OS_ObjectIdToArrayIndex(uint32 idtype, osal_id_t id, uint32 *ArrayIndex)
684-
{
685-
uint32 max_id;
686-
uint32 obj_index;
687-
uint32 actual_type;
688-
int32 return_code;
689-
690-
obj_index = OS_ObjectIdToSerialNumber_Impl(id);
691-
actual_type = OS_ObjectIdToType_Impl(id);
692-
693-
/*
694-
* If requested by the caller, enforce that the ID is of the correct type.
695-
* If the caller passed OS_OBJECT_TYPE_UNDEFINED, then anything is allowed.
696-
*/
697-
if (idtype != OS_OBJECT_TYPE_UNDEFINED && actual_type != idtype)
698-
{
699-
return_code = OS_ERR_INVALID_ID;
700-
}
701-
else
702-
{
703-
max_id = OS_GetMaxForObjectType(actual_type);
704-
if (max_id == 0)
705-
{
706-
return_code = OS_ERR_INVALID_ID;
707-
}
708-
else
709-
{
710-
return_code = OS_SUCCESS;
711-
*ArrayIndex = obj_index % max_id;
712-
}
713-
}
714-
715-
return return_code;
716-
} /* end OS_ObjectIdToArrayIndex */
717-
718666
/*----------------------------------------------------------------
719667
*
720668
* Function: OS_ObjectIdFinalizeNew
@@ -1249,3 +1197,54 @@ int32 OS_GetResourceName(osal_id_t object_id, char *buffer, uint32 buffer_size)
12491197
} /* end OS_GetResourceName */
12501198

12511199

1200+
/*----------------------------------------------------------------
1201+
*
1202+
* Function: OS_ObjectIdToArrayIndex
1203+
*
1204+
* Purpose: Convert an object ID (which must be of the given type) to a number suitable
1205+
* for use as an array index. The array index will be in the range of:
1206+
* 0 <= ArrayIndex < OS_MAX_<OBJTYPE>
1207+
*
1208+
* If the passed-in ID type is OS_OBJECT_TYPE_UNDEFINED, then any type
1209+
* is allowed.
1210+
*
1211+
* returns: If the passed-in ID is not of the proper type, OS_ERROR is returned
1212+
* Otherwise OS_SUCCESS is returned.
1213+
*
1214+
*-----------------------------------------------------------------*/
1215+
int32 OS_ObjectIdToArrayIndex(uint32 idtype, osal_id_t id, uint32 *ArrayIndex)
1216+
{
1217+
uint32 max_id;
1218+
uint32 obj_index;
1219+
uint32 actual_type;
1220+
int32 return_code;
1221+
1222+
obj_index = OS_ObjectIdToSerialNumber_Impl(id);
1223+
actual_type = OS_ObjectIdToType_Impl(id);
1224+
1225+
/*
1226+
* If requested by the caller, enforce that the ID is of the correct type.
1227+
* If the caller passed OS_OBJECT_TYPE_UNDEFINED, then anything is allowed.
1228+
*/
1229+
if (idtype != OS_OBJECT_TYPE_UNDEFINED && actual_type != idtype)
1230+
{
1231+
return_code = OS_ERR_INVALID_ID;
1232+
}
1233+
else
1234+
{
1235+
max_id = OS_GetMaxForObjectType(actual_type);
1236+
if (max_id == 0)
1237+
{
1238+
return_code = OS_ERR_INVALID_ID;
1239+
}
1240+
else
1241+
{
1242+
return_code = OS_SUCCESS;
1243+
*ArrayIndex = obj_index % max_id;
1244+
}
1245+
}
1246+
1247+
return return_code;
1248+
} /* end OS_ObjectIdToArrayIndex */
1249+
1250+

0 commit comments

Comments
 (0)