Opencv: cvmemstorage
Default category: 21:32:22 read 204 comments 0 font size: large, medium, and small
1. dynamic memory storage and operation functions
Cvmemstorage
Typedef struct cvmemstorage
{
Struct cvmemblock * bottom;
Struct cvmemblock * top;
Struct cvmemstorage * parent;
Int block_size;
Int free_space;
} Cvmemstorage;
Memory is an underlying structure that can be used to store dynamic growth data structures such as sequences, outlines, graphs, and subpartitions. It consists of a series of memory blocks of the same size. The list-based bottom domain refers to the first column, and the top domain refers to the block currently pointed to, but not necessarily the end of the column. all blocks (including bottom, excluding top) between bottom and top are fully occupied; all blocks (including block tails, excluding top) between top and column tails) it is empty, while the top block itself occupies part of the space-free_space refers to the number of remaining blank characters in the top block. Newly allocated memory buffer (or displayed through
Cvmemstoragealloc function allocation, or hidden advanced function allocation through cvseqpush, cvgraphaddedge, etc.) always starts from the remaining part of the current block (that is, the top block, if the remaining part meets the requirements (the allocation size is sufficient ). After the allocation, free_space reduces the size of the newly allocated memory, and some additional sizes used to save the appropriate column type. When the remaining space of the top block cannot meet the size of the allocated block (buffer zone), the next storage block of the top block is set to the current block (New top block) -- free_space is set to the size of the entire block previously allocated. If no empty storage block exists (that is, the top block is at the end of the column), you must assign a new block (or inherit from the parent). See
Cvcreatechildmemstorage) and add the block to the end of the column. As a result, memory storage is like a stack. Bottom points to the bottom of the stack, and (top, free_space) points to the top of the stack. Stack top can be saved through cvsavemstoragepos, restored to point through cvrestoremstoragepos, and reset through cvclearstorage.
Cvmemblock
Memory block structure
Typedef struct cvmemblock
{
Struct cvmemblock * Prev;
Struct cvmemblock * next;
} Cvmemblock;
Cvmemblock represents a separate memory block structure. The actual data in the memory block is stored after the header block (that is, there is a header pointing to the block header, which does not store data). Therefore, the I-th byte of the memory block can be obtained through the expression (char *) (mem_block_ptr + 1) [I. However, it is usually not necessary to directly obtain the domain of the storage structure.
Cvmemstoragepos
Memory block address
Typedef struct cvmemstoragepos
{
Cvmemblock * top;
Int free_space;
} Cvmemstoragepos;
This structure (as described below) saves the address of the top stack. The top stack can be saved through cvsavemstoragepos or cvrestoremstoragepos.
________________________________________
Cvcreatememstorage
Create a memory block
Cvmemstorage * cvcreatememstorage (INT block_size = 0 );
Block_size: the size of the storage block in bytes. If the block size is 0 bytes, set this block to the default value. The current default size is 64 KB.
The cvcreatememstorage function creates a memory block and returns a pointer to the first part of the block. At first, the storage block is empty. All the domain values of the header (that is, header) are 0, except for block_size.
Cvreleasememstorage
Release memory blocks
Void cvreleasememstorage (cvmemstorage ** storage );
Storage: pointer to the released storage Block
The cvreleasememstorage function releases all storage (memory) blocks or returns them to their respective parents (if needed ). Next, release the header block (that is, release the block pointed to by the header pointer head = free (head) and clear the pointer pointing to the block (that is, head = NULL ). Clear the child blocks before releasing them as parent blocks.
Cvclearmemstorage
Clear memory block
Void cvclearmemstorage (cvmemstorage * storage );
Storage: storage Block
The function cvclearmemstorage places the top of the block to the header of the block (Note: Clear the content stored in the block ). This function does not release the memory (only clears the memory ). If the memory block has a parent memory block (I .e., a memory block has a parent-child relationship with it), the function returns all the blocks to its parent.
Cvmemstoragealloc
Allocate to memory buffer in the storage Block
Void * cvmemstoragealloc (cvmemstorage * storage, size_t size );
Storage: memory block.
Size: the size of the buffer.
The cvmemstoragealloc function allocates a memory buffer in the storage block. The size of the buffer cannot exceed the size of the memory block. Otherwise, a running error occurs. The buffer address is changed to the cv_struct_align byte (currently sizeof (double )).
Cvmemstorageallocstring
Assign a text string to the bucket
Typedef struct cvstring
{
Int Len;
Char * PTR;
}
Cvstring;
Cvstring cvmemstorageallocstring (cvmemstorage * storage, const char * PTR, int Len =-1 );
Storage: storage Block
PTR: String
Len: the length of the string ('\ 0' is not counted '). If the parameter is negative, the function calculates the length of the string.
The cvmemstorageallostring function creates a copy of a string in the bucket. It returns a structure that contains the length of a string (transmitted by the user or calculated) and a pointer to the copied string.
Cvsavememstoragepos
Location (address) of the memory block to be saved)
Void cvsavememstoragepos (const cvmemstorage * storage, cvmemstoragepos * POS );
Storage: memory block.
POs: the top position of the memory block.
The cvsavemstoragepos function saves the current location of the storage block to the POs parameter. The cvrestoremstoragepos function can further obtain the location (address ).
Cvrestorememstoragepos
Restore the location of a memory block
Void cvrestorememstoragepos (cvmemstorage * storage, cvmemstoragepos * POS );
Storage: memory block.
POs: Location of the new storage Block
The cvrestoremstoragepos function restores the memory block position through the POs parameter. This function and function cvclearmemstorage are the only way to release occupied memory blocks. Note: There is no way to release part of the memory occupied by the storage block.
From: http://blog.sina.com.cn/s/blog_672c5a470100j1cm.html