uttrack.c revision 1.5 1 1.1 jruoho /******************************************************************************
2 1.1 jruoho *
3 1.1 jruoho * Module Name: uttrack - Memory allocation tracking routines (debug only)
4 1.1 jruoho *
5 1.1 jruoho *****************************************************************************/
6 1.1 jruoho
7 1.3 jruoho /*
8 1.5 christos * Copyright (C) 2000 - 2014, Intel Corp.
9 1.1 jruoho * All rights reserved.
10 1.1 jruoho *
11 1.3 jruoho * Redistribution and use in source and binary forms, with or without
12 1.3 jruoho * modification, are permitted provided that the following conditions
13 1.3 jruoho * are met:
14 1.3 jruoho * 1. Redistributions of source code must retain the above copyright
15 1.3 jruoho * notice, this list of conditions, and the following disclaimer,
16 1.3 jruoho * without modification.
17 1.3 jruoho * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 1.3 jruoho * substantially similar to the "NO WARRANTY" disclaimer below
19 1.3 jruoho * ("Disclaimer") and any redistribution must be conditioned upon
20 1.3 jruoho * including a substantially similar Disclaimer requirement for further
21 1.3 jruoho * binary redistribution.
22 1.3 jruoho * 3. Neither the names of the above-listed copyright holders nor the names
23 1.3 jruoho * of any contributors may be used to endorse or promote products derived
24 1.3 jruoho * from this software without specific prior written permission.
25 1.3 jruoho *
26 1.3 jruoho * Alternatively, this software may be distributed under the terms of the
27 1.3 jruoho * GNU General Public License ("GPL") version 2 as published by the Free
28 1.3 jruoho * Software Foundation.
29 1.3 jruoho *
30 1.3 jruoho * NO WARRANTY
31 1.3 jruoho * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 1.3 jruoho * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 1.3 jruoho * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34 1.3 jruoho * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 1.3 jruoho * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 1.3 jruoho * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 1.3 jruoho * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 1.3 jruoho * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 1.3 jruoho * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 1.3 jruoho * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 1.3 jruoho * POSSIBILITY OF SUCH DAMAGES.
42 1.3 jruoho */
43 1.1 jruoho
44 1.1 jruoho /*
45 1.1 jruoho * These procedures are used for tracking memory leaks in the subsystem, and
46 1.1 jruoho * they get compiled out when the ACPI_DBG_TRACK_ALLOCATIONS is not set.
47 1.1 jruoho *
48 1.4 christos * Each memory allocation is tracked via a doubly linked list. Each
49 1.1 jruoho * element contains the caller's component, module name, function name, and
50 1.4 christos * line number. AcpiUtAllocate and AcpiUtAllocateZeroed call
51 1.1 jruoho * AcpiUtTrackAllocation to add an element to the list; deletion
52 1.1 jruoho * occurs in the body of AcpiUtFree.
53 1.1 jruoho */
54 1.1 jruoho
55 1.1 jruoho #define __UTTRACK_C__
56 1.1 jruoho
57 1.1 jruoho #include "acpi.h"
58 1.1 jruoho #include "accommon.h"
59 1.1 jruoho
60 1.1 jruoho #ifdef ACPI_DBG_TRACK_ALLOCATIONS
61 1.1 jruoho
62 1.1 jruoho #define _COMPONENT ACPI_UTILITIES
63 1.1 jruoho ACPI_MODULE_NAME ("uttrack")
64 1.1 jruoho
65 1.4 christos
66 1.1 jruoho /* Local prototypes */
67 1.1 jruoho
68 1.1 jruoho static ACPI_DEBUG_MEM_BLOCK *
69 1.1 jruoho AcpiUtFindAllocation (
70 1.4 christos ACPI_DEBUG_MEM_BLOCK *Allocation);
71 1.1 jruoho
72 1.1 jruoho static ACPI_STATUS
73 1.1 jruoho AcpiUtTrackAllocation (
74 1.1 jruoho ACPI_DEBUG_MEM_BLOCK *Address,
75 1.1 jruoho ACPI_SIZE Size,
76 1.1 jruoho UINT8 AllocType,
77 1.1 jruoho UINT32 Component,
78 1.1 jruoho const char *Module,
79 1.1 jruoho UINT32 Line);
80 1.1 jruoho
81 1.1 jruoho static ACPI_STATUS
82 1.1 jruoho AcpiUtRemoveAllocation (
83 1.1 jruoho ACPI_DEBUG_MEM_BLOCK *Address,
84 1.1 jruoho UINT32 Component,
85 1.1 jruoho const char *Module,
86 1.1 jruoho UINT32 Line);
87 1.1 jruoho
88 1.1 jruoho
89 1.1 jruoho /*******************************************************************************
90 1.1 jruoho *
91 1.1 jruoho * FUNCTION: AcpiUtCreateList
92 1.1 jruoho *
93 1.1 jruoho * PARAMETERS: CacheName - Ascii name for the cache
94 1.1 jruoho * ObjectSize - Size of each cached object
95 1.1 jruoho * ReturnCache - Where the new cache object is returned
96 1.1 jruoho *
97 1.1 jruoho * RETURN: Status
98 1.1 jruoho *
99 1.1 jruoho * DESCRIPTION: Create a local memory list for tracking purposed
100 1.1 jruoho *
101 1.1 jruoho ******************************************************************************/
102 1.1 jruoho
103 1.1 jruoho ACPI_STATUS
104 1.1 jruoho AcpiUtCreateList (
105 1.2 jruoho const char *ListName,
106 1.1 jruoho UINT16 ObjectSize,
107 1.1 jruoho ACPI_MEMORY_LIST **ReturnCache)
108 1.1 jruoho {
109 1.1 jruoho ACPI_MEMORY_LIST *Cache;
110 1.1 jruoho
111 1.1 jruoho
112 1.1 jruoho Cache = AcpiOsAllocate (sizeof (ACPI_MEMORY_LIST));
113 1.1 jruoho if (!Cache)
114 1.1 jruoho {
115 1.1 jruoho return (AE_NO_MEMORY);
116 1.1 jruoho }
117 1.1 jruoho
118 1.1 jruoho ACPI_MEMSET (Cache, 0, sizeof (ACPI_MEMORY_LIST));
119 1.1 jruoho
120 1.2 jruoho Cache->ListName = __UNCONST(ListName);
121 1.1 jruoho Cache->ObjectSize = ObjectSize;
122 1.1 jruoho
123 1.1 jruoho *ReturnCache = Cache;
124 1.1 jruoho return (AE_OK);
125 1.1 jruoho }
126 1.1 jruoho
127 1.1 jruoho
128 1.1 jruoho /*******************************************************************************
129 1.1 jruoho *
130 1.1 jruoho * FUNCTION: AcpiUtAllocateAndTrack
131 1.1 jruoho *
132 1.1 jruoho * PARAMETERS: Size - Size of the allocation
133 1.1 jruoho * Component - Component type of caller
134 1.1 jruoho * Module - Source file name of caller
135 1.1 jruoho * Line - Line number of caller
136 1.1 jruoho *
137 1.1 jruoho * RETURN: Address of the allocated memory on success, NULL on failure.
138 1.1 jruoho *
139 1.1 jruoho * DESCRIPTION: The subsystem's equivalent of malloc.
140 1.1 jruoho *
141 1.1 jruoho ******************************************************************************/
142 1.1 jruoho
143 1.1 jruoho void *
144 1.1 jruoho AcpiUtAllocateAndTrack (
145 1.1 jruoho ACPI_SIZE Size,
146 1.1 jruoho UINT32 Component,
147 1.1 jruoho const char *Module,
148 1.1 jruoho UINT32 Line)
149 1.1 jruoho {
150 1.1 jruoho ACPI_DEBUG_MEM_BLOCK *Allocation;
151 1.1 jruoho ACPI_STATUS Status;
152 1.1 jruoho
153 1.1 jruoho
154 1.4 christos /* Check for an inadvertent size of zero bytes */
155 1.4 christos
156 1.4 christos if (!Size)
157 1.4 christos {
158 1.4 christos ACPI_WARNING ((Module, Line,
159 1.4 christos "Attempt to allocate zero bytes, allocating 1 byte"));
160 1.4 christos Size = 1;
161 1.4 christos }
162 1.4 christos
163 1.4 christos Allocation = AcpiOsAllocate (Size + sizeof (ACPI_DEBUG_MEM_HEADER));
164 1.1 jruoho if (!Allocation)
165 1.1 jruoho {
166 1.4 christos /* Report allocation error */
167 1.4 christos
168 1.4 christos ACPI_WARNING ((Module, Line,
169 1.4 christos "Could not allocate size %u", (UINT32) Size));
170 1.4 christos
171 1.1 jruoho return (NULL);
172 1.1 jruoho }
173 1.1 jruoho
174 1.1 jruoho Status = AcpiUtTrackAllocation (Allocation, Size,
175 1.1 jruoho ACPI_MEM_MALLOC, Component, Module, Line);
176 1.1 jruoho if (ACPI_FAILURE (Status))
177 1.1 jruoho {
178 1.1 jruoho AcpiOsFree (Allocation);
179 1.1 jruoho return (NULL);
180 1.1 jruoho }
181 1.1 jruoho
182 1.1 jruoho AcpiGbl_GlobalList->TotalAllocated++;
183 1.1 jruoho AcpiGbl_GlobalList->TotalSize += (UINT32) Size;
184 1.1 jruoho AcpiGbl_GlobalList->CurrentTotalSize += (UINT32) Size;
185 1.1 jruoho if (AcpiGbl_GlobalList->CurrentTotalSize > AcpiGbl_GlobalList->MaxOccupied)
186 1.1 jruoho {
187 1.1 jruoho AcpiGbl_GlobalList->MaxOccupied = AcpiGbl_GlobalList->CurrentTotalSize;
188 1.1 jruoho }
189 1.1 jruoho
190 1.1 jruoho return ((void *) &Allocation->UserSpace);
191 1.1 jruoho }
192 1.1 jruoho
193 1.1 jruoho
194 1.1 jruoho /*******************************************************************************
195 1.1 jruoho *
196 1.1 jruoho * FUNCTION: AcpiUtAllocateZeroedAndTrack
197 1.1 jruoho *
198 1.1 jruoho * PARAMETERS: Size - Size of the allocation
199 1.1 jruoho * Component - Component type of caller
200 1.1 jruoho * Module - Source file name of caller
201 1.1 jruoho * Line - Line number of caller
202 1.1 jruoho *
203 1.1 jruoho * RETURN: Address of the allocated memory on success, NULL on failure.
204 1.1 jruoho *
205 1.1 jruoho * DESCRIPTION: Subsystem equivalent of calloc.
206 1.1 jruoho *
207 1.1 jruoho ******************************************************************************/
208 1.1 jruoho
209 1.1 jruoho void *
210 1.1 jruoho AcpiUtAllocateZeroedAndTrack (
211 1.1 jruoho ACPI_SIZE Size,
212 1.1 jruoho UINT32 Component,
213 1.1 jruoho const char *Module,
214 1.1 jruoho UINT32 Line)
215 1.1 jruoho {
216 1.1 jruoho ACPI_DEBUG_MEM_BLOCK *Allocation;
217 1.1 jruoho ACPI_STATUS Status;
218 1.1 jruoho
219 1.1 jruoho
220 1.4 christos /* Check for an inadvertent size of zero bytes */
221 1.4 christos
222 1.4 christos if (!Size)
223 1.4 christos {
224 1.4 christos ACPI_WARNING ((Module, Line,
225 1.4 christos "Attempt to allocate zero bytes, allocating 1 byte"));
226 1.4 christos Size = 1;
227 1.4 christos }
228 1.4 christos
229 1.4 christos Allocation = AcpiOsAllocateZeroed (Size + sizeof (ACPI_DEBUG_MEM_HEADER));
230 1.1 jruoho if (!Allocation)
231 1.1 jruoho {
232 1.1 jruoho /* Report allocation error */
233 1.1 jruoho
234 1.1 jruoho ACPI_ERROR ((Module, Line,
235 1.1 jruoho "Could not allocate size %u", (UINT32) Size));
236 1.1 jruoho return (NULL);
237 1.1 jruoho }
238 1.1 jruoho
239 1.1 jruoho Status = AcpiUtTrackAllocation (Allocation, Size,
240 1.1 jruoho ACPI_MEM_CALLOC, Component, Module, Line);
241 1.1 jruoho if (ACPI_FAILURE (Status))
242 1.1 jruoho {
243 1.1 jruoho AcpiOsFree (Allocation);
244 1.1 jruoho return (NULL);
245 1.1 jruoho }
246 1.1 jruoho
247 1.1 jruoho AcpiGbl_GlobalList->TotalAllocated++;
248 1.1 jruoho AcpiGbl_GlobalList->TotalSize += (UINT32) Size;
249 1.1 jruoho AcpiGbl_GlobalList->CurrentTotalSize += (UINT32) Size;
250 1.1 jruoho if (AcpiGbl_GlobalList->CurrentTotalSize > AcpiGbl_GlobalList->MaxOccupied)
251 1.1 jruoho {
252 1.1 jruoho AcpiGbl_GlobalList->MaxOccupied = AcpiGbl_GlobalList->CurrentTotalSize;
253 1.1 jruoho }
254 1.1 jruoho
255 1.1 jruoho return ((void *) &Allocation->UserSpace);
256 1.1 jruoho }
257 1.1 jruoho
258 1.1 jruoho
259 1.1 jruoho /*******************************************************************************
260 1.1 jruoho *
261 1.1 jruoho * FUNCTION: AcpiUtFreeAndTrack
262 1.1 jruoho *
263 1.1 jruoho * PARAMETERS: Allocation - Address of the memory to deallocate
264 1.1 jruoho * Component - Component type of caller
265 1.1 jruoho * Module - Source file name of caller
266 1.1 jruoho * Line - Line number of caller
267 1.1 jruoho *
268 1.1 jruoho * RETURN: None
269 1.1 jruoho *
270 1.1 jruoho * DESCRIPTION: Frees the memory at Allocation
271 1.1 jruoho *
272 1.1 jruoho ******************************************************************************/
273 1.1 jruoho
274 1.1 jruoho void
275 1.1 jruoho AcpiUtFreeAndTrack (
276 1.1 jruoho void *Allocation,
277 1.1 jruoho UINT32 Component,
278 1.1 jruoho const char *Module,
279 1.1 jruoho UINT32 Line)
280 1.1 jruoho {
281 1.1 jruoho ACPI_DEBUG_MEM_BLOCK *DebugBlock;
282 1.1 jruoho ACPI_STATUS Status;
283 1.1 jruoho
284 1.1 jruoho
285 1.1 jruoho ACPI_FUNCTION_TRACE_PTR (UtFree, Allocation);
286 1.1 jruoho
287 1.1 jruoho
288 1.1 jruoho if (NULL == Allocation)
289 1.1 jruoho {
290 1.1 jruoho ACPI_ERROR ((Module, Line,
291 1.1 jruoho "Attempt to delete a NULL address"));
292 1.1 jruoho
293 1.1 jruoho return_VOID;
294 1.1 jruoho }
295 1.1 jruoho
296 1.1 jruoho DebugBlock = ACPI_CAST_PTR (ACPI_DEBUG_MEM_BLOCK,
297 1.1 jruoho (((char *) Allocation) - sizeof (ACPI_DEBUG_MEM_HEADER)));
298 1.1 jruoho
299 1.1 jruoho AcpiGbl_GlobalList->TotalFreed++;
300 1.1 jruoho AcpiGbl_GlobalList->CurrentTotalSize -= DebugBlock->Size;
301 1.1 jruoho
302 1.1 jruoho Status = AcpiUtRemoveAllocation (DebugBlock,
303 1.1 jruoho Component, Module, Line);
304 1.1 jruoho if (ACPI_FAILURE (Status))
305 1.1 jruoho {
306 1.1 jruoho ACPI_EXCEPTION ((AE_INFO, Status, "Could not free memory"));
307 1.1 jruoho }
308 1.1 jruoho
309 1.1 jruoho AcpiOsFree (DebugBlock);
310 1.5 christos ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "%p freed (block %p)\n",
311 1.5 christos Allocation, DebugBlock));
312 1.1 jruoho return_VOID;
313 1.1 jruoho }
314 1.1 jruoho
315 1.1 jruoho
316 1.1 jruoho /*******************************************************************************
317 1.1 jruoho *
318 1.1 jruoho * FUNCTION: AcpiUtFindAllocation
319 1.1 jruoho *
320 1.1 jruoho * PARAMETERS: Allocation - Address of allocated memory
321 1.1 jruoho *
322 1.4 christos * RETURN: Three cases:
323 1.4 christos * 1) List is empty, NULL is returned.
324 1.4 christos * 2) Element was found. Returns Allocation parameter.
325 1.4 christos * 3) Element was not found. Returns position where it should be
326 1.4 christos * inserted into the list.
327 1.1 jruoho *
328 1.1 jruoho * DESCRIPTION: Searches for an element in the global allocation tracking list.
329 1.4 christos * If the element is not found, returns the location within the
330 1.4 christos * list where the element should be inserted.
331 1.4 christos *
332 1.4 christos * Note: The list is ordered by larger-to-smaller addresses.
333 1.4 christos *
334 1.4 christos * This global list is used to detect memory leaks in ACPICA as
335 1.4 christos * well as other issues such as an attempt to release the same
336 1.4 christos * internal object more than once. Although expensive as far
337 1.4 christos * as cpu time, this list is much more helpful for finding these
338 1.4 christos * types of issues than using memory leak detectors outside of
339 1.4 christos * the ACPICA code.
340 1.1 jruoho *
341 1.1 jruoho ******************************************************************************/
342 1.1 jruoho
343 1.1 jruoho static ACPI_DEBUG_MEM_BLOCK *
344 1.1 jruoho AcpiUtFindAllocation (
345 1.4 christos ACPI_DEBUG_MEM_BLOCK *Allocation)
346 1.1 jruoho {
347 1.1 jruoho ACPI_DEBUG_MEM_BLOCK *Element;
348 1.1 jruoho
349 1.1 jruoho
350 1.1 jruoho Element = AcpiGbl_GlobalList->ListHead;
351 1.4 christos if (!Element)
352 1.4 christos {
353 1.4 christos return (NULL);
354 1.4 christos }
355 1.1 jruoho
356 1.4 christos /*
357 1.4 christos * Search for the address.
358 1.4 christos *
359 1.4 christos * Note: List is ordered by larger-to-smaller addresses, on the
360 1.4 christos * assumption that a new allocation usually has a larger address
361 1.4 christos * than previous allocations.
362 1.4 christos */
363 1.4 christos while (Element > Allocation)
364 1.4 christos {
365 1.4 christos /* Check for end-of-list */
366 1.1 jruoho
367 1.4 christos if (!Element->Next)
368 1.1 jruoho {
369 1.1 jruoho return (Element);
370 1.1 jruoho }
371 1.1 jruoho
372 1.1 jruoho Element = Element->Next;
373 1.1 jruoho }
374 1.1 jruoho
375 1.4 christos if (Element == Allocation)
376 1.4 christos {
377 1.4 christos return (Element);
378 1.4 christos }
379 1.4 christos
380 1.4 christos return (Element->Previous);
381 1.1 jruoho }
382 1.1 jruoho
383 1.1 jruoho
384 1.1 jruoho /*******************************************************************************
385 1.1 jruoho *
386 1.1 jruoho * FUNCTION: AcpiUtTrackAllocation
387 1.1 jruoho *
388 1.1 jruoho * PARAMETERS: Allocation - Address of allocated memory
389 1.1 jruoho * Size - Size of the allocation
390 1.1 jruoho * AllocType - MEM_MALLOC or MEM_CALLOC
391 1.1 jruoho * Component - Component type of caller
392 1.1 jruoho * Module - Source file name of caller
393 1.1 jruoho * Line - Line number of caller
394 1.1 jruoho *
395 1.4 christos * RETURN: Status
396 1.1 jruoho *
397 1.1 jruoho * DESCRIPTION: Inserts an element into the global allocation tracking list.
398 1.1 jruoho *
399 1.1 jruoho ******************************************************************************/
400 1.1 jruoho
401 1.1 jruoho static ACPI_STATUS
402 1.1 jruoho AcpiUtTrackAllocation (
403 1.1 jruoho ACPI_DEBUG_MEM_BLOCK *Allocation,
404 1.1 jruoho ACPI_SIZE Size,
405 1.1 jruoho UINT8 AllocType,
406 1.1 jruoho UINT32 Component,
407 1.1 jruoho const char *Module,
408 1.1 jruoho UINT32 Line)
409 1.1 jruoho {
410 1.1 jruoho ACPI_MEMORY_LIST *MemList;
411 1.1 jruoho ACPI_DEBUG_MEM_BLOCK *Element;
412 1.1 jruoho ACPI_STATUS Status = AE_OK;
413 1.1 jruoho
414 1.1 jruoho
415 1.1 jruoho ACPI_FUNCTION_TRACE_PTR (UtTrackAllocation, Allocation);
416 1.1 jruoho
417 1.1 jruoho
418 1.1 jruoho if (AcpiGbl_DisableMemTracking)
419 1.1 jruoho {
420 1.1 jruoho return_ACPI_STATUS (AE_OK);
421 1.1 jruoho }
422 1.1 jruoho
423 1.1 jruoho MemList = AcpiGbl_GlobalList;
424 1.1 jruoho Status = AcpiUtAcquireMutex (ACPI_MTX_MEMORY);
425 1.1 jruoho if (ACPI_FAILURE (Status))
426 1.1 jruoho {
427 1.1 jruoho return_ACPI_STATUS (Status);
428 1.1 jruoho }
429 1.1 jruoho
430 1.1 jruoho /*
431 1.4 christos * Search the global list for this address to make sure it is not
432 1.4 christos * already present. This will catch several kinds of problems.
433 1.1 jruoho */
434 1.1 jruoho Element = AcpiUtFindAllocation (Allocation);
435 1.4 christos if (Element == Allocation)
436 1.1 jruoho {
437 1.1 jruoho ACPI_ERROR ((AE_INFO,
438 1.4 christos "UtTrackAllocation: Allocation (%p) already present in global list!",
439 1.1 jruoho Allocation));
440 1.1 jruoho goto UnlockAndExit;
441 1.1 jruoho }
442 1.1 jruoho
443 1.4 christos /* Fill in the instance data */
444 1.1 jruoho
445 1.1 jruoho Allocation->Size = (UINT32) Size;
446 1.1 jruoho Allocation->AllocType = AllocType;
447 1.1 jruoho Allocation->Component = Component;
448 1.1 jruoho Allocation->Line = Line;
449 1.1 jruoho
450 1.1 jruoho ACPI_STRNCPY (Allocation->Module, Module, ACPI_MAX_MODULE_NAME);
451 1.1 jruoho Allocation->Module[ACPI_MAX_MODULE_NAME-1] = 0;
452 1.1 jruoho
453 1.4 christos if (!Element)
454 1.4 christos {
455 1.4 christos /* Insert at list head */
456 1.4 christos
457 1.4 christos if (MemList->ListHead)
458 1.4 christos {
459 1.4 christos ((ACPI_DEBUG_MEM_BLOCK *)(MemList->ListHead))->Previous = Allocation;
460 1.4 christos }
461 1.4 christos
462 1.4 christos Allocation->Next = MemList->ListHead;
463 1.4 christos Allocation->Previous = NULL;
464 1.1 jruoho
465 1.4 christos MemList->ListHead = Allocation;
466 1.4 christos }
467 1.4 christos else
468 1.1 jruoho {
469 1.4 christos /* Insert after element */
470 1.4 christos
471 1.4 christos Allocation->Next = Element->Next;
472 1.4 christos Allocation->Previous = Element;
473 1.1 jruoho
474 1.4 christos if (Element->Next)
475 1.4 christos {
476 1.4 christos (Element->Next)->Previous = Allocation;
477 1.4 christos }
478 1.1 jruoho
479 1.4 christos Element->Next = Allocation;
480 1.4 christos }
481 1.1 jruoho
482 1.1 jruoho
483 1.1 jruoho UnlockAndExit:
484 1.1 jruoho Status = AcpiUtReleaseMutex (ACPI_MTX_MEMORY);
485 1.1 jruoho return_ACPI_STATUS (Status);
486 1.1 jruoho }
487 1.1 jruoho
488 1.1 jruoho
489 1.1 jruoho /*******************************************************************************
490 1.1 jruoho *
491 1.1 jruoho * FUNCTION: AcpiUtRemoveAllocation
492 1.1 jruoho *
493 1.1 jruoho * PARAMETERS: Allocation - Address of allocated memory
494 1.1 jruoho * Component - Component type of caller
495 1.1 jruoho * Module - Source file name of caller
496 1.1 jruoho * Line - Line number of caller
497 1.1 jruoho *
498 1.4 christos * RETURN: Status
499 1.1 jruoho *
500 1.1 jruoho * DESCRIPTION: Deletes an element from the global allocation tracking list.
501 1.1 jruoho *
502 1.1 jruoho ******************************************************************************/
503 1.1 jruoho
504 1.1 jruoho static ACPI_STATUS
505 1.1 jruoho AcpiUtRemoveAllocation (
506 1.1 jruoho ACPI_DEBUG_MEM_BLOCK *Allocation,
507 1.1 jruoho UINT32 Component,
508 1.1 jruoho const char *Module,
509 1.1 jruoho UINT32 Line)
510 1.1 jruoho {
511 1.1 jruoho ACPI_MEMORY_LIST *MemList;
512 1.1 jruoho ACPI_STATUS Status;
513 1.1 jruoho
514 1.1 jruoho
515 1.4 christos ACPI_FUNCTION_NAME (UtRemoveAllocation);
516 1.1 jruoho
517 1.1 jruoho
518 1.1 jruoho if (AcpiGbl_DisableMemTracking)
519 1.1 jruoho {
520 1.4 christos return (AE_OK);
521 1.1 jruoho }
522 1.1 jruoho
523 1.1 jruoho MemList = AcpiGbl_GlobalList;
524 1.1 jruoho if (NULL == MemList->ListHead)
525 1.1 jruoho {
526 1.1 jruoho /* No allocations! */
527 1.1 jruoho
528 1.1 jruoho ACPI_ERROR ((Module, Line,
529 1.1 jruoho "Empty allocation list, nothing to free!"));
530 1.1 jruoho
531 1.4 christos return (AE_OK);
532 1.1 jruoho }
533 1.1 jruoho
534 1.1 jruoho Status = AcpiUtAcquireMutex (ACPI_MTX_MEMORY);
535 1.1 jruoho if (ACPI_FAILURE (Status))
536 1.1 jruoho {
537 1.4 christos return (Status);
538 1.1 jruoho }
539 1.1 jruoho
540 1.1 jruoho /* Unlink */
541 1.1 jruoho
542 1.1 jruoho if (Allocation->Previous)
543 1.1 jruoho {
544 1.1 jruoho (Allocation->Previous)->Next = Allocation->Next;
545 1.1 jruoho }
546 1.1 jruoho else
547 1.1 jruoho {
548 1.1 jruoho MemList->ListHead = Allocation->Next;
549 1.1 jruoho }
550 1.1 jruoho
551 1.1 jruoho if (Allocation->Next)
552 1.1 jruoho {
553 1.1 jruoho (Allocation->Next)->Previous = Allocation->Previous;
554 1.1 jruoho }
555 1.1 jruoho
556 1.4 christos ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "Freeing %p, size 0%X\n",
557 1.4 christos &Allocation->UserSpace, Allocation->Size));
558 1.4 christos
559 1.1 jruoho /* Mark the segment as deleted */
560 1.1 jruoho
561 1.1 jruoho ACPI_MEMSET (&Allocation->UserSpace, 0xEA, Allocation->Size);
562 1.1 jruoho
563 1.1 jruoho Status = AcpiUtReleaseMutex (ACPI_MTX_MEMORY);
564 1.4 christos return (Status);
565 1.1 jruoho }
566 1.1 jruoho
567 1.1 jruoho
568 1.1 jruoho /*******************************************************************************
569 1.1 jruoho *
570 1.1 jruoho * FUNCTION: AcpiUtDumpAllocationInfo
571 1.1 jruoho *
572 1.4 christos * PARAMETERS: None
573 1.1 jruoho *
574 1.1 jruoho * RETURN: None
575 1.1 jruoho *
576 1.1 jruoho * DESCRIPTION: Print some info about the outstanding allocations.
577 1.1 jruoho *
578 1.1 jruoho ******************************************************************************/
579 1.1 jruoho
580 1.1 jruoho void
581 1.1 jruoho AcpiUtDumpAllocationInfo (
582 1.1 jruoho void)
583 1.1 jruoho {
584 1.1 jruoho /*
585 1.1 jruoho ACPI_MEMORY_LIST *MemList;
586 1.1 jruoho */
587 1.1 jruoho
588 1.1 jruoho ACPI_FUNCTION_TRACE (UtDumpAllocationInfo);
589 1.1 jruoho
590 1.1 jruoho /*
591 1.1 jruoho ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
592 1.1 jruoho ("%30s: %4d (%3d Kb)\n", "Current allocations",
593 1.1 jruoho MemList->CurrentCount,
594 1.1 jruoho ROUND_UP_TO_1K (MemList->CurrentSize)));
595 1.1 jruoho
596 1.1 jruoho ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
597 1.1 jruoho ("%30s: %4d (%3d Kb)\n", "Max concurrent allocations",
598 1.1 jruoho MemList->MaxConcurrentCount,
599 1.1 jruoho ROUND_UP_TO_1K (MemList->MaxConcurrentSize)));
600 1.1 jruoho
601 1.1 jruoho
602 1.1 jruoho ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
603 1.1 jruoho ("%30s: %4d (%3d Kb)\n", "Total (all) internal objects",
604 1.1 jruoho RunningObjectCount,
605 1.1 jruoho ROUND_UP_TO_1K (RunningObjectSize)));
606 1.1 jruoho
607 1.1 jruoho ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
608 1.1 jruoho ("%30s: %4d (%3d Kb)\n", "Total (all) allocations",
609 1.1 jruoho RunningAllocCount,
610 1.1 jruoho ROUND_UP_TO_1K (RunningAllocSize)));
611 1.1 jruoho
612 1.1 jruoho
613 1.1 jruoho ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
614 1.1 jruoho ("%30s: %4d (%3d Kb)\n", "Current Nodes",
615 1.1 jruoho AcpiGbl_CurrentNodeCount,
616 1.1 jruoho ROUND_UP_TO_1K (AcpiGbl_CurrentNodeSize)));
617 1.1 jruoho
618 1.1 jruoho ACPI_DEBUG_PRINT (TRACE_ALLOCATIONS | TRACE_TABLES,
619 1.1 jruoho ("%30s: %4d (%3d Kb)\n", "Max Nodes",
620 1.1 jruoho AcpiGbl_MaxConcurrentNodeCount,
621 1.1 jruoho ROUND_UP_TO_1K ((AcpiGbl_MaxConcurrentNodeCount *
622 1.1 jruoho sizeof (ACPI_NAMESPACE_NODE)))));
623 1.1 jruoho */
624 1.1 jruoho return_VOID;
625 1.1 jruoho }
626 1.1 jruoho
627 1.1 jruoho
628 1.1 jruoho /*******************************************************************************
629 1.1 jruoho *
630 1.1 jruoho * FUNCTION: AcpiUtDumpAllocations
631 1.1 jruoho *
632 1.1 jruoho * PARAMETERS: Component - Component(s) to dump info for.
633 1.4 christos * Module - Module to dump info for. NULL means all.
634 1.1 jruoho *
635 1.1 jruoho * RETURN: None
636 1.1 jruoho *
637 1.1 jruoho * DESCRIPTION: Print a list of all outstanding allocations.
638 1.1 jruoho *
639 1.1 jruoho ******************************************************************************/
640 1.1 jruoho
641 1.1 jruoho void
642 1.1 jruoho AcpiUtDumpAllocations (
643 1.1 jruoho UINT32 Component,
644 1.1 jruoho const char *Module)
645 1.1 jruoho {
646 1.1 jruoho ACPI_DEBUG_MEM_BLOCK *Element;
647 1.1 jruoho ACPI_DESCRIPTOR *Descriptor;
648 1.1 jruoho UINT32 NumOutstanding = 0;
649 1.1 jruoho UINT8 DescriptorType;
650 1.1 jruoho
651 1.1 jruoho
652 1.1 jruoho ACPI_FUNCTION_TRACE (UtDumpAllocations);
653 1.1 jruoho
654 1.1 jruoho
655 1.1 jruoho if (AcpiGbl_DisableMemTracking)
656 1.1 jruoho {
657 1.4 christos return_VOID;
658 1.1 jruoho }
659 1.1 jruoho
660 1.1 jruoho /*
661 1.1 jruoho * Walk the allocation list.
662 1.1 jruoho */
663 1.1 jruoho if (ACPI_FAILURE (AcpiUtAcquireMutex (ACPI_MTX_MEMORY)))
664 1.1 jruoho {
665 1.4 christos return_VOID;
666 1.1 jruoho }
667 1.1 jruoho
668 1.1 jruoho Element = AcpiGbl_GlobalList->ListHead;
669 1.1 jruoho while (Element)
670 1.1 jruoho {
671 1.1 jruoho if ((Element->Component & Component) &&
672 1.1 jruoho ((Module == NULL) || (0 == ACPI_STRCMP (Module, Element->Module))))
673 1.1 jruoho {
674 1.1 jruoho Descriptor = ACPI_CAST_PTR (ACPI_DESCRIPTOR, &Element->UserSpace);
675 1.1 jruoho
676 1.1 jruoho if (Element->Size < sizeof (ACPI_COMMON_DESCRIPTOR))
677 1.1 jruoho {
678 1.1 jruoho AcpiOsPrintf ("%p Length 0x%04X %9.9s-%u "
679 1.1 jruoho "[Not a Descriptor - too small]\n",
680 1.1 jruoho Descriptor, Element->Size, Element->Module,
681 1.1 jruoho Element->Line);
682 1.1 jruoho }
683 1.1 jruoho else
684 1.1 jruoho {
685 1.1 jruoho /* Ignore allocated objects that are in a cache */
686 1.1 jruoho
687 1.1 jruoho if (ACPI_GET_DESCRIPTOR_TYPE (Descriptor) != ACPI_DESC_TYPE_CACHED)
688 1.1 jruoho {
689 1.1 jruoho AcpiOsPrintf ("%p Length 0x%04X %9.9s-%u [%s] ",
690 1.1 jruoho Descriptor, Element->Size, Element->Module,
691 1.1 jruoho Element->Line, AcpiUtGetDescriptorName (Descriptor));
692 1.1 jruoho
693 1.1 jruoho /* Validate the descriptor type using Type field and length */
694 1.1 jruoho
695 1.1 jruoho DescriptorType = 0; /* Not a valid descriptor type */
696 1.1 jruoho
697 1.1 jruoho switch (ACPI_GET_DESCRIPTOR_TYPE (Descriptor))
698 1.1 jruoho {
699 1.1 jruoho case ACPI_DESC_TYPE_OPERAND:
700 1.4 christos
701 1.4 christos if (Element->Size == sizeof (ACPI_OPERAND_OBJECT))
702 1.1 jruoho {
703 1.1 jruoho DescriptorType = ACPI_DESC_TYPE_OPERAND;
704 1.1 jruoho }
705 1.1 jruoho break;
706 1.1 jruoho
707 1.1 jruoho case ACPI_DESC_TYPE_PARSER:
708 1.4 christos
709 1.4 christos if (Element->Size == sizeof (ACPI_PARSE_OBJECT))
710 1.1 jruoho {
711 1.1 jruoho DescriptorType = ACPI_DESC_TYPE_PARSER;
712 1.1 jruoho }
713 1.1 jruoho break;
714 1.1 jruoho
715 1.1 jruoho case ACPI_DESC_TYPE_NAMED:
716 1.4 christos
717 1.4 christos if (Element->Size == sizeof (ACPI_NAMESPACE_NODE))
718 1.1 jruoho {
719 1.1 jruoho DescriptorType = ACPI_DESC_TYPE_NAMED;
720 1.1 jruoho }
721 1.1 jruoho break;
722 1.1 jruoho
723 1.1 jruoho default:
724 1.4 christos
725 1.1 jruoho break;
726 1.1 jruoho }
727 1.1 jruoho
728 1.1 jruoho /* Display additional info for the major descriptor types */
729 1.1 jruoho
730 1.1 jruoho switch (DescriptorType)
731 1.1 jruoho {
732 1.1 jruoho case ACPI_DESC_TYPE_OPERAND:
733 1.4 christos
734 1.1 jruoho AcpiOsPrintf ("%12.12s RefCount 0x%04X\n",
735 1.1 jruoho AcpiUtGetTypeName (Descriptor->Object.Common.Type),
736 1.1 jruoho Descriptor->Object.Common.ReferenceCount);
737 1.1 jruoho break;
738 1.1 jruoho
739 1.1 jruoho case ACPI_DESC_TYPE_PARSER:
740 1.4 christos
741 1.1 jruoho AcpiOsPrintf ("AmlOpcode 0x%04hX\n",
742 1.1 jruoho Descriptor->Op.Asl.AmlOpcode);
743 1.1 jruoho break;
744 1.1 jruoho
745 1.1 jruoho case ACPI_DESC_TYPE_NAMED:
746 1.4 christos
747 1.1 jruoho AcpiOsPrintf ("%4.4s\n",
748 1.1 jruoho AcpiUtGetNodeName (&Descriptor->Node));
749 1.1 jruoho break;
750 1.1 jruoho
751 1.1 jruoho default:
752 1.4 christos
753 1.1 jruoho AcpiOsPrintf ( "\n");
754 1.1 jruoho break;
755 1.1 jruoho }
756 1.1 jruoho }
757 1.1 jruoho }
758 1.1 jruoho
759 1.1 jruoho NumOutstanding++;
760 1.1 jruoho }
761 1.1 jruoho
762 1.1 jruoho Element = Element->Next;
763 1.1 jruoho }
764 1.1 jruoho
765 1.1 jruoho (void) AcpiUtReleaseMutex (ACPI_MTX_MEMORY);
766 1.1 jruoho
767 1.1 jruoho /* Print summary */
768 1.1 jruoho
769 1.1 jruoho if (!NumOutstanding)
770 1.1 jruoho {
771 1.1 jruoho ACPI_INFO ((AE_INFO, "No outstanding allocations"));
772 1.1 jruoho }
773 1.1 jruoho else
774 1.1 jruoho {
775 1.1 jruoho ACPI_ERROR ((AE_INFO, "%u(0x%X) Outstanding allocations",
776 1.1 jruoho NumOutstanding, NumOutstanding));
777 1.1 jruoho }
778 1.1 jruoho
779 1.1 jruoho return_VOID;
780 1.1 jruoho }
781 1.1 jruoho
782 1.1 jruoho #endif /* ACPI_DBG_TRACK_ALLOCATIONS */
783