Main Page   Class Hierarchy   Compound List   File List   Compound Members   File Members

CustomNew.cpp

Go to the documentation of this file.
00001 /*********************************************************************************
00002  *
00003  * Razor! Engine - A modular C++ presentation engine
00004  *
00005  * $Id$
00006  *
00007  * Copyright (c) 2000 Tilo Christ. All Rights Reserved.
00008  *
00009  * This library is free software; you can redistribute it and/or
00010  * modify it under the terms of the GNU Lesser General Public
00011  * License as published by the Free Software Foundation; either
00012  * version 2.1 of the License, or (at your option) any later version.
00013  *
00014  * This library is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017  * Lesser General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU Lesser General Public
00020  * License along with this library; if not, write to the Free Software
00021  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022  *
00023  **********************************************************************************/
00024 
00025 
00026 #include <PalmOS.h>
00027 #include "Device.h"
00028 #include "MemLeak.h"
00029 
00030 /**
00031  * @file CustomNew.cpp
00032  * Overloaded global operators new and delete. The default operators
00033  * throw an std:bad_alloc exception, which requires C++ exception
00034  * handling to be enabled. The customized versions will invoke
00035  * Device::panic in case of insufficient memory.
00036  */
00037 
00038 
00039 /**
00040  * Operator new that panics instead of throwing std::bad_alloc.
00041  */
00042 void * operator new (UInt32 size)
00043 {
00044     // C++ specs require the allocation of at least one byte, regardless of request size
00045     if (size == 0L)
00046         size = 1L;
00047 
00048     void *p = SafeMemPtrNew(size);
00049     
00050     if (p == NULL)
00051         Device::panic(memErrNotEnoughSpace, "Not enough memory for operator new");
00052 
00053     return p;               
00054 }
00055 
00056 
00057 /**
00058  * Operator new that panics instead of throwing std::bad_alloc.
00059  */
00060 void * operator new[] (UInt32 size)
00061 {
00062     // C++ specs require the allocation of at least one byte, regardless of request size
00063     if (size == 0L)
00064         size = 1L;
00065 
00066     void *p = SafeMemPtrNew(size);
00067     
00068     if (p == NULL)
00069         Device::panic(memErrNotEnoughSpace, "Not enough memory for operator new");
00070 
00071     return p;               
00072 }
00073 
00074 
00075 /**
00076  * Operator delete that is compatible with the customized operators new.
00077  */
00078 void operator delete(void* p)
00079 {
00080     Err error = SafeMemPtrFree(p);
00081     
00082     // It's probably a good idea to stop a program which has corrupted memory
00083     ErrFatalDisplayIf(error != errNone, "Invalid pointer for delete.");
00084 }
00085 
00086 
00087 /**
00088  * Operator delete that is compatible with the customized operators new.
00089  */
00090 void operator delete[] (void* p)
00091 {
00092     Err error = SafeMemPtrFree(p);
00093     
00094     // It's probably a good idea to stop a program which has corrupted memory
00095     ErrFatalDisplayIf(error != errNone, "Invalid pointer for delete.");
00096 }

Razor! Engine Developer's Guide. Copyright © by Tilo Christ. All Rights Reserved. Last updated: 4 Nov 2000