home *** CD-ROM | disk | FTP | other *** search
- /* MemClear.c - (c) 1986 John Hodgson
-
- MemClear walks through the free memory lists zeroing along the way.
- Written mainly as an exercise, but may prove useful to some. This kind of
- system manipulation should never be performed in interrupt code; see RKM:Exec,
- Pg. 56 for more information.
-
- Memory management consists of a linked list of MemHeaders, each
- of which points to a linked list of MemChunks. The address of the first
- MemHeader is contained in ExecBase, yielding access to the whole mess.
-
- Each MemHeader oversees a hardware RAM partition, such as CHIP
- memory or a contiguous block of FAST RAM. It contains information regarding
- Attribute, (CHIP|FAST|PUBLIC) the block's address space, free space remaining,
- free list pointer, and link. The free bytes count is the sum total of all
- the Length fields in its free list; a linked list of MemChunks each containing
- a Length field and a link.
-
- Note that there are no actual pointers to the free space itself;
- Rather, the MemChunks are like Exec Messages in that there is a link node
- on top followed by 0..N bytes of free space. The MemChunk Length fields
- include the sizeof() the MemChunk structure itself, so a MemChunk cannot be
- shorter than 2 longwords. What AllocMem() does is searches each MemHeader that
- matches your attribute and looks for the FIRST MemChunk that fits. The node is
- unlinked from the chain, and its address returned. If the allocation was
- smaller than the node, the excess space is again partitioned up and linked
- into the MemChunk free list. The Length field of the MemHeader is decreased
- by the size of your allocation. Keep in mind that all allocations are rounded
- up to the nearest 8 bytes. Also, MemChunks are coalesced together whenever
- their address spaces are contiguous; it is imperative that the largest
- contiguous space possible be maintained for large-chunk applications such as
- bitmap allocation. */
-
- extern struct ExecBase *SysBase;
-
- main()
- {
- struct MemHeader *memhdr;
- struct MemChunk *memchunk;
- long total=0,count=0;
-
- printf("MemClear (c) 1986 John Hodgson\n");
-
- Forbid(); /* task switching OFF */
-
- /* traverse linked list of MemHeaders */
-
- for (memhdr=(struct MemHeader *)SysBase->MemList.lh_Head;
- memhdr->mh_Node.ln_Succ;
- memhdr=(struct MemHeader *)memhdr->mh_Node.ln_Succ) {
-
- /* traverse linked list of MemChunks */
-
- for (memchunk=memhdr->mh_First;memchunk;memchunk=memchunk->mc_Next) {
-
- /* zero free space; but don't touch the node! */
-
- if (memchunk->mc_Bytes>sizeof(*memchunk))
- setmem((char *)memchunk+sizeof(*memchunk),
- memchunk->mc_Bytes-sizeof(*memchunk),0);
-
- total+=memchunk->mc_Bytes-sizeof(*memchunk); count++;
- }
- }
- Permit();
-
- printf("Cleared %d bytes in %d MemChunks.\n",total,count);
- }
-