OpenCV基础知识——动态结构(二)
来源:互联网 发布:准备配置windows要多久 编辑:程序博客网 时间:2024/05/29 05:02
(7)cvMemStorageAlloc
该函数,首先判断Storage的剩余空间和待分配的空间的大小关系,若满足分配条件就直接在对应的TOP块中分配对应的空间,并且更改相关参数。如果待分配空间>剩余空间,则调用函数icvGoNextMemBlock()对storage进行调整,然后再进行分配。
源码:
- CV_IMPL void*
- cvMemStorageAlloc( CvMemStorage* storage, size_t size )
- {
- char *ptr = 0;
- CV_FUNCNAME( "cvMemStorageAlloc" );
- __BEGIN__;
- if( !storage )
- CV_ERROR( CV_StsNullPtr, "NULL storage pointer" );
- if( size > INT_MAX )
- CV_ERROR( CV_StsOutOfRange, "Too large memory block is requested" );
- assert( storage->free_space % CV_STRUCT_ALIGN == 0 );
- if( (size_t)storage->free_space < size )
- {
- size_t max_free_space = cvAlignLeft(storage->block_size - sizeof(CvMemBlock), CV_STRUCT_ALIGN);
- if( max_free_space < size )
- CV_ERROR( CV_StsOutOfRange, "requested size is negative or too big" );
- CV_CALL( icvGoNextMemBlock( storage ));
- }
- ptr = ICV_FREE_PTR(storage);
- assert( (size_t)ptr % CV_STRUCT_ALIGN == 0 );
- storage->free_space = cvAlignLeft(storage->free_space - (int)size, CV_STRUCT_ALIGN );
- __END__;
- return ptr;
- }
(8)icvGoNextMemBlock
该函数,如果Storage的storage->top->next!=0,则 将Storage的TOP块指针移动到下一个Block块上,然后设置好相关的参数,返回。 如果是新分配的Storage,其storage->top==0,若其也没有Parent节点,那么就给他分配一块storage->block_size大小的空间
然后将其作为该Storage的第一个Block,并设置相关的参数;若其有Parent节点,则在他的父节点上找到一块Block空间分配。并将该区域从其父节点上取下了。(注意最后一种情况是一个递归过程)
源码:
- static void
- icvGoNextMemBlock( CvMemStorage * storage )
- {
- CV_FUNCNAME( "icvGoNextMemBlock" );
- __BEGIN__;
- if( !storage )
- CV_ERROR( CV_StsNullPtr, "" );
- if( !storage->top || !storage->top->next )
- {
- CvMemBlock *block;
- if( !(storage->parent) )
- {
- CV_CALL( block = (CvMemBlock *)cvAlloc( storage->block_size ));
- }
- else
- {
- CvMemStorage *parent = storage->parent;
- CvMemStoragePos parent_pos;
- cvSaveMemStoragePos( parent, &parent_pos );
- CV_CALL( icvGoNextMemBlock( parent ));
- block = parent->top;
- cvRestoreMemStoragePos( parent, &parent_pos );
- if( block == parent->top ) /* the single allocated block */
- {
- assert( parent->bottom == block );
- parent->top = parent->bottom = 0;
- parent->free_space = 0;
- }
- else
- {
- /* cut the block from the parent's list of blocks */
- parent->top->next = block->next;
- if( block->next )
- block->next->prev = parent->top;
- }
- }
- /* link block */
- block->next = 0;
- block->prev = storage->top;
- if( storage->top )
- storage->top->next = block;
- else
- storage->top = storage->bottom = block;
- }
- if( storage->top->next )
- storage->top = storage->top->next;
- storage->free_space = storage->block_size - sizeof(CvMemBlock);
- assert( storage->free_space % CV_STRUCT_ALIGN == 0 );
- __END__;
- }
(8)cvRestoreMemStoragePos( CvMemStorage * storage, CvMemStoragePos * pos )
该函数将Pos中相应的字段,赋值给Storage相对应的各个字段。注意当Storage是一个新初始化的情况时的特殊处理。
- CV_IMPL void
- cvRestoreMemStoragePos( CvMemStorage * storage, CvMemStoragePos * pos )
- {
- CV_FUNCNAME( "cvRestoreMemStoragePos" );
- __BEGIN__;
- if( !storage || !pos )
- CV_ERROR( CV_StsNullPtr, "" );
- if( pos->free_space > storage->block_size )
- CV_ERROR( CV_StsBadSize, "" );
- /*
- // this breaks icvGoNextMemBlock, so comment it off for now
- if( storage->parent && (!pos->top || pos->top->next) )
- {
- CvMemBlock* save_bottom;
- if( !pos->top )
- save_bottom = 0;
- else
- {
- save_bottom = storage->bottom;
- storage->bottom = pos->top->next;
- pos->top->next = 0;
- storage->bottom->prev = 0;
- }
- icvDestroyMemStorage( storage );
- storage->bottom = save_bottom;
- }*/
- storage->top = pos->top;
- storage->free_space = pos->free_space;
- if( !storage->top )
- {
- storage->top = storage->bottom;
- storage->free_space = storage->top ? storage->block_size - sizeof(CvMemBlock) : 0;
- }
- __END__;
- }
- OpenCV基础知识——动态结构(二)
- OpenCV基础知识——动态结构(一)
- OpenCV学习(4)——动态结构
- OpenCV学习笔记(二)—— OpenCV整体结构
- OpenCV学习笔记(二)——新版本模块结构
- OpenCV学习笔记(二)——新版本模块结构
- OpenCV学习笔记(二)——新版本模块结构
- OpenCV学习笔记(二)——新版本模块结构
- OpenCV学习笔记(二)——新版本模块结构
- OpenCV学习笔记(二)——新版本模块结构
- OpenCV学习笔记(二)——新版本模块结构
- XML基础知识——结构
- 动态规划——基础知识
- opencv——IplImage结构
- OpenCV基础知识——多维矩阵
- Android基础知识(二)—-Menu
- OpenCV2基础知识——Mat结构
- 黑马程序员————————C#基础知识之结构体(二)
- char*与wchar_t*(LPTSTR) 如何相互转换
- 板载音效解述
- (收集)关于 contentType 和 pageEncoding 的差异 和 中文JSP页的设定技巧
- 板载RAID解述
- Windows 系统文件保护
- OpenCV基础知识——动态结构(二)
- 让 IDA 的 F5 插件失效
- C++ 遍历文件夹中的所有文件
- 操纵DOM,创建table,显示获得的数据
- [英语阅读]菲尔普斯蓄须“溜进”北京
- Progress through persistence
- 20080806
- The related article about to recently study --2008,08,04
- 用单片机开发信号发生器