[Cbe-oss-dev] [PATCH 2/2]MARS/core: Wrap host malloc

Yuji Mano yuji.mano at am.sony.com
Fri Nov 21 12:44:14 EST 2008


Wrap malloc/memalign/free

This patch wraps memory allocation functions on host side and splits
host address space into two separate ones, system memory address and
shared memory space (EA), to prepare for NUMA support and hybrid
system support.

Signed-off-by: Kazunori Asayama <asayama at sm.sony.co.jp>

---
 core/include/host/mars/core.h       |   49 ++++++++++++++++++++++++++++++++
 core/src/host/lib/Makefile.am       |    2 +
 core/src/host/lib/alloc.c           |   55 ++++++++++++++++++++++++++++++++++++
 core/src/host/lib/context.c         |   21 ++++++-------
 core/src/host/lib/ea.c              |   51 +++++++++++++++++++++++++++++++++
 core/src/host/lib/mutex.c           |   10 +++---
 core/src/host/lib/task.c            |   13 +++-----
 core/src/host/lib/task_barrier.c    |    8 +----
 core/src/host/lib/task_event_flag.c |    7 +---
 core/src/host/lib/task_queue.c      |   11 +++----
 core/src/host/lib/task_semaphore.c  |    7 +---
 core/src/host/lib/workload_queue.c  |    5 +--
 12 files changed, 194 insertions(+), 45 deletions(-)

--- a/core/include/host/mars/core.h
+++ b/core/include/host/mars/core.h
@@ -44,6 +44,7 @@
  * \brief <b>[host]</b> MARS Core API
  */
 
+#include <stddef.h>
 #include <stdint.h>
 
 #if defined(__cplusplus)
@@ -52,6 +53,54 @@ extern "C" {
 
 /**
  * \ingroup group_mars_core
+ * \brief <b>[host]</b> Allocates memory in host storage.
+ *
+ * \param[in] size		- size of memory block to allocate
+ * \return
+ *	void *			- pointer to allocated memory block
+ */
+void *mars_malloc(size_t size);
+
+/**
+ * \ingroup group_mars_core
+ * \brief <b>[host]</b> Re-allocates memory in host storage.
+ *
+ * \param[in] ptr		- ptr to memory block to re-allocate
+ * \param[in] size		- size of memory block to resize to
+ * \return
+ *	void *			- pointer to re-allocated memory block
+ */
+void *mars_realloc(void *ptr, size_t size);
+
+/**
+ * \ingroup group_mars_core
+ * \brief <b>[host]</b> Frees memory allocated in host storage.
+ *
+ * \param[in] ptr		- ptr to memory block to free
+ */
+void mars_free(void *ptr);
+
+/**
+ * \ingroup group_mars_core
+ * \brief <b>[host]</b> Allocates memory in shared storage accessible from MPU.
+ *
+ * \param[in] boundary		- memory address will be a multiple of boundary
+ * \param[in] size		- size of memory block to allocate
+ * \return
+ *	void *			- pointer to allocated memory block
+ */
+void *mars_ea_memalign(size_t boundary, size_t size);
+
+/**
+ * \ingroup group_mars_core
+ * \brief <b>[host]</b> Frees memory allocated in shared storage.
+ *
+ * \param[in] void *		- pointer to allocated memory block to free
+ */
+void mars_ea_free(void *ptr);
+
+/**
+ * \ingroup group_mars_core
  * \brief <b>[host]</b> Converts a 64-bit address to pointer.
  *
  * \param[in] ea		- 64-bit address
--- a/core/src/host/lib/Makefile.am
+++ b/core/src/host/lib/Makefile.am
@@ -99,8 +99,10 @@ lib_LTLIBRARIES = libmars.la
 
 libmars_la_SOURCES = \
 	$(srcdir)/../../../src/common/*.h \
+	alloc.c \
 	context.c \
 	context_internal_types.h \
+	ea.c \
 	mutex.c \
 	task.c \
 	task_barrier.c \
--- /dev/null
+++ b/core/src/host/lib/alloc.c
@@ -0,0 +1,55 @@
+/*
+ * Copyright 2008 Sony Corporation of America
+ *
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this Library and associated documentation files (the
+ * "Library"), to deal in the Library without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Library, and to
+ * permit persons to whom the Library is furnished to do so, subject to
+ * the following conditions:
+ *
+ *  The above copyright notice and this permission notice shall be
+ *  included in all copies or substantial portions of the Library.
+ *
+ *  If you modify the Library, you may copy and distribute your modified
+ *  version of the Library in object code or as an executable provided
+ *  that you also do one of the following:
+ *
+ *   Accompany the modified version of the Library with the complete
+ *   corresponding machine-readable source code for the modified version
+ *   of the Library; or,
+ *
+ *   Accompany the modified version of the Library with a written offer
+ *   for a complete machine-readable copy of the corresponding source
+ *   code of the modified version of the Library.
+ *
+ *
+ * THE LIBRARY IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * LIBRARY OR THE USE OR OTHER DEALINGS IN THE LIBRARY.
+ */
+
+#include <stdlib.h>
+
+#include "mars/core.h"
+
+void *mars_malloc(size_t size)
+{
+	return malloc(size);
+}
+
+void *mars_realloc(void *ptr, size_t size)
+{
+	return realloc(ptr, size);
+}
+
+void mars_free(void *ptr)
+{
+	free(ptr);
+}
--- a/core/src/host/lib/context.c
+++ b/core/src/host/lib/context.c
@@ -35,7 +35,6 @@
  * LIBRARY OR THE USE OR OTHER DEALINGS IN THE LIBRARY.
  */
 
-#include <malloc.h>
 #include <string.h>
 #include <pthread.h>
 #include <libspe2.h>
@@ -195,7 +194,7 @@ int mars_context_create(struct mars_cont
 	}
 
 	/* allocate context */
-	mars = malloc(sizeof(struct mars_context));
+	mars = mars_malloc(sizeof(struct mars_context));
 	if (!mars) {
 		ret = MARS_ERROR_MEMORY;
 		goto error;
@@ -208,8 +207,8 @@ int mars_context_create(struct mars_cont
 	mars->reference_count++;
 
 	/* allocate kernel params */
-	mars->kernel_params = (struct mars_kernel_params *)
-		memalign(MARS_KERNEL_PARAMS_ALIGN,
+	mars->kernel_params = (struct mars_kernel_params *)mars_ea_memalign(
+			MARS_KERNEL_PARAMS_ALIGN,
 			sizeof(struct mars_kernel_params) * num_mpus_max);
 	if (!mars->kernel_params) {
 		ret = MARS_ERROR_MEMORY;
@@ -221,7 +220,7 @@ int mars_context_create(struct mars_cont
 
 	/* allocate mpu context thread array */
 	mars->mpu_context_threads = (pthread_t *)
-		malloc(sizeof(pthread_t) * num_mpus_max);
+		mars_malloc(sizeof(pthread_t) * num_mpus_max);
 	if (!mars->mpu_context_threads) {
 		ret = MARS_ERROR_MEMORY;
 		goto error_malloc_mpu_context_threads;
@@ -259,11 +258,11 @@ error_pthread_mutex_unlock:
 error_mpu_contexts_create:
 	mars_workload_queue_destroy(mars->workload_queue);
 error_workload_queue_create:
-	free(mars->mpu_context_threads);
+	mars_free(mars->mpu_context_threads);
 error_malloc_mpu_context_threads:
-	free(mars->kernel_params);
+	mars_ea_free(mars->kernel_params);
 error_malloc_kernel_params:
-	free(mars);
+	mars_free(mars);
 error:
 	pthread_mutex_unlock(&mars_mutex);
 
@@ -305,9 +304,9 @@ int mars_context_destroy(struct mars_con
 	}
 
 	/* free allocated memory */
-	free(mars->mpu_context_threads);
-	free(mars->kernel_params);
-	free(mars);
+	mars_free(mars->mpu_context_threads);
+	mars_ea_free(mars->kernel_params);
+	mars_free(mars);
 
 	/* check if it is the shared context pointer and set to NULL */
 	if (mars == mars_shared_context)
--- /dev/null
+++ b/core/src/host/lib/ea.c
@@ -0,0 +1,51 @@
+/*
+ * Copyright 2008 Sony Corporation of America
+ *
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this Library and associated documentation files (the
+ * "Library"), to deal in the Library without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Library, and to
+ * permit persons to whom the Library is furnished to do so, subject to
+ * the following conditions:
+ *
+ *  The above copyright notice and this permission notice shall be
+ *  included in all copies or substantial portions of the Library.
+ *
+ *  If you modify the Library, you may copy and distribute your modified
+ *  version of the Library in object code or as an executable provided
+ *  that you also do one of the following:
+ *
+ *   Accompany the modified version of the Library with the complete
+ *   corresponding machine-readable source code for the modified version
+ *   of the Library; or,
+ *
+ *   Accompany the modified version of the Library with a written offer
+ *   for a complete machine-readable copy of the corresponding source
+ *   code of the modified version of the Library.
+ *
+ *
+ * THE LIBRARY IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * LIBRARY OR THE USE OR OTHER DEALINGS IN THE LIBRARY.
+ */
+
+#include <stdlib.h>
+#include <malloc.h>
+
+#include "mars/core.h"
+
+void *mars_ea_memalign(size_t boundary, size_t size)
+{
+	return memalign(boundary, size);
+}
+
+void mars_ea_free(void *ptr)
+{
+	free(ptr);
+}
--- a/core/src/host/lib/mutex.c
+++ b/core/src/host/lib/mutex.c
@@ -35,11 +35,11 @@
  * LIBRARY OR THE USE OR OTHER DEALINGS IN THE LIBRARY.
  */
 
-#include <malloc.h>
 #include <ppu_intrinsics.h>
 
-#include "mars/mutex.h"
+#include "mars/core.h"
 #include "mars/error.h"
+#include "mars/mutex.h"
 
 #include "mutex_internal_types.h"
 
@@ -50,8 +50,8 @@ int mars_mutex_create(struct mars_mutex 
 	if (!mutex_ret)
 		return MARS_ERROR_NULL;
 
-	mutex = (struct mars_mutex *)
-		memalign(MARS_MUTEX_ALIGN, sizeof(struct mars_mutex));
+	mutex = (struct mars_mutex *)mars_ea_memalign(
+		MARS_MUTEX_ALIGN, sizeof(struct mars_mutex));
 	if (!mutex)
 		return MARS_ERROR_MEMORY;
 
@@ -68,7 +68,7 @@ int mars_mutex_destroy(struct mars_mutex
 	if (!mutex)
 		return MARS_ERROR_NULL;
 
-	free(mutex);
+	mars_ea_free(mutex);
 
 	return MARS_SUCCESS;
 }
--- a/core/src/host/lib/task.c
+++ b/core/src/host/lib/task.c
@@ -37,7 +37,6 @@
 
 #include <elf.h>
 #include <string.h>
-#include <malloc.h>
 
 #include "mars/task.h"
 #include "mars/core.h"
@@ -136,7 +135,7 @@ int mars_task_create(struct mars_context
 	}
 
 	/* allocate context save unit storage */
-	task->context_save_unit_ea = mars_ptr_to_ea(memalign(
+	task->context_save_unit_ea = mars_ptr_to_ea(mars_ea_memalign(
 		MARS_TASK_CONTEXT_SAVE_UNIT_ALIGN,
 		context_save_unit_count * MARS_TASK_CONTEXT_SAVE_UNIT_SIZE));
 	if (!task->context_save_unit_ea) {
@@ -145,7 +144,7 @@ int mars_task_create(struct mars_context
 	}
 
 	/* allocate context save area */
-	task->context_save_area_ea = mars_ptr_to_ea(memalign(
+	task->context_save_area_ea = mars_ptr_to_ea(mars_ea_memalign(
 		MARS_TASK_CONTEXT_SAVE_ALIGN, context_save_area_size));
 	if (!task->context_save_area_ea) {
 		ret = MARS_ERROR_MEMORY;
@@ -169,9 +168,9 @@ done:
 	return MARS_SUCCESS;
 
 error_workload_queue_add_end:
-	free(mars_ea_to_ptr(task->context_save_area_ea));
+	mars_ea_free(mars_ea_to_ptr(task->context_save_area_ea));
 error_malloc_context_save_area:
-	free(mars_ea_to_ptr(task->context_save_unit_ea));
+	mars_ea_free(mars_ea_to_ptr(task->context_save_unit_ea));
 error_malloc_context_save_unit:
 error_context_save_unit_addr_align:
 	mars_workload_queue_add_cancel(mars->workload_queue, workload_id);
@@ -208,11 +207,11 @@ int mars_task_destroy(struct mars_task_i
 
 	/* free the allocated context save area if it has one */
 	if (task->context_save_area_ea)
-		free(mars_ea_to_ptr(task->context_save_area_ea));
+		mars_ea_free(mars_ea_to_ptr(task->context_save_area_ea));
 
 	/* free the allocated context save unit storage if it has one */
 	if (task->context_save_unit_ea)
-		free(mars_ea_to_ptr(task->context_save_unit_ea));
+		mars_ea_free(mars_ea_to_ptr(task->context_save_unit_ea));
 
 	/* end process to remove the task from the workload queue */
 	ret = mars_workload_queue_remove_end(mars->workload_queue,
--- a/core/src/host/lib/task_barrier.c
+++ b/core/src/host/lib/task_barrier.c
@@ -35,8 +35,6 @@
  * LIBRARY OR THE USE OR OTHER DEALINGS IN THE LIBRARY.
  */
 
-#include <malloc.h>
-
 #include "mars/task_barrier.h"
 #include "mars/core.h"
 #include "mars/mutex.h"
@@ -61,8 +59,8 @@ int mars_task_barrier_create(struct mars
 		return MARS_ERROR_PARAMS;
 
 	/* allocate barrier instance */
-	barrier = (struct mars_task_barrier *)
-		memalign(MARS_TASK_BARRIER_ALIGN, MARS_TASK_BARRIER_SIZE);
+	barrier = (struct mars_task_barrier *)mars_ea_memalign(
+		MARS_TASK_BARRIER_ALIGN, MARS_TASK_BARRIER_SIZE);
 	if (!barrier)
 		return MARS_ERROR_MEMORY;
 
@@ -88,7 +86,7 @@ int mars_task_barrier_destroy(struct mar
 	if (!barrier)
 		return MARS_ERROR_NULL;
 
-	free(barrier);
+	mars_ea_free(barrier);
 
 	return MARS_SUCCESS;
 }
--- a/core/src/host/lib/task_event_flag.c
+++ b/core/src/host/lib/task_event_flag.c
@@ -37,7 +37,6 @@
 
 #include <sched.h>
 #include <string.h>
-#include <malloc.h>
 
 #include "mars/task_event_flag.h"
 #include "mars/core.h"
@@ -70,8 +69,8 @@ int mars_task_event_flag_create(struct m
 		return MARS_ERROR_PARAMS;
 
 	/* allocate event flag instance */
-	event_flag = (struct mars_task_event_flag *)
-		memalign(MARS_TASK_EVENT_FLAG_ALIGN, MARS_TASK_EVENT_FLAG_SIZE);
+	event_flag = (struct mars_task_event_flag *)mars_ea_memalign(
+		MARS_TASK_EVENT_FLAG_ALIGN, MARS_TASK_EVENT_FLAG_SIZE);
 	if (!event_flag)
 		return MARS_ERROR_MEMORY;
 
@@ -96,7 +95,7 @@ int mars_task_event_flag_destroy(struct 
 	if (!event_flag)
 		return MARS_ERROR_NULL;
 
-	free(event_flag);
+	mars_ea_free(event_flag);
 
 	return MARS_SUCCESS;
 }
--- a/core/src/host/lib/task_queue.c
+++ b/core/src/host/lib/task_queue.c
@@ -37,7 +37,6 @@
 
 #include <sched.h>
 #include <string.h>
-#include <malloc.h>
 
 #include "mars/task_queue.h"
 #include "mars/core.h"
@@ -73,13 +72,13 @@ int mars_task_queue_create(struct mars_c
 		return MARS_ERROR_PARAMS;
 
 	/* allocate queue instance */
-	queue = (struct mars_task_queue *)
-		memalign(MARS_TASK_QUEUE_ALIGN, MARS_TASK_QUEUE_SIZE);
+	queue = (struct mars_task_queue *)mars_ea_memalign(
+		MARS_TASK_QUEUE_ALIGN, MARS_TASK_QUEUE_SIZE);
 	if (!queue)
 		return MARS_ERROR_MEMORY;
 
 	/* allocate queue buffer instance */
-	buffer = memalign(MARS_TASK_QUEUE_BUFFER_ALIGN, size * depth);
+	buffer = mars_ea_memalign(MARS_TASK_QUEUE_BUFFER_ALIGN, size * depth);
 	if (!buffer)
 		return MARS_ERROR_MEMORY;
 
@@ -109,8 +108,8 @@ int mars_task_queue_destroy(struct mars_
 	if (!queue)
 		return MARS_ERROR_NULL;
 
-	free(mars_ea_to_ptr(queue->buffer_ea));
-	free(queue);
+	mars_ea_free(mars_ea_to_ptr(queue->buffer_ea));
+	mars_ea_free(queue);
 
 	return MARS_SUCCESS;
 }
--- a/core/src/host/lib/task_semaphore.c
+++ b/core/src/host/lib/task_semaphore.c
@@ -36,7 +36,6 @@
  */
 
 #include <stdlib.h>
-#include <malloc.h>
 
 #include "mars/task_semaphore.h"
 #include "mars/core.h"
@@ -60,8 +59,8 @@ int mars_task_semaphore_create(struct ma
 		return MARS_ERROR_NULL;
 
 	/* allocate semaphore instance */
-	semaphore = (struct mars_task_semaphore *)
-		memalign(MARS_TASK_SEMAPHORE_ALIGN, MARS_TASK_SEMAPHORE_SIZE);
+	semaphore = (struct mars_task_semaphore *)mars_ea_memalign(
+		MARS_TASK_SEMAPHORE_ALIGN, MARS_TASK_SEMAPHORE_SIZE);
 	if (!semaphore)
 		return MARS_ERROR_MEMORY;
 
@@ -84,7 +83,7 @@ int mars_task_semaphore_destroy(struct m
 	if (!semaphore)
 		return MARS_ERROR_NULL;
 
-	free(semaphore);
+	mars_ea_free(semaphore);
 
 	return MARS_SUCCESS;
 }
--- a/core/src/host/lib/workload_queue.c
+++ b/core/src/host/lib/workload_queue.c
@@ -38,7 +38,6 @@
 #include <sched.h>
 #include <unistd.h>
 #include <string.h>
-#include <malloc.h>
 
 #include "mars/core.h"
 #include "mars/mutex.h"
@@ -59,7 +58,7 @@ int mars_workload_queue_create(struct ma
 		return MARS_ERROR_NULL;
 
 	/* allocate workload instance */
-	queue = (struct mars_workload_queue *)memalign(
+	queue = (struct mars_workload_queue *)mars_ea_memalign(
 		MARS_WORKLOAD_QUEUE_ALIGN, sizeof(struct mars_workload_queue));
 	if (!queue)
 		return MARS_ERROR_MEMORY;
@@ -120,7 +119,7 @@ int mars_workload_queue_destroy(struct m
 	}
 
 	/* free workload queue instance */
-	free(queue);
+	mars_ea_free(queue);
 
 	return MARS_SUCCESS;
 }





More information about the cbe-oss-dev mailing list