Android BlueDroid分析: OSI中的List与alloctor的实现分析
来源:互联网 发布:美国大学gpa算法 编辑:程序博客网 时间:2024/06/05 13:30
说明
alloctor
定义
alloctor是一对函数函数指针的结构体,因此先要定义来个函数Pointer 类型。
typedef void *(*alloc_fn)(size_t size);typedef void (*free_fn)(void *ptr);
可以看到,这个和libc中的malloc与free arguments与return type都是一样的,因此可以认为是OSI为了抽象分隔不同的“OS Interface”(Memory中的Stack 管理)。因此如果我们需要将BlueDroid移植到其他OS的话,需要注意适配。
alloctor_t结构体包含两个函数Pointer,一个是分配的fn,一个是释放的fn,函数Pointer类型定义如下:
typedef struct { alloc_fn alloc; free_fn free;} allocator_t;
在Android中,使用的libc是bonic,和glibc一样,都实现C POSIX Library(参考Wikipedia),所以函数都是名称和传入参数都一样,也是malloc与free,因此:
void *osi_malloc(size_t size) { size_t real_size = allocation_tracker_resize_for_canary(size); return allocation_tracker_notify_alloc( alloc_allocator_id, malloc(real_size), size);}
calloc
calloc比malloc多了一个初始化功能.void *osi_calloc(size_t size) { size_t real_size = allocation_tracker_resize_for_canary(size); return allocation_tracker_notify_alloc( alloc_allocator_id, calloc(1, real_size), size);}void osi_free(void *ptr) { free(allocation_tracker_notify_free(alloc_allocator_id, ptr));}const allocator_t allocator_malloc = { osi_malloc, osi_free};const allocator_t allocator_calloc = { osi_calloc, osi_free};
其中allocation_tracker_resize_for_canary函数的实现与hashmap相关,这个在以后的文章中再说明.
List链表
定义与结构
struct list_node_t;typedef struct list_node_t list_node_t;struct list_t;typedef struct list_t list_t;typedef void (*list_free_cb)(void *data);
下面的data为void *,这样可以用来存储任何type的指针,一般都是指向数据的指针.
struct list_node_t { struct list_node_t *next; void *data;};typedef struct list_t { list_node_t *head; list_node_t *tail; size_t length; list_free_cb free_cb; const allocator_t *allocator;} list_t;
free的call_back函数, alloctor分配器,均为函数指针。
list包含一个头尾节点head与tail, 表示有多少个node的长度length, 用来free list node中的data指针指向数据的callback函数指针, 还有一个用来如何malloc与free list node(不是data的free)的alloctor.
List的其他utils函数
主要是:增删查改.包括empty判断,next, create,begin,end.需要说明的是创建与clear.
List的创建与使用
内核创建函数:
list_new_internalBlueDroid中的osi/src中的各类封装都有一个特点,就是有一个XXX_new同时还有一个XXX_new_internal, internal代表者这个接口(函数)为osi下的source code相互调用,一般是对XXX_new的封装, 而其他地方如果想new的话,一般使用XXX_new来调用.
例如下面:
list_t *list_new(list_free_cbcallback){ returnlist_new_internal(callback,&allocator_calloc);}
list_new_internal的parameter是callback, 其类型为list_free_cb, 用于list的free. 对于传入的第二个参数:
allocator_calloc
就是前面alloctor中的封装系统POSIX的malloc/free对.
List的Clear/Delete
List本身就是malloc出来的, 然后其结构中包含了一串的node,这些node是使用list_t中的alloctor中的malloc创建,所以要销毁一个list,想要销毁其里面的node,因此直接从head到tail,一个个销毁节点,然后给对应指针与length赋值即可:
void list_clear(list_t*list){ assert(list!= NULL); for (list_node_t *node =list->head;node; ) node = list_free_node_(list,node); list->head= NULL; list->tail= NULL; list->length= 0;}
而node的free就是调用alloctor中的free,即OSI封装的free,一般在linux中就是free:
static list_node_t *list_free_node_(list_t*list,list_node_t *node){ assert(list!= NULL);assert(node!= NULL); list_node_t *next =node->next; if (list->free_cb) list->free_cb(node->data); list->allocator->free(node); --list->length; return next;}
值得注意的是,这里面调用了free_cb, 即节点callback函数被调用了, 可以设想一下,如果这个callback fn为空,那么意味着其节点node中的data(void *data)数据就是一个数据, 如果不为空的话, 那么说明void *data指向的很可能是一片数据, 而这个数据直接调用free并传入data这个指针无法回收. 例如:
btif/src/btif_sock_sco.c92: sco_sockets = list_new((list_free_cb)sco_socket_free_locked);
其中其回收函数中逐层对sco_socket进行free, 最后才是对sco_socket指针的回收:
static void sco_socket_free_locked(sco_socket_t *sco_socket) { if (!sco_socket) return; if (sco_socket->sco_handle != BTM_INVALID_SCO_INDEX) BTM_RemoveSco(sco_socket->sco_handle); socket_free(sco_socket->socket); osi_free(sco_socket);}
GKI_getbuf 与 GKI_freebuf
但是在有一些地方使用的是GKI相关的free与alloc,这个是malloc下的二次封装, 为stack启动时候分配了一个pool,然后从这个pool中使用GKI_getbuf与GKI_freebuf来使用. 这个时候就不需要传入free callback fn了, 例如:
BT_HDR *fragment = GKI_getbuf(client->remote_mtu + L2CAP_MIN_OFFSET); fragment->offset = L2CAP_MIN_OFFSET; fragment->len = client->remote_mtu; memcpy(fragment->data + fragment->offset, bt_packet->data + bt_packet->offset, client->remote_mtu); list_append(client->outbound_fragments, fragment);
而其free的则通过获取节点数据(即void *data指针)来完成
GKI_freebuf(list_node(node));void *list_node(constlist_node_t *node){ assert(node!= NULL);return node->data;}
List中新添加Node:list_append
在system/bt中有下面这些地方用到了:
$ ag list_appendstack/l2cap/l2c_link.c1122: list_append(p_lcb->link_xmit_data_q, p_buf);stack/l2cap/l2cap_client.c116: list_append(l2cap_clients, ret);383: list_append(client->outbound_fragments, bt_packet);394: list_append(client->outbound_fragments, fragment);stack/l2cap/l2c_main.c166: list_append(l2cb.rcv_pending_q, p_msg);bta/av/bta_av_main.c1231: list_append(p_scbi->a2d_list, p_new);bta/av/bta_av_aact.c2287: list_append(p_scb->a2d_list, p_buf);append为将数据data放入一个新创建的node中.
- Android BlueDroid分析: OSI中的List与alloctor的实现分析
- Android BlueDroid分析: OSI中的HashMap的实现
- Android BlueDroid分析: OSI中的reactor的实现与使用分析
- Android BlueDroid分析: OSI中的FixeQueue的实现与使用分析
- Android BlueDroid分析: OSI中的Thread的实现与使用分析
- Andorid BlueDroid分析: Semophore的实现分析
- Android BlueDroid分析: Linux中的Eventfd
- linux2.6 的 slab alloctor 结构分析
- Android BlueDroid分析: 配置文件(bt_stack.conf bt_vendor.conf )的加载与分析
- Android BlueDroid 分析之扫描
- BlueDroid代码分析
- BlueDroid代码分析
- Bluedroid GKI源码分析
- BlueDroid代码分析之BlueDroid简介
- BlueDroid代码分析之回调函数的注册与调用
- 一个简单的alloctor类实现
- BlueDroid代码分析之GKI
- bluedroid底层分析之GKI
- 文章索引~python
- java并发测试代码
- 转:支付宝用AR集五福,VR和MR怎么看?
- iOS framework开发流程总结
- 异常
- Android BlueDroid分析: OSI中的List与alloctor的实现分析
- 《硅谷之谜》读书笔记:追求卓越,改变自己
- linux下du查看目录文件夹大小
- 播放网络视频(当Activity的背景)
- 读《技术人员的发展之路》有感
- Uva1152 4 Values whose Sum is 0
- 关于 PHP 表单安全性的重要提示
- ElasticSearch(一):介绍及安装
- android-目录学习