00001 /* 00002 * PROPRIETARY INFORMATION. This software is proprietary to 00003 * Side Effects Software Inc., and is not to be reproduced, 00004 * transmitted, or disclosed in any way without written permission. 00005 * 00006 * Produced by: 00007 * George ElKoura 00008 * Side Effects Software Inc 00009 * 477 Richmond Street West 00010 * Toronto, Ontario 00011 * Canada M5V 3E7 00012 * 416-504-9876 00013 * 00014 * NAME: UT_NoMemHandler.h ( UT Library, C++) 00015 * 00016 * COMMENTS: 00017 * 00018 * This class handles memory allocation failures. Currently only 00019 * implemented for Windows and Linux. 00020 * 00021 */ 00022 #ifndef __UT_NoMemHandler_h__ 00023 #define __UT_NoMemHandler_h__ 00024 00025 #include "UT_API.h" 00026 #include <stdlib.h> 00027 00028 // A UT_NoMemHandlerCallback is a callback that the application can register 00029 // if it wants to be notified when memory allocation has failed. 00030 typedef void (*UT_NoMemHandlerCallback)(void); 00031 00032 // The maximum number of callbacks we allow. We do not need a dynamic 00033 // array for these since they should all be registered at code startup. 00034 #define UT_MEMHANDLER_MAX_CALLBACKS 16 00035 00036 class UT_API UT_NoMemHandler 00037 { 00038 public: 00039 // Global static accessor to UT_NoMemHandler singleton. 00040 static UT_NoMemHandler &getNoMemHandler(); 00041 00042 // Call this method to add a memory handler callback. When memory 00043 // allocation fails, these callbacks are called. Their current purpose is 00044 // to try to free some memory so that we can save before the inevitable 00045 // crash. 00046 void addCallback(UT_NoMemHandlerCallback callback); 00047 00048 // The main new handlers called by the C-runtime library. 00049 int classNewHandler(size_t size); 00050 00051 private: 00052 // Private constructor/destructor because no one is allowed to allocate us. 00053 UT_NoMemHandler(); 00054 ~UT_NoMemHandler(); 00055 00056 00057 // The registered callbacks 00058 UT_NoMemHandlerCallback myCallbacks[UT_MEMHANDLER_MAX_CALLBACKS]; 00059 int myNumCallbacks; 00060 00061 // This flag is used to avoid recursive calls into our handler in case 00062 // one of the callbacks called from it causes another malloc/new to fail. 00063 bool myInsideHandler; 00064 }; 00065 00066 #endif
1.5.9