[comp.sys.amiga] AmigaDOS internals tidbit

cmcmanis%pepper@Sun.COM (Chuck McManis) (05/20/87)

If you are fiddling around the insides of AmigaDOS write this down.

The way DOS allocates memory is a wee bit strange, first it always
grabs an even multiple of four bytes and second the length of the
allocation is prepended to the block. Not knowing this has 
fatally flawed my C version of the Assign command. (I am working on
a new version.) To give an example ...

A device node structure is 44 bytes long (11 longwords) when the
BCPL version of assign grabs memory for it, it allocates 48 bytes
and puts the number '48' in the first longword and passes back a
pointer to the address following the length. The following C code
will do 'proper' allocations and memory frees ...

char *
DOSAlloc(size,req)

long	size, 	/* Amount of memory required  (in bytes!)  	*/
	req;	/* Requirements (MEMF_CHIP,MEMF_FAST,etc) 	*/

{
  long	*lp,length;

  length = (size + 7) & ~3; /* Length as even multiple of 4 + 1 long */
  lp = (long *) AllocMem(length,req);
  if (lp == NULL) return(NULL); /* fails with NULL returned 		 */
  *lp = length; 		/* save the Length in the allocation     */
  return(lp+1); 		/* return pointer to memory after length */
}

void
DOSFree(addr)

long	*addr;		/* pointer to memory block returned by DOSAlloc */

{
  FreeMem(addr-1,*(addr-1)); /* Free memory, length supplied. */
}


--Chuck McManis
uucp: {anywhere}!sun!cmcmanis   BIX: cmcmanis  ARPAnet: cmcmanis@sun.com
These opinions are my own and no one elses, but you knew that didn't you.