Low memory handler

From Amiga Development Wiki
Revision as of 17:01, 28 August 2015 by AmigaDeveloper (Talk | contribs) (Moved static variable to code block)

Jump to: navigation, search

Kickstart 3.0 added AddMemHandler() call to install memory handler to the system. Memory handlers are executed when the system can not allocate enough memory. User applications can use this to free their caches if deemed useful. Here is a short example how to use memory handlers on Amiga.

#include <proto/exec.h>
 
static ULONG LowMemHandlerCode(REG(A0, struct MemHandlerData *)mhd)
{
   /* REMEMBER: You are not allowed to break Forbid() */
   if (mhd->memh_Flags & MEMHF_RECYCLE)
   {
      /* free even more memory */
      return MEM_DID_EVERYTHING;
   }
   else
   {
      /* free memory here... */
      return MEM_TRY_AGAIN;
   }
}
 
int main()
{
   struct Interrupt LowMemHandler;
 
   // Create our memory handler
   LowMemHandler.is_Node.ln_Pri = 50;
   LowMemHandler.is_Node.ln_Name = "Amiga Wiki Example";
   LowMemHandler.is_Data = NULL;
   LowMemHandler.is_Code = (APTR)&LowMemHandlerCode;
   AddMemHandler(&LowMemHandler);
 
   Wait(SIGBREAKF_CTRL_C);
 
   RemMemHandler(&LowMemHandler);
}