/** @file
* IPRT - C++ Memory Resource Management.
*/
/*
* Copyright (C) 2006-2011 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*
* The contents of this file may alternatively be used under the terms
* of the Common Development and Distribution License Version 1.0
* (CDDL) only, as it comes in the "COPYING.CDDL" file of the
* VirtualBox OSE distribution, in which case the provisions of the
* CDDL are applicable instead of those of the GPL.
*
* You may elect to license modified versions of this file under the
* terms and conditions of either the GPL or the CDDL or both.
*/
#ifndef ___iprt_cpp_mem_h
#define ___iprt_cpp_mem_h
/** @defgroup grp_rt_cpp_autores_mem C++ Memory Resource Management
* @ingroup grp_rt_cpp_autores
* @{
*/
/**
* Template function wrapping RTMemFree to get the correct a_fnDestruct
* signature for RTCAutoRes.
*
* We can't use a more complex template here, because the g++ on RHEL 3
* chokes on it with an internal compiler error.
*
* @tparam T The data type that's being managed.
* @param aMem Pointer to the memory that should be free.
*/
{
}
/**
* RTCMemAutoPtr allocator which uses RTMemTmpAlloc().
*
* @returns Allocated memory on success, NULL on failure.
* @param pvOld What to reallocate, shall always be NULL.
* @param cbNew The amount of memory to allocate (in bytes).
*/
{
return RTMemTmpAlloc(cbNew);
}
/**
* Template function wrapping RTMemTmpFree to get the correct a_fnDestruct
* signature for RTCAutoRes.
*
* We can't use a more complex template here, because the g++ on RHEL 3
* chokes on it with an internal compiler error.
*
* @tparam T The data type that's being managed.
* @param aMem Pointer to the memory that should be free.
*/
{
}
/**
* Template function wrapping RTMemEfFree to get the correct a_fnDestruct
* signature for RTCAutoRes.
*
* We can't use a more complex template here, because the g++ on RHEL 3
* chokes on it with an internal compiler error.
*
* @tparam T The data type that's being managed.
* @param aMem Pointer to the memory that should be free.
*/
{
}
/**
* Template function wrapping NULL to get the correct NilRes signature
* for RTCAutoRes.
*
* @tparam T The data type that's being managed.
* @returns NULL with the right type.
*/
inline T *RTCMemAutoNil(void) RT_NO_THROW
{
return (T *)(NULL);
}
/**
* An auto pointer-type template class for managing memory allocating
* via C APIs like RTMem (the default).
*
* The main purpose of this class is to automatically free memory that
* isn't explicitly used (release()'ed) when the object goes out of scope.
*
* As an additional service it can also make the allocations and
* reallocations for you if you like, but it can also take of memory
* you hand it.
*
* @tparam T The data type to manage allocations for.
* @tparam a_fnDestruct The function to be used to free the resource.
* This will default to RTMemFree.
* @tparam a_fnAllocator The function to be used to allocate or reallocate
* the managed memory.
* This is standard realloc() like stuff, so it's
* possible to support simple allocation without
* actually having to support reallocating memory if
* that's a problem. This will default to
* RTMemRealloc.
*/
void a_fnDestruct(T *) = RTCMemAutoDestructor<T>,
# if defined(RTMEM_WRAP_TO_EF_APIS) && !defined(RTMEM_NO_WRAP_TO_EF_APIS)
# else
# endif
>
{
/**
* Constructor.
*
* @param aPtr Memory pointer to manage. Defaults to NULL.
*/
{
}
/**
* Constructor that allocates memory.
*
* @param a_cElements The number of elements (of the data type) to allocate.
* @param a_fZeroed Whether the memory should be memset with zeros after
* the allocation. Defaults to false.
*/
: RTCAutoRes<T *, a_fnDestruct, RTCMemAutoNil<T> >((T *)a_fnAllocator(NULL, a_cElements * sizeof(T), RTMEM_TAG))
{
}
/**
* Free current memory and start managing aPtr.
*
* @param aPtr Memory pointer to manage.
*/
{
return *this;
}
/**
* Dereference with * operator.
*/
T &operator*()
{
}
/**
* Dereference with -> operator.
*/
T *operator->()
{
}
/**
* Accessed with the subscript operator ([]).
*
* @returns Reference to the element.
* @param a_i The element to access.
*/
{
}
/**
* Allocates memory and start manage it.
*
* Any previously managed memory will be freed before making
* the new allocation.
*
* @returns Success indicator.
* @retval true if the new allocation succeeds.
* @retval false on failure, no memory is associated with the object.
*
* @param a_cElements The number of elements (of the data type) to allocate.
* This defaults to 1.
* @param a_fZeroed Whether the memory should be memset with zeros after
* the allocation. Defaults to false.
*/
{
}
/**
* Reallocate or allocates the memory resource.
*
* Free the old value if allocation fails.
*
* The content of any additional memory that was allocated is
* undefined when using the default allocator.
*
* @returns Success indicator.
* @retval true if the new allocation succeeds.
* @retval false on failure, no memory is associated with the object.
*
* @param a_cElements The new number of elements (of the data type) to
* allocate. The size of the allocation is the number of
* elements times the size of the data type - this is
* currently what's passed down to the a_fnAllocator.
* This defaults to 1.
*/
{
/* We want this both if aNewValue is non-NULL and if it is NULL. */
}
};
/** @} */
#endif