summaryrefslogtreecommitdiffstats
path: root/cpukit
diff options
context:
space:
mode:
authorSebastian Huber <sebastian.huber@embedded-brains.de>2016-03-18 07:25:23 +0100
committerSebastian Huber <sebastian.huber@embedded-brains.de>2016-03-18 15:36:58 +0100
commitc904df573396d95957dc79b242b3a76911063089 (patch)
treebe6406676689018e8af8a929b6a4ef5284f94c70 /cpukit
parentsptests/sptls02: Use GNU++11 (diff)
downloadrtems-c904df573396d95957dc79b242b3a76911063089.tar.bz2
score: Add _Objects_Get_by_name()
Replace _Objects_Name_to_id_string() with _Objects_Get_by_name() since all users of this function are interested in the object itself and not the identifier. Use the object allocator lock to protect the search. Update #2555.
Diffstat (limited to 'cpukit')
-rw-r--r--cpukit/posix/include/rtems/posix/mqueueimpl.h21
-rw-r--r--cpukit/posix/include/rtems/posix/posixapi.h31
-rw-r--r--cpukit/posix/include/rtems/posix/semaphoreimpl.h20
-rw-r--r--cpukit/posix/src/mqueueopen.c18
-rw-r--r--cpukit/posix/src/mqueueunlink.c35
-rw-r--r--cpukit/posix/src/psxnametoid.c44
-rw-r--r--cpukit/posix/src/semopen.c18
-rw-r--r--cpukit/posix/src/semunlink.c28
-rw-r--r--cpukit/score/include/rtems/score/objectimpl.h39
-rw-r--r--cpukit/score/src/objectnametoidstring.c59
10 files changed, 130 insertions, 183 deletions
diff --git a/cpukit/posix/include/rtems/posix/mqueueimpl.h b/cpukit/posix/include/rtems/posix/mqueueimpl.h
index 6646fa2c65..cf0f76af32 100644
--- a/cpukit/posix/include/rtems/posix/mqueueimpl.h
+++ b/cpukit/posix/include/rtems/posix/mqueueimpl.h
@@ -246,17 +246,20 @@ _POSIX_Message_queue_Get_fd_interrupt_disable(
lock_context
);
}
-
-/**
- * @see _POSIX_Name_to_id().
- */
-RTEMS_INLINE_ROUTINE int _POSIX_Message_queue_Name_to_id(
- const char *name,
- Objects_Id *id,
- size_t *len
+
+RTEMS_INLINE_ROUTINE POSIX_Message_queue_Control *
+_POSIX_Message_queue_Get_by_name(
+ const char *name,
+ size_t *name_length_p,
+ Objects_Get_by_name_error *error
)
{
- return _POSIX_Name_to_id( &_POSIX_Message_queue_Information, name, id, len );
+ return (POSIX_Message_queue_Control *) _Objects_Get_by_name(
+ &_POSIX_Message_queue_Information,
+ name,
+ name_length_p,
+ error
+ );
}
#ifdef __cplusplus
diff --git a/cpukit/posix/include/rtems/posix/posixapi.h b/cpukit/posix/include/rtems/posix/posixapi.h
index 8fee7a4f32..0348e28e16 100644
--- a/cpukit/posix/include/rtems/posix/posixapi.h
+++ b/cpukit/posix/include/rtems/posix/posixapi.h
@@ -20,6 +20,7 @@
#define _RTEMS_POSIX_POSIXAPI_H
#include <rtems/config.h>
+#include <rtems/score/assert.h>
#include <rtems/score/objectimpl.h>
/**
@@ -48,27 +49,15 @@ typedef enum {
*/
void _POSIX_Fatal_error( POSIX_Fatal_domain domain, int eno );
-/**
- * @brief Queries the object identifier @a id for a @a name.
- *
- * @param[in] information Object information.
- * @param[in] name Zero terminated name string to look up.
- * @param[out] id Pointer for identifier. The pointer must be valid.
- * @param[out] len Pointer for string length. The pointer must be valid.
- *
- * @retval 0 Successful operation.
- * @retval EINVAL The @a name pointer is @c NULL or the @a name string has
- * zero length.
- * @retval ENAMETOOLONG The @a name string length is greater than or equal to
- * @c NAME_MAX.
- * @retval ENOENT Found no corresponding identifier.
- */
-int _POSIX_Name_to_id(
- Objects_Information *information,
- const char *name,
- Objects_Id *id,
- size_t *len
-);
+extern const int _POSIX_Get_by_name_error_table[ 3 ];
+
+RTEMS_INLINE_ROUTINE int _POSIX_Get_by_name_error(
+ Objects_Get_by_name_error error
+)
+{
+ _Assert( (size_t) error < RTEMS_ARRAY_SIZE( _POSIX_Get_by_name_error_table ) );
+ return _POSIX_Get_by_name_error_table[ error ];
+}
/** @} */
diff --git a/cpukit/posix/include/rtems/posix/semaphoreimpl.h b/cpukit/posix/include/rtems/posix/semaphoreimpl.h
index fe5ee44034..8608e6f33d 100644
--- a/cpukit/posix/include/rtems/posix/semaphoreimpl.h
+++ b/cpukit/posix/include/rtems/posix/semaphoreimpl.h
@@ -163,17 +163,19 @@ RTEMS_INLINE_ROUTINE void _POSIX_Semaphore_Namespace_remove (
_Objects_Namespace_remove(
&_POSIX_Semaphore_Information, &the_semaphore->Object );
}
-
-/**
- * @see _POSIX_Name_to_id().
- */
-RTEMS_INLINE_ROUTINE int _POSIX_Semaphore_Name_to_id(
- const char *name,
- Objects_Id *id,
- size_t *len
+
+RTEMS_INLINE_ROUTINE POSIX_Semaphore_Control *_POSIX_Semaphore_Get_by_name(
+ const char *name,
+ size_t *name_length_p,
+ Objects_Get_by_name_error *error
)
{
- return _POSIX_Name_to_id( &_POSIX_Semaphore_Information, name, id, len );
+ return (POSIX_Semaphore_Control *) _Objects_Get_by_name(
+ &_POSIX_Semaphore_Information,
+ name,
+ name_length_p,
+ error
+ );
}
#ifdef __cplusplus
diff --git a/cpukit/posix/src/mqueueopen.c b/cpukit/posix/src/mqueueopen.c
index 005de74158..d327e13fac 100644
--- a/cpukit/posix/src/mqueueopen.c
+++ b/cpukit/posix/src/mqueueopen.c
@@ -67,11 +67,10 @@ mqd_t mq_open(
va_list arg;
struct mq_attr *attr = NULL;
int status;
- Objects_Id the_mq_id;
POSIX_Message_queue_Control *the_mq;
POSIX_Message_queue_Control_fd *the_mq_fd;
- Objects_Locations location;
size_t name_len;
+ Objects_Get_by_name_error error;
if ( oflag & O_CREAT ) {
va_start(arg, oflag);
@@ -87,7 +86,7 @@ mqd_t mq_open(
}
the_mq_fd->oflag = oflag;
- status = _POSIX_Message_queue_Name_to_id( name, &the_mq_id, &name_len );
+ the_mq = _POSIX_Message_queue_Get_by_name( name, &name_len, &error );
/*
* If the name to id translation worked, then the message queue exists
@@ -95,15 +94,18 @@ mqd_t mq_open(
* need to check to see if this is a "message queue does not exist"
* or some other miscellaneous error on the name.
*/
- if ( status ) {
+ if ( the_mq == NULL ) {
/*
* Unless provided a valid name that did not already exist
* and we are willing to create then it is an error.
*/
- if ( !( status == ENOENT && (oflag & O_CREAT) ) ) {
+ if ( !( error == OBJECTS_GET_BY_NAME_NO_OBJECT && (oflag & O_CREAT) ) ) {
_POSIX_Message_queue_Free_fd( the_mq_fd );
_Objects_Allocator_unlock();
- rtems_set_errno_and_return_value( status, MQ_OPEN_FAILED );
+ rtems_set_errno_and_return_value(
+ _POSIX_Get_by_name_error( error ),
+ MQ_OPEN_FAILED
+ );
}
} else { /* name -> ID translation succeeded */
@@ -120,7 +122,6 @@ mqd_t mq_open(
* In this case we need to do an ID->pointer conversion to
* check the mode.
*/
- the_mq = _POSIX_Message_queue_Get( the_mq_id, &location );
the_mq->open_count += 1;
the_mq_fd->Queue = the_mq;
_Objects_Open_string(
@@ -128,7 +129,6 @@ mqd_t mq_open(
&the_mq_fd->Object,
NULL
);
- _Thread_Enable_dispatch();
_Objects_Allocator_unlock();
return the_mq_fd->Object.id;
@@ -149,7 +149,7 @@ mqd_t mq_open(
/*
* errno was set by Create_support, so don't set it again.
*/
- if ( status == -1 ) {
+ if ( status != 0 ) {
_POSIX_Message_queue_Free_fd( the_mq_fd );
_Objects_Allocator_unlock();
return MQ_OPEN_FAILED;
diff --git a/cpukit/posix/src/mqueueunlink.c b/cpukit/posix/src/mqueueunlink.c
index 2be096a421..d5182d21fc 100644
--- a/cpukit/posix/src/mqueueunlink.c
+++ b/cpukit/posix/src/mqueueunlink.c
@@ -18,19 +18,10 @@
#include "config.h"
#endif
-#include <stdarg.h>
-
-#include <pthread.h>
-#include <limits.h>
-#include <errno.h>
-#include <fcntl.h>
#include <mqueue.h>
-#include <rtems/system.h>
-#include <rtems/score/watchdog.h>
-#include <rtems/score/wkspace.h>
-#include <rtems/seterr.h>
#include <rtems/posix/mqueueimpl.h>
+#include <rtems/seterr.h>
/*
* 15.2.2 Remove a Message Queue, P1003.1b-1993, p. 276
@@ -40,31 +31,21 @@ int mq_unlink(
const char *name
)
{
- int status;
- POSIX_Message_queue_Control *the_mq;
- Objects_Id the_mq_id;
- size_t name_len;
+ POSIX_Message_queue_Control *the_mq;
+ Objects_Get_by_name_error error;
_Objects_Allocator_lock();
- _Thread_Disable_dispatch();
- status = _POSIX_Message_queue_Name_to_id( name, &the_mq_id, &name_len );
- if ( status != 0 ) {
- _Thread_Enable_dispatch();
+ the_mq = _POSIX_Message_queue_Get_by_name( name, NULL, &error );
+ if ( the_mq == NULL ) {
_Objects_Allocator_unlock();
- rtems_set_errno_and_return_minus_one( status );
- }
-
- the_mq = (POSIX_Message_queue_Control *) _Objects_Get_local_object(
- &_POSIX_Message_queue_Information,
- _Objects_Get_index( the_mq_id )
- );
+ rtems_set_errno_and_return_minus_one( _POSIX_Get_by_name_error( error ) );
+ }
the_mq->linked = false;
_POSIX_Message_queue_Namespace_remove( the_mq );
_POSIX_Message_queue_Delete( the_mq );
- _Thread_Enable_dispatch();
- _Objects_Allocator_unlock();
+ _Objects_Allocator_unlock();
return 0;
}
diff --git a/cpukit/posix/src/psxnametoid.c b/cpukit/posix/src/psxnametoid.c
index 637de91212..eac8e84611 100644
--- a/cpukit/posix/src/psxnametoid.c
+++ b/cpukit/posix/src/psxnametoid.c
@@ -20,44 +20,10 @@
#include <rtems/posix/posixapi.h>
-#include <string.h>
-#include <limits.h>
#include <errno.h>
-/* pure ANSI mode does not have this prototype */
-size_t strnlen(const char *, size_t);
-
-int _POSIX_Name_to_id(
- Objects_Information *information,
- const char *name,
- Objects_Id *id,
- size_t *len
-)
-{
- int eno = EINVAL;
- size_t n = 0;
-
- if ( name != NULL && name [0] != '\0' ) {
- n = strnlen( name, NAME_MAX );
-
- if ( n < NAME_MAX ) {
- Objects_Name_or_id_lookup_errors status = _Objects_Name_to_id_string(
- information,
- name,
- id
- );
-
- if ( status == OBJECTS_NAME_OR_ID_LOOKUP_SUCCESSFUL ) {
- eno = 0;
- } else {
- eno = ENOENT;
- }
- } else {
- eno = ENAMETOOLONG;
- }
- }
-
- *len = n;
-
- return eno;
-}
+const int _POSIX_Get_by_name_error_table[ 3 ] = {
+ EINVAL,
+ ENAMETOOLONG,
+ ENOENT
+};
diff --git a/cpukit/posix/src/semopen.c b/cpukit/posix/src/semopen.c
index fe0dbebe7f..b844b08bb4 100644
--- a/cpukit/posix/src/semopen.c
+++ b/cpukit/posix/src/semopen.c
@@ -60,10 +60,9 @@ sem_t *sem_open(
va_list arg;
unsigned int value = 0;
int status;
- Objects_Id the_semaphore_id;
POSIX_Semaphore_Control *the_semaphore;
- Objects_Locations location;
size_t name_len;
+ Objects_Get_by_name_error error;
if ( oflag & O_CREAT ) {
va_start(arg, oflag);
@@ -73,7 +72,7 @@ sem_t *sem_open(
}
_Objects_Allocator_lock();
- status = _POSIX_Semaphore_Name_to_id( name, &the_semaphore_id, &name_len );
+ the_semaphore = _POSIX_Semaphore_Get_by_name( name, &name_len, &error );
/*
* If the name to id translation worked, then the semaphore exists
@@ -82,16 +81,19 @@ sem_t *sem_open(
* or some other miscellaneous error on the name.
*/
- if ( status ) {
+ if ( the_semaphore == NULL ) {
/*
* Unless provided a valid name that did not already exist
* and we are willing to create then it is an error.
*/
- if ( !( status == ENOENT && (oflag & O_CREAT) ) ) {
+ if ( !( error == OBJECTS_GET_BY_NAME_NO_OBJECT && (oflag & O_CREAT) ) ) {
_Objects_Allocator_unlock();
- rtems_set_errno_and_return_value( status, SEM_FAILED );
+ rtems_set_errno_and_return_value(
+ _POSIX_Get_by_name_error( error ),
+ SEM_FAILED
+ );
}
} else {
@@ -104,9 +106,7 @@ sem_t *sem_open(
rtems_set_errno_and_return_value( EEXIST, SEM_FAILED );
}
- the_semaphore = _POSIX_Semaphore_Get( (sem_t *) &the_semaphore_id, &location );
the_semaphore->open_count += 1;
- _Thread_Enable_dispatch();
_Objects_Allocator_unlock();
goto return_id;
}
@@ -130,7 +130,7 @@ sem_t *sem_open(
_Objects_Allocator_unlock();
- if ( status == -1 )
+ if ( status != 0 )
return SEM_FAILED;
return_id:
diff --git a/cpukit/posix/src/semunlink.c b/cpukit/posix/src/semunlink.c
index 863d25801d..b8406185d4 100644
--- a/cpukit/posix/src/semunlink.c
+++ b/cpukit/posix/src/semunlink.c
@@ -18,15 +18,8 @@
#include "config.h"
#endif
-#include <stdarg.h>
-
-#include <errno.h>
-#include <fcntl.h>
-#include <pthread.h>
#include <semaphore.h>
-#include <limits.h>
-#include <rtems/system.h>
#include <rtems/posix/semaphoreimpl.h>
#include <rtems/seterr.h>
@@ -34,32 +27,21 @@ int sem_unlink(
const char *name
)
{
- int status;
- POSIX_Semaphore_Control *the_semaphore;
- Objects_Id the_semaphore_id;
- size_t name_len;
+ POSIX_Semaphore_Control *the_semaphore;
+ Objects_Get_by_name_error error;
_Objects_Allocator_lock();
- _Thread_Disable_dispatch();
- status = _POSIX_Semaphore_Name_to_id( name, &the_semaphore_id, &name_len );
- if ( status != 0 ) {
- _Thread_Enable_dispatch();
+ the_semaphore = _POSIX_Semaphore_Get_by_name( name, NULL, &error );
+ if ( the_semaphore == NULL ) {
_Objects_Allocator_unlock();
- rtems_set_errno_and_return_minus_one( status );
+ rtems_set_errno_and_return_minus_one( _POSIX_Get_by_name_error( error ) );
}
- the_semaphore = (POSIX_Semaphore_Control *) _Objects_Get_local_object(
- &_POSIX_Semaphore_Information,
- _Objects_Get_index( the_semaphore_id )
- );
-
the_semaphore->linked = false;
_POSIX_Semaphore_Namespace_remove( the_semaphore );
_POSIX_Semaphore_Delete( the_semaphore );
- _Thread_Enable_dispatch();
_Objects_Allocator_unlock();
-
return 0;
}
diff --git a/cpukit/score/include/rtems/score/objectimpl.h b/cpukit/score/include/rtems/score/objectimpl.h
index fb2aca8408..f75ae59c0f 100644
--- a/cpukit/score/include/rtems/score/objectimpl.h
+++ b/cpukit/score/include/rtems/score/objectimpl.h
@@ -448,25 +448,31 @@ Objects_Name_or_id_lookup_errors _Objects_Name_to_id_u32(
);
#if defined(RTEMS_SCORE_OBJECT_ENABLE_STRING_NAMES)
+typedef enum {
+ OBJECTS_GET_BY_NAME_INVALID_NAME,
+ OBJECTS_GET_BY_NAME_NAME_TOO_LONG,
+ OBJECTS_GET_BY_NAME_NO_OBJECT
+} Objects_Get_by_name_error;
+
/**
- * @brief Converts an object name to an Id.
+ * @brief Gets an object control block identified by its name.
*
- * This method converts an object name to an Id. It performs a look up
- * using the object information block for this object class.
+ * The object information must use string names.
*
- * @param[in] information points to an object class information block.
- * @param[in] name is the name of the object to find.
- * @param[in] id will contain the Id if the search is successful.
+ * @param information The object information. Must not be NULL.
+ * @param name The object name.
+ * @param name_length_p Optional parameter to return the name length.
+ * @param error The error indication in case of failure. Must not be NULL.
*
- * @retval This method returns one of the values from the
- * @ref Objects_Name_or_id_lookup_errors enumeration to indicate
- * successful or failure. On success @a id will contain the Id of
- * the requested object.
+ * @retval NULL No object exists for this name or invalid parameters.
+ * @retval other The first object according to object index associated with
+ * this name.
*/
-Objects_Name_or_id_lookup_errors _Objects_Name_to_id_string(
- Objects_Information *information,
- const char *name,
- Objects_Id *id
+Objects_Control *_Objects_Get_by_name(
+ const Objects_Information *information,
+ const char *name,
+ size_t *name_length_p,
+ Objects_Get_by_name_error *error
);
#endif
@@ -1054,6 +1060,11 @@ RTEMS_INLINE_ROUTINE void _Objects_Allocator_unlock( void )
_RTEMS_Unlock_allocator();
}
+RTEMS_INLINE_ROUTINE bool _Objects_Allocator_is_owner( void )
+{
+ return _RTEMS_Allocator_is_owner();
+}
+
/** @} */
#ifdef __cplusplus
diff --git a/cpukit/score/src/objectnametoidstring.c b/cpukit/score/src/objectnametoidstring.c
index 00c84f04d3..bd4b4a9ead 100644
--- a/cpukit/score/src/objectnametoidstring.c
+++ b/cpukit/score/src/objectnametoidstring.c
@@ -23,40 +23,53 @@
#include <string.h>
#if defined(RTEMS_SCORE_OBJECT_ENABLE_STRING_NAMES)
-Objects_Name_or_id_lookup_errors _Objects_Name_to_id_string(
- Objects_Information *information,
- const char *name,
- Objects_Id *id
+Objects_Control *_Objects_Get_by_name(
+ const Objects_Information *information,
+ const char *name,
+ size_t *name_length_p,
+ Objects_Get_by_name_error *error
)
{
- Objects_Control *the_object;
- uint32_t index;
+ size_t name_length;
+ size_t max_name_length;
+ uint32_t index;
- /* ASSERT: information->is_string == true */
+ _Assert( information->is_string );
+ _Assert( _Objects_Allocator_is_owner() );
- if ( !id )
- return OBJECTS_INVALID_ADDRESS;
+ if ( name == NULL ) {
+ *error = OBJECTS_GET_BY_NAME_INVALID_NAME;
+ return NULL;
+ }
+
+ name_length = strnlen( name, information->name_length + 1 );
+ max_name_length = information->name_length;
+ if ( name_length > max_name_length ) {
+ *error = OBJECTS_GET_BY_NAME_NAME_TOO_LONG;
+ return NULL;
+ }
+
+ if ( name_length_p != NULL ) {
+ *name_length_p = name_length;
+ }
- if ( !name )
- return OBJECTS_INVALID_NAME;
+ for ( index = 1; index <= information->maximum; index++ ) {
+ Objects_Control *the_object;
- if ( information->maximum != 0 ) {
+ the_object = information->local_table[ index ];
- for ( index = 1; index <= information->maximum; index++ ) {
- the_object = information->local_table[ index ];
- if ( !the_object )
- continue;
+ if ( the_object == NULL )
+ continue;
- if ( !the_object->name.name_p )
- continue;
+ if ( the_object->name.name_p == NULL )
+ continue;
- if (!strncmp( name, the_object->name.name_p, information->name_length)) {
- *id = the_object->id;
- return OBJECTS_NAME_OR_ID_LOOKUP_SUCCESSFUL;
- }
+ if ( strncmp( name, the_object->name.name_p, max_name_length ) == 0 ) {
+ return the_object;
}
}
- return OBJECTS_INVALID_NAME;
+ *error = OBJECTS_GET_BY_NAME_NO_OBJECT;
+ return NULL;
}
#endif