diff mbox

add implement for odp_shm_free

Message ID 1416317970-6588-1-git-send-email-yan.songming@linaro.org
State New
Headers show

Commit Message

yan.songming Nov. 18, 2014, 1:39 p.m. UTC
From: Yan Sonming <yan.songming@linaro.com>

New API implementing odp_shm_free to match the odp_shm_reserve.

Signed-off-by: Yan Songming <yan.songming@linaro.org>
---
 platform/linux-generic/odp_shared_memory.c | 42 +++++++++++++++++++++++++-----
 1 file changed, 36 insertions(+), 6 deletions(-)

Comments

Maxim Uvarov Nov. 19, 2014, 12:20 p.m. UTC | #1
On 11/18/2014 04:39 PM, Yan Songming wrote:
> From: Yan Sonming <yan.songming@linaro.com>
>
> New API implementing odp_shm_free to match the odp_shm_reserve.
>
> Signed-off-by: Yan Songming <yan.songming@linaro.org>
> ---
>   platform/linux-generic/odp_shared_memory.c | 42 +++++++++++++++++++++++++-----
>   1 file changed, 36 insertions(+), 6 deletions(-)
>
> diff --git a/platform/linux-generic/odp_shared_memory.c b/platform/linux-generic/odp_shared_memory.c
> index 24a5d60..fd8f698 100644
> --- a/platform/linux-generic/odp_shared_memory.c
> +++ b/platform/linux-generic/odp_shared_memory.c
> @@ -114,6 +114,42 @@ static int find_block(const char *name, uint32_t *index)
>   	return 0;
>   }
>   
> +int odp_shm_free(odp_shm_t shm)
> +{
> +	uint32_t i;
> +	int ret;
> +	odp_shm_block_t *shm_block;
> +	i = from_handle(shm);
> +	uint64_t alloc_size;

please put variables on top and split variables and code with empty line .

Maxim.

> +	if (odp_shm_tbl->block[i].addr == NULL) {
> +		/* free block */
> +		ODP_DBG("odp_shm_free: Free block failed\n");
> +		return 0;
> +	}
> +
> +	odp_spinlock_lock(&odp_shm_tbl->lock);
> +	shm_block = &odp_shm_tbl->block[i];
> +
> +	alloc_size = shm_block->size + shm_block->align;
> +	ret = munmap(shm_block->addr, alloc_size);
> +	if (0 != ret) {
> +		ODP_DBG("odp_shm_free: munmap failed\n");
> +		odp_spinlock_unlock(&odp_shm_tbl->lock);
> +		return -1;
> +	}
> +
> +	if (shm_block->flags & ODP_SHM_PROC) {
> +		ret = shm_unlink(shm_block->name);
> +		if (0 != ret) {
> +			ODP_DBG("odp_shm_free: shm_unlink failed\n");
> +			odp_spinlock_unlock(&odp_shm_tbl->lock);
> +			return -1;
> +		}
> +	}
> +	memset(&odp_shm_tbl->block[i], 0, sizeof(odp_shm_block_t));
> +	odp_spinlock_unlock(&odp_shm_tbl->lock);
> +	return 0;
> +}
>   
>   odp_shm_t odp_shm_reserve(const char *name, uint64_t size, uint64_t align,
>   			  uint32_t flags)
> @@ -221,12 +257,6 @@ odp_shm_t odp_shm_reserve(const char *name, uint64_t size, uint64_t align,
>   	return block->hdl;
>   }
>   
> -int odp_shm_free(odp_shm_t shm ODP_UNUSED)
> -{
> -	ODP_UNIMPLEMENTED();
> -	return 0;
> -}
> -
>   odp_shm_t odp_shm_lookup(const char *name)
>   {
>   	uint32_t i;
diff mbox

Patch

diff --git a/platform/linux-generic/odp_shared_memory.c b/platform/linux-generic/odp_shared_memory.c
index 24a5d60..fd8f698 100644
--- a/platform/linux-generic/odp_shared_memory.c
+++ b/platform/linux-generic/odp_shared_memory.c
@@ -114,6 +114,42 @@  static int find_block(const char *name, uint32_t *index)
 	return 0;
 }
 
+int odp_shm_free(odp_shm_t shm)
+{
+	uint32_t i;
+	int ret;
+	odp_shm_block_t *shm_block;
+	i = from_handle(shm);
+	uint64_t alloc_size;
+	if (odp_shm_tbl->block[i].addr == NULL) {
+		/* free block */
+		ODP_DBG("odp_shm_free: Free block failed\n");
+		return 0;
+	}
+
+	odp_spinlock_lock(&odp_shm_tbl->lock);
+	shm_block = &odp_shm_tbl->block[i];
+
+	alloc_size = shm_block->size + shm_block->align;
+	ret = munmap(shm_block->addr, alloc_size);
+	if (0 != ret) {
+		ODP_DBG("odp_shm_free: munmap failed\n");
+		odp_spinlock_unlock(&odp_shm_tbl->lock);
+		return -1;
+	}
+
+	if (shm_block->flags & ODP_SHM_PROC) {
+		ret = shm_unlink(shm_block->name);
+		if (0 != ret) {
+			ODP_DBG("odp_shm_free: shm_unlink failed\n");
+			odp_spinlock_unlock(&odp_shm_tbl->lock);
+			return -1;
+		}
+	}
+	memset(&odp_shm_tbl->block[i], 0, sizeof(odp_shm_block_t));
+	odp_spinlock_unlock(&odp_shm_tbl->lock);
+	return 0;
+}
 
 odp_shm_t odp_shm_reserve(const char *name, uint64_t size, uint64_t align,
 			  uint32_t flags)
@@ -221,12 +257,6 @@  odp_shm_t odp_shm_reserve(const char *name, uint64_t size, uint64_t align,
 	return block->hdl;
 }
 
-int odp_shm_free(odp_shm_t shm ODP_UNUSED)
-{
-	ODP_UNIMPLEMENTED();
-	return 0;
-}
-
 odp_shm_t odp_shm_lookup(const char *name)
 {
 	uint32_t i;