2 Copyright © 1995-2001, The AROS Development Team. All rights reserved.
5 Desc: Free memory allocated by AllocPooled().
8 #include "exec_intern.h"
9 #include <aros/libcall.h>
11 #include <exec/memory.h>
12 #include <proto/exec.h>
15 /*****************************************************************************
19 AROS_LH3(void,FreePooled
,
22 AROS_LHA(APTR
, poolHeader
,A0
),
23 AROS_LHA(APTR
, memory
, A1
),
24 AROS_LHA(ULONG
,memSize
, D0
),
27 struct ExecBase
*, SysBase
, 119, Exec
)
30 Free memory allocated out of a private memory pool.
33 poolHeader - Handle of the memory pool
34 memory - Pointer to the memory
35 memSize - Size of the memory chunk
46 CreatePool(), DeletePool(), AllocPooled()
50 ******************************************************************************/
54 struct ProtectedPool
*pool
= (struct ProtectedPool
*)poolHeader
;
56 if (!memory
|| !memSize
) return;
58 if (pool
->pool
.Requirements
& MEMF_SEM_PROTECTED
)
60 ObtainSemaphore(&pool
->sem
);
63 /* If memSize is bigger than the ThreshSize it's allocated seperately. */
64 if(memSize
> pool
->pool
.ThreshSize
)
68 /* Get pointer to header */
69 bl
= (struct Block
*)((UBYTE
*)memory
- BLOCK_TOTAL
);
71 /* Remove it from the list */
72 Remove((struct Node
*)&bl
->Node
);
74 if (bl
->Size
!= memSize
+ BLOCK_TOTAL
)
76 kprintf("\nFreePooled: free size does not match alloc size: allocsize = %d freesize = %d!!!\n\n",
77 bl
->Size
- BLOCK_TOTAL
,
81 /* And Free the memory */
82 FreeMem(bl
, bl
->Size
);
87 /* Look for the right MemHeader */
88 struct MemHeader
*mh
= (struct MemHeader
*)pool
->pool
.PuddleList
.mlh_Head
;
92 #if !defined(NO_CONSISTENCY_CHECKS)
95 /* memory block is not in pool. */
96 Alert(AT_Recovery
| AN_MemCorrupt
);
101 /* The memory must be between the two borders */
102 if(memory
>= mh
->mh_Lower
&& memory
< mh
->mh_Upper
)
104 /* Found the MemHeader. Free the memory. */
105 Deallocate(mh
, memory
, memSize
);
107 /* Is this MemHeader completely free now? */
108 if(mh
->mh_Free
== pool
->pool
.PuddleSize
)
110 /* Yes. Remove it from the list. */
111 Remove(&mh
->mh_Node
);
114 FreeMem(mh
, pool
->pool
.PuddleSize
+ MEMHEADER_TOTAL
);
119 /* Try next MemHeader */
120 mh
= (struct MemHeader
*)mh
->mh_Node
.ln_Succ
;
124 if (pool
->pool
.Requirements
& MEMF_SEM_PROTECTED
)
126 ReleaseSemaphore(&pool
->sem
);