[Cbe-oss-dev] [PATCH 01/10]MARS: Add to from ea static inline functions

Yuji Mano yuji.mano at am.sony.com
Fri Oct 3 04:31:12 EST 2008


This adds static inline functions mars_ptr_to_ea and mars_ea_to_ptr to replace
the direct casting when converting back and forth from a pointer to 64-bit int.

Signed-off-by: Yuji Mano <yuji.mano at am.sony.com>

---
 include/host/mars/mars_context.h    |   10 ++++++++++
 src/host/lib/mars_context.c         |    4 ++--
 src/host/lib/mars_task.c            |   19 +++++++++----------
 src/host/lib/mars_task_barrier.c    |    3 ++-
 src/host/lib/mars_task_event_flag.c |    5 ++---
 src/host/lib/mars_task_queue.c      |   20 +++++++++-----------
 src/host/lib/mars_task_semaphore.c  |    3 ++-
 src/host/lib/mars_task_signal.c     |    3 +--
 src/host/lib/mars_workload_queue.c  |    5 +++--
 9 files changed, 40 insertions(+), 32 deletions(-)

--- a/include/host/mars/mars_context.h
+++ b/include/host/mars/mars_context.h
@@ -69,6 +69,16 @@ struct mars_context {
 extern "C" {
 #endif
 
+static inline void *mars_ea_to_ptr(uint64_t ea)
+{
+	return (void *)(uintptr_t)ea;
+}
+
+static inline uint64_t mars_ptr_to_ea(void *ptr)
+{
+	return (uint64_t)(uintptr_t)ptr;
+}
+
 /**
  * \ingroup group_mars_context
  * \brief [host] Initializes a single MARS context.
--- a/src/host/lib/mars_context.c
+++ b/src/host/lib/mars_context.c
@@ -84,9 +84,9 @@ static int create_mpu_contexts(struct ma
 		struct mars_kernel_params *params = &mars->kernel_params[i];
 		params->id = i;
 		params->mars_context_ea =
-				(uint64_t)(uintptr_t)mars;
+				mars_ptr_to_ea(mars);
 		params->workload_queue_ea =
-				(uint64_t)(uintptr_t)mars->workload_queue;
+				mars_ptr_to_ea(mars->workload_queue);
 
 		ret = pthread_create(&mars->mpu_context_threads[i], NULL,
 				mpu_context_thread, params);
--- a/src/host/lib/mars_task.c
+++ b/src/host/lib/mars_task.c
@@ -71,7 +71,7 @@ int mars_task_initialize(struct mars_con
 			MARS_ERROR_FORMAT);
 
 	/* initialize task context */
-	id->mars_context_ea = (uint64_t)(uintptr_t)mars;
+	id->mars_context_ea = mars_ptr_to_ea(mars);
 
 	/* copy the task name into task id */
 	if (name)
@@ -89,7 +89,7 @@ int mars_task_initialize(struct mars_con
 	memcpy(&task->id, id, sizeof(struct mars_task_id));
 
 	/* initialize the elf parameters */
-	task->exec = (uint64_t)(uintptr_t)((void *)ehdr + phdr->p_offset);
+	task->exec = mars_ptr_to_ea((void *)ehdr + phdr->p_offset);
 	task->exec_size = phdr->p_filesz;
 	task->bss_size = phdr->p_memsz - phdr->p_filesz;
 	task->vaddr = phdr->p_vaddr;
@@ -98,9 +98,8 @@ int mars_task_initialize(struct mars_con
 	/* allocate the task context area if specified */
 	if (context_save_size) {
 		task->context_save_size = context_save_size;
-		task->context_save_area = (uint64_t)(uintptr_t)
-			memalign(MARS_TASK_CONTEXT_SAVE_ALIGN,
-				task->context_save_size);
+		task->context_save_area = mars_ptr_to_ea(memalign(
+			MARS_TASK_CONTEXT_SAVE_ALIGN, task->context_save_size));
 
 		MARS_CHECK_CLEANUP_RET(task->context_save_area,
 			workload_queue_add_cancel(mars->workload_queue,
@@ -133,7 +132,7 @@ int mars_task_finalize(struct mars_task_
 	int ret;
 	struct mars_task_context *task;
 	struct mars_workload_context *workload;
-	struct mars_context *mars = (void *)(uintptr_t)id->mars_context_ea;
+	struct mars_context *mars = mars_ea_to_ptr(id->mars_context_ea);
 
 	/* begin process to remove the task from the workload queue */
 	ret = workload_queue_remove_begin(mars->workload_queue,
@@ -145,7 +144,7 @@ int mars_task_finalize(struct mars_task_
 
 	/* free the allocated task context area if it has one */
 	if (task->context_save_size)
-		free((void *)(uintptr_t)task->context_save_area);
+		free(mars_ea_to_ptr(task->context_save_area));
 
 	/* end process to remove the task from the workload queue */
 	ret = workload_queue_remove_end(mars->workload_queue, id->workload_id);
@@ -166,7 +165,7 @@ int mars_task_schedule(struct mars_task_
 	int ret;
 	struct mars_task_context *task;
 	struct mars_workload_context *workload;
-	struct mars_context *mars = (void *)(uintptr_t)id->mars_context_ea;
+	struct mars_context *mars = mars_ea_to_ptr(id->mars_context_ea);
 
 	/* begin process to schedule the workload in the workload queue */
 	ret = workload_queue_schedule_begin(mars->workload_queue,
@@ -200,7 +199,7 @@ int mars_task_wait(struct mars_task_id *
 	int ret;
 	struct mars_task_context *task;
 	struct mars_workload_context *workload;
-	struct mars_context *mars = (void *)(uintptr_t)id->mars_context_ea;
+	struct mars_context *mars = mars_ea_to_ptr(id->mars_context_ea);
 
 	/* blocking wait for workload completion */
 	ret = workload_queue_wait(mars->workload_queue, id->workload_id,
@@ -226,7 +225,7 @@ int mars_task_try_wait(struct mars_task_
 	int ret;
 	struct mars_task_context *task;
 	struct mars_workload_context *workload;
-	struct mars_context *mars = (void *)(uintptr_t)id->mars_context_ea;
+	struct mars_context *mars = mars_ea_to_ptr(id->mars_context_ea);
 
 	/* non-blocking wait for workload completion */
 	ret = workload_queue_try_wait(mars->workload_queue, id->workload_id,
--- a/src/host/lib/mars_task_barrier.c
+++ b/src/host/lib/mars_task_barrier.c
@@ -36,6 +36,7 @@
  */
 
 #include "mars/mars_task_barrier.h"
+#include "mars/mars_context.h"
 #include "mars/mars_mutex.h"
 #include "mars/mars_error.h"
 #include "mars/mars_debug.h"
@@ -53,7 +54,7 @@ int mars_task_barrier_initialize(struct 
 	mars_mutex_initialize((struct mars_mutex *)barrier);
 	mars_mutex_lock((struct mars_mutex *)barrier);
 
-	barrier->mars_context_ea = (uint64_t)(uintptr_t)mars;
+	barrier->mars_context_ea = mars_ptr_to_ea(mars);
 	barrier->total = total;
 	barrier->notified_count = 0;
 	barrier->waited_count = 0;
--- a/src/host/lib/mars_task_event_flag.c
+++ b/src/host/lib/mars_task_event_flag.c
@@ -65,7 +65,7 @@ int mars_task_event_flag_initialize(stru
 	mars_mutex_initialize((struct mars_mutex *)event_flag);
 	mars_mutex_lock((struct mars_mutex *)event_flag);
 
-	event_flag->mars_context_ea = (uint64_t)(uintptr_t)mars;
+	event_flag->mars_context_ea = mars_ptr_to_ea(mars);
 	event_flag->bits = 0;
 	event_flag->direction = direction;
 	event_flag->clear_mode = clear_mode;
@@ -105,8 +105,7 @@ int mars_task_event_flag_set(struct mars
 
 	int ret;
 	int i;
-	struct mars_context *mars =
-		(void *)(uintptr_t)event_flag->mars_context_ea;
+	struct mars_context *mars = mars_ea_to_ptr(event_flag->mars_context_ea);
 
 	mars_mutex_lock((struct mars_mutex *)event_flag);
 
--- a/src/host/lib/mars_task_queue.c
+++ b/src/host/lib/mars_task_queue.c
@@ -70,10 +70,10 @@ int mars_task_queue_initialize(struct ma
 	mars_mutex_initialize((struct mars_mutex *)queue);
 	mars_mutex_lock((struct mars_mutex *)queue);
 
-	queue->mars_context_ea = (uint64_t)(uintptr_t)mars;
-	queue->buffer_ea = (uint64_t)(uintptr_t)buffer;
-	queue->push_ea = queue->buffer_ea;
-	queue->pop_ea = queue->buffer_ea;
+	queue->mars_context_ea = mars_ptr_to_ea(mars);
+	queue->buffer_ea = mars_ptr_to_ea(buffer);
+	queue->push_ea = mars_ptr_to_ea(buffer);
+	queue->pop_ea = mars_ptr_to_ea(buffer);
 	queue->size = size;
 	queue->depth = depth;
 	queue->direction = direction;
@@ -110,8 +110,7 @@ int mars_task_queue_clear(struct mars_ta
 			MARS_ERROR_ALIGN);
 
 	int i;
-	struct mars_context *mars =
-			(void *)(uintptr_t)queue->mars_context_ea;
+	struct mars_context *mars = mars_ea_to_ptr(queue->mars_context_ea);
 
 	mars_mutex_lock((struct mars_mutex *)queue);
 
@@ -135,8 +134,7 @@ int mars_task_queue_clear(struct mars_ta
 
 static void push_update(struct mars_task_queue *queue)
 {
-	struct mars_context *mars =
-			(void *)(uintptr_t)queue->mars_context_ea;
+	struct mars_context *mars = mars_ea_to_ptr(queue->mars_context_ea);
 
 	/* signal waiting task that queue is ready for pop */
 	if (queue->pop_wait_count) {
@@ -186,7 +184,7 @@ static int push(struct mars_task_queue *
 	}
 
 	/* copy data into queue */
-	memcpy((void *)(uintptr_t)queue->push_ea, data, queue->size);
+	memcpy(mars_ea_to_ptr(queue->push_ea), data, queue->size);
 
 	/* update queue data */
 	push_update(queue);
@@ -208,7 +206,7 @@ int mars_task_queue_try_push(struct mars
 
 static void pop_update(struct mars_task_queue *queue)
 {
-	struct mars_context *mars = (void *)(uintptr_t)queue->mars_context_ea;
+	struct mars_context *mars = mars_ea_to_ptr(queue->mars_context_ea);
 
 	/* signal waiting task that queue is ready for push */
 	if (queue->push_wait_count) {
@@ -258,7 +256,7 @@ static int pop(struct mars_task_queue *q
 	}
 
 	/* copy data from queue */
-	memcpy(data, (void *)(uintptr_t)queue->pop_ea, queue->size);
+	memcpy(data, mars_ea_to_ptr(queue->pop_ea), queue->size);
 
 	/* update queue data only if this is not a peek operation */
 	if (!peek)
--- a/src/host/lib/mars_task_semaphore.c
+++ b/src/host/lib/mars_task_semaphore.c
@@ -38,6 +38,7 @@
 #include <stdlib.h>
 
 #include "mars/mars_task_semaphore.h"
+#include "mars/mars_context.h"
 #include "mars/mars_mutex.h"
 #include "mars/mars_error.h"
 #include "mars/mars_debug.h"
@@ -56,7 +57,7 @@ int mars_task_semaphore_initialize(struc
 	mars_mutex_initialize((struct mars_mutex *)semaphore);
 	mars_mutex_lock((struct mars_mutex *)semaphore);
 
-	semaphore->mars_context_ea = (uint64_t)(uintptr_t)mars;
+	semaphore->mars_context_ea = mars_ptr_to_ea(mars);
 	semaphore->count = count;
 	semaphore->wait_count = 0;
 
--- a/src/host/lib/mars_task_signal.c
+++ b/src/host/lib/mars_task_signal.c
@@ -40,7 +40,6 @@
 #include "mars/mars_context.h"
 #include "mars/mars_error.h"
 #include "mars/mars_debug.h"
-#include "mars/mars_workload_queue.h"
 
 int mars_task_signal_send(struct mars_task_id *id)
 {
@@ -48,7 +47,7 @@ int mars_task_signal_send(struct mars_ta
 	MARS_CHECK_RET(id->mars_context_ea, MARS_ERROR_PARAMS);
 
 	int ret;
-	struct mars_context *mars = (void *)(uintptr_t)id->mars_context_ea;
+	struct mars_context *mars = mars_ea_to_ptr(id->mars_context_ea);
 
 	ret = workload_queue_signal_send(mars->workload_queue, id->workload_id);
 	MARS_CHECK_RET(ret == MARS_SUCCESS, ret);
--- a/src/host/lib/mars_workload_queue.c
+++ b/src/host/lib/mars_workload_queue.c
@@ -41,6 +41,7 @@
 
 #include "mars/mars_workload_queue.h"
 #include "mars/mars_kernel_types.h"
+#include "mars/mars_context.h"
 #include "mars/mars_mutex.h"
 #include "mars/mars_error.h"
 #include "mars/mars_debug.h"
@@ -53,8 +54,8 @@ int workload_queue_initialize(struct mar
 	int index;
 
 	queue->header.flag = MARS_WORKLOAD_QUEUE_FLAG_NONE;
-	queue->header.queue_ea = (uint64_t)(uintptr_t)queue;
-	queue->header.context_ea = (uint64_t)(uintptr_t)&queue->context;
+	queue->header.queue_ea = mars_ptr_to_ea(queue);
+	queue->header.context_ea = mars_ptr_to_ea(&queue->context);
 
 	/* initialize workload queue blocks */
 	for (block = 0; block < MARS_WORKLOAD_NUM_BLOCKS; block++) {






More information about the cbe-oss-dev mailing list