1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
---|
2 | /* ***** BEGIN LICENSE BLOCK *****
|
---|
3 | * Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
---|
4 | *
|
---|
5 | * The contents of this file are subject to the Mozilla Public License Version
|
---|
6 | * 1.1 (the "License"); you may not use this file except in compliance with
|
---|
7 | * the License. You may obtain a copy of the License at
|
---|
8 | * http://www.mozilla.org/MPL/
|
---|
9 | *
|
---|
10 | * Software distributed under the License is distributed on an "AS IS" basis,
|
---|
11 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
---|
12 | * for the specific language governing rights and limitations under the
|
---|
13 | * License.
|
---|
14 | *
|
---|
15 | * The Original Code is mozilla.org code.
|
---|
16 | *
|
---|
17 | * The Initial Developer of the Original Code is
|
---|
18 | * Netscape Communications Corporation.
|
---|
19 | * Portions created by the Initial Developer are Copyright (C) 1998
|
---|
20 | * the Initial Developer. All Rights Reserved.
|
---|
21 | *
|
---|
22 | * Contributor(s):
|
---|
23 | *
|
---|
24 | * Alternatively, the contents of this file may be used under the terms of
|
---|
25 | * either of the GNU General Public License Version 2 or later (the "GPL"),
|
---|
26 | * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
---|
27 | * in which case the provisions of the GPL or the LGPL are applicable instead
|
---|
28 | * of those above. If you wish to allow use of your version of this file only
|
---|
29 | * under the terms of either the GPL or the LGPL, and not to allow others to
|
---|
30 | * use your version of this file under the terms of the MPL, indicate your
|
---|
31 | * decision by deleting the provisions above and replace them with the notice
|
---|
32 | * and other provisions required by the GPL or the LGPL. If you do not delete
|
---|
33 | * the provisions above, a recipient may use your version of this file under
|
---|
34 | * the terms of any one of the MPL, the GPL or the LGPL.
|
---|
35 | *
|
---|
36 | * ***** END LICENSE BLOCK ***** */
|
---|
37 |
|
---|
38 | #include "nsMemoryImpl.h"
|
---|
39 | #include "prmem.h"
|
---|
40 | #include "nsAlgorithm.h"
|
---|
41 | #include "nsIServiceManager.h"
|
---|
42 | #include "nsIObserverService.h"
|
---|
43 | #include "nsAutoLock.h"
|
---|
44 | #include "nsIEventQueueService.h"
|
---|
45 | #include "nsString.h"
|
---|
46 |
|
---|
47 | nsMemoryImpl* gMemory = nsnull;
|
---|
48 |
|
---|
49 | NS_IMPL_THREADSAFE_ISUPPORTS1(nsMemoryImpl, nsIMemory)
|
---|
50 |
|
---|
51 | NS_METHOD
|
---|
52 | nsMemoryImpl::Create(nsISupports* outer, const nsIID& aIID, void* *aInstancePtr)
|
---|
53 | {
|
---|
54 | NS_ENSURE_ARG_POINTER(aInstancePtr);
|
---|
55 | NS_ENSURE_PROPER_AGGREGATION(outer, aIID);
|
---|
56 | if (gMemory && NS_SUCCEEDED(gMemory->QueryInterface(aIID, aInstancePtr)))
|
---|
57 | return NS_OK;
|
---|
58 |
|
---|
59 | nsMemoryImpl* mm = new nsMemoryImpl();
|
---|
60 | if (mm == NULL)
|
---|
61 | return NS_ERROR_OUT_OF_MEMORY;
|
---|
62 |
|
---|
63 | nsresult rv;
|
---|
64 |
|
---|
65 | do {
|
---|
66 | rv = mm->QueryInterface(aIID, aInstancePtr);
|
---|
67 | if (NS_FAILED(rv))
|
---|
68 | break;
|
---|
69 |
|
---|
70 | rv = NS_OK;
|
---|
71 | } while (0);
|
---|
72 |
|
---|
73 | if (NS_FAILED(rv))
|
---|
74 | delete mm;
|
---|
75 |
|
---|
76 | return rv;
|
---|
77 | }
|
---|
78 |
|
---|
79 |
|
---|
80 | nsMemoryImpl::nsMemoryImpl()
|
---|
81 | {
|
---|
82 | }
|
---|
83 |
|
---|
84 | nsMemoryImpl::~nsMemoryImpl()
|
---|
85 | {
|
---|
86 | }
|
---|
87 |
|
---|
88 | ////////////////////////////////////////////////////////////////////////////////
|
---|
89 |
|
---|
90 | NS_IMETHODIMP_(void *)
|
---|
91 | nsMemoryImpl::Alloc(PRSize size)
|
---|
92 | {
|
---|
93 | NS_ASSERTION(size, "nsMemoryImpl::Alloc of 0");
|
---|
94 | void* result = PR_Malloc(size);
|
---|
95 | return result;
|
---|
96 | }
|
---|
97 |
|
---|
98 | NS_IMETHODIMP_(void *)
|
---|
99 | nsMemoryImpl::Realloc(void * ptr, PRSize size)
|
---|
100 | {
|
---|
101 | void* result = PR_Realloc(ptr, size);
|
---|
102 | return result;
|
---|
103 | }
|
---|
104 |
|
---|
105 | NS_IMETHODIMP_(void)
|
---|
106 | nsMemoryImpl::Free(void * ptr)
|
---|
107 | {
|
---|
108 | PR_Free(ptr);
|
---|
109 | }
|
---|
110 |
|
---|
111 | NS_IMETHODIMP
|
---|
112 | nsMemoryImpl::HeapMinimize(PRBool aImmediate)
|
---|
113 | {
|
---|
114 | return NS_OK;
|
---|
115 | }
|
---|
116 |
|
---|
117 | NS_IMETHODIMP
|
---|
118 | nsMemoryImpl::IsLowMemory(PRBool *result)
|
---|
119 | {
|
---|
120 | *result = PR_FALSE;
|
---|
121 | return NS_OK;
|
---|
122 | }
|
---|
123 |
|
---|
124 | static void
|
---|
125 | EnsureGlobalMemoryService()
|
---|
126 | {
|
---|
127 | if (gMemory) return;
|
---|
128 | nsresult rv = nsMemoryImpl::Create(nsnull, NS_GET_IID(nsIMemory), (void**)&gMemory);
|
---|
129 | NS_ASSERTION(NS_SUCCEEDED(rv), "nsMemoryImpl::Create failed");
|
---|
130 | NS_ASSERTION(gMemory, "improper xpcom initialization");
|
---|
131 | }
|
---|
132 |
|
---|
133 | nsresult
|
---|
134 | nsMemoryImpl::Startup()
|
---|
135 | {
|
---|
136 | EnsureGlobalMemoryService();
|
---|
137 | if (! gMemory)
|
---|
138 | return NS_ERROR_FAILURE;
|
---|
139 |
|
---|
140 | return NS_OK;
|
---|
141 | }
|
---|
142 |
|
---|
143 | nsresult
|
---|
144 | nsMemoryImpl::Shutdown()
|
---|
145 | {
|
---|
146 | if (gMemory) {
|
---|
147 | NS_RELEASE(gMemory);
|
---|
148 | gMemory = nsnull;
|
---|
149 | }
|
---|
150 |
|
---|
151 | return NS_OK;
|
---|
152 | }
|
---|