diff mbox

[API-NEXT,3/5] linux-gen: drv: shm: adding buddy allocator

Message ID 1482319702-5766-4-git-send-email-christophe.milard@linaro.org
State New
Headers show

Commit Message

Christophe Milard Dec. 21, 2016, 11:28 a.m. UTC
A pool for buddy allocation of small memory sizes is created at boot
time and functions to reserve/free memory from this pool added
(using the _ishm equivalent, of course).
Memory allocated from this pool is sharable between ODP threads,
(which malloc() would not guarantee in the odptreads as process case)

Signed-off-by: Christophe Milard <christophe.milard@linaro.org>

---
 platform/linux-generic/drv_shm.c                   | 44 +++++++++++++++++++++-
 .../linux-generic/include/odp_config_internal.h    | 10 +++++
 platform/linux-generic/include/odp_internal.h      |  4 ++
 platform/linux-generic/odp_init.c                  | 13 +++++++
 4 files changed, 70 insertions(+), 1 deletion(-)

-- 
2.7.4
diff mbox

Patch

diff --git a/platform/linux-generic/drv_shm.c b/platform/linux-generic/drv_shm.c
index 9b2560d..dec3e6c 100644
--- a/platform/linux-generic/drv_shm.c
+++ b/platform/linux-generic/drv_shm.c
@@ -7,7 +7,14 @@ 
 #include <odp_config_internal.h>
 #include <odp/api/std_types.h>
 #include <odp/drv/shm.h>
+#include <odp_internal.h>
+#include <odp_config_internal.h>
+#include <odp/api/align.h>
 #include <_ishm_internal.h>
+#include <_ishmbuddy_internal.h>
+
+#define BUDDYPOOL_NAME "drvshm_buddy_pool"
+static _odpdrv_shm_bpool_t *buddy_pool;
 
 static inline uint32_t from_handle(odpdrv_shm_t shm)
 {
@@ -98,5 +105,40 @@  int odpdrv_shm_info(odpdrv_shm_t shm, odpdrv_shm_info_t *info)
 
 int odpdrv_shm_print_all(const char *title)
 {
-	return _odp_ishm_status(title);
+	int res;
+
+	/* print shm status */
+	res =  _odp_ishm_status(title);
+	/* print buddy allocator status: */
+	(void)_odp_ishmbud_status(buddy_pool);
+
+	return res;
+}
+
+void *odpdrv_shm_sreserve(uint64_t size)
+{
+	return _odp_ishmbud_alloc(buddy_pool, size);
+}
+
+int odpdrv_shm_sfree(void *addr)
+{
+	return _odp_ishmbud_free(buddy_pool, addr);
+}
+
+int _odpdrv_shm_init_global(void)
+{
+	/* create a buddy pool of granularity = cache_line_size: */
+	buddy_pool = _odp_ishmbud_pool_create(BUDDYPOOL_NAME,
+					      ODP_CONFIG_DRVSHM_BUDDY_POOL_SZ,
+					      ODP_CACHE_LINE_SIZE, 0);
+	if (!buddy_pool)
+		return -1;
+
+	return 0;
+}
+
+int _odpdrv_shm_term_global(void)
+{
+	/* destroy the buddy pool: */
+	return _odp_ishmbud_pool_destroy(buddy_pool);
 }
diff --git a/platform/linux-generic/include/odp_config_internal.h b/platform/linux-generic/include/odp_config_internal.h
index c494660..0b810c8 100644
--- a/platform/linux-generic/include/odp_config_internal.h
+++ b/platform/linux-generic/include/odp_config_internal.h
@@ -116,6 +116,16 @@  extern "C" {
  */
 #define ODP_CONFIG_ISHM_VA_PREALLOC_SZ (536870912L)
 
+/*
+ * Size of the DRVSHM buddy allocator pool, available on south interface.
+ *
+ * This pool is preallocated at init time. Memory from this pool can
+ * be allocated using odpdrv_shm_sreserve().
+ * should be a power of 2 (or will be rounded as such)
+ * In bytes.
+ */
+#define ODP_CONFIG_DRVSHM_BUDDY_POOL_SZ (1048576UL)
+
 /* Maximum number of shared memory blocks available on the driver interface.
  *
  * This the the number of separate SHM areas that can be reserved concurrently
diff --git a/platform/linux-generic/include/odp_internal.h b/platform/linux-generic/include/odp_internal.h
index b313b1f..908891f 100644
--- a/platform/linux-generic/include/odp_internal.h
+++ b/platform/linux-generic/include/odp_internal.h
@@ -59,6 +59,7 @@  enum init_stage {
 	SYSINFO_INIT,
 	FDSERVER_INIT,
 	ISHM_INIT,
+	DRVSHM_INIT,
 	THREAD_INIT,
 	POOL_INIT,
 	QUEUE_INIT,
@@ -127,6 +128,9 @@  int _odp_ishm_init_local(void);
 int _odp_ishm_term_global(void);
 int _odp_ishm_term_local(void);
 
+int _odpdrv_shm_init_global(void);
+int _odpdrv_shm_term_global(void);
+
 int cpuinfo_parser(FILE *file, system_info_t *sysinfo);
 uint64_t odp_cpu_hz_current(int id);
 
diff --git a/platform/linux-generic/odp_init.c b/platform/linux-generic/odp_init.c
index 1b0d8f8..dd396be 100644
--- a/platform/linux-generic/odp_init.c
+++ b/platform/linux-generic/odp_init.c
@@ -120,6 +120,12 @@  int odp_init_global(odp_instance_t *instance,
 	}
 	stage = ISHM_INIT;
 
+	if (_odpdrv_shm_init_global()) {
+		ODP_ERR("ODP drvshm init failed.\n");
+		goto init_failed;
+	}
+	stage = DRVSHM_INIT;
+
 	if (odp_thread_init_global()) {
 		ODP_ERR("ODP thread init failed.\n");
 		goto init_failed;
@@ -273,6 +279,13 @@  int _odp_term_global(enum init_stage stage)
 		}
 		/* Fall through */
 
+	case DRVSHM_INIT:
+		if (_odpdrv_shm_term_global()) {
+			ODP_ERR("ODP drvshm term failed.\n");
+			rc = -1;
+		}
+		/* Fall through */
+
 	case ISHM_INIT:
 		if (_odp_ishm_term_global()) {
 			ODP_ERR("ODP ishm term failed.\n");