浏览代码

Utilities/libadt: support adt API for DM

Add reference with rt_atomic in resources' put/get management.
Add bitmap operator base on rt_ubase_t.
Add hashmap for key->data map management.

Signed-off-by: GuEe-GUI <GuEe-GUI@github.com>
wusongjie 1 年之前
父节点
当前提交
950d71e1ac

+ 24 - 0
components/utilities/libadt/Kconfig

@@ -0,0 +1,24 @@
+menuconfig RT_USING_ADT
+    bool "Enable ADT(abstract data type)"
+    default y if ARCH_MM_MMU
+    default n
+
+config RT_USING_ADT_AVL
+    bool "AVL tree"
+    depends on RT_USING_ADT
+    default y
+
+config RT_USING_ADT_BITMAP
+    bool "Bitmap"
+    depends on RT_USING_ADT
+    default y
+
+config RT_USING_ADT_HASHMAP
+    bool "HashMap"
+    depends on RT_USING_ADT
+    default y
+
+config RT_USING_ADT_REF
+    bool "Reference API"
+    depends on RT_USING_ADT
+    default y

+ 11 - 4
components/utilities/libadt/SConscript

@@ -1,8 +1,15 @@
 from building import *
 
 cwd     = GetCurrentDir()
-src     = Glob('*.c')
-CPPPATH = [cwd]
-group   = DefineGroup('Utilities', src, depend = ['RT_USING_ADT'], CPPPATH = CPPPATH)
+list    = os.listdir(cwd)
+objs    = []
 
-Return('group')
+if not GetDepend(['RT_USING_ADT']):
+    Return('objs')
+
+for d in list:
+    path = os.path.join(cwd, d)
+    if os.path.isfile(os.path.join(path, 'SConscript')):
+        objs = objs + SConscript(os.path.join(d, 'SConscript'))
+
+Return('objs')

+ 9 - 0
components/utilities/libadt/avl/SConscript

@@ -0,0 +1,9 @@
+from building import *
+
+cwd     = GetCurrentDir()
+src     = Glob('*.c')
+CPPPATH = [cwd]
+group   = []
+
+group = DefineGroup('LIBADT', src, depend = ['RT_USING_ADT_AVL'], CPPPATH = CPPPATH)
+Return('group')

+ 0 - 0
components/utilities/libadt/avl.c → components/utilities/libadt/avl/avl.c


+ 0 - 0
components/utilities/libadt/avl.h → components/utilities/libadt/avl/avl.h


+ 9 - 0
components/utilities/libadt/bitmap/SConscript

@@ -0,0 +1,9 @@
+from building import *
+
+cwd     = GetCurrentDir()
+src     = list()
+CPPPATH = [cwd]
+group   = []
+
+group = DefineGroup('LIBADT', src, depend = ['RT_USING_ADT_BITMAP'], CPPPATH = CPPPATH)
+Return('group')

+ 77 - 0
components/utilities/libadt/bitmap/bitmap.h

@@ -0,0 +1,77 @@
+/*
+ * Copyright (c) 2006-2022, RT-Thread Development Team
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Change Logs:
+ * Date           Author       Notes
+ * 2022-6-27      GuEe-GUI     first version
+ */
+
+#ifndef __UTIL_BITMAP_H__
+#define __UTIL_BITMAP_H__
+
+#include <rtdef.h>
+
+typedef rt_ubase_t rt_bitmap_t;
+
+#define RT_BITMAP_BITS_MIN              (sizeof(rt_bitmap_t) * 8)
+#define RT_BITMAP_LEN(bits)             (((bits) + (RT_BITMAP_BITS_MIN) - 1) / (RT_BITMAP_BITS_MIN))
+#define RT_BITMAP_BIT_LEN(nr)           (nr * RT_BITMAP_BITS_MIN)
+#define RT_DECLARE_BITMAP(name, bits)   rt_bitmap_t name[RT_BITMAP_LEN(bits)]
+
+rt_inline void rt_bitmap_set_bit(rt_bitmap_t *bitmap, rt_uint32_t bit)
+{
+    bitmap[bit / RT_BITMAP_BITS_MIN] |= (1UL << (bit & (RT_BITMAP_BITS_MIN - 1)));
+}
+
+rt_inline rt_bool_t rt_bitmap_test_bit(rt_bitmap_t *bitmap, rt_uint32_t bit)
+{
+    return !!(bitmap[bit / RT_BITMAP_BITS_MIN] & (1UL << (bit & (RT_BITMAP_BITS_MIN - 1))));
+}
+
+rt_inline void rt_bitmap_clear_bit(rt_bitmap_t *bitmap, rt_uint32_t bit)
+{
+    bitmap[bit / RT_BITMAP_BITS_MIN] &= ~(1UL << (bit & (RT_BITMAP_BITS_MIN - 1)));
+}
+
+rt_inline rt_size_t rt_bitmap_next_set_bit(rt_bitmap_t *bitmap, rt_size_t start, rt_size_t limit)
+{
+    rt_size_t bit;
+
+    for (bit = start; bit < limit && !rt_bitmap_test_bit(bitmap, bit); ++bit)
+    {
+    }
+
+    return bit;
+}
+
+rt_inline rt_size_t rt_bitmap_next_clear_bit(rt_bitmap_t *bitmap, rt_size_t start, rt_size_t limit)
+{
+    rt_size_t bit;
+
+    for (bit = start; bit < limit && rt_bitmap_test_bit(bitmap, bit); ++bit)
+    {
+    }
+
+    return bit;
+}
+
+#define rt_bitmap_for_each_bit_from(state, bitmap, from, bit, limit)        \
+    for ((bit) = rt_bitmap_next_##state##_bit((bitmap), (from), (limit));   \
+         (bit) < (limit);                                                   \
+         (bit) = rt_bitmap_next_##state##_bit((bitmap), (bit + 1), (limit)))
+
+#define rt_bitmap_for_each_set_bit_from(bitmap, from, bit, limit) \
+    rt_bitmap_for_each_bit_from(set, bitmap, from, bit, limit)
+
+#define rt_bitmap_for_each_set_bit(bitmap, bit, limit) \
+    rt_bitmap_for_each_set_bit_from(bitmap, 0, bit, limit)
+
+#define rt_bitmap_for_each_clear_bit_from(bitmap, from, bit, limit) \
+    rt_bitmap_for_each_bit_from(clear, bitmap, from, bit, limit)
+
+#define rt_bitmap_for_each_clear_bit(bitmap, bit, limit) \
+    rt_bitmap_for_each_clear_bit_from(bitmap, 0, bit, limit)
+
+#endif /* __UTIL_BITMAP_H__ */

+ 9 - 0
components/utilities/libadt/hashmap/SConscript

@@ -0,0 +1,9 @@
+from building import *
+
+cwd     = GetCurrentDir()
+src     = list()
+CPPPATH = [cwd]
+group   = []
+
+group = DefineGroup('LIBADT', src, depend = ['RT_USING_ADT_HASHMAP'], CPPPATH = CPPPATH)
+Return('group')

+ 41 - 0
components/utilities/libadt/hashmap/hashmap.h

@@ -0,0 +1,41 @@
+/*
+ * Copyright (c) 2006-2022, RT-Thread Development Team
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Change Logs:
+ * Date           Author       Notes
+ * 2022-08-01     GuEe-GUI     first version
+ */
+
+#ifndef __UTIL_HASHMAP_H__
+#define __UTIL_HASHMAP_H__
+
+#include <rtdef.h>
+
+/*
+ * http://www.citi.umich.edu/techreports/reports/citi-tr-00-1.pdf
+ *
+ * GoldenRatio = ~(Math.pow(2, 32) / ((Math.sqrt(5) - 1) / 2)) + 1
+ */
+#define RT_HASHMAP_GOLDEN_RATIO_32  0x61C88647
+#define RT_HASHMAP_GOLDEN_RATIO_64  0X61C8864680B583EBULL
+
+rt_inline rt_uint32_t rt_hashmap_32(rt_uint32_t val, rt_uint32_t bits)
+{
+    /* High bits are more random, so use them. */
+    return (val * RT_HASHMAP_GOLDEN_RATIO_32) >> (32 - bits);
+}
+
+rt_inline rt_uint32_t rt_hashmap_64(rt_uint64_t val, rt_uint32_t bits)
+{
+#ifdef ARCH_CPU_64BIT
+    /* 64x64-bit multiply is efficient on all 64-bit processors */
+    return val * RT_HASHMAP_GOLDEN_RATIO_64 >> (64 - bits);
+#else
+    /* Hash 64 bits using only 32x32-bit multiply. */
+    return rt_hashmap_32((rt_uint32_t)val ^ ((val >> 32) * RT_HASHMAP_GOLDEN_RATIO_32), bits);
+#endif
+}
+
+#endif /* __UTIL_HASHMAP_H__ */

+ 9 - 0
components/utilities/libadt/ref/SConscript

@@ -0,0 +1,9 @@
+from building import *
+
+cwd     = GetCurrentDir()
+src     = list()
+CPPPATH = [cwd]
+group   = []
+
+group = DefineGroup('LIBADT', src, depend = ['RT_USING_ADT_REF'], CPPPATH = CPPPATH)
+Return('group')

+ 75 - 0
components/utilities/libadt/ref/ref.h

@@ -0,0 +1,75 @@
+/*
+ * Copyright (c) 2006-2023, RT-Thread Development Team
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Change Logs:
+ * Date           Author       Notes
+ * 2022-3-1       zhouxiaohu   first version
+ * 2023-5-18      GuEe-GUI     implemented by rtatomic
+ */
+
+#ifndef __UTIL_REF_H__
+#define __UTIL_REF_H__
+
+#include <rtatomic.h>
+
+/**
+ * struct ref must be embedded in an object.
+ * it acts as a reference counter for the object.
+ */
+struct rt_ref
+{
+    rt_atomic_t refcount;
+};
+
+#define RT_REF_INIT(n)  { .refcount = n, }
+
+rt_inline void rt_ref_init(struct rt_ref *r)
+{
+    rt_atomic_store(&r->refcount, 1);
+}
+
+rt_inline unsigned int rt_ref_read(struct rt_ref *r)
+{
+    return rt_atomic_load(&r->refcount);
+}
+
+/**
+ * ref_get
+ * increment reference counter for object.
+ */
+rt_inline void rt_ref_get(struct rt_ref *r)
+{
+    rt_atomic_add(&r->refcount, 1);
+}
+
+/**
+ * ref_put
+ * decrement reference counter for object.
+ * If the reference counter is zero, call release().
+ *
+ * Return 1 means the object's reference counter is zero and release() is called.
+ */
+rt_inline int rt_ref_put(struct rt_ref *r, void (*release)(struct rt_ref *r))
+{
+    if (rt_atomic_dec_and_test(&r->refcount))
+    {
+        release(r);
+
+        return 1;
+    }
+
+    return 0;
+}
+
+/**
+ * ref_get_unless_zero - Increment refcount for object unless it is zero.
+ * Return non-zero if the increment succeeded. Otherwise return 0.
+ */
+rt_inline int rt_ref_get_unless_zero(struct rt_ref *r)
+{
+    return (int)rt_atomic_inc_not_zero(&r->refcount);
+}
+
+#endif /* __UTIL_REF_H__ */