csutil/customallocated.h Source File (Crystal Space 2.0 Public API Reference)

| ## Public API Reference

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

csutil/customallocated.h
Go to the documentation of this file.

    00001 /*
    00002   Copyright (C) 2007 by Frank Richter
    00003
    00004   This library is free software; you can redistribute it and/or
    00005   modify it under the terms of the GNU Library General Public
    00006   License as published by the Free Software Foundation; either
    00007   version 2 of the License, or (at your option) any later version.
    00008
    00009   This library is distributed in the hope that it will be useful,
    00010   but WITHOUT ANY WARRANTY; without even the implied warranty of
    00011   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    00012   Library General Public License for more details.
    00013
    00014   You should have received a copy of the GNU Library General Public
    00015   License along with this library; if not, write to the Free
    00016   Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
    00017 */
    00018
    00019 #ifndef __CS_CSUTIL_CUSTOMALLOCATED_H__
    00020 #define __CS_CSUTIL_CUSTOMALLOCATED_H__
    00021
    00022 #include "csutil/custom_new_disable.h"
    00023
    00028 namespace CS
    00029 {
    00030   namespace Memory
    00031   {
    00046     class CustomAllocated
    00047     {
    00048     public:
    00049       // Potentially throwing versions
    00050     #ifndef CS_NO_EXCEPTIONS
    00051       CS_FORCEINLINE void* operator new (size_t s) throw (std::bad_alloc)
    00052       {
    00053         void* p = cs_malloc (s);
    00054         if (!p) throw std::bad_alloc();
    00055         return p;
    00056       }
    00057       CS_FORCEINLINE void* operator new[] (size_t s) throw (std::bad_alloc)
    00058       {
    00059         void* p = cs_malloc (s);
    00060         if (!p) throw std::bad_alloc();
    00061         return p;
    00062       }
    00063     #else
    00064       CS_FORCEINLINE void* operator new (size_t s) throw ()
    00065       { return cs_malloc (s); }
    00066       CS_FORCEINLINE void* operator new[] (size_t s) throw ()
    00067       { return cs_malloc (s); }
    00068     #endif
    00069
    00070       CS_FORCEINLINE void operator delete (void* p) throw()
    00071       { cs_free (p); }
    00072       CS_FORCEINLINE void operator delete[] (void* p) throw()
    00073       { cs_free (p); }
    00074
    00075       // Nothrow versions
    00076       CS_FORCEINLINE void* operator new (size_t s, const std::nothrow_t&) throw()
    00077       { return cs_malloc (s); }
    00078       CS_FORCEINLINE void* operator new[] (size_t s, const std::nothrow_t&) throw()
    00079       { return cs_malloc (s); }
    00080       CS_FORCEINLINE void operator delete (void* p, const std::nothrow_t&) throw()
    00081       { cs_free (p); }
    00082       CS_FORCEINLINE void operator delete[] (void* p, const std::nothrow_t&) throw()
    00083       { cs_free (p); }
    00084
    00085       // Placement versions
    00086       CS_FORCEINLINE void* operator new(size_t /*s*/, void* p) throw() { return p; }
    00087       CS_FORCEINLINE void* operator new[](size_t /*s*/, void* p) throw() { return p; }
    00088
    00089       CS_FORCEINLINE void operator delete(void*, void*) throw() { }
    00090       CS_FORCEINLINE void operator delete[](void*, void*) throw() { }
    00091
    00092     #if defined(CS_EXTENSIVE_MEMDEBUG) || defined(CS_MEMORY_TRACKER)
    00093       CS_FORCEINLINE void* operator new (size_t s, void*, int)
    00094       { return cs_malloc (s); }
    00095       CS_FORCEINLINE void operator delete (void* p, void*, int)
    00096       { cs_free (p); }
    00097       CS_FORCEINLINE void* operator new[] (size_t s,  void*, int)
    00098       { return cs_malloc (s); }
    00099       CS_FORCEINLINE void operator delete[] (void* p, void*, int)
    00100       { cs_free (p); }
    00101     #endif
    00102     };
    00103
    00111     template<typename T>
    00112     class CustomAllocatedDerived : public T
    00113     {
    00114     public:
    00115       CustomAllocatedDerived () {}
    00116       template<typename A>
    00117       CustomAllocatedDerived (const A& a) : T (a) {}
    00118
    00119       // Potentially throwing versions
    00120     #ifndef CS_NO_EXCEPTIONS
    00121       CS_FORCEINLINE_TEMPLATEMETHOD void* operator new (size_t s) throw (std::bad_alloc)
    00122       {
    00123         void* p = cs_malloc (s);
    00124         if (!p) throw std::bad_alloc();
    00125         return p;
    00126       }
    00127       CS_FORCEINLINE_TEMPLATEMETHOD void* operator new[] (size_t s) throw (std::bad_alloc)
    00128       {
    00129         void* p = cs_malloc (s);
    00130         if (!p) throw std::bad_alloc();
    00131         return p;
    00132       }
    00133     #else
    00134       CS_FORCEINLINE_TEMPLATEMETHOD void* operator new (size_t s) throw ()
    00135       { return cs_malloc (s); }
    00136       CS_FORCEINLINE_TEMPLATEMETHOD void* operator new[] (size_t s) throw ()
    00137       { return cs_malloc (s); }
    00138     #endif
    00139
    00140       CS_FORCEINLINE_TEMPLATEMETHOD void operator delete (void* p) throw()
    00141       { cs_free (p); }
    00142       CS_FORCEINLINE_TEMPLATEMETHOD void operator delete[] (void* p) throw()
    00143       { cs_free (p); }
    00144
    00145       // Nothrow versions
    00146       CS_FORCEINLINE_TEMPLATEMETHOD void* operator new (size_t s, const std::nothrow_t&) throw()
    00147       { return cs_malloc (s); }
    00148       CS_FORCEINLINE_TEMPLATEMETHOD void* operator new[] (size_t s, const std::nothrow_t&) throw()
    00149       { return cs_malloc (s); }
    00150       CS_FORCEINLINE_TEMPLATEMETHOD void operator delete (void* p, const std::nothrow_t&) throw()
    00151       { cs_free (p); }
    00152       CS_FORCEINLINE_TEMPLATEMETHOD void operator delete[] (void* p, const std::nothrow_t&) throw()
    00153       { cs_free (p); }
    00154
    00155       // Placement versions
    00156       CS_FORCEINLINE_TEMPLATEMETHOD void* operator new(size_t /*s*/, void* p) throw() { return p; }
    00157       CS_FORCEINLINE_TEMPLATEMETHOD void* operator new[](size_t /*s*/, void* p) throw() { return p; }
    00158
    00159       CS_FORCEINLINE_TEMPLATEMETHOD void operator delete(void*, void*) throw() { }
    00160       CS_FORCEINLINE_TEMPLATEMETHOD void operator delete[](void*, void*) throw() { }
    00161
    00162     #if defined(CS_EXTENSIVE_MEMDEBUG) || defined(CS_MEMORY_TRACKER)
    00163       CS_FORCEINLINE_TEMPLATEMETHOD void* operator new (size_t s, void*, int)
    00164       { return cs_malloc (s); }
    00165       CS_FORCEINLINE_TEMPLATEMETHOD void operator delete (void* p, void*, int)
    00166       { cs_free (p); }
    00167       CS_FORCEINLINE_TEMPLATEMETHOD void* operator new[] (size_t s,  void*, int)
    00168       { return cs_malloc (s); }
    00169       CS_FORCEINLINE_TEMPLATEMETHOD void operator delete[] (void* p, void*, int)
    00170       { cs_free (p); }
    00171     #endif
    00172     };
    00173   } // namespace Memory
    00174 } // namespace CS
    00175
    00176 #include "csutil/custom_new_enable.h"
    00177
    00178 #endif // __CS_CSUTIL_CUSTOMALLOCATED_H__