7  Chunk.c源码分析
7.1 chunk数据结构体定义
chunk结构体内指定了两种块类型,一种是MEM_CHUNK,一种是FILE_CHUNK,对于各自块类型给出了相应的数据保存结构字段。另外有个next字段可以将各个chunk结构体连接起来组成链表
typedef struct chunk {
郑凯个人资料
enum { UNUSED_CHUNK, MEM_CHUNK, FILE_CHUNK } type;
buffer *mem; /* either the storage of the mem-chunk or the read-ahead buffer */ {
struct
*/
/*
filechunk
浮夸原唱buffer *name; /* name of the file */
off_t start; /* starting offset in the file */
/*从start开始需要发送的字节数目*/
off_t length; /* octets to send from the starting offset *//*octet 就是byte*/
int    fd;
汪峰 爸爸
struct {
char *start; /* the start pointer of the mmap'ed area */
size_t length; /* size of the mmap'ed area */
off_t offset; /* start is <n> octet away from the start of the file */
}
mmap;
int is_temp; /* file is temporary and will be deleted if on cleanup */
file;
}
off_t offset; /* octets sent from this chunk
the size of the chunk is either
- mem-chunk: mem->used - 1
file-chunk:
file.length
-
*/
struct chunk *next;
} chunk;
chunkqueue利用chunk组成一个数据结构体,该数据结构体包含两条链表,一条是将所有正在被使用的块串联起来形成的链表(记录有该链表的头、尾),一条记录已经使用完毕但还没有释放的块结构(仅记录链表头以及链表内块结构数目,最大数目为5),这些块可以看作是用作缓冲池被程序再次利用,避免反复的进行内存分配与释放,减少内存碎片,可以认为其为内存池实现的一种方式。
讨论:邮箱:地址:重庆大学区二舍
QQ45438969 lenky0401@163 A1我心永恒爱无止境
typedef struct {
*first;
chunk
*last;
chunk
*unused;
chunk
歌曲美丽家园
unused_chunks;
size_t
*tempdirs;
array
bytes_out;
off_t bytes_in,
} chunkqueue;
7.2 chunkqueue数据结构体操作
客官不可以的歌词对于结构体chunk和chunkqueue的操作实现都比较简单,理解起来都没什么难点,下面简单列出各个函数的功能。
函数名功能
音乐 论坛chunkqueue_init 初始化并返回一个chunkqueue结构体。chunk_init 初始化并返回一个chunk结构体。
chunk_free 释放一个chunk结构体。
chunk_reset 重置一个chunk结构体。
chunkqueue_free 释放一个chunkqueue结构体。chunkqueue_get_unused_chunk 从chunkqueue中获取一个未使用的chunk,
如果没有,则新建立一个chunk并返回它。chunkqueue_prepend_chunk 将一指定块添加到chunkqueue单链表表头。chunkqueue_append_chunk 将一指定块添加到chunkqueue单链表表尾。chunkqueue_reset 重置chunkqueue。
chunkqueue_append_file 给chunkqueue增加一块file类型的chunk块
到尾部。
chunkqueue_append_buffer 给chunkqueue增加一块mem类型的chunk
块到尾部,新增块内容为指定buffer的一个
拷贝。
chunkqueue_append_buffer_weak 给chunkqueue增加一块mem类型的chunk
2讨论QQ:45438969 邮箱:lenky0401@163 地址:重庆大学A区二舍
我心永恒爱无止境
讨论QQ:45438969 邮箱:lenky0401@163 地址:重庆大学A区二舍我心永恒爱无止境3
块到尾部,新增块内容为指定buffer的一个
引用。
chunkqueue_prepend_buffer 给chunkqueue增加一块mem类型的chunk
块到头部,新增块内容为指定buffer的一个
拷贝。
chunkqueue_append_mem 给chunkqueue增加一块mem类型的chunk
块到尾部,新增块内容为指定字符串的一个
子串。
chunkqueue_get_prepend_buffer 从chunkqueue中获取一块未使用的mem类
型chunk块并添加到chunkqueue头部。chunkqueue_get_append_buffer 从chunkqueue中获取一块未使用的mem类
型chunk块并添加到chunkqueue尾部。chunkqueue_set_tempdirs 设置chunkqueue结构字段tempdirs。chunkqueue_get_append_tempfile 建立临时目录,当建立不成功时,chunk里的
file.fd为-1。
chunkqueue_length 获取所有chunk块的字节长度,mem长度+file
长度。
chunkqueue_written 获取所有chunk块的已写字节数。chunkqueue_is_empty 检测chunkqueue是否为空。
chunkqueue_remove_finished_chunks 从链表头开始清理已经使用完毕的chunk块。