[PATCH 3/3] posix: Simplify key implementation

2014-08-03 Thread Sebastian Huber
---
 cpukit/posix/include/rtems/posix/key.h | 33 ++
 cpukit/posix/include/rtems/posix/keyimpl.h |  4 ++--
 cpukit/posix/src/key.c | 20 ++
 cpukit/posix/src/keygetspecific.c  |  2 +-
 cpukit/posix/src/keysetspecific.c  |  6 --
 5 files changed, 44 insertions(+), 21 deletions(-)

diff --git a/cpukit/posix/include/rtems/posix/key.h 
b/cpukit/posix/include/rtems/posix/key.h
index bfa05b1..7cc179c 100644
--- a/cpukit/posix/include/rtems/posix/key.h
+++ b/cpukit/posix/include/rtems/posix/key.h
@@ -25,6 +25,7 @@
 #include rtems/score/chain.h
 #include rtems/score/object.h
 #include rtems/score/rbtree.h
+#include rtems/score/thread.h
 
 #ifdef __cplusplus
 extern C {
@@ -39,20 +40,36 @@ extern C {
 /**@{**/
 
 /**
- * @brief The rbtree node used to manage a POSIX key and value.
+ * @brief Represents POSIX key and value pair.
  */
 typedef struct {
-  /** This field is the chain node structure. */
+  /**
+   * @brief The chain node for the per-thread value chain.
+   */
   Chain_Node Key_values_per_thread_node;
-  /** This field is the rbtree node structure. */
+
+  /**
+   * @brief The tree node for the lookup tree.
+   */
   RBTree_Node Key_value_lookup_node;
-  /** This field is the POSIX key used as an rbtree key */
+
+  /**
+   * @brief The POSIX key identifier used in combination with the thread
+   * pointer as the tree key.
+   */
   pthread_key_t key;
-  /** This field is the Thread id also used as an rbtree key */
-  Objects_Id thread_id;
-  /** This field points to the POSIX key value of specific thread */
+
+  /**
+   * @brief The thread pointer used in combination with the POSIX key
+   * identifier as the tree key.
+   */
+  Thread_Control *thread;
+
+  /**
+   * @brief The thread specific POSIX key value.
+   */
   const void *value;
-}  POSIX_Keys_Key_value_pair;
+} POSIX_Keys_Key_value_pair;
 
 /**
  * @brief The data structure used to manage a POSIX key.
diff --git a/cpukit/posix/include/rtems/posix/keyimpl.h 
b/cpukit/posix/include/rtems/posix/keyimpl.h
index ded030d..42989b0 100644
--- a/cpukit/posix/include/rtems/posix/keyimpl.h
+++ b/cpukit/posix/include/rtems/posix/keyimpl.h
@@ -170,12 +170,12 @@ RTEMS_INLINE_ROUTINE void _POSIX_Keys_Key_value_pair_free(
 
 RTEMS_INLINE_ROUTINE RBTree_Node *_POSIX_Keys_Find(
   pthread_key_t  key,
-  Objects_Id thread_id,
+  Thread_Control*thread,
   POSIX_Keys_Key_value_pair *search_node
 )
 {
   search_node-key = key;
-  search_node-thread_id = thread_id;
+  search_node-thread = thread;
 
   return _RBTree_Find(
 _POSIX_Keys_Key_value_lookup_tree,
diff --git a/cpukit/posix/src/key.c b/cpukit/posix/src/key.c
index 67c6e27..6753d57 100644
--- a/cpukit/posix/src/key.c
+++ b/cpukit/posix/src/key.c
@@ -51,7 +51,8 @@ RBTree_Compare_result _POSIX_Keys_Key_value_compare(
 {
   POSIX_Keys_Key_value_pair *n1;
   POSIX_Keys_Key_value_pair *n2;
-  Objects_Id thread_id1, thread_id2;
+  Thread_Control *thread1;
+  Thread_Control *thread2;
   RBTree_Compare_result diff;
 
   n1 = POSIX_KEYS_RBTREE_NODE_TO_KEY_VALUE_PAIR( node1 );
@@ -61,15 +62,18 @@ RBTree_Compare_result _POSIX_Keys_Key_value_compare(
   if ( diff )
 return diff;
 
-  thread_id1 = n1-thread_id;
-  thread_id2 = n2-thread_id;
+  thread1 = n1-thread;
+  thread2 = n2-thread;
 
-  /**
-   * if thread_id1 or thread_id2 equals to 0, only key1 and key2 is valued.
-   * it enables us search node only by pthread_key_t type key.
+  /*
+   * If thread1 or thread2 equals to NULL, only key1 and key2 is valued.  It
+   * enables us search node only by pthread_key_t type key.  Exploit that the
+   * thread control alignment is at least two to avoid integer overflows.
*/
-  if ( thread_id1  thread_id2 )
-return thread_id1 - thread_id2;
+  if ( thread1 != NULL  thread2 != NULL )
+return (RBTree_Compare_result) ( (uintptr_t) thread1  1 )
+  - (RBTree_Compare_result) ( (uintptr_t) thread2  1 );
+
   return 0;
 }
 
diff --git a/cpukit/posix/src/keygetspecific.c 
b/cpukit/posix/src/keygetspecific.c
index f7e7b71..5ab37a7 100644
--- a/cpukit/posix/src/keygetspecific.c
+++ b/cpukit/posix/src/keygetspecific.c
@@ -49,7 +49,7 @@ void *pthread_getspecific(
   switch ( location ) {
 
 case OBJECTS_LOCAL:
-  p = _POSIX_Keys_Find( key, _Thread_Executing-Object.id, search_node );
+  p = _POSIX_Keys_Find( key, _Thread_Executing, search_node );
   if ( p != NULL ) {
 value_pair_p = POSIX_KEYS_RBTREE_NODE_TO_KEY_VALUE_PAIR( p );
 key_data = value_pair_p-value;
diff --git a/cpukit/posix/src/keysetspecific.c 
b/cpukit/posix/src/keysetspecific.c
index ec17d47..ee85ac2 100644
--- a/cpukit/posix/src/keysetspecific.c
+++ b/cpukit/posix/src/keysetspecific.c
@@ -39,12 +39,14 @@ int pthread_setspecific(
   POSIX_Keys_Key_value_pair   *value_pair_ptr;
   RBTree_Node *p;
   POSIX_Keys_Key_value_pairsearch_node;
+  Thread_Control  

[PATCH 2/3] rbtree: Add and use RBTree_Compare_result

2014-08-03 Thread Sebastian Huber
---
 cpukit/posix/include/rtems/posix/keyimpl.h |  2 +-
 cpukit/posix/src/key.c |  4 +-
 cpukit/sapi/include/rtems/rbheap.h |  1 -
 cpukit/sapi/include/rtems/rbtree.h |  9 ++-
 cpukit/sapi/src/rbheap.c   | 70 --
 cpukit/score/include/rtems/score/rbtree.h  | 11 +++-
 cpukit/score/include/rtems/score/rbtreeimpl.h  |  8 ++-
 .../score/include/rtems/score/scheduleredfimpl.h   |  2 +-
 cpukit/score/include/rtems/score/threadqimpl.h |  2 +-
 cpukit/score/src/rbtreefind.c  |  4 +-
 cpukit/score/src/rbtreeinsert.c| 13 +++-
 cpukit/score/src/scheduleredf.c|  2 +-
 cpukit/score/src/threadq.c |  2 +-
 testsuites/libtests/rbheap01/init.c| 17 --
 testsuites/sptests/sprbtree01/init.c   |  2 +-
 15 files changed, 82 insertions(+), 67 deletions(-)

diff --git a/cpukit/posix/include/rtems/posix/keyimpl.h 
b/cpukit/posix/include/rtems/posix/keyimpl.h
index aff9749..ded030d 100644
--- a/cpukit/posix/include/rtems/posix/keyimpl.h
+++ b/cpukit/posix/include/rtems/posix/keyimpl.h
@@ -64,7 +64,7 @@ void _POSIX_Key_Manager_initialization(void);
  *
  * This routine compares the rbtree node
  */
-int _POSIX_Keys_Key_value_compare(
+RBTree_Compare_result _POSIX_Keys_Key_value_compare(
   const RBTree_Node *node1,
   const RBTree_Node *node2
 );
diff --git a/cpukit/posix/src/key.c b/cpukit/posix/src/key.c
index e231299..67c6e27 100644
--- a/cpukit/posix/src/key.c
+++ b/cpukit/posix/src/key.c
@@ -44,7 +44,7 @@ RBTREE_DEFINE_EMPTY( _POSIX_Keys_Key_value_lookup_tree );
  * impossible
  */
 
-int _POSIX_Keys_Key_value_compare(
+RBTree_Compare_result _POSIX_Keys_Key_value_compare(
   const RBTree_Node *node1,
   const RBTree_Node *node2
 )
@@ -52,7 +52,7 @@ int _POSIX_Keys_Key_value_compare(
   POSIX_Keys_Key_value_pair *n1;
   POSIX_Keys_Key_value_pair *n2;
   Objects_Id thread_id1, thread_id2;
-  int diff;
+  RBTree_Compare_result diff;
 
   n1 = POSIX_KEYS_RBTREE_NODE_TO_KEY_VALUE_PAIR( node1 );
   n2 = POSIX_KEYS_RBTREE_NODE_TO_KEY_VALUE_PAIR( node2 );
diff --git a/cpukit/sapi/include/rtems/rbheap.h 
b/cpukit/sapi/include/rtems/rbheap.h
index 0848b69..c008721 100644
--- a/cpukit/sapi/include/rtems/rbheap.h
+++ b/cpukit/sapi/include/rtems/rbheap.h
@@ -154,7 +154,6 @@ struct rtems_rbheap_control {
  * @param[in] handler_arg The handler argument.
  *
  * @retval RTEMS_SUCCESSFUL Successful operation.
- * @retval RTEMS_INVALID_NUMBER The alignment is not positive.
  * @retval RTEMS_INVALID_ADDRESS The memory area is invalid.
  * @retval RTEMS_NO_MEMORY Not enough chunk descriptors.
  */
diff --git a/cpukit/sapi/include/rtems/rbtree.h 
b/cpukit/sapi/include/rtems/rbtree.h
index eaf2b6e..0e2ea2c 100644
--- a/cpukit/sapi/include/rtems/rbtree.h
+++ b/cpukit/sapi/include/rtems/rbtree.h
@@ -55,9 +55,12 @@ typedef RBTree_Node rtems_rbtree_node;
 typedef RBTree_Control rtems_rbtree_control;
 
 /**
- * This type defines function pointers for user-provided comparison
- * function. The function compares two nodes in order to determine
- * the order in a red-black tree.
+ * @copydoc RBTree_Compare_result
+ */
+typedef RBTree_Compare_result rtems_rbtree_compare_result;
+
+/**
+ * @copydoc RBTree_Compare
  */
 typedef RBTree_Compare rtems_rbtree_compare;
 
diff --git a/cpukit/sapi/src/rbheap.c b/cpukit/sapi/src/rbheap.c
index 20338eb..049a64d 100644
--- a/cpukit/sapi/src/rbheap.c
+++ b/cpukit/sapi/src/rbheap.c
@@ -46,12 +46,16 @@ static uintptr_t align_down(uintptr_t alignment, uintptr_t 
value)
   return value - excess;
 }
 
-static int chunk_compare(const rtems_rbtree_node *a, const rtems_rbtree_node 
*b)
+static rtems_rbtree_compare_result chunk_compare(
+  const rtems_rbtree_node *a,
+  const rtems_rbtree_node *b
+)
 {
   const rtems_rbheap_chunk *left = rtems_rbheap_chunk_of_node(a);
   const rtems_rbheap_chunk *right = rtems_rbheap_chunk_of_node(b);
 
-  return (int) (left-begin - right-begin);
+  return (rtems_rbtree_compare_result)
+((left-begin  1) - (right-begin  1));
 }
 
 static rtems_rbheap_chunk *get_chunk(rtems_rbheap_control *control)
@@ -93,39 +97,43 @@ rtems_status_code rtems_rbheap_initialize(
 )
 {
   rtems_status_code sc = RTEMS_SUCCESSFUL;
+  uintptr_t begin = (uintptr_t) area_begin;
+  uintptr_t end = begin + area_size;
+  uintptr_t aligned_begin;
+  uintptr_t aligned_end;
 
-  if (alignment  0) {
-uintptr_t begin = (uintptr_t) area_begin;
-uintptr_t end = begin + area_size;
-uintptr_t aligned_begin = align_up(alignment, begin);
-uintptr_t aligned_end = align_down(alignment, end);
-
-if (begin  end  begin = aligned_begin  aligned_begin  aligned_end) {
-  rtems_chain_control *free_chain = control-free_chunk_chain;
-  rtems_rbtree_control *chunk_tree = control-chunk_tree;
-  rtems_rbheap_chunk *first = NULL;
-
-  

[PATCH 1/3] Add and use RTEMS_CONTAINER_OF()

2014-08-03 Thread Sebastian Huber
---
 cpukit/libblock/src/bdbuf.c|  4 +-
 cpukit/posix/include/rtems/posix/keyimpl.h |  3 ++
 cpukit/posix/src/key.c |  4 +-
 cpukit/posix/src/keyfreememory.c   |  8 ++--
 cpukit/posix/src/keygetspecific.c  |  4 +-
 cpukit/posix/src/keysetspecific.c  |  5 +--
 cpukit/sapi/include/rtems/rbheap.h |  2 +-
 cpukit/sapi/include/rtems/rbtree.h | 10 -
 cpukit/score/include/rtems/score/basedefs.h| 10 +
 cpukit/score/include/rtems/score/mrspimpl.h|  2 +-
 cpukit/score/include/rtems/score/rbtree.h  | 14 ---
 .../score/include/rtems/score/scheduleredfimpl.h   |  2 +-
 cpukit/score/include/rtems/score/schedulerimpl.h   |  2 +-
 cpukit/score/include/rtems/score/threadimpl.h  | 18 -
 cpukit/score/src/resourceiterate.c |  6 +--
 cpukit/score/src/schedulerchangeroot.c |  2 +-
 cpukit/score/src/scheduleredf.c|  4 +-
 cpukit/score/src/threadq.c | 12 +++---
 cpukit/score/src/threadqdequeue.c  |  2 +-
 cpukit/score/src/threadqfirst.c|  5 ++-
 testsuites/sptests/sprbtree01/init.c   | 44 +++---
 21 files changed, 72 insertions(+), 91 deletions(-)

diff --git a/cpukit/libblock/src/bdbuf.c b/cpukit/libblock/src/bdbuf.c
index 31dd289..f215911 100644
--- a/cpukit/libblock/src/bdbuf.c
+++ b/cpukit/libblock/src/bdbuf.c
@@ -3178,8 +3178,8 @@ rtems_bdbuf_read_ahead_task (rtems_task_argument arg)
 
 while ((node = rtems_chain_get_unprotected (chain)) != NULL)
 {
-  rtems_disk_device *dd = (rtems_disk_device *)
-((char *) node - offsetof (rtems_disk_device, read_ahead.node));
+  rtems_disk_device *dd =
+RTEMS_CONTAINER_OF (node, rtems_disk_device, read_ahead.node);
   rtems_blkdev_bnum block = dd-read_ahead.next;
   rtems_blkdev_bnum media_block = 0;
   rtems_status_code sc =
diff --git a/cpukit/posix/include/rtems/posix/keyimpl.h 
b/cpukit/posix/include/rtems/posix/keyimpl.h
index b21c1d3..aff9749 100644
--- a/cpukit/posix/include/rtems/posix/keyimpl.h
+++ b/cpukit/posix/include/rtems/posix/keyimpl.h
@@ -49,6 +49,9 @@ extern RBTree_Control _POSIX_Keys_Key_value_lookup_tree;
  */
 POSIX_EXTERN Freechain_Control _POSIX_Keys_Keypool;
 
+#define POSIX_KEYS_RBTREE_NODE_TO_KEY_VALUE_PAIR( node ) \
+  RTEMS_CONTAINER_OF( node, POSIX_Keys_Key_value_pair, Key_value_lookup_node )
+
 /**
  * @brief POSIX key manager initialization.
  *
diff --git a/cpukit/posix/src/key.c b/cpukit/posix/src/key.c
index 105706a..e231299 100644
--- a/cpukit/posix/src/key.c
+++ b/cpukit/posix/src/key.c
@@ -54,8 +54,8 @@ int _POSIX_Keys_Key_value_compare(
   Objects_Id thread_id1, thread_id2;
   int diff;
 
-  n1 = _RBTree_Container_of( node1, POSIX_Keys_Key_value_pair, 
Key_value_lookup_node );
-  n2 = _RBTree_Container_of( node2, POSIX_Keys_Key_value_pair, 
Key_value_lookup_node );
+  n1 = POSIX_KEYS_RBTREE_NODE_TO_KEY_VALUE_PAIR( node1 );
+  n2 = POSIX_KEYS_RBTREE_NODE_TO_KEY_VALUE_PAIR( node2 );
 
   diff = n1-key - n2-key;
   if ( diff )
diff --git a/cpukit/posix/src/keyfreememory.c b/cpukit/posix/src/keyfreememory.c
index b419f1f..4e19832 100644
--- a/cpukit/posix/src/keyfreememory.c
+++ b/cpukit/posix/src/keyfreememory.c
@@ -39,17 +39,17 @@ void _POSIX_Keys_Free_memory(
* find the smallest thread_id node in the rbtree.
*/
   next = _RBTree_Next( iter, RBT_LEFT );
-  p = _RBTree_Container_of( next, POSIX_Keys_Key_value_pair, 
Key_value_lookup_node );
+  p = POSIX_KEYS_RBTREE_NODE_TO_KEY_VALUE_PAIR( next );
   while ( next != NULL  p-key == key_id) {
 iter = next;
 next = _RBTree_Next( iter, RBT_LEFT );
-p = _RBTree_Container_of( next, POSIX_Keys_Key_value_pair, 
Key_value_lookup_node );
+p = POSIX_KEYS_RBTREE_NODE_TO_KEY_VALUE_PAIR( next );
   }
 
   /**
* delete all nodes belongs to the_key from the rbtree and chain.
*/
-  p = _RBTree_Container_of( iter, POSIX_Keys_Key_value_pair, 
Key_value_lookup_node );
+  p = POSIX_KEYS_RBTREE_NODE_TO_KEY_VALUE_PAIR( iter );
   while ( iter != NULL  p-key == key_id ) {
 next = _RBTree_Next( iter, RBT_RIGHT );
 _RBTree_Extract( _POSIX_Keys_Key_value_lookup_tree, iter );
@@ -57,6 +57,6 @@ void _POSIX_Keys_Free_memory(
 _POSIX_Keys_Key_value_pair_free( p );
 
 iter = next;
-p = _RBTree_Container_of( iter, POSIX_Keys_Key_value_pair, 
Key_value_lookup_node );
+p = POSIX_KEYS_RBTREE_NODE_TO_KEY_VALUE_PAIR( iter );
   }
 }
diff --git a/cpukit/posix/src/keygetspecific.c 
b/cpukit/posix/src/keygetspecific.c
index 9c54112..f7e7b71 100644
--- a/cpukit/posix/src/keygetspecific.c
+++ b/cpukit/posix/src/keygetspecific.c
@@ -51,9 +51,7 @@ void *pthread_getspecific(
 case OBJECTS_LOCAL:
   p = _POSIX_Keys_Find( key, _Thread_Executing-Object.id, search_node );
   if ( p != NULL ) {
-value_pair_p = 

Python RAP and Reloc related for RTL

2014-08-03 Thread Peng Fan
Hi,

Create this new thread to talk about this topic and the reloc related.

1.
As you suggested, I have compiled toolchain for arm using option
`-mlong-calls` on arm realview qemu platform, and now the python.rap file
can be correctly loaded and python interpreter can run the xxx.py in
startup like this `rap ld ./python-library.rap  tmp a.py`.
A small hack should be done is about gcc search dirs. I'll try to fix this
later.

Another issue is that, when python rap is loaded without and py file passed
to it, it can not respond to input. The msg output is :

RTEMS SHELL (Ver.1.0-FRC):/dev/console. Aug  3 2014. 'help' to list
commands.
[/] # rap ld ./python-library.rap
rtl: loading './python-library.rap'
rtl: rap: input machine=0
rtl: rap: machinetype=40
rtl: rap: datatype=1
rtl: rap: class=1
rtl: rap: input header=12
rtl: rap: load: symtab=16068 (1339) strtab=23867 relocs=0
rtl: rap: input .text=32948
rtl: rap: input .const=1088028
rtl: rap: input .data=1233652
rtl: rap: input symbols=1465241
rtl: rap: input relocs=1505176
Could not find platform independent libraries prefix
Could not find platform dependent libraries exec_prefix
Consider setting $PYTHONHOME to prefix[:exec_prefix]
Python 2.7.8 (default, Aug  3 2014, 21:17:53)
[GCC 4.8.3 20140522 (RTEMS
4.11-RSB-7c46699472b0d0adea2010f735722e2610c8c6ae-1, on linux2
Type help, copyright, credits or license for more information.


It does not respond to input, any ideas about this?

2. I have not tried on sparc sis, because it's memory is small. How to
modify it's memory size in sis-gdb?

Regards,
Peng.
___
devel mailing list
devel@rtems.org
http://lists.rtems.org/mailman/listinfo/devel