[comp.sys.amiga.tech] Tip for effective memory usage

glewis@cit-vax.Caltech.Edu (Glenn M. Lewis) (06/29/88)

[]

	This little gem was sent to me by Tomas Rokicki (of AmigaTeX, previewer,
profiler, BlitLab, etc., fame).  It checks to see if data that *needs* to be
in MEMF_CHIP is *already* in MEMF_CHIP, and if so, does not AllocMem() more
chip memory wastefully, but leaves it there and returns a pointer to it.
	I thought this was worth passing on.  I haven't tested it yet, but it
sure looks good :-)

							-- Glenn Lewis

>From: Tomas G. Rokicki <rokicki@polya.Stanford.EDU>

[Here] is a function that tells you if something is in Chip RAM or not:

	TypeOfMem(ptr)

will return memory type; AND it with MEMF_CHIP to see if things are
already in Chip.  Thus, you might do something like

struct Image foo = { . . . }
struct Image *usefoo ;

initialize() {
   foo = (struct Image *)checkchip(foo, sizeof(struct Image)) ;
}

void *checkchip(data, len)
void *data ;
int len ;
{
   void *ptr ;

   if (TypeOfMem(data) & MEMF_CHIP)
      return(data) ;
   ptr = AllocMem((long)len, MEMF_CHIP) ;
   if (ptr == NULL)
      error("! out of core") ;
   CopyMem(data, ptr, (long)len) ;
   return(ptr) ;
}


-- 
glewis@cit-vax.caltech.edu