r4684@vps: verhaegs | 2007-05-04 21:04:10 -0400
[AROS.git] / rom / exec / freepooled.c
blob8b6e0d356813e985f7269a541e620686d9bbdf8d
1 /*
2 Copyright © 1995-2001, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc: Free memory allocated by AllocPooled().
6 Lang: english
7 */
8 #include "exec_intern.h"
9 #include <aros/libcall.h>
10 #include "memory.h"
11 #include <exec/memory.h>
12 #include <proto/exec.h>
15 /*****************************************************************************
17 NAME */
19 AROS_LH3(void,FreePooled,
21 /* SYNOPSIS */
22 AROS_LHA(APTR, poolHeader,A0),
23 AROS_LHA(APTR, memory, A1),
24 AROS_LHA(ULONG,memSize, D0),
26 /* LOCATION */
27 struct ExecBase *, SysBase, 119, Exec)
29 /* FUNCTION
30 Free memory allocated out of a private memory pool.
32 INPUTS
33 poolHeader - Handle of the memory pool
34 memory - Pointer to the memory
35 memSize - Size of the memory chunk
37 RESULT
39 NOTES
41 EXAMPLE
43 BUGS
45 SEE ALSO
46 CreatePool(), DeletePool(), AllocPooled()
48 INTERNALS
50 ******************************************************************************/
52 AROS_LIBFUNC_INIT
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)
66 struct Block *bl;
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,
78 memSize);
81 /* And Free the memory */
82 FreeMem(bl, bl->Size);
85 else
87 /* Look for the right MemHeader */
88 struct MemHeader *mh = (struct MemHeader *)pool->pool.PuddleList.mlh_Head;
90 for(;;)
92 /* The memory must be between the two borders */
93 if(memory >= mh->mh_Lower && memory < mh->mh_Upper)
95 /* Found the MemHeader. Free the memory. */
96 Deallocate(mh, memory, memSize);
98 /* Is this MemHeader completely free now? */
99 if(mh->mh_Free == pool->pool.PuddleSize)
101 /* Yes. Remove it from the list. */
102 Remove(&mh->mh_Node);
104 /* And free it. */
105 FreeMem(mh, pool->pool.PuddleSize + MEMHEADER_TOTAL);
107 /* All done. */
108 break;
110 /* Try next MemHeader */
111 mh = (struct MemHeader *)mh->mh_Node.ln_Succ;
115 if (pool->pool.Requirements & MEMF_SEM_PROTECTED)
117 ReleaseSemaphore(&pool->sem);
120 AROS_LIBFUNC_EXIT
122 } /* FreePooled */