Back to index

lightning-sunbird  0.9+nobinonly
mac_memory.cpp
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is mozilla.org code.
00015  *
00016  * The Initial Developer of the Original Code is
00017  * Netscape Communications Corporation.
00018  * Portions created by the Initial Developer are Copyright (C) 1998
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *
00023  * Alternatively, the contents of this file may be used under the terms of
00024  * either the GNU General Public License Version 2 or later (the "GPL"), or
00025  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00026  * in which case the provisions of the GPL or the LGPL are applicable instead
00027  * of those above. If you wish to allow use of your version of this file only
00028  * under the terms of either the GPL or the LGPL, and not to allow others to
00029  * use your version of this file under the terms of the MPL, indicate your
00030  * decision by deleting the provisions above and replace them with the notice
00031  * and other provisions required by the GPL or the LGPL. If you do not delete
00032  * the provisions above, a recipient may use your version of this file under
00033  * the terms of any one of the MPL, the GPL or the LGPL.
00034  *
00035  * ***** END LICENSE BLOCK ***** */
00036 
00037 /*
00038        mac_memory.cpp
00039  */
00040 
00041 #include <new.h>
00042 #include <stdlib.h>
00043 
00044 #include <Files.h>
00045 #include <Memory.h>
00046 
00047 #include "DropInCompilerLinker.h"
00048 #include "CompilerMapping.h"
00049 #include "CWPluginErrors.h"
00050 
00051 extern CWPluginContext gPluginContext;
00052 
00060 const Boolean kTemporaryAllocation = false;
00061 
00062 void* operator new(size_t size)
00063 {
00064        void* ptr = NULL;
00065        if (CWAllocateMemory(gPluginContext, size, kTemporaryAllocation, &ptr) == cwNoErr)
00066               return ptr;
00067        return NULL;
00068 }
00069 
00070 void operator delete(void* ptr)
00071 {
00072        if (ptr != NULL)
00073               CWFreeMemory(gPluginContext, ptr, kTemporaryAllocation);
00074 }
00075 
00076 void* operator new[] (size_t size)
00077 {
00078        void* ptr = NULL;
00079        if (CWAllocateMemory(gPluginContext, size, kTemporaryAllocation, &ptr) == cwNoErr)
00080               return ptr;
00081        return NULL;
00082 }
00083 
00084 void operator delete[](void* ptr)
00085 {
00086        if (ptr != NULL)
00087               CWFreeMemory(gPluginContext, ptr, kTemporaryAllocation);
00088 }
00089 
00090 namespace std {
00091 
00092 #define TRACK_ALLOCATION
00093 #define kTrackedCookie 'TRKD'
00094 
00095 void* malloc(size_t size)
00096 {
00097 #if defined(TRACK_ALLOCATION)
00098        OSType* ptr = (OSType*) new char[sizeof(OSType) + size];
00099        if (ptr != NULL)
00100               *ptr++ = kTrackedCookie;
00101        return ptr;
00102 #else
00103        return new char[size];
00104 #endif
00105 }
00106 
00107 void free(void *ptr)
00108 {
00109 #if defined(TRACK_ALLOCATION)
00110        OSType* type = (OSType*)ptr;
00111        if (*--type == kTrackedCookie)
00112               delete[] (char*) type;
00113        else
00114               DebugStr("\pillegal block passed to free.");
00115 #else
00116        delete[] (char*) ptr;
00117 #endif
00118 }
00119 
00120 void* calloc(size_t nmemb, size_t size)
00121 {
00122        size *= nmemb;
00123        void* ptr = malloc(size);
00124        if (ptr != NULL) {
00125               BlockZero(ptr, size);
00126        }
00127        return ptr;
00128 }
00129 
00130 void* realloc(void * ptr, size_t size)
00131 {
00132        void* newptr = NULL;
00133 
00134        if (size > 0)
00135               newptr = malloc(size);
00136 
00137        if (ptr != NULL && newptr != NULL)
00138               BlockMoveData(ptr, newptr, size);
00139 
00140        if (ptr != NULL)
00141               free(ptr);
00142 
00143        return newptr;
00144 }
00145 
00146 }