ConicBundle
Classes | Public Member Functions | Private Member Functions | Private Attributes | List of all members
CH_Matrix_Classes::Memarray Class Reference

A simple memory manager for frequent allocation and deallocation of arrays of roughly the same size. More...

#include <memarray.hxx>

Classes

class  Entry
 holds the information of one allocated block and serves as an item in the singly linked lists More...
 

Public Member Functions

 Memarray (long number_of_entries, int number_of_sizes, int address_bits)
 specify inital number of Entry items, the number of 2^i classes, the number of last bits used in finding entries to used adresses
 
unsigned long get_in_use () const
 returns the number of CH_Matrix_Classes::Memarray::Entry items in use
 
unsigned long get_memarray_users () const
 returns the number of announced "living" users of this Memory manager
 
unsigned long increment_memarray_users ()
 announce a new user
 
unsigned long decrement_memarray_users ()
 decrement number of announced users
 
long get (long size, char *&addr)
 request a character array of at least this size, the address is then stored in addr and the actual size is returned
 
long get (long size, double *&addr)
 request a double array of at least this size, the address is then stored in addr and the actual size is returned
 
long get (long size, float *&addr)
 request a float array of at least this size, the address is then stored in addr and the actual size is returned
 
long get (long size, long *&addr)
 request a long array of at least this size, the address is then stored in addr and the actual size is returned
 
long get (long size, int *&addr)
 request an int array of at least this size, the address is then stored in addr and the actual size is returned
 
int free (void *addr)
 free the array pointed to by addr (addr must be an address returned by get)
 

Private Member Functions

int size_index (long size)
 compute index for free list to a request of size
 
long index_size (int index)
 compute size of blocks in the free list with this index
 
int addr_index (const char *addr)
 compute index into address class to retrieve entry of "addr"
 
int get_more_entries ()
 double number of available CH_Matrix_Classes::Memarray::Entry items
 

Private Attributes

long max_entries
 current number of CH_Matrix_Classes::Memarray::Entry items available
 
long max_sizes
 current number of lists holding free blocks
 
long max_addr_entr
 current number of lists hodling occupied blocks
 
unsigned long addr_mask
 mask to extract last bits of an address as index for freeing
 
unsigned long in_use
 number of CH_Matrix_Classes::Memarray::Entry items in use (pointing to an allocated block)
 
unsigned long memarray_users
 number of objects announced as "living" users of this memory manager
 
Entry first_empty
 its next pointer points to the first free CH_Matrix_Classes::Memarray::Entry item, that does not yet hold an allocated block
 
Entryentry_store
 points to the allocated array of CH_Matrix_Classes::Memarray::Entry items
 
Entryfirst_free
 points to an array of size max_sizes, its Entry i start the free list for size 2^i
 
Entryfirst_used
 points to an array of size max_addr_entr, its Entry i start the list of entries whose memory is in use and the last bits of the address form the number i
 

Detailed Description

A simple memory manager for frequent allocation and deallocation of arrays of roughly the same size.

The Manager only allocates blocks of size 2^n and keeps for each n a singly linked list holding the free blocks of size 2^n. The occupied blocks are kept in singly linked lists indexed by the last few bits of their addresses in order to facilitate fast retrieval of the corresponding memory management entry. Currently, a block once allocated is not freed again unless the memory manager is destructed.

Information about the allocated blocks is stored in CH_Matrix_Classes::Memarray::Entry items that also serve for forming the linked lists. A large array of these items is allocated initially and whenever no more free Entry items are available (i.e. all are filled with allocated blocks) a new array of Entry items, double in size, is allocated and the old information of the old array is copied to the first half of the new items.


The documentation for this class was generated from the following file: