您的位置:首页 > 其它

VLC的block_t

2017-02-08 16:29 204 查看

0 VLC的block_t

1 发现问题

payload 数据内存起始位置是p_buffer, 大小是i_buffer ,

block本身也有个内存起始位置 p_start , 大小i_size ,

令人疑惑。

struct block_t
{
block_t    *p_next;

uint8_t    *p_buffer; /**< Payload start */
size_t      i_buffer; /**< Payload length */
uint8_t    *p_start; /**< Buffer start */
size_t      i_size; /**< Buffer total size */

uint32_t    i_flags;
unsigned    i_nb_samples; /* Used for audio */

mtime_t     i_pts;
mtime_t     i_dts;
mtime_t     i_length;

/* Rudimentary support for overloading block (de)allocation. */
block_free_t pf_release;
};


2 init构造

构造时发现payload数据内存位置和block缓冲的内存位置初始化为一样的。

大小值也是一样的。

void block_Init( block_t *restrict b, void *buf, size_t size )
{
/* Fill all fields to their default */
b->p_next = NULL;
b->p_buffer = buf;
b->i_buffer = size;
b->p_start = buf;
b->i_size = size;
b->i_flags = 0;
b->i_nb_samples = 0;
b->i_pts =
b->i_dts = VLC_TS_INVALID;
b->i_length = 0;
#ifndef NDEBUG
b->pf_release = BlockNoRelease;
#endif
}


3 实际分配alloc

实际block分配的时候,占用了更多内存,

传递给p_start 和p_buffer的是跳过了block_t 大小(64字节)内存的内存地址;

相应的,i_size和i_buffer,也减少了 block_t大小。

如果用户要求分配size大小,实际分配的是size+64字节+32(对齐)0+32(头部填充)+32(尾部填充)=size+160(字节),block_t内部拥有的内存大小是size+96字节。

block_t *block_Alloc (size_t size)
{
/* 2 * BLOCK_PADDING: pre + post padding */
const size_t alloc = sizeof (block_t)/*64*/ + BLOCK_ALIGN + (2 * BLOCK_PADDING)
+ size;
if (unlikely(alloc <= size))
return NULL;

block_t *b = malloc (alloc);
if (unlikely(b == NULL))
return NULL;

block_Init (b, b + 1, alloc - sizeof (*b));
static_assert ((BLOCK_PADDING % BLOCK_ALIGN) == 0,
"BLOCK_PADDING must be a multiple of BLOCK_ALIGN");
b->p_buffer += BLOCK_PADDING + BLOCK_ALIGN - 1;
b->p_buffer = (void *)(((uintptr_t)b->p_buffer) & ~(BLOCK_ALIGN - 1));
b->i_buffer = size;
b->pf_release = block_generic_Release;
return b;
}


4 最后还要对齐和调整

参考 []http://blog.csdn.net/fuzhuo233/article/details/8182335] 线性内存分配器的实现 /TODO/

static_assert ((BLOCK_PADDING % BLOCK_ALIGN) == 0,
"BLOCK_PADDING must be a multiple of BLOCK_ALIGN");
b->p_buffer += BLOCK_PADDING + BLOCK_ALIGN - 1;
b->p_buffer = (void *)(((uintptr_t)b->p_buffer) & ~(BLOCK_ALIGN - 1));


payload的缓冲b->p_buffer 还是要后移然后对齐

payload的缓冲大小 还是要等于用户需要的size大小。

那这样的话,block的缓冲地址start和size就与payload的区分开了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: