1 #include <aros/debug.h>
2 #include <exec/rawfmt.h>
3 #include <proto/kernel.h>
5 #include "exec_intern.h"
14 * Find MemHeader to which address belongs.
15 * This function is legal to be called in supervisor mode (we use TypeOfMem()
16 * in order to validate addresses in tons of places). So, here are checks.
18 struct MemHeader
*FindMem(APTR address
, struct ExecBase
*SysBase
)
20 int usermode
= (KernelBase
!= NULL
) && (KrnIsSuper() == 0);
23 /* Nobody should change the memory list now. */
24 if (usermode
) MEM_LOCK_SHARED
;
26 /* Follow the list of MemHeaders */
27 mh
= (struct MemHeader
*)SysBase
->MemList
.lh_Head
;
29 while(mh
->mh_Node
.ln_Succ
!= NULL
)
31 /* Check if this MemHeader fits */
32 if(address
>= mh
->mh_Lower
&& address
< mh
->mh_Upper
)
35 if (usermode
) MEM_UNLOCK
;
39 /* Go to next MemHeader */
40 mh
= (struct MemHeader
*)mh
->mh_Node
.ln_Succ
;
43 if (usermode
) MEM_UNLOCK
;
47 char *FormatMMContext(char *buffer
, struct MMContext
*ctx
, struct ExecBase
*SysBase
)
50 buffer
= NewRawDoFmt("In %s, block at 0x%p, size %lu", (VOID_FUNC
)RAWFMTFUNC_STRING
, buffer
, ctx
->func
, ctx
->addr
, ctx
->size
) - 1;
52 buffer
= NewRawDoFmt("In %s, size %lu", (VOID_FUNC
)RAWFMTFUNC_STRING
, buffer
, ctx
->func
, ctx
->size
) - 1;
56 buffer
= NewRawDoFmt("\nCorrupted MemChunk 0x%p (next 0x%p, size %lu)", (VOID_FUNC
)RAWFMTFUNC_STRING
, buffer
, ctx
->mc
, ctx
->mc
->mc_Next
, ctx
->mc
->mc_Bytes
) - 1;
59 buffer
= NewRawDoFmt("\nPrevious MemChunk 0x%p (next 0x%p, size %lu)", (VOID_FUNC
)RAWFMTFUNC_STRING
, buffer
, ctx
->mcPrev
, ctx
->mcPrev
->mc_Next
, ctx
->mcPrev
->mc_Bytes
) - 1;
62 /* Print MemHeader details */
63 buffer
= NewRawDoFmt("\nMemHeader 0x%p (0x%p - 0x%p)", (VOID_FUNC
)RAWFMTFUNC_STRING
, buffer
, ctx
->mh
, ctx
->mh
->mh_Lower
, ctx
->mh
->mh_Upper
) - 1;
64 if ((IPTR
)ctx
->mh
->mh_First
& (MEMCHUNK_TOTAL
- 1))
65 buffer
= NewRawDoFmt("\n- Unaligned first chunk address (0x%p)", (VOID_FUNC
)RAWFMTFUNC_STRING
, buffer
, ctx
->mh
->mh_First
) - 1;
67 if (ctx
->mh
->mh_Free
& (MEMCHUNK_TOTAL
- 1))
68 buffer
= NewRawDoFmt("\n- Unaligned free space count (0x%p)", (VOID_FUNC
)RAWFMTFUNC_STRING
, buffer
, ctx
->mh
->mh_Free
) - 1;
70 if (ctx
->mh
->mh_First
)
72 if ((APTR
)ctx
->mh
->mh_First
< ctx
->mh
->mh_Lower
)
73 buffer
= NewRawDoFmt("\n- First chunk (0x%p) below lower address", (VOID_FUNC
)RAWFMTFUNC_STRING
, buffer
, ctx
->mh
->mh_First
) - 1;
75 if (((APTR
)ctx
->mh
->mh_First
+ ctx
->mh
->mh_Free
> ctx
->mh
->mh_Upper
))
76 buffer
= NewRawDoFmt("\n- Free space count too large (%lu, first chunk 0x%xp)", (VOID_FUNC
)RAWFMTFUNC_STRING
, buffer
, ctx
->mh
->mh_Free
, ctx
->mh
->mh_First
) - 1;
82 #ifdef NO_CONSISTENCY_CHECKS
84 #define validateHeader(mh, op, addr, size, SysBase) TRUE
85 #define validateChunk(mc, prev, mh, op, addr, size, SysBase) TRUE
89 static ULONG memAlerts
[] =
91 AT_DeadEnd
|AN_MemoryInsane
, /* MM_ALLOC */
92 AT_DeadEnd
|AN_MemCorrupt
, /* MM_FREE */
93 AN_FreeTwice
/* MM_OVERLAP */
97 * MemHeader validation routine. Rules are:
99 * 1. Both mh_First and mh_Free must be MEMCHUNK_TOTAL-aligned.
100 * 2. Free space (if present) must completely fit in between mh_Lower and mh_Upper.
101 * We intentionally don't check header's own location. We assume that in future we'll
102 * be able to put MEMF_CHIP headers inside MEMF_FAST memory, for speed up.
104 static BOOL
validateHeader(struct MemHeader
*mh
, UBYTE op
, APTR addr
, IPTR size
, struct TraceLocation
*tp
, struct ExecBase
*SysBase
)
106 if (((IPTR
)mh
->mh_First
& (MEMCHUNK_TOTAL
- 1)) || (mh
->mh_Free
& (MEMCHUNK_TOTAL
- 1)) || /* 1 */
108 (((APTR
)mh
->mh_First
< mh
->mh_Lower
) || ((APTR
)mh
->mh_First
+ mh
->mh_Free
> mh
->mh_Upper
)))) /* 2 */
112 /* TraceLocation is not supplied by PrepareExecBase(). Fail silently. */
113 struct MMContext alertData
;
117 alertData
.mcPrev
= NULL
;
118 alertData
.func
= tp
->function
;
119 alertData
.addr
= addr
;
120 alertData
.size
= size
;
123 Exec_ExtAlert(memAlerts
[op
], tp
->caller
, tp
->stack
, AT_MEMORY
, &alertData
, SysBase
);
127 * Theoretically during very early boot we can fail to post an alert (no KernelBase yet).
128 * In this case we return with fault indication.
136 * MemChunk consistency check. Rules are:
138 * 1. Both mc_Next and mc_Bytes must me MEMCHUNK_TOTAL-aligned, and mc_Bytes can not be zero.
139 * 2. End of this chunk must not be greater than mh->mh_Upper
140 * 3. mc_Next (if present) must point in between end of this chunk and mh->mh_Upper - MEMCHUNK_TOTAL.
141 * There must be at least MEMHCUNK_TOTAL allocated bytes between free chunks.
143 * This function is inlined for speed improvements.
145 static inline BOOL
validateChunk(struct MemChunk
*p2
, struct MemChunk
*p1
, struct MemHeader
*mh
,
146 UBYTE op
, APTR addr
, IPTR size
,
147 struct TraceLocation
*tp
, struct ExecBase
*SysBase
)
149 if (((IPTR
)p2
->mc_Next
& (MEMCHUNK_TOTAL
-1)) || (p2
->mc_Bytes
== 0) || (p2
->mc_Bytes
& (MEMCHUNK_TOTAL
-1)) || /* 1 */
150 ((APTR
)p2
+ p2
->mc_Bytes
> mh
->mh_Upper
) || /* 2 */
151 (p2
->mc_Next
&& (((APTR
)p2
->mc_Next
< (APTR
)p2
+ p2
->mc_Bytes
+ MEMCHUNK_TOTAL
) || /* 3 */
152 ((APTR
)p2
->mc_Next
> mh
->mh_Upper
- MEMCHUNK_TOTAL
))))
156 struct MMContext alertData
;
160 alertData
.mcPrev
= (p1
== (struct MemChunk
*)&mh
->mh_First
) ? NULL
: p1
;
161 alertData
.func
= tp
->function
;
162 alertData
.addr
= addr
;
163 alertData
.size
= size
;
166 Exec_ExtAlert(memAlerts
[op
], tp
->caller
, tp
->stack
, AT_MEMORY
, &alertData
, SysBase
);
177 * Allocate block from the given MemHeader in a specific way.
178 * This routine can be called with SysBase = NULL.
180 APTR
stdAlloc(struct MemHeader
*mh
, IPTR size
, ULONG requirements
, struct TraceLocation
*tp
, struct ExecBase
*SysBase
)
182 /* First round byteSize up to a multiple of MEMCHUNK_TOTAL */
183 IPTR byteSize
= AROS_ROUNDUP2(size
, MEMCHUNK_TOTAL
);
184 struct MemChunk
*mc
=NULL
, *p1
, *p2
;
186 /* Validate MemHeader before doing anything. */
187 if (!validateHeader(mh
, MM_ALLOC
, NULL
, size
, tp
, SysBase
))
191 * The free memory list is only single linked, i.e. to remove
192 * elements from the list I need node's predessor. For the
193 * first element I can use mh->mh_First instead of a real predessor.
195 p1
= (struct MemChunk
*)&mh
->mh_First
;
199 * Follow the memory list. p1 is the previous MemChunk, p2 is the current one.
200 * On 1st pass p1 points to mh->mh_First, so that changing p1->mc_Next actually
201 * changes mh->mh_First.
205 /* Validate the current chunk */
206 if (!validateChunk(p2
, p1
, mh
, MM_ALLOC
, NULL
, size
, tp
, SysBase
))
209 /* Check if the current block is large enough */
210 if (p2
->mc_Bytes
>=byteSize
)
215 /* Use this one if MEMF_REVERSE is not set.*/
216 if (!(requirements
& MEMF_REVERSE
))
218 /* Else continue - there may be more to come. */
221 /* Go to next block */
226 /* Something found? */
229 /* Remember: if MEMF_REVERSE is set p1 and p2 are now invalid. */
233 /* Remove the block from the list and return it. */
234 if (p2
->mc_Bytes
== byteSize
)
236 /* Fits exactly. Just relink the list. */
237 p1
->mc_Next
= p2
->mc_Next
;
242 if (requirements
& MEMF_REVERSE
)
244 /* Return the last bytes. */
246 mc
= (struct MemChunk
*)((UBYTE
*)p2
+p2
->mc_Bytes
-byteSize
);
250 /* Return the first bytes. */
251 p1
->mc_Next
=(struct MemChunk
*)((UBYTE
*)p2
+byteSize
);
256 p1
->mc_Next
= p2
->mc_Next
;
257 p1
->mc_Bytes
= p2
->mc_Bytes
-byteSize
;
260 mh
->mh_Free
-= byteSize
;
262 /* Clear the block if requested */
263 if (requirements
& MEMF_CLEAR
)
264 memset(mc
, 0, byteSize
);
270 /* Free 'byteSize' bytes starting at 'memoryBlock' belonging to MemHeader 'freeList' */
271 void stdDealloc(struct MemHeader
*freeList
, APTR addr
, IPTR size
, struct TraceLocation
*tp
, struct ExecBase
*SysBase
)
275 struct MemChunk
*p1
, *p2
, *p3
;
278 /* Make sure the MemHeader is OK */
279 if (!validateHeader(freeList
, MM_FREE
, addr
, size
, tp
, SysBase
))
282 /* Align size to the requirements */
283 byteSize
= size
+ ((IPTR
)addr
& (MEMCHUNK_TOTAL
-1));
284 byteSize
= (byteSize
+ MEMCHUNK_TOTAL
-1) & ~(MEMCHUNK_TOTAL
-1);
286 /* Align the block as well */
287 memoryBlock
= (APTR
)((IPTR
)addr
& ~(MEMCHUNK_TOTAL
-1));
290 The free memory list is only single linked, i.e. to insert
291 elements into the list I need the node as well as it's
292 predessor. For the first element I can use freeList->mh_First
293 instead of a real predessor.
295 p1
=(struct MemChunk
*)&freeList
->mh_First
;
296 p2
=freeList
->mh_First
;
298 /* Start and end(+1) of the block */
299 p3
=(struct MemChunk
*)memoryBlock
;
300 p4
=(UBYTE
*)p3
+byteSize
;
302 /* No chunk in list? Just insert the current one and return. */
305 p3
->mc_Bytes
=byteSize
;
308 freeList
->mh_Free
+=byteSize
;
312 /* Follow the list to find a place where to insert our memory. */
315 if (!validateChunk(p2
, p1
, freeList
, MM_FREE
, addr
, size
, tp
, SysBase
))
318 /* Found a block with a higher address? */
321 #if !defined(NO_CONSISTENCY_CHECKS)
323 If the memory to be freed overlaps with the current
324 block something must be wrong.
328 bug("[MM] Chunk allocator error\n");
329 bug("[MM] Attempt to free %u bytes at 0x%p from MemHeader 0x%p\n", byteSize
, memoryBlock
, freeList
);
330 bug("[MM] Block overlaps with chunk 0x%p (%u bytes)\n", p2
, p2
->mc_Bytes
);
336 /* End the loop with p2 non-zero */
339 /* goto next block */
343 /* If the loop ends with p2 zero add it at the end. */
346 /* If there was a previous block merge with it. */
347 if(p1
!=(struct MemChunk
*)&freeList
->mh_First
)
349 #if !defined(NO_CONSISTENCY_CHECKS)
350 /* Check if they overlap. */
351 if ((UBYTE
*)p1
+p1
->mc_Bytes
>(UBYTE
*)p3
)
353 bug("[MM] Chunk allocator error\n");
354 bug("[MM] Attempt to free %u bytes at 0x%p from MemHeader 0x%p\n", byteSize
, memoryBlock
, freeList
);
355 bug("[MM] Block overlaps with chunk 0x%p (%u bytes)\n", p1
, p1
->mc_Bytes
);
361 /* Merge if possible */
362 if((UBYTE
*)p1
+p1
->mc_Bytes
==(UBYTE
*)p3
)
365 /* Not possible to merge */
369 There was no previous block. Just insert the memory at
370 the start of the list.
374 /* Try to merge with next block (if there is one ;-) ). */
375 if(p4
==(UBYTE
*)p2
&&p2
!=NULL
)
378 Overlap checking already done. Doing it here after
379 the list potentially changed would be a bad idea.
384 /* relink the list and return. */
386 p3
->mc_Bytes
=p4
-(UBYTE
*)p3
;
387 freeList
->mh_Free
+=byteSize
;
392 * During transition period four routines below use nommu allocator.
393 * When transition is complete they should use them only if MMU
394 * is inactive. Otherwise they should use KrnAllocPages()/KrnFreePages().
397 /* Non-mungwalled AllocAbs(). Does not destroy sideways regions. */
398 APTR
InternalAllocAbs(APTR location
, IPTR byteSize
, struct ExecBase
*SysBase
)
400 return nommu_AllocAbs(location
, byteSize
, SysBase
);
404 * Use this if you want to free region allocated by InternalAllocAbs().
405 * Otherwise you hit mungwall problem (FreeMem() expects header).
407 void InternalFreeMem(APTR location
, IPTR byteSize
, struct TraceLocation
*loc
, struct ExecBase
*SysBase
)
409 nommu_FreeMem(location
, byteSize
, loc
, SysBase
);
412 /* Allocate a region managed by own header */
413 APTR
AllocMemHeader(IPTR size
, ULONG flags
, struct TraceLocation
*loc
, struct ExecBase
*SysBase
)
415 struct MemHeader
*mh
;
417 mh
= nommu_AllocMem(size
, flags
, loc
, SysBase
);
418 DMH(bug("[AllocMemHeader] Allocated %u bytes at 0x%p\n", size
, mh
));
422 struct MemHeader
*orig
= FindMem(mh
, SysBase
);
424 size
-= MEMHEADER_TOTAL
;
427 * Initialize new MemHeader.
428 * Inherit attributes from system MemHeader from which
429 * our chunk was allocated.
431 mh
->mh_Node
.ln_Type
= NT_MEMORY
;
432 mh
->mh_Node
.ln_Pri
= orig
->mh_Node
.ln_Pri
;
433 mh
->mh_Attributes
= orig
->mh_Attributes
;
434 mh
->mh_Lower
= (APTR
)mh
+ MEMHEADER_TOTAL
;
435 mh
->mh_Upper
= mh
->mh_Lower
+ size
;
436 mh
->mh_First
= mh
->mh_Lower
;
439 /* Create the first (and the only) MemChunk */
440 mh
->mh_First
->mc_Next
= NULL
;
441 mh
->mh_First
->mc_Bytes
= size
;
446 /* Free a region allocated by AllocMemHeader() */
447 void FreeMemHeader(APTR addr
, struct TraceLocation
*loc
, struct ExecBase
*SysBase
)
449 ULONG size
= ((struct MemHeader
*)addr
)->mh_Upper
- addr
;
451 DMH(bug("[FreeMemHeader] Freeing %u bytes at 0x%p\n", size
, addr
));
452 nommu_FreeMem(addr
, size
, loc
, SysBase
);
456 * This is our own Enqueue() version. Currently the only differece is that
457 * we insert our node before the first node with LOWER OR EQUAL priority,
458 * so that for nodes with equal priority it will be LIFO, not FIFO queue.
459 * This speeds up the allocator.
460 * TODO: implement secondary sorting by mh_Free. This will allow to
461 * implement best-match algorithm (so that puddles with smaller free space
462 * will be picked up first). This way the smallest allocations will reuse
463 * smallest chunks instead of fragmenting large ones.
465 static void EnqueueMemHeader(struct MinList
*list
, struct MemHeader
*mh
)
467 struct MemHeader
*next
;
469 /* Look through the list */
470 ForeachNode (list
, next
)
473 Look for the first MemHeader with a lower or equal pri as the node
474 we have to insert into the list.
476 if (mh
->mh_Node
.ln_Pri
>= next
->mh_Node
.ln_Pri
)
480 /* Insert the node before next */
481 mh
->mh_Node
.ln_Pred
= next
->mh_Node
.ln_Pred
;
482 mh
->mh_Node
.ln_Succ
= &next
->mh_Node
;
483 next
->mh_Node
.ln_Pred
->ln_Succ
= &mh
->mh_Node
;
484 next
->mh_Node
.ln_Pred
= &mh
->mh_Node
;
488 * Allocate memory with given physical properties from the given pool.
489 * Our pools can be mixed. This means that different puddles from the
490 * pool can have different physical flags. For example the same pool
491 * can contain puddles from both CHIP and FAST memory. This is done in
492 * order to provide a single system default pool for all types of memory.
494 APTR
InternalAllocPooled(APTR poolHeader
, IPTR memSize
, ULONG flags
, struct TraceLocation
*loc
, struct ExecBase
*SysBase
)
496 struct ProtectedPool
*pool
= poolHeader
+ MEMHEADER_TOTAL
;
499 struct MemHeader
*mh
;
501 D(bug("[exec] InternalAllocPooled(0x%p, %u, 0x%08X), header 0x%p\n", poolHeader
, memSize
, flags
, pool
));
504 * Memory blocks allocated from the pool store pointers to the MemHeader they were
505 * allocated from. This is done in order to avoid slow lookups in InternalFreePooled().
506 * This is done in AllocVec()-alike manner, the pointer is placed right before the block.
508 memSize
+= sizeof(struct MemHeader
*);
511 /* If mungwall is enabled, count also size of walls */
512 if (PrivExecBase(SysBase
)->IntFlags
& EXECF_MungWall
)
513 memSize
+= MUNGWALL_TOTAL_SIZE
;
515 if (pool
->pool
.Requirements
& MEMF_SEM_PROTECTED
)
517 ObtainSemaphore(&pool
->sem
);
520 /* Follow the list of MemHeaders */
521 mh
= (struct MemHeader
*)pool
->pool
.PuddleList
.mlh_Head
;
524 ULONG physFlags
= flags
& MEMF_PHYSICAL_MASK
;
526 /* Are there no more MemHeaders? */
527 if (mh
->mh_Node
.ln_Succ
==NULL
)
531 * Usually we allocate puddles of default size, specified during
532 * pool creation. However we can be asked to allocate block whose
533 * size will be larger than default puddle size.
534 * Previously this was handled by threshSize parameter. In our new
535 * implementation we just allocate enlarged puddle. This is done
536 * in order not to waste page tails beyond the allocated large block.
537 * These tails will be used for our pool too. Their size is smaller
538 * than page size but they still perfectly fit for small allocations
539 * (the primary use for pools).
540 * Since our large block is also a puddle, it will be reused for our
541 * pool when the block is freed. It can also be reused for another
542 * large allocation, if it fits in.
543 * Our final puddle size still includes MEMHEADER_TOTAL in any case.
545 IPTR puddleSize
= pool
->pool
.PuddleSize
;
547 if (memSize
> puddleSize
- MEMHEADER_TOTAL
)
549 IPTR align
= PrivExecBase(SysBase
)->PageSize
- 1;
551 puddleSize
= memSize
+ MEMHEADER_TOTAL
;
552 /* Align the size up to page boundary */
553 puddleSize
= (puddleSize
+ align
) & ~align
;
556 mh
= AllocMemHeader(puddleSize
, flags
, loc
, SysBase
);
557 D(bug("[InternalAllocPooled] Allocated new puddle 0x%p, size %u\n", mh
, puddleSize
));
559 /* No memory left? */
563 /* Add the new puddle to our pool */
564 mh
->mh_Node
.ln_Name
= (STRPTR
)pool
;
565 Enqueue((struct List
*)&pool
->pool
.PuddleList
, &mh
->mh_Node
);
567 /* Fall through to get the memory */
571 /* Ignore existing MemHeaders with memory type that differ from the requested ones */
572 if (physFlags
& ~mh
->mh_Attributes
)
574 D(bug("[InternalAllocPooled] Wrong flags for puddle 0x%p (wanted 0x%08X, have 0x%08X\n", flags
, mh
->mh_Attributes
));
576 mh
= (struct MemHeader
*)mh
->mh_Node
.ln_Succ
;
581 /* Try to get the memory */
582 ret
= stdAlloc(mh
, memSize
, flags
, loc
, SysBase
);
583 D(bug("[InternalAllocPooled] Allocated memory at 0x%p from puddle 0x%p\n", ret
, mh
));
589 * If this is not the first MemHeader and it has some free space,
590 * move it forward (so that the next allocation will attempt to use it first).
591 * IMPORTANT: We use modification of Enqueue() because we still sort MemHeaders
592 * according to their priority (which they inherit from system MemHeaders).
593 * This allows us to have mixed pools (e. g. with both CHIP and FAST regions). This
594 * will be needed in future for memory protection.
596 if (mh
->mh_Node
.ln_Pred
!= NULL
&& mh
->mh_Free
> 32)
598 D(bug("[InternalAllocPooled] Re-sorting puddle list\n"));
599 Remove(&mh
->mh_Node
);
600 EnqueueMemHeader(&pool
->pool
.PuddleList
, mh
);
606 /* No. Try next MemHeader */
607 mh
= (struct MemHeader
*)mh
->mh_Node
.ln_Succ
;
610 if (pool
->pool
.Requirements
& MEMF_SEM_PROTECTED
)
612 ReleaseSemaphore(&pool
->sem
);
617 /* Build munge walls if requested */
618 ret
= MungWall_Build(ret
, pool
, origSize
, flags
, loc
, SysBase
);
620 /* Remember where we were allocated from */
621 *((struct MemHeader
**)ret
) = mh
;
622 ret
+= sizeof(struct MemHeader
*);
625 /* Everything fine */
630 * This is a pair to InternalAllocPooled()
631 * This code separated from FreePooled() in order to provide compatibility with various
632 * memory tracking patches. If some exec code calls InternalAllocPooled() directly
633 * (AllocMem() will do it), it has to call also InternalFreePooled() directly.
634 * Our chunks remember from which pool they came, so we don't need a pointer to pool
635 * header here. This will save us from headaches in future FreeMem() implementation.
637 void InternalFreePooled(APTR memory
, IPTR memSize
, struct TraceLocation
*loc
, struct ExecBase
*SysBase
)
639 struct MemHeader
*mh
;
643 D(bug("[exec] InternalFreePooled(0x%p, %u)\n", memory
, memSize
));
645 if (!memory
|| !memSize
) return;
647 /* Get MemHeader pointer. It is stored right before our block. */
648 freeStart
= memory
- sizeof(struct MemHeader
*);
649 freeSize
= memSize
+ sizeof(struct MemHeader
*);
650 mh
= *((struct MemHeader
**)freeStart
);
652 /* Check walls first */
653 freeStart
= MungWall_Check(freeStart
, freeSize
, loc
, SysBase
);
654 if (PrivExecBase(SysBase
)->IntFlags
& EXECF_MungWall
)
655 freeSize
+= MUNGWALL_TOTAL_SIZE
;
657 /* Verify that MemHeader pointer is correct */
658 if ((mh
->mh_Node
.ln_Type
!= NT_MEMORY
) ||
659 (freeStart
< mh
->mh_Lower
) || (freeStart
+ freeSize
> mh
->mh_Upper
))
662 * Something is wrong.
663 * TODO: the following should actually be printed as part of the alert.
664 * In future there should be some kind of "alert context". CPU alerts
665 * (like illegal access) should remember CPU context there. Memory manager
666 * alerts (like this one) should remember some own information.
668 bug("[MM] Pool manager error\n");
669 bug("[MM] Attempt to free %u bytes at 0x%p\n", memSize
, memory
);
670 bug("[MM] The chunk does not belong to a pool\n");
672 Alert(AN_BadFreeAddr
);
676 struct ProtectedPool
*pool
= (struct ProtectedPool
*)mh
->mh_Node
.ln_Name
;
679 if (pool
->pool
.Requirements
& MEMF_SEM_PROTECTED
)
681 ObtainSemaphore(&pool
->sem
);
684 size
= mh
->mh_Upper
- mh
->mh_Lower
;
685 D(bug("[FreePooled] Allocated from puddle 0x%p, size %u\n", mh
, size
));
687 /* Free the memory. */
688 stdDealloc(mh
, freeStart
, freeSize
, loc
, SysBase
);
689 D(bug("[FreePooled] Deallocated chunk, %u free bytes in the puddle\n", mh
->mh_Free
));
691 /* Is this MemHeader completely free now? */
692 if (mh
->mh_Free
== size
)
694 D(bug("[FreePooled] Puddle is empty, giving back to the system\n"));
696 /* Yes. Remove it from the list. */
697 Remove(&mh
->mh_Node
);
699 FreeMemHeader(mh
, loc
, SysBase
);
703 if (pool
->pool
.Requirements
& MEMF_SEM_PROTECTED
)
705 ReleaseSemaphore(&pool
->sem
);
710 ULONG
checkMemHandlers(struct checkMemHandlersState
*cmhs
, struct ExecBase
*SysBase
)
713 struct Interrupt
*lmh
;
715 if (cmhs
->cmhs_Data
.memh_RequestFlags
& MEMF_NO_EXPUNGE
)
716 return MEM_DID_NOTHING
;
718 /* In order to keep things clean, we must run in a single thread */
719 ObtainSemaphore(&PrivExecBase(SysBase
)->LowMemSem
);
722 * Loop over low memory handlers. Handlers can remove
723 * themselves from the list while being invoked, thus
724 * we need to be careful!
726 for (lmh
= (struct Interrupt
*)cmhs
->cmhs_CurNode
;
727 (tmp
= lmh
->is_Node
.ln_Succ
);
728 lmh
= (struct Interrupt
*)(cmhs
->cmhs_CurNode
= tmp
))
732 ret
= AROS_UFC3 (LONG
, lmh
->is_Code
,
733 AROS_UFCA(struct MemHandlerData
*, &cmhs
->cmhs_Data
, A0
),
734 AROS_UFCA(APTR
, lmh
->is_Data
, A1
),
735 AROS_UFCA(struct ExecBase
*, SysBase
, A6
)
738 if (ret
== MEM_TRY_AGAIN
)
740 /* MemHandler said he did something. Try again. */
741 /* Is there any program that depends on this flag??? */
742 cmhs
->cmhs_Data
.memh_Flags
|= MEMHF_RECYCLE
;
744 ReleaseSemaphore(&PrivExecBase(SysBase
)->LowMemSem
);
745 return MEM_TRY_AGAIN
;
748 /* Nothing more to expect from this handler. */
749 cmhs
->cmhs_Data
.memh_Flags
&= ~MEMHF_RECYCLE
;
752 ReleaseSemaphore(&PrivExecBase(SysBase
)->LowMemSem
);
753 return MEM_DID_NOTHING
;