VirtualBox

Changeset 11008 in vbox


Ignore:
Timestamp:
Jul 30, 2008 6:04:09 PM (16 years ago)
Author:
vboxsync
Message:

iprt: Moved RTMemAutoPtr into iprt/mem.h.

Location:
trunk/include/iprt
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/iprt/autores.h

    r11007 r11008  
    194194};
    195195
    196 
    197 /**
    198  * Template function wrapping RTMemFree to get the correct Destruct
    199  * signature for RTAutoRes.
    200  *
    201  * We can't use a more complex template here, because the g++ on RHEL 3
    202  * chokes on it with an internal compiler error.
    203  *
    204  * @param   T           The data type that's being managed.
    205  * @param   aMem        Pointer to the memory that should be free.
    206  */
    207 template <class T>
    208 void RTMemAutoFree(T *aMem)
    209 {
    210     RTMemFree(aMem);
    211 }
    212 
    213 
    214 /**
    215  * Template function wrapping NULL to get the correct NilRes signature
    216  * for RTAutoRes.
    217  *
    218  * @param   T           The data type that's being managed.
    219  * @returns NULL with the right type.
    220  */
    221 template <class T>
    222 T *RTMemAutoNil(void)
    223 {
    224     return (T *)(NULL);
    225 }
    226 
    227 
    228 /**
    229  * An auto pointer-type template class for managing memory allocating
    230  * via C APIs like RTMem (the default).
    231  *
    232  * The main purpose of this class is to automatically free memory that
    233  * isn't explicitly used (released()) when the object goes out of scope.
    234  *
    235  * As an additional service it can also make the allocations and
    236  * reallocations for you if you like, but it can also take of memory
    237  * you hand it.
    238  *
    239  * @param   T           The data type to manage allocations for.
    240  * @param   Destruct    The function to be used to free the resource.
    241  *                      This will default to RTMemFree.
    242  * @param   Allocator   The function to be used to allocate or reallocate
    243  *                      the managed memory.
    244  *                      This is standard realloc() like stuff, so it's possible
    245  *                      to support simple allocation without actually having
    246  *                      to support reallocating memory if that's a problem.
    247  *                      This will default to RTMemRealloc.
    248  */
    249 template <class T, void Destruct(T *) = RTMemAutoFree<T>, void *Allocator(void *, size_t) = RTMemRealloc >
    250 class RTMemAutoPtr
    251     : public RTAutoRes<T *, Destruct, RTMemAutoNil<T> >
    252 {
    253 public:
    254     /**
    255      * Constructor.
    256      *
    257      * @param   aPtr    Memory pointer to manage. Defaults to NULL.
    258      */
    259     RTMemAutoPtr(T *aPtr = NULL)
    260         : RTAutoRes<T *, Destruct, RTMemAutoNil<T> >(aPtr)
    261     {
    262     }
    263 
    264     /**
    265      * Free current memory and start managing aPtr.
    266      *
    267      * @param   aPtr    Memory pointer to manage.
    268      */
    269     RTMemAutoPtr &operator=(T *aPtr)
    270     {
    271         this->RTAutoRes<T *, Destruct, RTMemAutoNil<T> >::operator=(aPtr);
    272         return *this;
    273      }
    274 
    275     /**
    276      * Dereference with * operator.
    277      */
    278     T &operator*()
    279     {
    280          return *this->get();
    281     }
    282 
    283     /**
    284      * Dereference with -> operator.
    285      */
    286     T *operator->()
    287     {
    288         return this->get();
    289     }
    290 
    291     /**
    292      * Accessed with the subscript operator ([]).
    293      *
    294      * @returns Reference to the element.
    295      * @param   a_i     The element to access.
    296      */
    297     T &operator[](size_t a_i)
    298     {
    299         return this->get()[a_i];
    300     }
    301 
    302     /**
    303      * Allocates memory and start manage it.
    304      *
    305      * Any previously managed memory will be freed before making
    306      * the new allocation.
    307      *
    308      * The content of the new memory is undefined when using
    309      * the default allocator.
    310      *
    311      * @returns Success indicator.
    312      * @retval  true if the new allocation succeeds.
    313      * @retval  false on failure, no memory is associated with the object.
    314      *
    315      * @param   cElements   The new number of elements (of the data type) to allocate.
    316      *                      This defaults to 1.
    317      */
    318     bool alloc(size_t a_cElements = 1)
    319     {
    320         this->reset(NULL);
    321         T *aNewValue = (T *)(Allocator(NULL, a_cElements * sizeof(T)));
    322         this->reset(aNewValue);
    323         return aNewValue != NULL;
    324     }
    325 
    326     /**
    327      * Reallocate or allocates the memory resource.
    328      *
    329      * Free the old value if allocation fails.
    330      *
    331      * The content of any additional memory that was allocated is
    332      * undefined when using the default allocator.
    333      *
    334      * @returns Success indicator.
    335      * @retval  true if the new allocation succeeds.
    336      * @retval  false on failure, no memory is associated with the object.
    337      *
    338      * @param   cElements   The new number of elements (of the data type) to allocate.
    339      *                      The size of the allocation is the number of elements times
    340      *                      the size of the data type - this is currently what's passed
    341      *                      down to the Allocator.
    342      *                      This defaults to 1.
    343      */
    344     bool realloc(size_t a_cElements = 1)
    345     {
    346         T *aNewValue = (T *)(Allocator(this->get(), a_cElements * sizeof(T)));
    347         if (aNewValue != NULL)
    348             this->release();
    349         /* We want this both if aNewValue is non-NULL and if it is NULL. */
    350         this->reset(aNewValue);
    351         return aNewValue != NULL;
    352     }
    353 };
    354 
    355196#endif
    356197
  • trunk/include/iprt/mem.h

    r8245 r11008  
    3434#include <iprt/cdefs.h>
    3535#include <iprt/types.h>
     36#ifdef __cplusplus
     37# include <iprt/autores.h>
     38#endif
     39
    3640
    3741#ifdef IN_GC
     
    3943#endif
    4044
    41 __BEGIN_DECLS
    4245
    4346/** @defgroup grp_rt_mem    RTMem - Memory Management and Manipulation
     
    4548 * @{
    4649 */
     50
     51__BEGIN_DECLS
    4752
    4853/** @def RTMEM_ALIGNMENT
     
    361366/** @} */
    362367
     368__END_DECLS
     369
     370
     371#ifdef __cplusplus
     372
     373/**
     374 * Template function wrapping RTMemFree to get the correct Destruct
     375 * signature for RTAutoRes.
     376 *
     377 * We can't use a more complex template here, because the g++ on RHEL 3
     378 * chokes on it with an internal compiler error.
     379 *
     380 * @param   T           The data type that's being managed.
     381 * @param   aMem        Pointer to the memory that should be free.
     382 */
     383template <class T>
     384void RTMemAutoFree(T *aMem)
     385{
     386    RTMemFree(aMem);
     387}
     388
     389
     390/**
     391 * Template function wrapping NULL to get the correct NilRes signature
     392 * for RTAutoRes.
     393 *
     394 * @param   T           The data type that's being managed.
     395 * @returns NULL with the right type.
     396 */
     397template <class T>
     398T *RTMemAutoNil(void)
     399{
     400    return (T *)(NULL);
     401}
     402
     403
     404/**
     405 * An auto pointer-type template class for managing memory allocating
     406 * via C APIs like RTMem (the default).
     407 *
     408 * The main purpose of this class is to automatically free memory that
     409 * isn't explicitly used (released()) when the object goes out of scope.
     410 *
     411 * As an additional service it can also make the allocations and
     412 * reallocations for you if you like, but it can also take of memory
     413 * you hand it.
     414 *
     415 * @param   T           The data type to manage allocations for.
     416 * @param   Destruct    The function to be used to free the resource.
     417 *                      This will default to RTMemFree.
     418 * @param   Allocator   The function to be used to allocate or reallocate
     419 *                      the managed memory.
     420 *                      This is standard realloc() like stuff, so it's possible
     421 *                      to support simple allocation without actually having
     422 *                      to support reallocating memory if that's a problem.
     423 *                      This will default to RTMemRealloc.
     424 */
     425template <class T, void Destruct(T *) = RTMemAutoFree<T>, void *Allocator(void *, size_t) = RTMemRealloc >
     426class RTMemAutoPtr
     427    : public RTAutoRes<T *, Destruct, RTMemAutoNil<T> >
     428{
     429public:
     430    /**
     431     * Constructor.
     432     *
     433     * @param   aPtr    Memory pointer to manage. Defaults to NULL.
     434     */
     435    RTMemAutoPtr(T *aPtr = NULL)
     436        : RTAutoRes<T *, Destruct, RTMemAutoNil<T> >(aPtr)
     437    {
     438    }
     439
     440    /**
     441     * Free current memory and start managing aPtr.
     442     *
     443     * @param   aPtr    Memory pointer to manage.
     444     */
     445    RTMemAutoPtr &operator=(T *aPtr)
     446    {
     447        this->RTAutoRes<T *, Destruct, RTMemAutoNil<T> >::operator=(aPtr);
     448        return *this;
     449     }
     450
     451    /**
     452     * Dereference with * operator.
     453     */
     454    T &operator*()
     455    {
     456         return *this->get();
     457    }
     458
     459    /**
     460     * Dereference with -> operator.
     461     */
     462    T *operator->()
     463    {
     464        return this->get();
     465    }
     466
     467    /**
     468     * Accessed with the subscript operator ([]).
     469     *
     470     * @returns Reference to the element.
     471     * @param   a_i     The element to access.
     472     */
     473    T &operator[](size_t a_i)
     474    {
     475        return this->get()[a_i];
     476    }
     477
     478    /**
     479     * Allocates memory and start manage it.
     480     *
     481     * Any previously managed memory will be freed before making
     482     * the new allocation.
     483     *
     484     * The content of the new memory is undefined when using
     485     * the default allocator.
     486     *
     487     * @returns Success indicator.
     488     * @retval  true if the new allocation succeeds.
     489     * @retval  false on failure, no memory is associated with the object.
     490     *
     491     * @param   cElements   The new number of elements (of the data type) to allocate.
     492     *                      This defaults to 1.
     493     */
     494    bool alloc(size_t a_cElements = 1)
     495    {
     496        this->reset(NULL);
     497        T *aNewValue = (T *)(Allocator(NULL, a_cElements * sizeof(T)));
     498        this->reset(aNewValue);
     499        return aNewValue != NULL;
     500    }
     501
     502    /**
     503     * Reallocate or allocates the memory resource.
     504     *
     505     * Free the old value if allocation fails.
     506     *
     507     * The content of any additional memory that was allocated is
     508     * undefined when using the default allocator.
     509     *
     510     * @returns Success indicator.
     511     * @retval  true if the new allocation succeeds.
     512     * @retval  false on failure, no memory is associated with the object.
     513     *
     514     * @param   cElements   The new number of elements (of the data type) to allocate.
     515     *                      The size of the allocation is the number of elements times
     516     *                      the size of the data type - this is currently what's passed
     517     *                      down to the Allocator.
     518     *                      This defaults to 1.
     519     */
     520    bool realloc(size_t a_cElements = 1)
     521    {
     522        T *aNewValue = (T *)(Allocator(this->get(), a_cElements * sizeof(T)));
     523        if (aNewValue != NULL)
     524            this->release();
     525        /* We want this both if aNewValue is non-NULL and if it is NULL. */
     526        this->reset(aNewValue);
     527        return aNewValue != NULL;
     528    }
     529};
     530
     531#endif /* __cplusplus */
     532
     533
    363534/** @} */
    364535
    365 __END_DECLS
    366536
    367537#endif
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette