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;};構造時發現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}實際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;}參考 [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的區分開了。
新聞熱點
疑難解答