rt-thread中的list测试

rt-thread通过list管理相关设备,分层以及接口管理,list的大致框架如下:

测试代码:

#include <stdio.h> 
#include <stdlib.h>
#include <stdint.h>
#include <string.h>


enum obj_class_type
{
    Obj_Test1 = 0,
    Obj_Test2,
    Obj_Test3
};

struct list_node
{
    struct list_node *next;
    struct list_node *prev;
};
typedef struct list_node list_t;

struct object_info
{
    enum obj_class_type   type;
    list_t                object_list;
    uint32_t              object_size;
};

struct test1
{
    char name[16];
    uint32_t flag;
    list_t list;
};

struct test2
{
    uint32_t a;
    list_t list;
};

struct test3
{
    list_t list;
    uint8_t aaa;
    uint8_t bbb;
};

#define _OBJ_CONTAINER_LIST_INIT(c)     \
    {&(obj_container[c].object_list), &(obj_container[c].object_list)}

static struct object_info obj_container[3] = 
{
    {Obj_Test1, _OBJ_CONTAINER_LIST_INIT(Obj_Test1),sizeof(struct test1)},
    {Obj_Test2, _OBJ_CONTAINER_LIST_INIT(Obj_Test2),sizeof(struct test2)},
    {Obj_Test3, _OBJ_CONTAINER_LIST_INIT(Obj_Test3),sizeof(struct test3)}
};

static void list_init(list_t *l)
{
    l->next = l->prev = l;
}

static void list_insert(list_t *l, list_t *n)
{
    l->next->prev = n;
    n->next = l->next;

    l->next = n;
    n->prev = l;
}

static void list_remove(list_t *n)
{
    n->next->prev = n->prev;
    n->prev->next = n->next;

    n->next = n->prev = n;
}

static int list_isempty(const list_t *l)
{
    return l->next == l;
}

#define list_entry(ptr, type, member) \
    ((type *)((char *)(ptr) - (unsigned long)(&((type *)0)->member)))


struct test1 t1;
struct test2 t2;
struct test3 t3;

void test1_init()
{
    struct object_info *info = &obj_container[Obj_Test1];
    
    strcpy(t1.name, "aaa");
    t1.flag = 1;
    list_insert(&(info->object_list), &(t1.list));

    struct test1 *t = (struct test1 *)malloc(sizeof(struct test1));
    strcpy(t->name, "bbb");
    t->flag = 2;
    list_insert(&(info->object_list), &(t->list));
}

void test2_init()
{
    struct object_info *info = &obj_container[Obj_Test2];
    
    t2.a = 123;
    list_insert(&(info->object_list), &(t2.list));

    struct test2 *t = (struct test2 *)malloc(sizeof(struct test2));
    t->a = 456;
    list_insert(&(info->object_list), &(t->list));
} 

void test3_init()
{
    struct object_info *info = &obj_container[Obj_Test3];
    
    t3.aaa = 11;
    t3.bbb = 22;
    list_insert(&(info->object_list), &(t3.list));

    struct test3 *tmp1 = (struct test3 *)malloc(sizeof(struct test3));
    tmp1->aaa = 33;
    tmp1->bbb = 44;
    list_insert(&(info->object_list), &(tmp1->list));

    struct test3 *tmp2 = (struct test3 *)malloc(sizeof(struct test3));
    tmp2->aaa = 55;
    tmp2->bbb = 66;
    list_insert(&(info->object_list), &(tmp2->list));
}

void print_test1()
{
    struct test1 *tmp;
    struct object_info *info = &obj_container[Obj_Test1];
    struct list_node *node = info->object_list.next;
    printf("============== test1 =============\n");
    for (; node != &(info->object_list); node = node->next)
    {
        tmp = list_entry(node, struct test1, list);
        printf("name=%s,\tflag=%d\n",tmp->name, tmp->flag);
    }
}

void print_test2()
{
    struct test2 *tmp;
    struct object_info *info = &obj_container[Obj_Test2];
    struct list_node *node = info->object_list.next;
    printf("============== test2 =============\n");
    for (; node != &(info->object_list); node = node->next)
    {
        tmp = list_entry(node, struct test2, list);
        printf("a=%d\n", tmp->a);
    }
}

void print_test3()
{
    struct test3 *tmp;
    struct object_info *info = &obj_container[Obj_Test3];
    struct list_node *node = info->object_list.next;
    printf("============== test3 =============\n");
    for (; node != &(info->object_list); node = node->next)
    {
        tmp = list_entry(node, struct test3, list);
        printf("aaa=%d,\tbbb=%d\n", tmp->aaa, tmp->bbb);
    }
}

void test3_rm()
{
    struct test3 *tmp;
    struct object_info *info = &obj_container[Obj_Test3];
    struct list_node *node = info->object_list.next;
    for (; node != &(info->object_list); node = node->next)
    {
        tmp = list_entry(node, struct test3, list);
        if (tmp->aaa == 33)
        {
            printf("--- rm ---\n");
            list_remove(node);
        //    list_remove(&(tmp->list));
            free(tmp);
            break;
        }
    }
}

int main()
{
    test1_init();
    test2_init();
    test3_init();

    print_test1();   
    print_test2();
    print_test3();

    test3_rm();
    print_test3();

    return 0;
}

运行结果:

$ ./test
============== test1 =============
name=bbb,       flag=2
name=aaa,       flag=1
============== test2 =============
a=456
a=123
============== test3 =============
aaa=55, bbb=66
aaa=33, bbb=44
aaa=11, bbb=22
--- rm ---
============== test3 =============
aaa=55, bbb=66
aaa=11, bbb=22

代码中的list_entry应用很巧妙,在linux中经常看到。

#define list_entry(ptr, type, member) \
    ((type *)((char *)(ptr) - (unsigned long)(&((type *)0)->member)))

其中:

(unsigned long)(&((type *)0)->member)   

计算member在type结构体中的偏移地址,(type *)0只是读取所以不会出现段错误。

(char *)(ptr)

根据list中的偏移地址来获取继承的结构体对象的首地址。

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值