/* LowFrag - patches AllocMem(), trying to ease up
   memory fragmentation - version 1.3 */
/*

1.3 - Wasn't properly patching AllocMem().  Removed that part for now.
    - Now promotes larger allocs, so won't slow down the whole system
      as much.

 Future ideas:

      Task filter list?
      Promote "MEMF_PUBLIC" allocations?

   -Safer removal (check if no-one patched over me before removing).
*/

MODULE 'exec/memory','dos/dos'

->CONST VECTOR = -198 /* AllocMem()*/
CONST VECTOR = -684 /* AllocVec() */

PROC main()
DEF oldfalloc
DEF limit

    IF StrLen(arg) >0
       limit:=Val(arg)
       IF (limit <10) OR (limit > 1000000)
          WriteF('Invalid value!\n')
          CleanUp(20)
       ENDIF
       PutLong({threshold},limit)
    ENDIF

    Forbid()     -> Protect our back.

-> Patch AllocMem()...

    IF oldfalloc:=SetFunction(execbase, VECTOR, {newfalloc})
       PutLong({patchalloc}, oldfalloc)

-> Return to normal.
       Permit()

-> Wait for CTRL-C.
       Wait(SIGBREAKF_CTRL_C)

-> Restoring the original vectors.   NOTE: Not safe - should check if no one
-> has done a SetFunction() over me!

       Forbid()
       SetFunction(execbase, VECTOR, oldfalloc)
    ENDIF

    Permit()

ENDPROC


-> Storage for the threshold value
-> Default to 32 Kb.  This value seemed to be pretty good on my system.
threshold:
LONG 32768

-> Storage for the original vectors
patchalloc:
LONG 0


/* The new code, which will "promote" allocations when needed, before
   returning to the real Alloc#?() code. */

newfalloc:
  CMP.L threshold(PC), D0
  BLE exit2

  OR.L #MEMF_REVERSE, D1
exit2:
  MOVE.L patchalloc(PC), -(A7)
  RTS


CHAR '$VER:lowfrag 1.3 (20.12.96) By Eric Sauvageau.',0
