diff options
author | Jacek Antonelli | 2008-08-15 23:44:46 -0500 |
---|---|---|
committer | Jacek Antonelli | 2008-08-15 23:44:46 -0500 |
commit | 38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4 (patch) | |
tree | adca584755d22ca041a2dbfc35d4eca01f70b32c /linden/indra/llcommon/llmemory.cpp | |
parent | README.txt (diff) | |
download | meta-impy-38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4.zip meta-impy-38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4.tar.gz meta-impy-38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4.tar.bz2 meta-impy-38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4.tar.xz |
Second Life viewer sources 1.13.2.12
Diffstat (limited to 'linden/indra/llcommon/llmemory.cpp')
-rw-r--r-- | linden/indra/llcommon/llmemory.cpp | 312 |
1 files changed, 312 insertions, 0 deletions
diff --git a/linden/indra/llcommon/llmemory.cpp b/linden/indra/llcommon/llmemory.cpp new file mode 100644 index 0000000..9bfbf88 --- /dev/null +++ b/linden/indra/llcommon/llmemory.cpp | |||
@@ -0,0 +1,312 @@ | |||
1 | /** | ||
2 | * @file llmemory.cpp | ||
3 | * @brief Very special memory allocation/deallocation stuff here | ||
4 | * | ||
5 | * Copyright (c) 2002-2007, Linden Research, Inc. | ||
6 | * | ||
7 | * The source code in this file ("Source Code") is provided by Linden Lab | ||
8 | * to you under the terms of the GNU General Public License, version 2.0 | ||
9 | * ("GPL"), unless you have obtained a separate licensing agreement | ||
10 | * ("Other License"), formally executed by you and Linden Lab. Terms of | ||
11 | * the GPL can be found in doc/GPL-license.txt in this distribution, or | ||
12 | * online at http://secondlife.com/developers/opensource/gplv2 | ||
13 | * | ||
14 | * There are special exceptions to the terms and conditions of the GPL as | ||
15 | * it is applied to this Source Code. View the full text of the exception | ||
16 | * in the file doc/FLOSS-exception.txt in this software distribution, or | ||
17 | * online at http://secondlife.com/developers/opensource/flossexception | ||
18 | * | ||
19 | * By copying, modifying or distributing this software, you acknowledge | ||
20 | * that you have read and understood your obligations described above, | ||
21 | * and agree to abide by those obligations. | ||
22 | * | ||
23 | * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO | ||
24 | * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY, | ||
25 | * COMPLETENESS OR PERFORMANCE. | ||
26 | */ | ||
27 | |||
28 | #include "linden_common.h" | ||
29 | |||
30 | #include "llmemory.h" | ||
31 | |||
32 | //---------------------------------------------------------------------------- | ||
33 | |||
34 | //static | ||
35 | char* LLMemory::reserveMem = 0; | ||
36 | |||
37 | //static | ||
38 | void LLMemory::initClass() | ||
39 | { | ||
40 | if (!reserveMem) | ||
41 | { | ||
42 | reserveMem = new char[16*1024]; // reserve 16K for out of memory error handling | ||
43 | } | ||
44 | } | ||
45 | |||
46 | //static | ||
47 | void LLMemory::cleanupClass() | ||
48 | { | ||
49 | delete [] reserveMem; | ||
50 | reserveMem = NULL; | ||
51 | } | ||
52 | |||
53 | //static | ||
54 | void LLMemory::freeReserve() | ||
55 | { | ||
56 | delete [] reserveMem; | ||
57 | reserveMem = NULL; | ||
58 | } | ||
59 | |||
60 | |||
61 | //---------------------------------------------------------------------------- | ||
62 | |||
63 | //static | ||
64 | #if MEM_TRACK_TYPE | ||
65 | S32 LLMemType::sCurDepth = 0; | ||
66 | S32 LLMemType::sCurType = LLMemType::MTYPE_INIT; | ||
67 | S32 LLMemType::sType[LLMemType::MTYPE_MAX_DEPTH]; | ||
68 | S32 LLMemType::sMemCount[LLMemType::MTYPE_NUM_TYPES] = { 0 }; | ||
69 | S32 LLMemType::sMaxMemCount[LLMemType::MTYPE_NUM_TYPES] = { 0 }; | ||
70 | S32 LLMemType::sOverheadMem = 0; | ||
71 | |||
72 | const char* LLMemType::sTypeDesc[LLMemType::MTYPE_NUM_TYPES] = | ||
73 | { | ||
74 | "INIT", | ||
75 | "STARTUP", | ||
76 | "MAIN", | ||
77 | |||
78 | "IMAGEBASE", | ||
79 | "IMAGERAW", | ||
80 | "IMAGEFORMATTED", | ||
81 | |||
82 | "APPFMTIMAGE", | ||
83 | "APPRAWIMAGE", | ||
84 | "APPAUXRAWIMAGE", | ||
85 | |||
86 | "DRAWABLE", | ||
87 | "OBJECT", | ||
88 | "PIPELINE", | ||
89 | "AVATAR", | ||
90 | "PARTICLES", | ||
91 | "REGIONS", | ||
92 | "INVENTORY", | ||
93 | "ANIMATION", | ||
94 | "NETWORK", | ||
95 | "PHYSICS", | ||
96 | "INTERESTLIST", | ||
97 | |||
98 | "SCRIPT", | ||
99 | "SCRIPT_RUN", | ||
100 | "SCRIPT_BYTECODE", | ||
101 | |||
102 | "IO_PUMP", | ||
103 | "IO_TCP", | ||
104 | "IO_BUFFER", | ||
105 | "IO_HTTP_SERVER" | ||
106 | "IO_SD_SERVER", | ||
107 | "IO_SD_CLIENT", | ||
108 | "IO_URL_REQUEST", | ||
109 | |||
110 | "TEMP1", | ||
111 | "TEMP2", | ||
112 | "TEMP3", | ||
113 | "TEMP4", | ||
114 | "TEMP5", | ||
115 | "TEMP6", | ||
116 | "TEMP7", | ||
117 | "TEMP8", | ||
118 | "TEMP9" | ||
119 | }; | ||
120 | |||
121 | #endif | ||
122 | S32 LLMemType::sTotalMem = 0; | ||
123 | S32 LLMemType::sMaxTotalMem = 0; | ||
124 | |||
125 | //static | ||
126 | void LLMemType::printMem() | ||
127 | { | ||
128 | S32 misc_mem = sTotalMem; | ||
129 | #if MEM_TRACK_TYPE | ||
130 | for (S32 i=0; i<MTYPE_NUM_TYPES; i++) | ||
131 | { | ||
132 | if (sMemCount[i]) | ||
133 | { | ||
134 | llinfos << llformat("MEM: % 20s %03d MB (%03d MB)",sTypeDesc[i],sMemCount[i]>>20,sMaxMemCount[i]>>20) << llendl; | ||
135 | } | ||
136 | misc_mem -= sMemCount[i]; | ||
137 | } | ||
138 | #endif | ||
139 | llinfos << llformat("MEM: % 20s %03d MB","MISC",misc_mem>>20) << llendl; | ||
140 | llinfos << llformat("MEM: % 20s %03d MB (Max=%d MB)","TOTAL",sTotalMem>>20,sMaxTotalMem>>20) << llendl; | ||
141 | } | ||
142 | |||
143 | #if MEM_TRACK_MEM | ||
144 | |||
145 | void* ll_allocate (size_t size) | ||
146 | { | ||
147 | if (size == 0) | ||
148 | { | ||
149 | llwarns << "Null allocation" << llendl; | ||
150 | } | ||
151 | |||
152 | size = (size+3)&~3; | ||
153 | S32 alloc_size = size + 4; | ||
154 | #if MEM_TRACK_TYPE | ||
155 | alloc_size += 4; | ||
156 | #endif | ||
157 | char* p = (char*)malloc(alloc_size); | ||
158 | if (p == NULL) | ||
159 | { | ||
160 | LLMemory::freeReserve(); | ||
161 | llerrs << "Out of memory Error" << llendl; | ||
162 | } | ||
163 | LLMemType::sTotalMem += size; | ||
164 | LLMemType::sMaxTotalMem = llmax(LLMemType::sTotalMem, LLMemType::sMaxTotalMem); | ||
165 | LLMemType::sOverheadMem += 4; | ||
166 | *(size_t*)p = size; | ||
167 | p += 4; | ||
168 | #if MEM_TRACK_TYPE | ||
169 | if (LLMemType::sCurType < 0 || LLMemType::sCurType >= LLMemType::MTYPE_NUM_TYPES) | ||
170 | { | ||
171 | llerrs << "Memory Type Error: new" << llendl; | ||
172 | } | ||
173 | LLMemType::sOverheadMem += 4; | ||
174 | *(S32*)p = LLMemType::sCurType; | ||
175 | p += 4; | ||
176 | LLMemType::sMemCount[LLMemType::sCurType] += size; | ||
177 | if (LLMemType::sMemCount[LLMemType::sCurType] > LLMemType::sMaxMemCount[LLMemType::sCurType]) | ||
178 | { | ||
179 | LLMemType::sMaxMemCount[LLMemType::sCurType] = LLMemType::sMemCount[LLMemType::sCurType]; | ||
180 | } | ||
181 | #endif | ||
182 | return (void*)p; | ||
183 | } | ||
184 | |||
185 | void ll_release (void *pin) | ||
186 | { | ||
187 | if (!pin) | ||
188 | { | ||
189 | return; | ||
190 | } | ||
191 | char* p = (char*)pin; | ||
192 | #if MEM_TRACK_TYPE | ||
193 | p -= 4; | ||
194 | S32 type = *(S32*)p; | ||
195 | if (type < 0 || type >= LLMemType::MTYPE_NUM_TYPES) | ||
196 | { | ||
197 | llerrs << "Memory Type Error: delete" << llendl; | ||
198 | } | ||
199 | #endif | ||
200 | p -= 4; | ||
201 | S32 size = *(size_t*)p; | ||
202 | LLMemType::sOverheadMem -= 4; | ||
203 | #if MEM_TRACK_TYPE | ||
204 | LLMemType::sMemCount[type] -= size; | ||
205 | LLMemType::sOverheadMem -= 4; | ||
206 | #endif | ||
207 | LLMemType::sTotalMem -= size; | ||
208 | free(p); | ||
209 | } | ||
210 | |||
211 | #else | ||
212 | |||
213 | void* ll_allocate (size_t size) | ||
214 | { | ||
215 | if (size == 0) | ||
216 | { | ||
217 | llwarns << "Null allocation" << llendl; | ||
218 | } | ||
219 | void *p = malloc(size); | ||
220 | if (p == NULL) | ||
221 | { | ||
222 | LLMemory::freeReserve(); | ||
223 | llerrs << "Out of memory Error" << llendl; | ||
224 | } | ||
225 | return p; | ||
226 | } | ||
227 | |||
228 | void ll_release (void *p) | ||
229 | { | ||
230 | free(p); | ||
231 | } | ||
232 | |||
233 | #endif | ||
234 | |||
235 | #if MEM_TRACK_MEM | ||
236 | |||
237 | void* operator new (size_t size) | ||
238 | { | ||
239 | return ll_allocate(size); | ||
240 | } | ||
241 | |||
242 | void* operator new[] (size_t size) | ||
243 | { | ||
244 | return ll_allocate(size); | ||
245 | } | ||
246 | |||
247 | void operator delete (void *p) | ||
248 | { | ||
249 | ll_release(p); | ||
250 | } | ||
251 | |||
252 | void operator delete[] (void *p) | ||
253 | { | ||
254 | ll_release(p); | ||
255 | } | ||
256 | |||
257 | #endif | ||
258 | |||
259 | //---------------------------------------------------------------------------- | ||
260 | |||
261 | //static | ||
262 | LLMutex* LLThreadSafeRefCount::sMutex = 0; | ||
263 | |||
264 | //static | ||
265 | void LLThreadSafeRefCount::initClass() | ||
266 | { | ||
267 | if (!sMutex) | ||
268 | { | ||
269 | sMutex = new LLMutex(0); | ||
270 | } | ||
271 | } | ||
272 | |||
273 | //static | ||
274 | void LLThreadSafeRefCount::cleanupClass() | ||
275 | { | ||
276 | delete sMutex; | ||
277 | sMutex = NULL; | ||
278 | } | ||
279 | |||
280 | |||
281 | //---------------------------------------------------------------------------- | ||
282 | |||
283 | LLThreadSafeRefCount::LLThreadSafeRefCount() : | ||
284 | mRef(0) | ||
285 | { | ||
286 | } | ||
287 | |||
288 | LLThreadSafeRefCount::~LLThreadSafeRefCount() | ||
289 | { | ||
290 | if (mRef != 0) | ||
291 | { | ||
292 | llerrs << "deleting non-zero reference" << llendl; | ||
293 | } | ||
294 | } | ||
295 | |||
296 | //---------------------------------------------------------------------------- | ||
297 | |||
298 | LLRefCount::LLRefCount() : | ||
299 | mRef(0) | ||
300 | { | ||
301 | } | ||
302 | |||
303 | LLRefCount::~LLRefCount() | ||
304 | { | ||
305 | if (mRef != 0) | ||
306 | { | ||
307 | llerrs << "deleting non-zero reference" << llendl; | ||
308 | } | ||
309 | } | ||
310 | |||
311 | //---------------------------------------------------------------------------- | ||
312 | |||