项少龙 为什么不和善柔:x264_malloc及free
来源:百度文库 编辑:九乡新闻网 时间:2024/04/30 00:47:26
做移植时刚好看到这一段,开始也搞不明白。后来查了一,这其中有两个关键点:
- void *x264_malloc( int i_size )
- {
- #ifdef SYS_MACOSX
- /* Mac OS X always returns 16 bytes aligned memory */
- return malloc( i_size );
- #elif defined( HAVE_MALLOC_H )
- return memalign( 16, i_size );
- #else
- uint8_t * buf;
- uint8_t * align_buf;
- buf = (uint8_t *) malloc( i_size + 15 + sizeof( void ** ) +
- sizeof( int ) );
- align_buf = buf + 15 + sizeof( void ** ) + sizeof( int );
- align_buf -= (long) align_buf & 15;
- *( (void **) ( align_buf - sizeof( void ** ) ) ) = buf;
- *( (int *) ( align_buf - sizeof( void ** ) - sizeof( int ) ) ) = i_size;
- return align_buf;
- #endif
- }
1是二级指针的使用,二级指针可以看成是一个一维的指针数组。
2是内存管理思想。
下面具体分析一下:
这段代码主要是为了实现:返回一个16字节对齐的 i_size 大小的内存;同时再分配该内存时添加了一些额外信息在所分配的内存的头部。
- buf = (uint8_t *) malloc( i_size + 15 + sizeof( void ** ) +sizeof( int ) );
15 + sizeof( void ** ) +sizeof( int )是为了字节对齐以及放置所分配的内存块的额外添加的信息的。
- align_buf -= (long) align_buf & 15;
- *( (void **) ( align_buf - sizeof( void ** ) ) ) = buf;
- *( (int *) ( align_buf - sizeof( void ** ) - sizeof( int ) ) ) = i_size;
下面再说一下,内存的释放:
- void H264_free( void *p )
- {
- if( p )
- {
- #if defined( HAVE_MALLOC_H ) || defined( SYS_MACOSX )
- free( p );
- #else
- free( *( ( ( void **) p ) - 1 ) );
- #endif
- }
- }
*( ( ( void **) p ) - 1 )这里得到的其实就是上面的 buf 指针。可见释放是不会造成内存泄露的。
x264_malloc及free
Lan Free及Server Free备份方式读解
linux常用命令及实例三:free、su、reboot、kill、shu
Free Loop
Free Demurrage & free detention - yanjiefan12...
Today ,I am free
StockCharts.com - Free Charts
free英文原版书-----
free英文原版书-
Free Radicals---游離基
v20111010 salt free
Free Demurrage & Free Detention - 福步新人 - 福步外贸...
《BE FREE》NikeRunning 2011free跑鞋创意图集
Memory of a Free Festival
Linux PS 命令详解 free
Naturalism (literature) - Wikipedia, the free...
Arctic - Wikipedia, the free encyclopedia
jQuery - Wikipedia, the free encyclopedia
Storage virtualization - Wikipedia, the free ...
gofavor fashion jewelry free shipping
Carbamate - Wikipedia, the free encyclopedia
Free Speech and the Internet
free?talk?topic?:?Thanksgiving?Day
High Earth orbit - Wikipedia, the free encycl...