?? blockpool.h
字號:
#ifndef BLOCKPOOL_H#define BLOCKPOOL_H#include <assert.h>/** \fileThis file contains the following class definitions:<ul><li>big_block_alloc : large block memory allocator</li><li>block_pool : block pool management</li><li>big_block_pool : large memory pool memory allocation/deallocation</li></ul>These classes support allocation of memory pools. A memory pool isallocated in blocks and smaller chunks of memory are usuallyallocated from these blocks. The entire memory pool is deallocated atonce.The big_block_pool object creates a pool of large blocks. These aremore time consuming to create initially (since they are initializedto zero), but they are, in theory, faster, since fewer of them willhave to be created. The big_block_pool is designed for internaldata structures that are large and have a relatively long live (likethe Abstract Syntax Trees generated by a compiler front end).The page_block_pool allocator is designed for short lived datapools. Creation of a page_block_pool is faster, but it is not, intheory, as efficient for large internal data structures. The dataallocated by the page_block_pool object is also initialized tozero.The "in theory" caveat above exists because in tests on Windows NT 4.0the large block and the page block memory allocators are very similarin performance. In fact, contrary to expectation, the large blockallocator is slightly slower in allocation, but much faster on deallocation.*//*===============================<o>=====================================Copyright 1996, 1997, 2004 Ian Kaplan, Bear Products International,www.bearcave.com.All Rights ReservedYou may use this software in software components for which you donot collect money (e.g., non-commercial software). All commercialuse is reserved.===============================<o>=====================================*//** Support for the allocation of large memory blocks. These blocks contain the memory "chunks" allocated by the pooled memory allocator.*/class big_block_alloc { private: unsigned int page_size; unsigned int alloc_gran; private: void GetSysInfo(void); void alloc_error(void); public: void get_info(unsigned int &pg_size, unsigned int &gran ) { assert((page_size != 0 && alloc_gran != 0)); pg_size = page_size; gran = alloc_gran; } void *MemoryAlloc( unsigned int num_bytes ); void MemoryFree( void *address ); big_block_alloc(void) { GetSysInfo(); }}; // big_block_alloc/** An abstract base class for pooled memory allocation.*/class block_pool { private: // typedefs and variables /** the largest block of memory that can be allocated is the page_size * max_block_multiple */ typedef enum { max_block_multiple = 256 } bogus; typedef struct block_chain_struct { /** pointer to the current block */ void *block; /** number of bytes used in the block */ unsigned int bytes_used; /** total block size */ unsigned int block_size; /** pointer to the next block */ block_chain_struct *next_block; } block_chain; unsigned int page_size; unsigned int alloc_gran; /** start of the block list for this pool */ block_chain *block_list_start; /** current block memory is being allocated from */ block_chain *current_block; public: /** This provides a publicly visible "type handle" that references a private type. Sun is quite permissive and does not require this sort of contortion, but HP does. */ typedef block_chain blk_chain; private: // class functions blk_chain *new_block( unsigned int block_size ); void *add_block( unsigned int block_size ); void init_pool(void); public: // class functions block_pool(void); void free_pool(void); void *pool_alloc( unsigned int block_size ); void print_block_pool_info( FILE *fp = stdout ); virtual void getinfo(unsigned int &p_size, unsigned int &a_gran ) = 0; virtual void *MemAlloc( unsigned int n_bytes ) = 0; virtual void MemFree( void *addr ) = 0;}; // class block_pool/* macros for block_chain pointers */#define Chain_block(p) ((p)->block)#define Chain_bytes_used(p) ((p)->bytes_used)#define Chain_block_size(p) ((p)->block_size)#define Chain_next(p) ((p)->next_block)/** An implementation of pooled memory allocation that makes use of the large memory block allocation class, big_block_alloc.*/class big_block_pool : public block_pool { private: big_block_alloc block_alloc; protected: void getinfo(unsigned int &p_size, unsigned int &a_gran ) { block_alloc.get_info( p_size, a_gran ); } public: big_block_pool(void) : block_pool() {} ~big_block_pool(void) { free_pool(); // inherited from block_pool } /** Implementations of block_pool virtual functions low level, system dependent, memory allocation. */ void *MemAlloc( unsigned int num_bytes ) { return block_alloc.MemoryAlloc( num_bytes ); } /** low level, system dependent memory free */ void MemFree( void *address ) { block_alloc.MemoryFree( address ); }}; // big_block_pool#endif
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -