libsswf: sswf::MemoryManager Class Reference

# sswf::MemoryManager Class Reference

A garbage collector manager. More...

`#include <libsswf.h>`

Inheritance diagram for sswf::MemoryManager:

List of all members.

## Public Member Functions
void * | MemAlloc (size_t size, const char *info)
| Allocate a new buffer of memory.
void | MemAttach (MemBuffer *ptr, size_t size, const char *info)
| Attach a memory buffer to a manager.
void | MemClean (void *ptr)
| Free a memory buffer and clean the holding pointer.
void | MemFree (void *ptr)
| Free a memory buffer.
| MemoryManager (void)
| Initialize a memory manager object.
void * | MemRealloc (void *ptr, size_t size, const char *info)
| Allocate or re-allocate a memory buffer pointer.
unsigned long | Size (void *ptr)
| Return the current size of a buffer of memory.
char * | StrCat (const char *s1, const char *s2)
| Concatenate two strings using a managed buffer.
char * | StrDup (const char *string)
| Duplicate a string using a managed buffer.
virtual | ~MemoryManager ()
| Delete all the memory buffers.
## Private Attributes
Buffer * | f_head

## Detailed Description

The MemoryManager class is used as a garbage collection for all the object derived from it. These objects will allocate memory buffers and attach them to the collection of buffers in their memory manager.

Then when the object is deleted, all the buffers are automatically deleted.

The sswf::MemoryManager uses the Buffer and sswf::MemBuffer classes to manage the buffers.

See also:
  sswf::Buffer

  sswf::MemBuffer

## Constructor & Destructor Documentation

MemoryManager::MemoryManager | ( | void |  | ) |

This constructor initialize the memory manager by clearing the head of the memory buffers.

References f_head.

MemoryManager::~MemoryManager | ( |  | ) | `[virtual]`

The destructor goes through the list of memory buffers allocated by the memory manager functions and delete them all.

References f_head.

## Member Function Documentation

void * MemoryManager::MemAlloc | ( | size_t | size,
|  | const char * | info |
| ) |  |  |

The memory manager allocates a new buffer and returns the pointer to the data of the buffer.

Note that the allocation of the sswf::Buffer object is hidden from view.

Parameters:

  | size | Size of the memory block to allocate
  | info | Information about the buffer (a static const string)

Returns:
  A pointer to the newly allocated memory block

References sswf::Buffer::Data(), and f_head.

Referenced by sswf::TagText::AddText(), sswf::TagEditText::AddUsedString(), sswf::TagText::DefineText(), sswf::Data::GetString(), sswf::TagImage::LoadJPEG(), sswf::TagImage::LoadTGA(), sswf::TagSound::LoadWaveFile(), MemRealloc(), sswf::TagFrameLabel::ParseTag(), sswf::TagEditText::PreSave(), sswf::TagFont::PreSave2ndPass(), sswf::TagMetadata::Save(), sswf::TagImage::Save(), sswf::TagHeader::Save(), sswf::TagHeader::SaveEncodedString(), sswf::TagImage::SaveJPEG(), sswf::TagSound::SetData(), sswf::TagBinaryData::SetData(), sswf::TagSceneFrameData::SetFileData(), sswf::TagSceneFrameData::SetSceneFrameData(), sswf::TagEditText::SetUsedGlyphs(), StrCat(), and StrDup().

void MemoryManager::MemAttach | ( | MemBuffer * | ptr,
|  | size_t | size,
|  | const char * | info |
| ) |  |  |

This function takes the specified memory buffer under control.

This means it will delete the buffer whenever the object is deleted or when MemFree() is called.

Parameters:

  | ptr | The pointer to take control over
  | size | The size of the buffer pointed by ptr
  | info | Information about the buffer (a static const string)

References f_head.

Referenced by sswf::ActionPushData::AddBoolean(), sswf::ActionPushData::AddDouble(), sswf::TagShape::AddEdge(), sswf::SoundInfo::AddEnvelope(), sswf::ActionPushData::AddFloat(), sswf::TagFont::AddGlyph(), sswf::ActionPushData::AddInteger(), sswf::TagFont::AddKern(), sswf::ActionPushData::AddLookup(), sswf::TagShape::AddMove(), sswf::TagImport::AddName(), sswf::ActionPushData::AddNull(), sswf::ActionFunction::AddParameter(), sswf::ActionPushData::AddRegister(), sswf::ActionPushData::AddString(), sswf::ActionDictionary::AddString(), sswf::TagShape::AddStyle(), sswf::TagText::AddText(), sswf::ActionPushData::AddUndefined(), sswf::Action::FindLabelAtOffset(), sswf::TagShape::NewEdges(), sswf::TagShape::NewSetup(), sswf::TagShape::NewStyles(), sswf::ActionPushData::ParseData(), sswf::ActionDictionary::ParseData(), sswf::TagExport::ParseTag(), sswf::TagButton::ParseTag(), sswf::TagText::RecordSetup(), sswf::Edges::Set(), sswf::TagDoAction::SetAction(), sswf::TagButton::SetAction(), sswf::TagExport::SetObject(), and sswf::TagButton::SetState().

void MemoryManager::MemClean | ( | void * | ptr | ) |

This function is a convenience function which (1) calls MemFree() on the specified pointer and then clears the pointer.

This is particularly useful to not have to do the cleaning of the pointer yourself.

            char *buf;

            buf = MemAlloc(123);
            ...
            if(some flag) {
                    MemClean(&buf);

                    // is equivalent to:
                    //  MemFree(buf);
                    //  buf = 0;
            }
            ...
            if(buf != 0) {
                    ...
            }

Note:
  The compilers have a problem with a 'void **' type here. Thus I use 'void *' which can look a bit confusing. Sorry about that!

Parameters:

  | ptr | The pointer to the pointer to free

References sswf::assert(), and sswf::Buffer::FindBuffer().

Referenced by sswf::Data::GetString(), sswf::ActionFunction::ParseData(), sswf::TagProtect::ParseTag(), sswf::TagFrameLabel::ParseTag(), sswf::TagText::PreSave(), sswf::TagFont::PreSave2ndPass(), sswf::TagSceneFrameData::SetFileData(), sswf::TagImage::SetFilename(), sswf::TagImage::SetImage(), sswf::TagSceneFrameData::SetSceneFrameData(), and sswf::Vectors::SetSize().

void MemoryManager::MemFree | ( | void * | ptr | ) |

This function release the memory associated with the specified pointer.

Parameters:

  | ptr | The pointer to the memory to release

References sswf::Buffer::FindBuffer().

Referenced by sswf::TagEditText::AddUsedString(), sswf::TagSound::LoadWaveFile(), sswf::TagEditText::PreSave(), sswf::TagMetadata::Save(), sswf::TagImage::Save(), sswf::TagHeader::Save(), sswf::TagHeader::SaveEncodedString(), sswf::TagImage::SaveJPEG(), sswf::TagMetadata::SetAuthor(), sswf::TagMetadata::SetCopyright(), sswf::TagFont::SetCopyright(), sswf::TagSound::SetData(), sswf::TagBinaryData::SetData(), sswf::TagMetadata::SetDescription(), sswf::TagFont::SetDisplayName(), sswf::TagProtect::SetEncodedPassword(), sswf::TagMetadata::SetFilename(), sswf::TagImport::SetFilename(), sswf::TagImage::SetFilename(), sswf::TagFrameLabel::SetFrameLabel(), sswf::ActionWaitForFrame::SetFrameName(), sswf::ActionGoto::SetFrameName(), sswf::ActionTry::SetIdentifier(), sswf::TagInfo::SetInfo(), sswf::TagBase::SetLabel(), sswf::ActionLabel::SetLabel(), sswf::ActionBranch::SetLabel(), sswf::TagMetadata::SetMetadata(), sswf::TagPlace::SetName(), sswf::TagFont::SetName(), sswf::ActionFunction::SetName(), sswf::TagHeader::SetOutputEncoding(), sswf::TagProtect::SetPassword(), sswf::TagMetadata::SetPublisher(), sswf::ActionSetTarget::SetTarget(), sswf::TagEditText::SetText(), sswf::TagMetadata::SetTitle(), sswf::TagMetadata::SetURL(), sswf::TagImport::SetURL(), sswf::ActionURL::SetURL(), sswf::TagEditText::SetUsedGlyphs(), and sswf::TagEditText::SetVariableName().

void * MemoryManager::MemRealloc | ( | void * | ptr,
|  | size_t | size,
|  | const char * | info |
| ) |  |  |

This function is used to enlarge or reduce the size of a previously allocated buffer or to allocate a new buffer.

When ptr is set to NULL, it behaves like sswf::MemoryManager::MemAlloc().

When ptr is not NULL, then the function searches the buffer. If the buffer was not previously allocated by MemAlloc() or some similar function, then an assert(3) will be emited in debug mode. In release, the buffer will be assumed correct and you will corrupt your memory.

Parameters:

  | ptr | The pointer to re-allocate
  | size | The size of the new buffer
  | info | Information about the buffer (a static const string)

Returns:
  The pointer to the reallocated memory block

References sswf::Buffer::FindBuffer(), MemAlloc(), and sswf::Buffer::Realloc().

Referenced by sswf::Data::AdjustSize(), sswf::Data::GetString(), sswf::TagSound::LoadMP3File(), sswf::TagBinaryData::ParseTag(), and sswf::Vectors::SetSize().

unsigned long MemoryManager::Size | ( | void * | ptr | ) |

This function searches for the specified buffer and returns its size.

Parameters:

  | ptr | The pointer of which the size is to be returned

Returns:
  The size of the buffer pointed by that pointer

References sswf::Buffer::FindBuffer(), and sswf::Buffer::Size().

Referenced by sswf::TagText::DefineText().

char * MemoryManager::StrCat | ( | const char * | s1,
|  | const char * | s2 |
| ) |  |  |

This function concatenates two strings into one. The new string buffer is allocated using the memory manager.

Parameters:

  | s1 | The string to appear on the left
  | s2 | The string to appear on the right

Returns:
  The result is a pointer to the newly allocated string

References MemAlloc(), and StrDup().

Referenced by sswf::TagMetadata::Save().

char * MemoryManager::StrDup | ( | const char * | str | ) |

This function is used to duplicate a memory which buffer will be held in the memory manager.

Parameters:

  | str | The string to duplicate

Returns:
  A pointer to the duplicate of the input string

References MemAlloc().

Referenced by sswf::TagImport::AddName(), sswf::ActionFunction::AddParameter(), sswf::ActionPushData::AddString(), sswf::ActionDictionary::AddString(), sswf::TagMetadata::Save(), sswf::TagMetadata::SetAuthor(), sswf::TagMetadata::SetCopyright(), sswf::TagFont::SetCopyright(), sswf::TagMetadata::SetDescription(), sswf::TagFont::SetDisplayName(), sswf::TagProtect::SetEncodedPassword(), sswf::TagMetadata::SetFilename(), sswf::TagImport::SetFilename(), sswf::TagFrameLabel::SetFrameLabel(), sswf::ActionWaitForFrame::SetFrameName(), sswf::ActionGoto::SetFrameName(), sswf::ActionTry::SetIdentifier(), sswf::TagInfo::SetInfo(), sswf::TagBase::SetLabel(), sswf::ActionLabel::SetLabel(), sswf::ActionBranch::SetLabel(), sswf::TagMetadata::SetMetadata(), sswf::TagPlace::SetName(), sswf::TagFont::SetName(), sswf::ActionFunction::SetName(), sswf::TagExport::SetObject(), sswf::TagHeader::SetOutputEncoding(), sswf::TagProtect::SetPassword(), sswf::TagMetadata::SetPublisher(), sswf::ActionSetTarget::SetTarget(), sswf::TagEditText::SetText(), sswf::TagMetadata::SetTitle(), sswf::TagMetadata::SetURL(), sswf::TagImport::SetURL(), sswf::ActionURL::SetURL(), sswf::TagEditText::SetVariableName(), and StrCat().

## Member Data Documentation

Buffer* sswf::MemoryManager::f_head`[private]`

Referenced by MemAlloc(), MemAttach(), MemoryManager(), and ~MemoryManager().

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

- libsswf.h
- libsswf_memory.c++