VirtualBox

source: vbox/trunk/src/libs/xpcom18a4/xpcom/typelib/xpidl/macplugin/mac_memory.cpp@ 4837

Last change on this file since 4837 was 1, checked in by vboxsync, 54 years ago

import

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.7 KB
Line 
1/* ***** BEGIN LICENSE BLOCK *****
2 * Version: NPL 1.1/GPL 2.0/LGPL 2.1
3 *
4 * The contents of this file are subject to the Netscape Public License
5 * Version 1.1 (the "License"); you may not use this file except in
6 * compliance with the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/NPL/
8 *
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
12 * License.
13 *
14 * The Original Code is mozilla.org code.
15 *
16 * The Initial Developer of the Original Code is
17 * Netscape Communications Corporation.
18 * Portions created by the Initial Developer are Copyright (C) 1998
19 * the Initial Developer. All Rights Reserved.
20 *
21 * Contributor(s):
22 *
23 * Alternatively, the contents of this file may be used under the terms of
24 * either the GNU General Public License Version 2 or later (the "GPL"), or
25 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26 * in which case the provisions of the GPL or the LGPL are applicable instead
27 * of those above. If you wish to allow use of your version of this file only
28 * under the terms of either the GPL or the LGPL, and not to allow others to
29 * use your version of this file under the terms of the NPL, indicate your
30 * decision by deleting the provisions above and replace them with the notice
31 * and other provisions required by the GPL or the LGPL. If you do not delete
32 * the provisions above, a recipient may use your version of this file under
33 * the terms of any one of the NPL, the GPL or the LGPL.
34 *
35 * ***** END LICENSE BLOCK ***** */
36
37/*
38 mac_memory.cpp
39 */
40
41#include <new.h>
42#include <stdlib.h>
43
44#include <Files.h>
45#include <Memory.h>
46
47#include "DropInCompilerLinker.h"
48#include "CompilerMapping.h"
49#include "CWPluginErrors.h"
50
51extern CWPluginContext gPluginContext;
52
53/**
54 * Note: memory allocated by these operators will automatically be freed after the
55 * current call into xpidl_compiler completes. This should be fine in most cases,
56 * as we are also having the compiler be reloaded for every request to reinitialize
57 * global data. Just be careful out there!
58 */
59
60const Boolean kTemporaryAllocation = false;
61
62void* operator new(size_t size)
63{
64 void* ptr = NULL;
65 if (CWAllocateMemory(gPluginContext, size, kTemporaryAllocation, &ptr) == cwNoErr)
66 return ptr;
67 return NULL;
68}
69
70void operator delete(void* ptr)
71{
72 if (ptr != NULL)
73 CWFreeMemory(gPluginContext, ptr, kTemporaryAllocation);
74}
75
76void* operator new[] (size_t size)
77{
78 void* ptr = NULL;
79 if (CWAllocateMemory(gPluginContext, size, kTemporaryAllocation, &ptr) == cwNoErr)
80 return ptr;
81 return NULL;
82}
83
84void operator delete[](void* ptr)
85{
86 if (ptr != NULL)
87 CWFreeMemory(gPluginContext, ptr, kTemporaryAllocation);
88}
89
90namespace std {
91
92#define TRACK_ALLOCATION
93#define kTrackedCookie 'TRKD'
94
95void* malloc(size_t size)
96{
97#if defined(TRACK_ALLOCATION)
98 OSType* ptr = (OSType*) new char[sizeof(OSType) + size];
99 if (ptr != NULL)
100 *ptr++ = kTrackedCookie;
101 return ptr;
102#else
103 return new char[size];
104#endif
105}
106
107void free(void *ptr)
108{
109#if defined(TRACK_ALLOCATION)
110 OSType* type = (OSType*)ptr;
111 if (*--type == kTrackedCookie)
112 delete[] (char*) type;
113 else
114 DebugStr("\pillegal block passed to free.");
115#else
116 delete[] (char*) ptr;
117#endif
118}
119
120void* calloc(size_t nmemb, size_t size)
121{
122 size *= nmemb;
123 void* ptr = malloc(size);
124 if (ptr != NULL) {
125 BlockZero(ptr, size);
126 }
127 return ptr;
128}
129
130void* realloc(void * ptr, size_t size)
131{
132 void* newptr = NULL;
133
134 if (size > 0)
135 newptr = malloc(size);
136
137 if (ptr != NULL && newptr != NULL)
138 BlockMoveData(ptr, newptr, size);
139
140 if (ptr != NULL)
141 free(ptr);
142
143 return newptr;
144}
145
146}
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use