Re: [lng-odp] [PATCHv4 2/5] helper: table: add impl of hashtable

2015-12-08 Thread Ola Liljedahl
On 3 December 2015 at 14:14, Ivan Khoronzhuk 
wrote:

> It's in master already but I wonder how checkpatch missed it.
>
> On 05.11.15 13:20, huanggaoyang wrote:
>
>> Signed-off-by: huanggaoyang 
>> ---
>>   helper/hashtable.c  | 346
>> 
>>   helper/odph_hashtable.h |  42 ++
>>   helper/odph_list_internal.h |  85 +++
>>   3 files changed, 473 insertions(+)
>>   create mode 100644 helper/hashtable.c
>>   create mode 100644 helper/odph_hashtable.h
>>   create mode 100644 helper/odph_list_internal.h
>>
>> diff --git a/helper/hashtable.c b/helper/hashtable.c
>> new file mode 100644
>> index 000..0b29814
>> --- /dev/null
>> +++ b/helper/hashtable.c
>> @@ -0,0 +1,346 @@
>> +/* Copyright (c) 2015, Linaro Limited
>> + * All rights reserved.
>> + *
>> + * SPDX-License-Identifier:   BSD-3-Clause
>> + */
>> +#include 
>> +#include 
>> +#include 
>> +
>> +#include "odph_hashtable.h"
>> +#include "odph_list_internal.h"
>> +#include "odph_debug.h"
>> +#include 
>> +
>> +#defineODPH_SUCCESS0
>> +#defineODPH_FAIL   -1
>> +
>> +/** @magic word, write to the first byte of the memory block
>> + * to indicate this block is used by a hash table structure
>> + */
>> +#defineODPH_HASH_TABLE_MAGIC_WORD  0xABABBABA
>>
> tabs after define, how it was missed?
>
And surely the value should be 0xABBAABBA!?!


>
> +
>> +/** @support 64k buckets. Bucket is a list that composed of
>> + * elements with the same HASH-value but different keys
>> + */
>> +#defineODPH_MAX_BUCKET_NUM 0x1
>>
> tabs after define, how it was missed?
>
> +
>> +/** @inner element structure of hash table
>> + * To resolve the hash confict:
>> + * we put the elements with different keys but a same HASH-value
>> + * into a list
>> + */
>> +typedef struct odph_hash_node {
>> +   /** list structure,for list opt */
>> +   odph_list_object list_node;
>> +   /** Flexible Array,memory will be alloced when table has been
>> created
>> +* Its length is key_size + value_size,
>> +* suppose key_size = m; value_size = n;
>> +* its structure is like:
>> +* k_byte1 k_byte2...k_byten v_byte1...v_bytem
>> +*/
>> +   char content[0];
>> +} odph_hash_node;
>> +
>> +typedef struct {
>> +   uint32_t magicword; /**< for check */
>> +   uint32_t key_size; /**< input param when create,in Bytes */
>> +   uint32_t value_size; /**< input param when create,in Bytes */
>> +   uint32_t init_cap; /**< input param when create,in Bytes */
>> +   /** multi-process support,every list has one rw lock */
>> +   odp_rwlock_t *lock_pool;
>> +   /** table bucket pool,every hash value has one list head */
>> +   odph_list_head *list_head_pool;
>> +   /** number of the list head in list_head_pool */
>> +   uint32_t head_num;
>> +   /** table element pool */
>> +   odph_hash_node *hash_node_pool;
>> +   /** number of element in the hash_node_pool */
>> +   uint32_t hash_node_num;
>> +   char rsv[7]; /**< Reserved,for alignment */
>> +   char name[ODPH_TABLE_NAME_LEN]; /**< table name */
>> +} odph_hash_table_imp;
>> +
>> +odph_table_t odph_hash_table_create(const char *name, uint32_t capacity,
>> +   uint32_t key_size,
>> +   uint32_t value_size)
>> +{
>> +   int idx, i;
>> +   uint32_t node_num;
>> +   odph_hash_table_imp *tbl;
>> +   odp_shm_t shmem;
>> +   uint32_t node_mem;
>> +
>> +   if (strlen(name) >= ODPH_TABLE_NAME_LEN || capacity < 1 ||
>> +   capacity >= 0x1000 || key_size == 0 || value_size == 0) {
>> +   ODPH_DBG("create para input error!\n");
>> +   return NULL;
>> +   }
>> +   tbl = (odph_hash_table_imp *)odp_shm_addr(odp_shm_lookup(name));
>> +   if (tbl != NULL) {
>> +   ODPH_DBG("name already exist\n");
>> +   return NULL;
>> +   }
>> +   shmem = odp_shm_reserve(name, capacity << 20, 64,
>> ODP_SHM_SW_ONLY);
>> +   if (shmem == ODP_SHM_INVALID) {
>> +   ODPH_DBG("shm reserve fail\n");
>> +   return NULL;
>> +   }
>> +   tbl = (odph_hash_table_imp *)odp_shm_addr(shmem);
>> +
>> +   /* clean this block of memory */
>> +   memset(tbl, 0, capacity << 20);
>> +
>> +   tbl->init_cap = capacity << 20;
>> +   strncpy(tbl->name, name, ODPH_TABLE_NAME_LEN);
>> +   tbl->key_size = key_size;
>> +   tbl->value_size = value_size;
>> +
>> +   /* header of this mem block is the table control struct,
>> +* then the lock pool, then the list header pool
>> +* the last part is the element node pool
>> +*/
>> +
>> +   tbl->lock_pool = (odp_rwlock_t *)((char *)tbl
>> +   + sizeof(odph_hash_table_imp));
>> +   tbl->list_head_pool = (odph_list_head 

Re: [lng-odp] [PATCHv4 2/5] helper: table: add impl of hashtable

2015-12-08 Thread Stuart Haslam
On Thu, Dec 03, 2015 at 08:39:26AM -0500, Mike Holmes wrote:
> It does miss it apparently - just shows you we still need a human eye
> 

I have this in my ~/.gitconfig

[core]
whitespace = trailing-space,space-before-tab,indent-with-non-tab

and it warns about it:

Applying: helper: table: add impl of hashtable
odp/.git/rebase-apply/patch:361: new blank line at EOF.
+
odp/.git/rebase-apply/patch:409: new blank line at EOF.
+
odp/.git/rebase-apply/patch:500: new blank line at EOF.
+
warning: 3 lines add whitespace errors.

-- 
Stuart.

> mike@mike-desktop:~/git/odp$ git format-patch -1 a775afd
> 0001-helper-table-add-impl-of-hashtable.patch
> mike@mike-desktop:~/git/odp$ ./scripts/checkpatch.pl
> 0001-helper-table-add-impl-of-hashtable.patch
> total: 0 errors, 0 warnings, 0 checks, 473 lines checked
> 
> NOTE: Ignored message types: COMPARISON_TO_NULL DEPRECATED_VARIABLE
> NEW_TYPEDEFS SPLIT_STRING
> 
> 0001-helper-table-add-impl-of-hashtable.patch has no obvious style problems
> and is ready for submission.
> 
> 
> On 3 December 2015 at 08:16, Ivan Khoronzhuk 
> wrote:
> 
> >
> >
> > On 05.11.15 13:20, huanggaoyang wrote:
> >
> >> Signed-off-by: huanggaoyang 
> >> ---
> >>   helper/hashtable.c  | 346
> >> 
> >>   helper/odph_hashtable.h |  42 ++
> >>   helper/odph_list_internal.h |  85 +++
> >>   3 files changed, 473 insertions(+)
> >>   create mode 100644 helper/hashtable.c
> >>   create mode 100644 helper/odph_hashtable.h
> >>   create mode 100644 helper/odph_list_internal.h
> >>
> >> diff --git a/helper/hashtable.c b/helper/hashtable.c
> >> new file mode 100644
> >> index 000..0b29814
> >> --- /dev/null
> >> +++ b/helper/hashtable.c
> >> @@ -0,0 +1,346 @@
> >> +/* Copyright (c) 2015, Linaro Limited
> >> + * All rights reserved.
> >> + *
> >> + * SPDX-License-Identifier:   BSD-3-Clause
> >> + */
> >> +#include 
> >> +#include 
> >> +#include 
> >> +
> >> +#include "odph_hashtable.h"
> >> +#include "odph_list_internal.h"
> >> +#include "odph_debug.h"
> >> +#include 
> >> +
> >> +#defineODPH_SUCCESS0
> >> +#defineODPH_FAIL   -1
> >> +
> >>
> > .
> >
> >> +odph_table_ops_t odph_hash_table_ops = {
> >> +   odph_hash_table_create,
> >> +   odph_hash_table_lookup,
> >> +   odph_hash_table_destroy,
> >> +   odph_hash_put_value,
> >> +   odph_hash_get_value,
> >> +   odph_hash_remove_value};
> >> +
> >>
> >
> > 
> > Blank line at the end of file.
> > Need to add check in checkpatch.
> >
> > --
> > Regards,
> > Ivan Khoronzhuk
> > ___
> > lng-odp mailing list
> > lng-odp@lists.linaro.org
> > https://lists.linaro.org/mailman/listinfo/lng-odp
> >
> 
> 
> 
> -- 
> Mike Holmes
> Technical Manager - Linaro Networking Group
> Linaro.org  *│ *Open source software for ARM SoCs

___
lng-odp mailing list
lng-odp@lists.linaro.org
https://lists.linaro.org/mailman/listinfo/lng-odp


Re: [lng-odp] [PATCHv4 2/5] helper: table: add impl of hashtable

2015-12-08 Thread Mike Holmes
On 8 December 2015 at 10:49, Stuart Haslam  wrote:

> On Thu, Dec 03, 2015 at 08:39:26AM -0500, Mike Holmes wrote:
> > It does miss it apparently - just shows you we still need a human eye
> >
>
> I have this in my ~/.gitconfig
> [core]
> whitespace = trailing-space,space-before-tab,indent-with-non-tab
>
I hadwhitespace = trailing-space,sapce-before-tab,error-all

Thanks for pointer I had typo and a missing setting, I will send a patch to
the contributing section for the settings once I check what we want in a
little more detail

>
> and it warns about it:
>
> Applying: helper: table: add impl of hashtable
> odp/.git/rebase-apply/patch:361: new blank line at EOF.
> +
> odp/.git/rebase-apply/patch:409: new blank line at EOF.
> +
> odp/.git/rebase-apply/patch:500: new blank line at EOF.
> +
> warning: 3 lines add whitespace errors.
>
> --
> Stuart.
>
> > mike@mike-desktop:~/git/odp$ git format-patch -1 a775afd
> > 0001-helper-table-add-impl-of-hashtable.patch
> > mike@mike-desktop:~/git/odp$ ./scripts/checkpatch.pl
> > 0001-helper-table-add-impl-of-hashtable.patch
> > total: 0 errors, 0 warnings, 0 checks, 473 lines checked
> >
> > NOTE: Ignored message types: COMPARISON_TO_NULL DEPRECATED_VARIABLE
> > NEW_TYPEDEFS SPLIT_STRING
> >
> > 0001-helper-table-add-impl-of-hashtable.patch has no obvious style
> problems
> > and is ready for submission.
> >
> >
> > On 3 December 2015 at 08:16, Ivan Khoronzhuk  >
> > wrote:
> >
> > >
> > >
> > > On 05.11.15 13:20, huanggaoyang wrote:
> > >
> > >> Signed-off-by: huanggaoyang 
> > >> ---
> > >>   helper/hashtable.c  | 346
> > >> 
> > >>   helper/odph_hashtable.h |  42 ++
> > >>   helper/odph_list_internal.h |  85 +++
> > >>   3 files changed, 473 insertions(+)
> > >>   create mode 100644 helper/hashtable.c
> > >>   create mode 100644 helper/odph_hashtable.h
> > >>   create mode 100644 helper/odph_list_internal.h
> > >>
> > >> diff --git a/helper/hashtable.c b/helper/hashtable.c
> > >> new file mode 100644
> > >> index 000..0b29814
> > >> --- /dev/null
> > >> +++ b/helper/hashtable.c
> > >> @@ -0,0 +1,346 @@
> > >> +/* Copyright (c) 2015, Linaro Limited
> > >> + * All rights reserved.
> > >> + *
> > >> + * SPDX-License-Identifier:   BSD-3-Clause
> > >> + */
> > >> +#include 
> > >> +#include 
> > >> +#include 
> > >> +
> > >> +#include "odph_hashtable.h"
> > >> +#include "odph_list_internal.h"
> > >> +#include "odph_debug.h"
> > >> +#include 
> > >> +
> > >> +#defineODPH_SUCCESS0
> > >> +#defineODPH_FAIL   -1
> > >> +
> > >>
> > > .
> > >
> > >> +odph_table_ops_t odph_hash_table_ops = {
> > >> +   odph_hash_table_create,
> > >> +   odph_hash_table_lookup,
> > >> +   odph_hash_table_destroy,
> > >> +   odph_hash_put_value,
> > >> +   odph_hash_get_value,
> > >> +   odph_hash_remove_value};
> > >> +
> > >>
> > >
> > > 
> > > Blank line at the end of file.
> > > Need to add check in checkpatch.
> > >
> > > --
> > > Regards,
> > > Ivan Khoronzhuk
> > > ___
> > > lng-odp mailing list
> > > lng-odp@lists.linaro.org
> > > https://lists.linaro.org/mailman/listinfo/lng-odp
> > >
> >
> >
> >
> > --
> > Mike Holmes
> > Technical Manager - Linaro Networking Group
> > Linaro.org  *│ *Open source software for ARM
> SoCs
>
>


-- 
Mike Holmes
Technical Manager - Linaro Networking Group
Linaro.org  *│ *Open source software for ARM SoCs
___
lng-odp mailing list
lng-odp@lists.linaro.org
https://lists.linaro.org/mailman/listinfo/lng-odp


Re: [lng-odp] [PATCHv4 2/5] helper: table: add impl of hashtable

2015-12-03 Thread Ivan Khoronzhuk



On 05.11.15 13:20, huanggaoyang wrote:

Signed-off-by: huanggaoyang 
---
  helper/hashtable.c  | 346 
  helper/odph_hashtable.h |  42 ++
  helper/odph_list_internal.h |  85 +++
  3 files changed, 473 insertions(+)
  create mode 100644 helper/hashtable.c
  create mode 100644 helper/odph_hashtable.h
  create mode 100644 helper/odph_list_internal.h

diff --git a/helper/hashtable.c b/helper/hashtable.c
new file mode 100644
index 000..0b29814
--- /dev/null
+++ b/helper/hashtable.c
@@ -0,0 +1,346 @@
+/* Copyright (c) 2015, Linaro Limited
+ * All rights reserved.
+ *
+ * SPDX-License-Identifier:   BSD-3-Clause
+ */
+#include 
+#include 
+#include 
+
+#include "odph_hashtable.h"
+#include "odph_list_internal.h"
+#include "odph_debug.h"
+#include 
+
+#defineODPH_SUCCESS0
+#defineODPH_FAIL   -1
+

.

+odph_table_ops_t odph_hash_table_ops = {
+   odph_hash_table_create,
+   odph_hash_table_lookup,
+   odph_hash_table_destroy,
+   odph_hash_put_value,
+   odph_hash_get_value,
+   odph_hash_remove_value};
+



Blank line at the end of file.
Need to add check in checkpatch.

--
Regards,
Ivan Khoronzhuk
___
lng-odp mailing list
lng-odp@lists.linaro.org
https://lists.linaro.org/mailman/listinfo/lng-odp


Re: [lng-odp] [PATCHv4 2/5] helper: table: add impl of hashtable

2015-12-03 Thread Ivan Khoronzhuk

It's in master already but I wonder how checkpatch missed it.

On 05.11.15 13:20, huanggaoyang wrote:

Signed-off-by: huanggaoyang 
---
  helper/hashtable.c  | 346 
  helper/odph_hashtable.h |  42 ++
  helper/odph_list_internal.h |  85 +++
  3 files changed, 473 insertions(+)
  create mode 100644 helper/hashtable.c
  create mode 100644 helper/odph_hashtable.h
  create mode 100644 helper/odph_list_internal.h

diff --git a/helper/hashtable.c b/helper/hashtable.c
new file mode 100644
index 000..0b29814
--- /dev/null
+++ b/helper/hashtable.c
@@ -0,0 +1,346 @@
+/* Copyright (c) 2015, Linaro Limited
+ * All rights reserved.
+ *
+ * SPDX-License-Identifier:   BSD-3-Clause
+ */
+#include 
+#include 
+#include 
+
+#include "odph_hashtable.h"
+#include "odph_list_internal.h"
+#include "odph_debug.h"
+#include 
+
+#defineODPH_SUCCESS0
+#defineODPH_FAIL   -1
+
+/** @magic word, write to the first byte of the memory block
+ * to indicate this block is used by a hash table structure
+ */
+#defineODPH_HASH_TABLE_MAGIC_WORD  0xABABBABA

tabs after define, how it was missed?


+
+/** @support 64k buckets. Bucket is a list that composed of
+ * elements with the same HASH-value but different keys
+ */
+#defineODPH_MAX_BUCKET_NUM 0x1

tabs after define, how it was missed?


+
+/** @inner element structure of hash table
+ * To resolve the hash confict:
+ * we put the elements with different keys but a same HASH-value
+ * into a list
+ */
+typedef struct odph_hash_node {
+   /** list structure,for list opt */
+   odph_list_object list_node;
+   /** Flexible Array,memory will be alloced when table has been created
+* Its length is key_size + value_size,
+* suppose key_size = m; value_size = n;
+* its structure is like:
+* k_byte1 k_byte2...k_byten v_byte1...v_bytem
+*/
+   char content[0];
+} odph_hash_node;
+
+typedef struct {
+   uint32_t magicword; /**< for check */
+   uint32_t key_size; /**< input param when create,in Bytes */
+   uint32_t value_size; /**< input param when create,in Bytes */
+   uint32_t init_cap; /**< input param when create,in Bytes */
+   /** multi-process support,every list has one rw lock */
+   odp_rwlock_t *lock_pool;
+   /** table bucket pool,every hash value has one list head */
+   odph_list_head *list_head_pool;
+   /** number of the list head in list_head_pool */
+   uint32_t head_num;
+   /** table element pool */
+   odph_hash_node *hash_node_pool;
+   /** number of element in the hash_node_pool */
+   uint32_t hash_node_num;
+   char rsv[7]; /**< Reserved,for alignment */
+   char name[ODPH_TABLE_NAME_LEN]; /**< table name */
+} odph_hash_table_imp;
+
+odph_table_t odph_hash_table_create(const char *name, uint32_t capacity,
+   uint32_t key_size,
+   uint32_t value_size)
+{
+   int idx, i;
+   uint32_t node_num;
+   odph_hash_table_imp *tbl;
+   odp_shm_t shmem;
+   uint32_t node_mem;
+
+   if (strlen(name) >= ODPH_TABLE_NAME_LEN || capacity < 1 ||
+   capacity >= 0x1000 || key_size == 0 || value_size == 0) {
+   ODPH_DBG("create para input error!\n");
+   return NULL;
+   }
+   tbl = (odph_hash_table_imp *)odp_shm_addr(odp_shm_lookup(name));
+   if (tbl != NULL) {
+   ODPH_DBG("name already exist\n");
+   return NULL;
+   }
+   shmem = odp_shm_reserve(name, capacity << 20, 64, ODP_SHM_SW_ONLY);
+   if (shmem == ODP_SHM_INVALID) {
+   ODPH_DBG("shm reserve fail\n");
+   return NULL;
+   }
+   tbl = (odph_hash_table_imp *)odp_shm_addr(shmem);
+
+   /* clean this block of memory */
+   memset(tbl, 0, capacity << 20);
+
+   tbl->init_cap = capacity << 20;
+   strncpy(tbl->name, name, ODPH_TABLE_NAME_LEN);
+   tbl->key_size = key_size;
+   tbl->value_size = value_size;
+
+   /* header of this mem block is the table control struct,
+* then the lock pool, then the list header pool
+* the last part is the element node pool
+*/
+
+   tbl->lock_pool = (odp_rwlock_t *)((char *)tbl
+   + sizeof(odph_hash_table_imp));
+   tbl->list_head_pool = (odph_list_head *)((char *)tbl->lock_pool
+   + ODPH_MAX_BUCKET_NUM * sizeof(odp_rwlock_t));
+
+   node_mem = tbl->init_cap - sizeof(odph_hash_table_imp)
+   - ODPH_MAX_BUCKET_NUM * sizeof(odph_list_head)
+   - ODPH_MAX_BUCKET_NUM * sizeof(odp_rwlock_t);
+
+   node_num = node_mem / (sizeof(odph_hash_node) + key_size + value_size);
+   tbl->hash_node_num = node_num;
+   tbl->hash_node_pool = (odph_hash_node *)((char *)tbl->list_head_pool
+   

Re: [lng-odp] [PATCHv4 2/5] helper: table: add impl of hashtable

2015-12-03 Thread Mike Holmes
It does miss it apparently - just shows you we still need a human eye

mike@mike-desktop:~/git/odp$ git format-patch -1 a775afd
0001-helper-table-add-impl-of-hashtable.patch
mike@mike-desktop:~/git/odp$ ./scripts/checkpatch.pl
0001-helper-table-add-impl-of-hashtable.patch
total: 0 errors, 0 warnings, 0 checks, 473 lines checked

NOTE: Ignored message types: COMPARISON_TO_NULL DEPRECATED_VARIABLE
NEW_TYPEDEFS SPLIT_STRING

0001-helper-table-add-impl-of-hashtable.patch has no obvious style problems
and is ready for submission.


On 3 December 2015 at 08:16, Ivan Khoronzhuk 
wrote:

>
>
> On 05.11.15 13:20, huanggaoyang wrote:
>
>> Signed-off-by: huanggaoyang 
>> ---
>>   helper/hashtable.c  | 346
>> 
>>   helper/odph_hashtable.h |  42 ++
>>   helper/odph_list_internal.h |  85 +++
>>   3 files changed, 473 insertions(+)
>>   create mode 100644 helper/hashtable.c
>>   create mode 100644 helper/odph_hashtable.h
>>   create mode 100644 helper/odph_list_internal.h
>>
>> diff --git a/helper/hashtable.c b/helper/hashtable.c
>> new file mode 100644
>> index 000..0b29814
>> --- /dev/null
>> +++ b/helper/hashtable.c
>> @@ -0,0 +1,346 @@
>> +/* Copyright (c) 2015, Linaro Limited
>> + * All rights reserved.
>> + *
>> + * SPDX-License-Identifier:   BSD-3-Clause
>> + */
>> +#include 
>> +#include 
>> +#include 
>> +
>> +#include "odph_hashtable.h"
>> +#include "odph_list_internal.h"
>> +#include "odph_debug.h"
>> +#include 
>> +
>> +#defineODPH_SUCCESS0
>> +#defineODPH_FAIL   -1
>> +
>>
> .
>
>> +odph_table_ops_t odph_hash_table_ops = {
>> +   odph_hash_table_create,
>> +   odph_hash_table_lookup,
>> +   odph_hash_table_destroy,
>> +   odph_hash_put_value,
>> +   odph_hash_get_value,
>> +   odph_hash_remove_value};
>> +
>>
>
> 
> Blank line at the end of file.
> Need to add check in checkpatch.
>
> --
> Regards,
> Ivan Khoronzhuk
> ___
> lng-odp mailing list
> lng-odp@lists.linaro.org
> https://lists.linaro.org/mailman/listinfo/lng-odp
>



-- 
Mike Holmes
Technical Manager - Linaro Networking Group
Linaro.org  *│ *Open source software for ARM SoCs
___
lng-odp mailing list
lng-odp@lists.linaro.org
https://lists.linaro.org/mailman/listinfo/lng-odp


Re: [lng-odp] [PATCHv4 2/5] helper: table: add impl of hashtable

2015-12-01 Thread Ivan Khoronzhuk


On 05.11.15 13:20, huanggaoyang wrote:

Signed-off-by: huanggaoyang 
---
  helper/hashtable.c  | 346 
  helper/odph_hashtable.h |  42 ++
  helper/odph_list_internal.h |  85 +++
  3 files changed, 473 insertions(+)
  create mode 100644 helper/hashtable.c
  create mode 100644 helper/odph_hashtable.h
  create mode 100644 helper/odph_list_internal.h

diff --git a/helper/hashtable.c b/helper/hashtable.c
new file mode 100644
index 000..0b29814
--- /dev/null
+++ b/helper/hashtable.c
@@ -0,0 +1,346 @@
+/* Copyright (c) 2015, Linaro Limited
+ * All rights reserved.
+ *
+ * SPDX-License-Identifier:   BSD-3-Clause
+ */
+#include 
+#include 
+#include 
+
+#include "odph_hashtable.h"
+#include "odph_list_internal.h"
+#include "odph_debug.h"
+#include 
+
+#defineODPH_SUCCESS0
+#defineODPH_FAIL   -1
+
+/** @magic word, write to the first byte of the memory block
+ * to indicate this block is used by a hash table structure
+ */
+#defineODPH_HASH_TABLE_MAGIC_WORD  0xABABBABA
+
+/** @support 64k buckets. Bucket is a list that composed of
+ * elements with the same HASH-value but different keys
+ */
+#defineODPH_MAX_BUCKET_NUM 0x1
+
+/** @inner element structure of hash table
+ * To resolve the hash confict:
+ * we put the elements with different keys but a same HASH-value
+ * into a list
+ */
+typedef struct odph_hash_node {
+   /** list structure,for list opt */
+   odph_list_object list_node;
+   /** Flexible Array,memory will be alloced when table has been created
+* Its length is key_size + value_size,
+* suppose key_size = m; value_size = n;
+* its structure is like:
+* k_byte1 k_byte2...k_byten v_byte1...v_bytem
+*/
+   char content[0];
+} odph_hash_node;
+
+typedef struct {
+   uint32_t magicword; /**< for check */
+   uint32_t key_size; /**< input param when create,in Bytes */
+   uint32_t value_size; /**< input param when create,in Bytes */
+   uint32_t init_cap; /**< input param when create,in Bytes */
+   /** multi-process support,every list has one rw lock */
+   odp_rwlock_t *lock_pool;
+   /** table bucket pool,every hash value has one list head */
+   odph_list_head *list_head_pool;
+   /** number of the list head in list_head_pool */
+   uint32_t head_num;
+   /** table element pool */
+   odph_hash_node *hash_node_pool;
+   /** number of element in the hash_node_pool */
+   uint32_t hash_node_num;
+   char rsv[7]; /**< Reserved,for alignment */
+   char name[ODPH_TABLE_NAME_LEN]; /**< table name */
+} odph_hash_table_imp;
+
+odph_table_t odph_hash_table_create(const char *name, uint32_t capacity,
+   uint32_t key_size,
+   uint32_t value_size)
+{
+   int idx, i;
+   uint32_t node_num;
+   odph_hash_table_imp *tbl;
+   odp_shm_t shmem;
+   uint32_t node_mem;
+
+   if (strlen(name) >= ODPH_TABLE_NAME_LEN || capacity < 1 ||
+   capacity >= 0x1000 || key_size == 0 || value_size == 0) {
+   ODPH_DBG("create para input error!\n");
+   return NULL;
+   }
+   tbl = (odph_hash_table_imp *)odp_shm_addr(odp_shm_lookup(name));
+   if (tbl != NULL) {
+   ODPH_DBG("name already exist\n");
+   return NULL;
+   }
+   shmem = odp_shm_reserve(name, capacity << 20, 64, ODP_SHM_SW_ONLY);
+   if (shmem == ODP_SHM_INVALID) {
+   ODPH_DBG("shm reserve fail\n");
+   return NULL;
+   }
+   tbl = (odph_hash_table_imp *)odp_shm_addr(shmem);
+
+   /* clean this block of memory */
+   memset(tbl, 0, capacity << 20);
+
+   tbl->init_cap = capacity << 20;
+   strncpy(tbl->name, name, ODPH_TABLE_NAME_LEN);
+   tbl->key_size = key_size;
+   tbl->value_size = value_size;
+
+   /* header of this mem block is the table control struct,
+* then the lock pool, then the list header pool
+* the last part is the element node pool
+*/
+
+   tbl->lock_pool = (odp_rwlock_t *)((char *)tbl
+   + sizeof(odph_hash_table_imp));
+   tbl->list_head_pool = (odph_list_head *)((char *)tbl->lock_pool
+   + ODPH_MAX_BUCKET_NUM * sizeof(odp_rwlock_t));
+
+   node_mem = tbl->init_cap - sizeof(odph_hash_table_imp)
+   - ODPH_MAX_BUCKET_NUM * sizeof(odph_list_head)
+   - ODPH_MAX_BUCKET_NUM * sizeof(odp_rwlock_t);
+
+   node_num = node_mem / (sizeof(odph_hash_node) + key_size + value_size);
+   tbl->hash_node_num = node_num;
+   tbl->hash_node_pool = (odph_hash_node *)((char *)tbl->list_head_pool
+   + ODPH_MAX_BUCKET_NUM * sizeof(odph_list_head));
+
+   /* init every list head and rw lock */
+   for (i = 0; i < ODPH_MAX_BUCKET_NUM; 

[lng-odp] [PATCHv4 2/5] helper: table: add impl of hashtable

2015-11-05 Thread huanggaoyang
Signed-off-by: huanggaoyang 
---
 helper/hashtable.c  | 346 
 helper/odph_hashtable.h |  42 ++
 helper/odph_list_internal.h |  85 +++
 3 files changed, 473 insertions(+)
 create mode 100644 helper/hashtable.c
 create mode 100644 helper/odph_hashtable.h
 create mode 100644 helper/odph_list_internal.h

diff --git a/helper/hashtable.c b/helper/hashtable.c
new file mode 100644
index 000..0b29814
--- /dev/null
+++ b/helper/hashtable.c
@@ -0,0 +1,346 @@
+/* Copyright (c) 2015, Linaro Limited
+ * All rights reserved.
+ *
+ * SPDX-License-Identifier:   BSD-3-Clause
+ */
+#include 
+#include 
+#include 
+
+#include "odph_hashtable.h"
+#include "odph_list_internal.h"
+#include "odph_debug.h"
+#include 
+
+#defineODPH_SUCCESS0
+#defineODPH_FAIL   -1
+
+/** @magic word, write to the first byte of the memory block
+ * to indicate this block is used by a hash table structure
+ */
+#defineODPH_HASH_TABLE_MAGIC_WORD  0xABABBABA
+
+/** @support 64k buckets. Bucket is a list that composed of
+ * elements with the same HASH-value but different keys
+ */
+#defineODPH_MAX_BUCKET_NUM 0x1
+
+/** @inner element structure of hash table
+ * To resolve the hash confict:
+ * we put the elements with different keys but a same HASH-value
+ * into a list
+ */
+typedef struct odph_hash_node {
+   /** list structure,for list opt */
+   odph_list_object list_node;
+   /** Flexible Array,memory will be alloced when table has been created
+* Its length is key_size + value_size,
+* suppose key_size = m; value_size = n;
+* its structure is like:
+* k_byte1 k_byte2...k_byten v_byte1...v_bytem
+*/
+   char content[0];
+} odph_hash_node;
+
+typedef struct {
+   uint32_t magicword; /**< for check */
+   uint32_t key_size; /**< input param when create,in Bytes */
+   uint32_t value_size; /**< input param when create,in Bytes */
+   uint32_t init_cap; /**< input param when create,in Bytes */
+   /** multi-process support,every list has one rw lock */
+   odp_rwlock_t *lock_pool;
+   /** table bucket pool,every hash value has one list head */
+   odph_list_head *list_head_pool;
+   /** number of the list head in list_head_pool */
+   uint32_t head_num;
+   /** table element pool */
+   odph_hash_node *hash_node_pool;
+   /** number of element in the hash_node_pool */
+   uint32_t hash_node_num;
+   char rsv[7]; /**< Reserved,for alignment */
+   char name[ODPH_TABLE_NAME_LEN]; /**< table name */
+} odph_hash_table_imp;
+
+odph_table_t odph_hash_table_create(const char *name, uint32_t capacity,
+   uint32_t key_size,
+   uint32_t value_size)
+{
+   int idx, i;
+   uint32_t node_num;
+   odph_hash_table_imp *tbl;
+   odp_shm_t shmem;
+   uint32_t node_mem;
+
+   if (strlen(name) >= ODPH_TABLE_NAME_LEN || capacity < 1 ||
+   capacity >= 0x1000 || key_size == 0 || value_size == 0) {
+   ODPH_DBG("create para input error!\n");
+   return NULL;
+   }
+   tbl = (odph_hash_table_imp *)odp_shm_addr(odp_shm_lookup(name));
+   if (tbl != NULL) {
+   ODPH_DBG("name already exist\n");
+   return NULL;
+   }
+   shmem = odp_shm_reserve(name, capacity << 20, 64, ODP_SHM_SW_ONLY);
+   if (shmem == ODP_SHM_INVALID) {
+   ODPH_DBG("shm reserve fail\n");
+   return NULL;
+   }
+   tbl = (odph_hash_table_imp *)odp_shm_addr(shmem);
+
+   /* clean this block of memory */
+   memset(tbl, 0, capacity << 20);
+
+   tbl->init_cap = capacity << 20;
+   strncpy(tbl->name, name, ODPH_TABLE_NAME_LEN);
+   tbl->key_size = key_size;
+   tbl->value_size = value_size;
+
+   /* header of this mem block is the table control struct,
+* then the lock pool, then the list header pool
+* the last part is the element node pool
+*/
+
+   tbl->lock_pool = (odp_rwlock_t *)((char *)tbl
+   + sizeof(odph_hash_table_imp));
+   tbl->list_head_pool = (odph_list_head *)((char *)tbl->lock_pool
+   + ODPH_MAX_BUCKET_NUM * sizeof(odp_rwlock_t));
+
+   node_mem = tbl->init_cap - sizeof(odph_hash_table_imp)
+   - ODPH_MAX_BUCKET_NUM * sizeof(odph_list_head)
+   - ODPH_MAX_BUCKET_NUM * sizeof(odp_rwlock_t);
+
+   node_num = node_mem / (sizeof(odph_hash_node) + key_size + value_size);
+   tbl->hash_node_num = node_num;
+   tbl->hash_node_pool = (odph_hash_node *)((char *)tbl->list_head_pool
+   + ODPH_MAX_BUCKET_NUM * sizeof(odph_list_head));
+
+   /* init every list head and rw lock */
+   for (i = 0; i < ODPH_MAX_BUCKET_NUM; i++) {
+