Difference between revisions of "Low memory handler"

From Amiga Development Wiki
Jump to: navigation, search
(Moved static variable to code block)
m
 
Line 1: Line 1:
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.
+
Kickstart 3.0 added AddMemHandler() call to install memory handler to the system. Memory handlers are executed when the system cannot find enough free 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.
  
 
<source lang="c">
 
<source lang="c">

Latest revision as of 13:21, 30 August 2015

Kickstart 3.0 added AddMemHandler() call to install memory handler to the system. Memory handlers are executed when the system cannot find enough free 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);
}