Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

/ray/src/lib/salloc.h File Reference

Standard memory (de)allocation routines which should be used instead of malloc()/free(). Also implements default C++ operators new and delete. More...

#include <lib/sconfig.h>

Include dependency graph for salloc.h:

Include dependency graph

This graph shows which files directly or indirectly include this file:

Included by dependency graph

Go to the source code of this file.

Classes

struct  LMallocUsage

Defines

#define _SRC_SALLOC_H_   1

Functions

void LMallocGetUsage (struct LMallocUsage *dest)
void LMallocSetLimit (size_t limit)
void _AllocFailure (size_t size)
 Report allocation failure error and exit (internal).

void * LMalloc (size_t size)
 Allocate some memory (internal).

void * LFree (void *ptr)
 Free memory allocated via LMalloc (internal).

void * LRealloc (void *ptr, size_t size)
 Resize memory allocated via LMalloc (internal).

template<typename T> T * ALLOC (size_t nelem)
 Allocate array of PODs.

template<typename T> T * FREE (T *ptr)
 Free array of PODs.

template<typename T> T * REALLOC (T *ptr, size_t nelem)
 Re-allocate array of PODs.

void * operator new (size_t size)
 Standard C++ allocation operator for single objects.

void * operator new (size_t,void *ptr)
 C++ allocation operator for single objects with explicit placement.

void operator delete (void *ptr)
 Standard C++ deallocation operator for single objects.

template<typename T> void DELETE (T *&ptr)
 Improved version of operator delete.

void * operator new[] (size_t size)
 Standard C++ allocation operator for arrays.

void operator delete[] (void *ptr)
 Standard C++ deallocation operator for arrays.

template<typename T> void DELARRAY (T *&ptr)
 Better version of operator delete[].


Detailed Description

Standard memory (de)allocation routines which should be used instead of malloc()/free(). Also implements default C++ operators new and delete.

Author:
Wolfgang Wieser ] wwieser (a) gmx <*> de [

Definition in file salloc.h.


Define Documentation

#define _SRC_SALLOC_H_   1
 

Definition at line 18 of file salloc.h.


Function Documentation

void _AllocFailure size_t  size  ) 
 

Report allocation failure error and exit (internal).

Todo:
use an exception or portable error reporting mechanisms.

Definition at line 435 of file salloc.cc.

References _LMallocExit().

Referenced by LMalloc(), and LRealloc().

template<typename T>
T* ALLOC size_t  nelem  )  [inline]
 

Allocate array of PODs.

Standard allocation and deallocation routines. NEVER USE malloc()/realloc()/free() directly.

These templates for simple (POD) types like int, double, etc. which do not have a constructor. Use operator new[] for objects with constructor. Mostly used to allocate buffers (char*) and the like.

Use FREE to free the memory again.

Will abort on allocation failure.

Parameters:
nelem number of elements of size sizeof(T) to allocate
Returns:
(properly casted) pointer to first element.
Returns NULL in case nelem=0.

Definition at line 134 of file salloc.h.

References LMalloc().

template<typename T>
void DELARRAY T *&  ptr  )  [inline]
 

Better version of operator delete[].

Better use this version of the delete[] operator than operator delete[] directly since this will also set the passed pointer to NULL (and only delete non-NULL arrays).

Definition at line 269 of file salloc.h.

Referenced by VM::NonResizeableArray< VTableEntry, uint32 >::_free().

template<typename T>
void DELETE T *&  ptr  )  [inline]
 

Improved version of operator delete.

Better use this version of the delete operator than operator delete directly since this will also set the passed pointer to NULL (and only delete non-NULL objects).

Definition at line 235 of file salloc.h.

Referenced by VM::VMLinker::_CreateInitFunction(), _InternalSourceFileNode::_DestroyPosCache(), VM::AssemblerFile_Plaintext::_ParseProgram_Label(), VM::AssemblerFile_Plaintext::_ParseSymbols_Class(), VM::AssemblerFile_Plaintext::_WriteFunctionProgram(), VM::VMLinker::LinkAll(), main(), _InternalFlexScannerBase::SetInput(), VM::AssemblerFile_Plaintext::WriteFile(), _InternalSourceFileNode::~_InternalSourceFileNode(), VM::AssemblerFile::~AssemblerFile(), VM::AssemblerFile_Plaintext::~AssemblerFile_Plaintext(), VM::VMLinker::FileNode::~FileNode(), and VM::VMLinker::~VMLinker().

template<typename T>
T* FREE T *  ptr  )  [inline]
 

Free array of PODs.

Standard allocation and deallocation routines. NEVER USE malloc()/realloc()/free() directly.

Free memory as allocated using ALLOC.

Parameters:
ptr pointer to the memory region to be freed.
Returns:
always returns NULL.
Note:
You should always use
   ptr = FREE(ptr);
to make sure that the pointer is NULL after freeing the memory.

Definition at line 155 of file salloc.h.

References LFree().

Referenced by _DoSortAndTest(), TLDynamicBitField< T >::_Free(), VM::VMLinker::_MergeNamespaceAndVTableInfo(), VM::VMLinker::_MergeNamespaceInfo_Recursive(), MYOP< T >::free(), TLDefaultOperators_Allocation::free(), main(), VM::InstructionStorage::~InstructionStorage(), NUM::RandomNumberGenerator_LibCRandom::~RandomNumberGenerator_LibCRandom(), and TLString::SData::~SData().

void* LFree void *  ptr  ) 
 

Free memory allocated via LMalloc (internal).

For internal use only.

These are the internal (de)allocation routines. Do not use these directly but use the below templates ALLOC, REALLOC, FREE instead.

Free memory as allocated by LMalloc or LRealloc. If called with a NULL pointer, nothing is done.

Parameters:
ptr pointer to the memory region as returned by LMalloc or LRealloc.
Returns:
always returns NULL.

Definition at line 453 of file salloc.cc.

References _LFree().

Referenced by _LRealloc(), FREE(), LRealloc(), operator delete(), and operator delete[]().

void* LMalloc size_t  size  ) 
 

Allocate some memory (internal).

For internal use only.

These are the internal (de)allocation routines. Do not use these directly but use the below templates ALLOC, REALLOC, FREE instead.

Parameters:
size desired size in bytes of the memory region
Returns:
always returns pointer to memory region; aborts using _AllocFailure on failure.
Return value is NULL if size=0.
See also:
ALLOC,REALLOC,FREE

Definition at line 443 of file salloc.cc.

References _AllocFailure(), and _LMalloc().

Referenced by _LRealloc(), ALLOC(), LRealloc(), operator new(), and operator new[]().

void LMallocGetUsage struct LMallocUsage dest  ) 
 

Get the LMallocUsage; pass a pointer where to store the values:

Definition at line 61 of file salloc.cc.

References lmu.

Referenced by main().

void LMallocSetLimit size_t  limit  ) 
 

Allocation limitation feature: Sets memory usage limit (0 -> no limit)

Definition at line 57 of file salloc.cc.

References LMallocUsage::alloc_limit, and lmu.

void* LRealloc void *  ptr,
size_t  size
 

Resize memory allocated via LMalloc (internal).

For internal use only.

These are the internal (de)allocation routines. Do not use these directly but use the below templates ALLOC, REALLOC, FREE instead.

Re-allocate memory allocated by LMalloc, i.e. change the size of the memory region. A pointer to the new memory region is returned.

  • If called with a NULL pointer, it behaves just as LMalloc(size)
  • If called with a size of 0, it it behaves exactly like LFree(ptr).
Parameters:
ptr pointer to the memory region as returned by LMalloc or LRealloc.
size new size in bytes of the memory region
Returns:
pointer to new memory region of specified size; aborts using _AllocFailure on failure.

Definition at line 461 of file salloc.cc.

References _AllocFailure(), _LRealloc(), LFree(), and LMalloc().

Referenced by REALLOC().

void operator delete void *  ptr  )  [inline]
 

Standard C++ deallocation operator for single objects.

Implementation of the (default) C++ deallocation operator for single objects (operator delete).

Note:
Use DELETE instead of operator delete for improved safety.

Definition at line 225 of file salloc.h.

References LFree().

void operator delete[] void *  ptr  )  [inline]
 

Standard C++ deallocation operator for arrays.

Implementation of the (default) C++ deallocation operator for arrays (operator delete[]).

Note:
Use DELARRAY instead of operator delete[] for improved safety.

Definition at line 259 of file salloc.h.

References LFree().

void* operator new size_t  ,
void *  ptr
[inline]
 

C++ allocation operator for single objects with explicit placement.

This operator new can be used to "allocate" a object at the specified memory region. No real memory allocation will be performed by the operator.

Actually, the standard operator new (without placement) can be "emulated" by this one as follows:

   A *obj=new A(x,y);
   // ...is just the same as...
   void *ptr=ALLOC<A>(1);  // one element
   A *obj=new(ptr) A(x,y);
   // ...and just the same as...
   void *ptr=ALLOC<char>(sizeof(A));
   A *obj=new(ptr) A(x,y);

Definition at line 214 of file salloc.h.

void* operator new size_t  size  )  [inline]
 

Standard C++ allocation operator for single objects.

Implementation of the (default) C++ allocation operator for single objects (operator new).

Definition at line 192 of file salloc.h.

References LMalloc().

void* operator new[] size_t  size  )  [inline]
 

Standard C++ allocation operator for arrays.

Implementation of the (default) C++ allocation operator for arrays (operator new[]).

Attention:
Use delete[] and not plain delete to free arrays.

Definition at line 247 of file salloc.h.

References LMalloc().

template<typename T>
T* REALLOC T *  ptr,
size_t  nelem
[inline]
 

Re-allocate array of PODs.

Standard allocation and deallocation routines. NEVER USE malloc()/realloc()/free() directly.

Resize memory area as returned by ALLOC

  • If ptr is NULL, acts like ALLOC(size).
  • If size==0, behaves like FREE(ptr).
  • Use FREE to free the memory again.
Will abort on allocation failure.

Parameters:
ptr pointer to the memory region to be resized
nelem nelem number of elements of size sizeof(T) to re-allocate
Returns:
new pointer to array with nelem elements of size sizeof(T) or NULL if size=0.
Note:
You should always use
   ptr = REALLOC(ptr,new_size);

Definition at line 182 of file salloc.h.

References LRealloc().

Referenced by TLString::SData::_DoTightenSize(), TLString::SData::_EnsureSize(), VM::InstructionStorage::_ReallocStore(), TLString::SData::DownSizeIfNeeded(), MYOP< T >::realloc(), TLDefaultOperators_Allocation::realloc(), NUM::RandomNumberGenerator_LibCRandom::seed(), and VM::InstructionStorage::TightenSize().


Generated on Sat Feb 19 22:34:28 2005 for Ray by doxygen 1.3.5