EPICS ARCHIVER V4
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups
Classes | Public Member Functions | Static Public Attributes | List of all members
FileAllocator Class Reference

Maintains memory blocks within a file. More...

#include <FileAllocator.h>

Public Member Functions

 FileAllocator ()
 Constructor/destructor check if attach/detach have been called. More...
 
 ~FileAllocator ()
 Destructor checks if detach has been called.
 
bool attach (FILE *f, FileOffset reserved_space, bool init)
 Must be invoked to attach (& initialize) a file. More...
 
FILE * getFile () const
 
void detach ()
 Must be called before destroying the FileAllocator and closing the file.
 
FileOffset allocate (FileOffset num_bytes)
 Allocate a block with given size, returning a file offset (for fseek). More...
 
FileOffset size () const
 
void free (FileOffset offset)
 Release a file block (will be placed in free list). More...
 
bool dump (int level=1, FILE *out=stdout)
 Show ASCII-type info about the file structure. More...
 

Static Public Attributes

static FileOffset minimum_size
 To avoid allocating tiny areas, also to avoid splitting free blocks into pieces that are then too small to be ever useful, all memory regions are at least this big. More...
 
static FileOffset file_size_increment
 Setting file_size_increment will cause the file size to jump in the given increments. More...
 

Detailed Description

Maintains memory blocks within a file.

The FileAllocator maintains the space in a given file by keeping track of allocated blocks. Sections can also be released, in which case they are placed in a free-list for future allocations.

Constructor & Destructor Documentation

FileAllocator::FileAllocator ( )

Constructor/destructor check if attach/detach have been called.

We could have made these perform the attach/detach, but then the error reporting would have had to use exceptions which we'd like to avoid.

Member Function Documentation

FileOffset FileAllocator::allocate ( FileOffset  num_bytes)

Allocate a block with given size, returning a file offset (for fseek).

See Also
free()
Exceptions
GenericExceptionon error.
bool FileAllocator::attach ( FILE *  f,
FileOffset  reserved_space,
bool  init 
)

Must be invoked to attach (& initialize) a file.

Can be invoked on an empty file with init=true, in thich case the reserved space and some FileAllocator header will be added to the file. Can also be invoked on an existing file (init=false or true) which was initialized by the FileAllocator, in which case the reserved_space has to match the value that was used when initializing the file!

init=true means this file is used for writing, and we are allowed to create a new file allocator header for an empty file.

Returns
true when an empty file was initialized. Otherwise, some weak tests are performed so see if this file does indeed contain a FileAllocator header, and false is returned.
Exceptions
GenericExceptionon error.
bool FileAllocator::dump ( int  level = 1,
FILE *  out = stdout 
)

Show ASCII-type info about the file structure.

This routines lists all the allocated and free blocks together with some other size information. It also performs some sanity checks. When called with level=0, it will only perform the tests and only report possible problems.

Should not throw exceptions but simply return false on errors.

Parameters
levelLevel of verbosity.
outWhen non-zero, it is used instead of stdout.

Returns true for 'OK'.

void FileAllocator::free ( FileOffset  offset)

Release a file block (will be placed in free list).

Parameters
offsetA file offset previously obtained from allocate()
Warning
It is an error to free space that wasn't allocated. There is no 100% dependable way to check this, but free() will perform some basic test and return false for inknown memory regions.
Exceptions
GenericExceptionon error.
FILE* FileAllocator::getFile ( ) const
Returns
After attaching to a file, this returns the file.
FileOffset FileAllocator::size ( ) const
inline
Returns
Total size of file in bytes.

Member Data Documentation

FileOffset FileAllocator::file_size_increment
static

Setting file_size_increment will cause the file size to jump in the given increments.

FileOffset FileAllocator::minimum_size
static

To avoid allocating tiny areas, also to avoid splitting free blocks into pieces that are then too small to be ever useful, all memory regions are at least this big.

Meaning: Whenever you allocate less than minimum_size, you will get a block that's actually minimum_size.


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