Polygon Crucher SDK - Documentation
Documentation
Loading...
Searching...
No Matches
XMemoryMacros.h File Reference

definitions of macros for SDK new / delete / allocation operators. More...

Detailed Description

definitions of macros for SDK new / delete / allocation operators.

#include <assert.h>

Go to the source code of this file.

Macros

#define xAllocateFunc(p_type, xfunc)    (p_type *)mootools::xfunc(sizeof(p_type))
 
#define xDeallocateFunc(p_pointer, xfunc)    mootools::xfunc(p_pointer)
 
#define xAllocateArrayFunc(p_type, p_count, xfunc)    (p_type *)mootools::xfunc(sizeof(p_type), p_count)
 
#define xDeallocateArrayFunc(p_pointer, p_count, xfunc)    mootools::xfunc(p_pointer, p_count)
 
Simple memory block allocation
  • xAllocate: Allocates memory for a single of type p_type.
  • xAllocateArray: Allocates memory for an array of type p_type.
  • xDeallocate: Deallocate memory pointed by p_pointer.
  • xDeallocateArray: Deallocate an memory array pointed by p_pointer.
    Parameters
    p_typeThe type of object to allocate.
    Returns
    A pointer to the allocated memory.
    Note
    xAllocation has no reallocation possibilities.
    This introduces incompatibility when MOOTOOLS_USE_NEW_AND_DELETE is defined, because malloc / free allocation are made using a new operator
    you can use xmalloc, xfree and xrealloc instead xAllocate if you want to perform standard reallocation.
    cf. note below
#define xAllocate(p_type)    xAllocateFunc(p_type, xmalloc)
 
#define xDeallocate(p_pointer, xfunc)    xDeallocateFunc(p_type, xfree)
 
#define xAllocateArray(p_type, p_count)    xAllocateArrayFunc(p_type, p_count, xmalloc_array)
 
#define xDeallocateArray(p_pointer)    xDeallocateArrayFunc(p_pointer, 1, xfree_array)
 
In-place constructor / destructor
  • xConstruct: Construct an object of type p_type at the specified memory location.
  • xConstructParams: Construct an object of type p_type at the specified memory location and using specific parameters.
  • xDestroy: Destroy an object pointed by p_pointer.
  • xConstructArray: Construct an array of objects of type p_type at the specified memory location.
  • xDestroyArray: Destroy an array of objects pointed by p_pointer.
Parameters
p_typeThe type of object to construct.
p_pointerThe memory location of the object.
p_countLe length of the array.
Returns
A pointer to the constructed object.
#define xConstruct(p_type, p_pointer)    new (p_pointer)p_type
 
#define xConstructParams(p_type, p_pointer, ...)    new (p_pointer)p_type(__VA_ARGS__)
 
#define xConstructArray(p_type, p_pointer, p_count)    mootools::xTConstructArray<p_type>(p_pointer, p_count)
 
#define xDestroyArray(p_pointer, p_count)    mootools::xTDestroyArray(p_pointer, p_count)
 
#define xDestroy(p_pointer)    mootools::xTDestroy(p_pointer)
 
Memory and inheritance

xCast is used to correctly destroy the memory allocated by an object in case of multi inheritance
using dynamic_cast for polymorphic object only. Without that the following code fails:
if class AB : public A, public B
AB *ab = xNew(AB);
B *b = (B *)ab; => b points to B object. b pointer is not egal to ab which is the allocated memory
xDelete(b); => fails, b value was never allocated

Note
xCast is automatically called by xNew, xDelete, xDeleteArray, so you don't have to worry
#define xCast(p_pointer)    mootools::xTCast(p_pointer)
 
New and delete operators
  • xNew: Construct an object of type p_type. Intended to be used in place of the default new operator.
  • xNewParams: Construct an object, providing constructor parameters in place of the default new operator
  • xNewArray: Creates an array of type p_type. Intended to be used in place of the default new[] operator.
  • xDelete: Deletes an object. Intended to be used in place of the default delete operator.
  • xDeleteArray: Deletes an array. Intended to be used in place of the default delete[] operator.
    Parameters
    p_typeThe type of object to create.
    p_countThe length of the array.
    Note
    If p_type is a template with multiple argument (separated by a comma) macro expansion fails. use a typedef in this case
#define xNew(p_type)    xConstruct(p_type, (xAllocateFunc(p_type, xnew)))
 
#define xNewParams(p_type, ...)    xConstructParams(p_type, xAllocateFunc(p_type, xnew), __VA_ARGS__)
 
#define xNewArray(p_type, p_count)    xConstructArray(p_type, xAllocateArrayFunc(p_type, p_count, xnew_array), p_count)
 
#define xDelete(p_pointer)
 
#define xDeleteArray(p_pointer, p_count)
 

Functions

Template functions for constructing and deleting objects
Note
Compiler optimization removes these functions if the calling class has no constructor or destructor.
This means that it does not consume any time when called on basic types (float, int...)
this also means that xAllocateArray / xDeallocateArray and xNewArray / xDeleteArray has similar performance for basic types.
Anyway xDeallocateArray has the advantage that it does not requires any count parameters.
template<typename T >
void xTDestroy (T *p_pointer)
 
template<typename T >
TxTConstructArray (T *p_pointer, size_t p_count)
 
template<typename T >
void xTDestroyArray (const T *p_pointer, size_t p_count)
 
template<typename T >
voidxTCast (T *p_pointer)
 

Macro Definition Documentation

◆ xDelete

#define xDelete (   p_pointer)
Value:
{ \
void *dynamic_casted_ptr = xCast(p_pointer); \
}
DLL_TOOLSFUNCTION void xdelete(void *p)
Only used by macros xDelete.
The class defines an x, y, z 3D point which can use int, float or double.
Definition 3DPoint.h:27

◆ xDeleteArray

#define xDeleteArray (   p_pointer,
  p_count 
)