virtmem
virtual memory library for Arduino
virtmem::VAlloc< Properties, Derived > Class Template Reference

Base template class for virtual memory allocators. More...

#include <alloc.h>

Inheritance diagram for virtmem::VAlloc< Properties, Derived >:
virtmem::BaseVAlloc

Classes

struct  TVPtr
 Generalized shortcut to virtual pointer type linked to this allocator. More...
 

Public Types

template<typename T >
using VPtr = virtmem::VPtr< T, Derived >
 

Public Member Functions

template<typename T >
VPtr< T, Derived > alloc (VPtrSize size=sizeof(T))
 Allocates memory from the linked virtual memory allocator. More...
 
template<typename T >
void free (VPtr< T, Derived > &p)
 Frees a block of virtual memory. More...
 
template<typename T >
VPtr< T, Derived > newClass (VPtrSize size=sizeof(T))
 Allocates memory and constructs data type. More...
 
template<typename T >
void deleteClass (VPtr< T, Derived > &p)
 Destructs data type and frees memory. More...
 
template<typename T >
VPtr< T, Derived > newArray (VPtrSize elements)
 Allocates and constructs an array of objects. More...
 
template<typename T >
void deleteArray (VPtr< T, Derived > &p)
 Destructs and frees an array of objects. More...
 
- Public Member Functions inherited from virtmem::BaseVAlloc
void start (void)
 Starts the allocator. More...
 
void stop (void)
 Deinitializes the allocator. More...
 
void setPoolSize (VPtrSize ps)
 Sets the total size of the memory pool. More...
 
VPtrNum allocRaw (VPtrSize size)
 Allocates a piece of raw (virtual) memory. More...
 
void freeRaw (VPtrNum ptr)
 Frees a memory block for re-usage. More...
 
void * read (VPtrNum p, VPtrSize size)
 Reads a raw block of (virtual) memory. More...
 
void write (VPtrNum p, const void *d, VPtrSize size)
 Writes a piece of raw data to (virtual) memory. More...
 
void flush (void)
 Synchronizes all big memory pages. More...
 
void clearPages (void)
 Synchronizes and clears all big memory pages. More...
 
uint8_t getFreeBigPages (void) const
 
uint8_t getUnlockedSmallPages (void) const
 Returns amount of small pages which are not locked.
 
uint8_t getUnlockedMediumPages (void) const
 Returns amount of medium pages which are not locked.
 
uint8_t getUnlockedBigPages (void) const
 Returns amount of big pages which are not locked.
 
uint8_t getSmallPageCount (void) const
 Returns total amount of small pages.
 
uint8_t getMediumPageCount (void) const
 Returns total amount of medium pages.
 
uint8_t getBigPageCount (void) const
 Returns total amount of big pages.
 
VirtPageSize getSmallPageSize (void) const
 Returns the size of a small page.
 
VirtPageSize getMediumPageSize (void) const
 Returns the size of a medium page.
 
VirtPageSize getBigPageSize (void) const
 Returns the size of a big page.
 
VPtrSize getPoolSize (void) const
 Returns the size the memory pool. More...
 
VPtrSize getMemUsed (void) const
 Returns total memory used.
 
VPtrSize getMaxMemUsed (void) const
 Returns the maximum memory used so far.
 
uint32_t getBigPageReads (void) const
 Returns the times big pages were read (swapped).
 
uint32_t getBigPageWrites (void) const
 Returns the times big pages written (synchronized).
 
uint32_t getBytesRead (void) const
 Returns the amount of bytes read as a result of page swaps.
 
uint32_t getBytesWritten (void) const
 Returns the amount of bytes written as a results of page swaps.
 
void resetStats (void)
 Reset all statistics. Called by start()
 

Static Public Member Functions

static VAllocgetInstance (void)
 Returns a pointer to the instance of the class. More...
 

Additional Inherited Members

- Protected Member Functions inherited from virtmem::BaseVAlloc
void writeZeros (VPtrNum start, VPtrSize n)
 Writes zeros to raw virtual memory. Can be used to initialize the memory pool. More...
 
virtual void doStart (void)=0
 
virtual void doStop (void)=0
 
virtual void doRead (void *data, VPtrSize offset, VPtrSize size)=0
 
virtual void doWrite (const void *data, VPtrSize offset, VPtrSize size)=0
 

Detailed Description

template<typename Properties, typename Derived>
class virtmem::VAlloc< Properties, Derived >

Base template class for virtual memory allocators.

This template class is used as parent class for all allocators. Most of the actual code is defined in BaseVAlloc, while this class only contains code dependent upon template parameters (i.e. page settings).

Template Parameters
PropertiesAllocator properties, see DefaultAllocProperties
DerivedDummy parameter, used to create unique singleton instances for each derived class.

Member Typedef Documentation

template<typename Properties, typename Derived>
template<typename T >
using virtmem::VAlloc< Properties, Derived >::VPtr = virtmem::VPtr<T, Derived>

Similar to TVPtr, but slightly shorter syntax (C++11 only) Example:

virtmem::SDAlloc::VPtr<int> vIntPtr; // virtual pointer to int, linked to SD allocator.
Template Parameters
TType of virtual pointer

Member Function Documentation

template<typename Properties, typename Derived>
template<typename T >
VPtr<T, Derived> virtmem::VAlloc< Properties, Derived >::alloc ( VPtrSize  size = sizeof(T))
inline

Allocates memory from the linked virtual memory allocator.

Template Parameters
TThe data type to allocate for.
Parameters
sizeThe number of bytes to allocate. By default this is the size of the type pointed to.
Returns
Virtual pointer pointing to allocated memory.

This function is the equivelant of the C malloc function. To allocate memory for an array multiply the array size by the size of the type, for instance:

// allocate array of 10 integers.
vptr = virtmem::VPtr<int, SDVAlloc>::alloc(10 * sizeof(int));
See also
free, newClass, newArray, BaseVAlloc::allocRaw
template<typename Properties, typename Derived>
template<typename T >
void virtmem::VAlloc< Properties, Derived >::deleteArray ( VPtr< T, Derived > &  p)
inline

Destructs and frees an array of objects.

Parameters
pVirtual pointer to array that should be deleted

This function is similar to the C++ delete [] operator. After calling all the destructors of each object, the respective memory block will be freed.

Note
This function should be used together with newArray.
See also
newArray, deleteClass, free
template<typename Properties, typename Derived>
template<typename T >
void virtmem::VAlloc< Properties, Derived >::deleteClass ( VPtr< T, Derived > &  p)
inline

Destructs data type and frees memory.

Parameters
pvirtual pointer to delete

This function is similar to the C++ delete operator. After calling the destructor of the data type the respective memory block will be freed.

Note
This function should be used together with newClass.
See also
newClass, deleteArray, free
template<typename Properties, typename Derived>
template<typename T >
void virtmem::VAlloc< Properties, Derived >::free ( VPtr< T, Derived > &  p)
inline

Frees a block of virtual memory.

Parameters
pvirtual pointer that points to block to be freed

This function is the equivelant of the C free function. The pointer will be set to null.

See also
alloc, deleteClass, deleteArray, BaseVAlloc::freeRaw
template<typename Properties, typename Derived>
static VAlloc* virtmem::VAlloc< Properties, Derived >::getInstance ( void  )
inlinestatic

Returns a pointer to the instance of the class.

All allocator classes are singletons: only one instance can exist for a particular allocator.

Note that, since allocators are template classes, one instance is generated for every set of unique template parameters. For example:

virtmem::SDVAlloc alloc1; // allocator with default template parameters
virtmem::SDVAllocP<mysettings> alloc2; // allocator with custom template parameter

In this case, alloc1 and alloc2 are variables with a different type, hence getInstance() will return a different instance for both classes.

template<typename Properties, typename Derived>
template<typename T >
VPtr<T, Derived> virtmem::VAlloc< Properties, Derived >::newArray ( VPtrSize  elements)
inline

Allocates and constructs an array of objects.

Parameters
elementsthe size of the array
Returns
Virtual pointer to start of the array

This function is similar to the C++ new [] operator. After allocating sufficient size for the array, the default constructor will be called for each object.

Note
This function should be used together with deleteArray.
See also
deleteArray, newClass, alloc
template<typename Properties, typename Derived>
template<typename T >
VPtr<T, Derived> virtmem::VAlloc< Properties, Derived >::newClass ( VPtrSize  size = sizeof(T))
inline

Allocates memory and constructs data type.

Template Parameters
TThe data type to allocate for.
Parameters
sizeThe number of bytes to allocate. By default this is the size of the type pointed to.
Returns
Virtual pointer pointing to allocated memory.

This function is similar to the C++ new operator. Similar to alloc, this function will allocate a block of virtual memory. Subsequently, the default constructor of the data type is called. For this reason, this function is typically used for C++ classes.

Note
This function should be used together with deleteClass.
See also
deleteClass, newArray, alloc

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