diff mbox

[PATCHv3,2/3] Buffer pool API Part 2 of 3: changes for buffer pool APIs

Message ID 1416693854-29711-2-git-send-email-bill.fischofer@linaro.org
State Superseded
Headers show

Commit Message

Bill Fischofer Nov. 22, 2014, 10:04 p.m. UTC
Signed-off-by: Bill Fischofer <bill.fischofer@linaro.org>
---
Merge parts 1, 2, and 3 of this patch after review.

 .../linux-generic/include/api/odp_platform_types.h |  9 ++
 .../linux-generic/include/api/odp_shared_memory.h  | 10 +--
 .../include/odp_buffer_pool_internal.h             |  8 ++
 platform/linux-generic/odp_buffer_pool.c           | 98 +++++++++++++++++++---
 platform/linux-generic/odp_schedule.c              | 20 ++---
 5 files changed, 111 insertions(+), 34 deletions(-)

Comments

Bill Fischofer Dec. 1, 2014, 4:42 p.m. UTC | #1
This patch has been superseded and is withdrawn.

On Sat, Nov 22, 2014 at 5:04 PM, Bill Fischofer <bill.fischofer@linaro.org>
wrote:

> Signed-off-by: Bill Fischofer <bill.fischofer@linaro.org>
> ---
> Merge parts 1, 2, and 3 of this patch after review.
>
>  .../linux-generic/include/api/odp_platform_types.h |  9 ++
>  .../linux-generic/include/api/odp_shared_memory.h  | 10 +--
>  .../include/odp_buffer_pool_internal.h             |  8 ++
>  platform/linux-generic/odp_buffer_pool.c           | 98
> +++++++++++++++++++---
>  platform/linux-generic/odp_schedule.c              | 20 ++---
>  5 files changed, 111 insertions(+), 34 deletions(-)
>
> diff --git a/platform/linux-generic/include/api/odp_platform_types.h
> b/platform/linux-generic/include/api/odp_platform_types.h
> index 4db47d3..b9b3aea 100644
> --- a/platform/linux-generic/include/api/odp_platform_types.h
> +++ b/platform/linux-generic/include/api/odp_platform_types.h
> @@ -65,6 +65,15 @@ typedef uint32_t odp_pktio_t;
>  #define ODP_PKTIO_ANY ((odp_pktio_t)~0)
>
>  /**
> + * ODP shared memory block
> + */
> +typedef uint32_t odp_shm_t;
> +
> +/** Invalid shared memory block */
> +#define ODP_SHM_INVALID 0
> +#define ODP_SHM_NULL ODP_SHM_INVALID /**< Synonym for buffer pool use */
> +
> +/**
>   * @}
>   */
>
> diff --git a/platform/linux-generic/include/api/odp_shared_memory.h
> b/platform/linux-generic/include/api/odp_shared_memory.h
> index 26e208b..f70db5a 100644
> --- a/platform/linux-generic/include/api/odp_shared_memory.h
> +++ b/platform/linux-generic/include/api/odp_shared_memory.h
> @@ -20,6 +20,7 @@ extern "C" {
>
>
>  #include <odp_std_types.h>
> +#include <odp_platform_types.h>
>
>  /** @defgroup odp_shared_memory ODP SHARED MEMORY
>   *  Operations on shared memory.
> @@ -38,15 +39,6 @@ extern "C" {
>  #define ODP_SHM_PROC    0x2 /**< Share with external processes */
>
>  /**
> - * ODP shared memory block
> - */
> -typedef uint32_t odp_shm_t;
> -
> -/** Invalid shared memory block */
> -#define ODP_SHM_INVALID 0
> -
> -
> -/**
>   * Shared memory block info
>   */
>  typedef struct odp_shm_info_t {
> diff --git a/platform/linux-generic/include/odp_buffer_pool_internal.h
> b/platform/linux-generic/include/odp_buffer_pool_internal.h
> index e0210bd..a1c0990 100644
> --- a/platform/linux-generic/include/odp_buffer_pool_internal.h
> +++ b/platform/linux-generic/include/odp_buffer_pool_internal.h
> @@ -56,6 +56,14 @@ struct pool_entry_s {
>         size_t                  buf_size;
>         size_t                  buf_offset;
>         uint64_t                num_bufs;
> +       odp_shm_t               pool_shm;
> +       union {
> +               uint32_t all;
> +               struct {
> +                       uint32_t has_name:1;
> +                       uint32_t user_supplied_shm:1;
> +               };
> +       } flags;
>         void                   *pool_base_addr;
>         uint64_t                pool_size;
>         size_t                  user_size;
> diff --git a/platform/linux-generic/odp_buffer_pool.c
> b/platform/linux-generic/odp_buffer_pool.c
> index 6a0a6b2..a599228 100644
> --- a/platform/linux-generic/odp_buffer_pool.c
> +++ b/platform/linux-generic/odp_buffer_pool.c
> @@ -369,16 +369,19 @@ static void link_bufs(pool_entry_t *pool)
>         }
>  }
>
> -
>  odp_buffer_pool_t odp_buffer_pool_create(const char *name,
> -                                        void *base_addr, uint64_t size,
> -                                        size_t buf_size, size_t buf_align,
> -                                        int buf_type)
> +                                        odp_shm_t shm,
> +                                        odp_buffer_pool_param_t *params)
>  {
>         odp_buffer_pool_t pool_hdl = ODP_BUFFER_POOL_INVALID;
>         pool_entry_t *pool;
> +       void *base_addr;
> +       size_t base_size;
>         uint32_t i;
>
> +       if (params == NULL)
> +               return ODP_BUFFER_POOL_INVALID;
> +
>         for (i = 0; i < ODP_CONFIG_BUFFER_POOLS; i++) {
>                 pool = get_pool_entry(i);
>
> @@ -386,15 +389,48 @@ odp_buffer_pool_t odp_buffer_pool_create(const char
> *name,
>
>                 if (pool->s.buf_base == 0) {
>                         /* found free pool */
> +                       pool->s.flags.all = 0;
> +
> +                       if (name == NULL) {
> +                               pool->s.name[0] = 0;
> +                       } else {
> +                               strncpy(pool->s.name, name,
> +                                       ODP_BUFFER_POOL_NAME_LEN - 1);
> +                               pool->s.name[ODP_BUFFER_POOL_NAME_LEN -
> 1] = 0;
> +                               pool->s.flags.has_name = 1;
> +                       }
> +
> +                       if (shm == ODP_SHM_NULL) {
> +                               base_size =
> +                                       ODP_PAGE_SIZE_ROUNDUP(
> +                                               params->buf_size *
> +                                               params->num_bufs);
> +                               shm = odp_shm_reserve(pool->s.name,
> +                                                     base_size,
> +                                                     ODP_PAGE_SIZE, 0);
> +                               if (shm == ODP_SHM_INVALID) {
> +                                       UNLOCK(&pool->s.lock);
> +                                       return ODP_BUFFER_INVALID;
> +                               }
> +                       } else {
> +                               odp_shm_info_t info;
> +                               if (odp_shm_info(shm, &info) != 0) {
> +                                       UNLOCK(&pool->s.lock);
> +                                       return ODP_BUFFER_POOL_INVALID;
> +                               }
> +                               base_size = info.size;
> +                               pool->s.flags.user_supplied_shm = 1;
> +                       }
> +
> +                       pool->s.pool_shm = shm;
> +                       base_addr = odp_shm_addr(shm);
>
> -                       strncpy(pool->s.name, name,
> -                               ODP_BUFFER_POOL_NAME_LEN - 1);
> -                       pool->s.name[ODP_BUFFER_POOL_NAME_LEN - 1] = 0;
>                         pool->s.pool_base_addr = base_addr;
> -                       pool->s.pool_size      = size;
> -                       pool->s.user_size      = buf_size;
> -                       pool->s.user_align     = buf_align;
> -                       pool->s.buf_type       = buf_type;
> +                       pool->s.pool_size      = base_size;
> +                       pool->s.user_size      = params->buf_size;
> +                       pool->s.user_align     = params->buf_align == 0 ?
> +                               ODP_CACHE_LINE_SIZE : params->buf_align;
> +                       pool->s.buf_type       = params->buf_type;
>
>                         link_bufs(pool);
>
> @@ -431,6 +467,46 @@ odp_buffer_pool_t odp_buffer_pool_lookup(const char
> *name)
>         return ODP_BUFFER_POOL_INVALID;
>  }
>
> +int odp_buffer_pool_info(odp_buffer_pool_t pool_hdl,
> +                        odp_shm_t *shm,
> +                        odp_buffer_pool_info_t *info)
> +{
> +       uint32_t pool_id = pool_handle_to_index(pool_hdl);
> +       pool_entry_t *pool = get_pool_entry(pool_id);
> +
> +       if (pool == NULL || info == NULL)
> +               return -1;
> +
> +       *shm = pool->s.flags.user_supplied_shm ?
> +               pool->s.pool_shm : ODP_SHM_NULL;
> +       info->name = pool->s.name;
> +       info->params.buf_size = pool->s.buf_size;
> +       info->params.buf_align = pool->s.user_align;
> +       info->params.num_bufs = pool->s.num_bufs;
> +       info->params.buf_type = pool->s.buf_type;
> +
> +       return 0;
> +}
> +
> +int odp_buffer_pool_destroy(odp_buffer_pool_t pool_hdl)
> +{
> +       uint32_t pool_id = pool_handle_to_index(pool_hdl);
> +       pool_entry_t *pool = get_pool_entry(pool_id);
> +
> +       if (pool == NULL)
> +               return -1;
> +
> +       LOCK(&pool->s.lock);
> +
> +       if (!pool->s.flags.user_supplied_shm)
> +               odp_shm_free(pool->s.pool_shm);
> +
> +       pool->s.buf_base = 0;
> +       UNLOCK(&pool->s.lock);
> +
> +       return 0;
> +}
> +
>
>  odp_buffer_t odp_buffer_alloc(odp_buffer_pool_t pool_hdl)
>  {
> diff --git a/platform/linux-generic/odp_schedule.c
> b/platform/linux-generic/odp_schedule.c
> index 1bf819b..85e249f 100644
> --- a/platform/linux-generic/odp_schedule.c
> +++ b/platform/linux-generic/odp_schedule.c
> @@ -83,8 +83,8 @@ int odp_schedule_init_global(void)
>  {
>         odp_shm_t shm;
>         odp_buffer_pool_t pool;
> -       void *pool_base;
>         int i, j;
> +       odp_buffer_pool_param_t params;
>
>         ODP_DBG("Schedule init ... ");
>
> @@ -99,20 +99,12 @@ int odp_schedule_init_global(void)
>                 return -1;
>         }
>
> -       shm = odp_shm_reserve("odp_sched_pool",
> -                             SCHED_POOL_SIZE, ODP_CACHE_LINE_SIZE, 0);
> +       params.buf_size  = sizeof(queue_desc_t);
> +       params.buf_align = ODP_CACHE_LINE_SIZE;
> +       params.num_bufs  = SCHED_POOL_SIZE/sizeof(queue_desc_t);
> +       params.buf_type  = ODP_BUFFER_TYPE_RAW;
>
> -       pool_base = odp_shm_addr(shm);
> -
> -       if (pool_base == NULL) {
> -               ODP_ERR("Schedule init: Shm reserve failed.\n");
> -               return -1;
> -       }
> -
> -       pool = odp_buffer_pool_create("odp_sched_pool", pool_base,
> -                                     SCHED_POOL_SIZE,
> sizeof(queue_desc_t),
> -                                     ODP_CACHE_LINE_SIZE,
> -                                     ODP_BUFFER_TYPE_RAW);
> +       pool = odp_buffer_pool_create("odp_sched_pool", ODP_SHM_NULL,
> &params);
>
>         if (pool == ODP_BUFFER_POOL_INVALID) {
>                 ODP_ERR("Schedule init: Pool create failed.\n");
> --
> 1.8.3.2
>
>
diff mbox

Patch

diff --git a/platform/linux-generic/include/api/odp_platform_types.h b/platform/linux-generic/include/api/odp_platform_types.h
index 4db47d3..b9b3aea 100644
--- a/platform/linux-generic/include/api/odp_platform_types.h
+++ b/platform/linux-generic/include/api/odp_platform_types.h
@@ -65,6 +65,15 @@  typedef uint32_t odp_pktio_t;
 #define ODP_PKTIO_ANY ((odp_pktio_t)~0)
 
 /**
+ * ODP shared memory block
+ */
+typedef uint32_t odp_shm_t;
+
+/** Invalid shared memory block */
+#define ODP_SHM_INVALID 0
+#define ODP_SHM_NULL ODP_SHM_INVALID /**< Synonym for buffer pool use */
+
+/**
  * @}
  */
 
diff --git a/platform/linux-generic/include/api/odp_shared_memory.h b/platform/linux-generic/include/api/odp_shared_memory.h
index 26e208b..f70db5a 100644
--- a/platform/linux-generic/include/api/odp_shared_memory.h
+++ b/platform/linux-generic/include/api/odp_shared_memory.h
@@ -20,6 +20,7 @@  extern "C" {
 
 
 #include <odp_std_types.h>
+#include <odp_platform_types.h>
 
 /** @defgroup odp_shared_memory ODP SHARED MEMORY
  *  Operations on shared memory.
@@ -38,15 +39,6 @@  extern "C" {
 #define ODP_SHM_PROC    0x2 /**< Share with external processes */
 
 /**
- * ODP shared memory block
- */
-typedef uint32_t odp_shm_t;
-
-/** Invalid shared memory block */
-#define ODP_SHM_INVALID 0
-
-
-/**
  * Shared memory block info
  */
 typedef struct odp_shm_info_t {
diff --git a/platform/linux-generic/include/odp_buffer_pool_internal.h b/platform/linux-generic/include/odp_buffer_pool_internal.h
index e0210bd..a1c0990 100644
--- a/platform/linux-generic/include/odp_buffer_pool_internal.h
+++ b/platform/linux-generic/include/odp_buffer_pool_internal.h
@@ -56,6 +56,14 @@  struct pool_entry_s {
 	size_t                  buf_size;
 	size_t                  buf_offset;
 	uint64_t                num_bufs;
+	odp_shm_t               pool_shm;
+	union {
+		uint32_t all;
+		struct {
+			uint32_t has_name:1;
+			uint32_t user_supplied_shm:1;
+		};
+	} flags;
 	void                   *pool_base_addr;
 	uint64_t                pool_size;
 	size_t                  user_size;
diff --git a/platform/linux-generic/odp_buffer_pool.c b/platform/linux-generic/odp_buffer_pool.c
index 6a0a6b2..a599228 100644
--- a/platform/linux-generic/odp_buffer_pool.c
+++ b/platform/linux-generic/odp_buffer_pool.c
@@ -369,16 +369,19 @@  static void link_bufs(pool_entry_t *pool)
 	}
 }
 
-
 odp_buffer_pool_t odp_buffer_pool_create(const char *name,
-					 void *base_addr, uint64_t size,
-					 size_t buf_size, size_t buf_align,
-					 int buf_type)
+					 odp_shm_t shm,
+					 odp_buffer_pool_param_t *params)
 {
 	odp_buffer_pool_t pool_hdl = ODP_BUFFER_POOL_INVALID;
 	pool_entry_t *pool;
+	void *base_addr;
+	size_t base_size;
 	uint32_t i;
 
+	if (params == NULL)
+		return ODP_BUFFER_POOL_INVALID;
+
 	for (i = 0; i < ODP_CONFIG_BUFFER_POOLS; i++) {
 		pool = get_pool_entry(i);
 
@@ -386,15 +389,48 @@  odp_buffer_pool_t odp_buffer_pool_create(const char *name,
 
 		if (pool->s.buf_base == 0) {
 			/* found free pool */
+			pool->s.flags.all = 0;
+
+			if (name == NULL) {
+				pool->s.name[0] = 0;
+			} else {
+				strncpy(pool->s.name, name,
+					ODP_BUFFER_POOL_NAME_LEN - 1);
+				pool->s.name[ODP_BUFFER_POOL_NAME_LEN - 1] = 0;
+				pool->s.flags.has_name = 1;
+			}
+
+			if (shm == ODP_SHM_NULL) {
+				base_size =
+					ODP_PAGE_SIZE_ROUNDUP(
+						params->buf_size *
+						params->num_bufs);
+				shm = odp_shm_reserve(pool->s.name,
+						      base_size,
+						      ODP_PAGE_SIZE, 0);
+				if (shm == ODP_SHM_INVALID) {
+					UNLOCK(&pool->s.lock);
+					return ODP_BUFFER_INVALID;
+				}
+			} else {
+				odp_shm_info_t info;
+				if (odp_shm_info(shm, &info) != 0) {
+					UNLOCK(&pool->s.lock);
+					return ODP_BUFFER_POOL_INVALID;
+				}
+				base_size = info.size;
+				pool->s.flags.user_supplied_shm = 1;
+			}
+
+			pool->s.pool_shm = shm;
+			base_addr = odp_shm_addr(shm);
 
-			strncpy(pool->s.name, name,
-				ODP_BUFFER_POOL_NAME_LEN - 1);
-			pool->s.name[ODP_BUFFER_POOL_NAME_LEN - 1] = 0;
 			pool->s.pool_base_addr = base_addr;
-			pool->s.pool_size      = size;
-			pool->s.user_size      = buf_size;
-			pool->s.user_align     = buf_align;
-			pool->s.buf_type       = buf_type;
+			pool->s.pool_size      = base_size;
+			pool->s.user_size      = params->buf_size;
+			pool->s.user_align     = params->buf_align == 0 ?
+				ODP_CACHE_LINE_SIZE : params->buf_align;
+			pool->s.buf_type       = params->buf_type;
 
 			link_bufs(pool);
 
@@ -431,6 +467,46 @@  odp_buffer_pool_t odp_buffer_pool_lookup(const char *name)
 	return ODP_BUFFER_POOL_INVALID;
 }
 
+int odp_buffer_pool_info(odp_buffer_pool_t pool_hdl,
+			 odp_shm_t *shm,
+			 odp_buffer_pool_info_t *info)
+{
+	uint32_t pool_id = pool_handle_to_index(pool_hdl);
+	pool_entry_t *pool = get_pool_entry(pool_id);
+
+	if (pool == NULL || info == NULL)
+		return -1;
+
+	*shm = pool->s.flags.user_supplied_shm ?
+		pool->s.pool_shm : ODP_SHM_NULL;
+	info->name = pool->s.name;
+	info->params.buf_size = pool->s.buf_size;
+	info->params.buf_align = pool->s.user_align;
+	info->params.num_bufs = pool->s.num_bufs;
+	info->params.buf_type = pool->s.buf_type;
+
+	return 0;
+}
+
+int odp_buffer_pool_destroy(odp_buffer_pool_t pool_hdl)
+{
+	uint32_t pool_id = pool_handle_to_index(pool_hdl);
+	pool_entry_t *pool = get_pool_entry(pool_id);
+
+	if (pool == NULL)
+		return -1;
+
+	LOCK(&pool->s.lock);
+
+	if (!pool->s.flags.user_supplied_shm)
+		odp_shm_free(pool->s.pool_shm);
+
+	pool->s.buf_base = 0;
+	UNLOCK(&pool->s.lock);
+
+	return 0;
+}
+
 
 odp_buffer_t odp_buffer_alloc(odp_buffer_pool_t pool_hdl)
 {
diff --git a/platform/linux-generic/odp_schedule.c b/platform/linux-generic/odp_schedule.c
index 1bf819b..85e249f 100644
--- a/platform/linux-generic/odp_schedule.c
+++ b/platform/linux-generic/odp_schedule.c
@@ -83,8 +83,8 @@  int odp_schedule_init_global(void)
 {
 	odp_shm_t shm;
 	odp_buffer_pool_t pool;
-	void *pool_base;
 	int i, j;
+	odp_buffer_pool_param_t params;
 
 	ODP_DBG("Schedule init ... ");
 
@@ -99,20 +99,12 @@  int odp_schedule_init_global(void)
 		return -1;
 	}
 
-	shm = odp_shm_reserve("odp_sched_pool",
-			      SCHED_POOL_SIZE, ODP_CACHE_LINE_SIZE, 0);
+	params.buf_size  = sizeof(queue_desc_t);
+	params.buf_align = ODP_CACHE_LINE_SIZE;
+	params.num_bufs  = SCHED_POOL_SIZE/sizeof(queue_desc_t);
+	params.buf_type  = ODP_BUFFER_TYPE_RAW;
 
-	pool_base = odp_shm_addr(shm);
-
-	if (pool_base == NULL) {
-		ODP_ERR("Schedule init: Shm reserve failed.\n");
-		return -1;
-	}
-
-	pool = odp_buffer_pool_create("odp_sched_pool", pool_base,
-				      SCHED_POOL_SIZE, sizeof(queue_desc_t),
-				      ODP_CACHE_LINE_SIZE,
-				      ODP_BUFFER_TYPE_RAW);
+	pool = odp_buffer_pool_create("odp_sched_pool", ODP_SHM_NULL, &params);
 
 	if (pool == ODP_BUFFER_POOL_INVALID) {
 		ODP_ERR("Schedule init: Pool create failed.\n");