home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / macfe / utility / fredmem.cp < prev    next >
Encoding:
Text File  |  1998-04-08  |  7.0 KB  |  277 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. #ifndef NSPR20
  20. #include "prmacos.h"
  21. #endif
  22. #include "fredmem.h"
  23.  
  24. #include "shist.h"
  25. #include "shistele.h"
  26. #include "lo_ele.h"
  27. #include "proto.h"
  28. #include "net.h"
  29. #include "libimg.h"
  30. #include "fe_proto.h"
  31. #include "uerrmgr.h"
  32. #include "PascalString.h"
  33. #include "resgui.h"
  34. #include "MacMemAllocator.h"
  35.  
  36. //#include "MToolkit.h"
  37.  
  38. enum {
  39.     kEmergencyMemoryFundSize            = 64 * 1024
  40. };
  41.  
  42.  
  43. class LMallocFlusherPeriodical : public LPeriodical {
  44.     public:
  45.                             LMallocFlusherPeriodical();
  46.         virtual    void        SpendTime(const EventRecord &inMacEvent);
  47.         void                FlushMemory ( void ) { mFlushMemory = true; }
  48.     
  49.     private:
  50.         Boolean                mFlushMemory;
  51. };
  52.  
  53.  
  54. class LLowMallocWarningPeriodical : public LPeriodical {
  55.     public:
  56.                             LLowMallocWarningPeriodical();
  57.         virtual    void        SpendTime(const EventRecord &inMacEvent);
  58.         void                WarnUserMallocLow ( void ) { mWarnUser = true; }
  59.  
  60.     private:
  61.         Boolean                mWarnUser;
  62. };
  63.  
  64.  
  65. unsigned char MacFEMemoryFlusher ( size_t bytesNeeded );
  66. void ImageCacheMemoryFlusher(void);
  67. void LayoutCacheMemoryFlusher(void);
  68. void NetlibCacheMemoryFlusher(void);
  69. void LibNeoCacheMemoryFlusher(void);
  70.  
  71. void MallocIsLowWarning(void);
  72.  
  73. static LMallocFlusherPeriodical     gMallocFlusher;
  74. static LLowMallocWarningPeriodical    gMallocWarner;
  75.  
  76. void Memory_InitializeMemory()
  77. {
  78.     new LGrowZone(kEmergencyMemoryFundSize);
  79.     new LMemoryFlusherListener();
  80.  
  81.     InstallMemoryCacheFlusher(&MacFEMemoryFlusher);
  82.  
  83.     // install our low memory (malloc heap) warning proc
  84.     InstallMallocHeapLowProc ( &MallocIsLowWarning );
  85. }
  86.  
  87. void Memory_DoneWithMemory()
  88. {
  89.     //    No cleanup necessary
  90. }
  91.  
  92. size_t Memory_MaxApplMem()
  93. {
  94.     return 0;
  95. }
  96.  
  97. Boolean Memory_MemoryIsLow()
  98. {
  99.     return (LGrowZone::GetGrowZone())->MemoryIsLow();
  100. }
  101.  
  102. //##############################################################################
  103. //##############################################################################
  104. #pragma mark MAC FE MEMORY FLUSH PROC
  105. unsigned char MacFEMemoryFlusher ( size_t bytesNeeded )
  106. {
  107.     /* signal the flusher to do it's thing. I would rather start the idler here */
  108.     /* but that involves allocating memory (and so could be reentrant)... */
  109.     gMallocFlusher.FlushMemory();
  110.     
  111.     return 0;
  112. }
  113.  
  114. //##############################################################################
  115. //##############################################################################
  116. #pragma mark LAYOUT CACHE FLUSHING
  117.  
  118. LArray    sFlushableLayoutContextList;
  119.  
  120. void FM_SetFlushable(MWContext* context, Boolean canFlush)
  121. {
  122.     if (canFlush) {
  123.         if (sFlushableLayoutContextList.FetchIndexOf(&context) == 0)
  124.             sFlushableLayoutContextList.InsertItemsAt(1, LArray::index_Last, &context);
  125.     }
  126.     
  127.     else {
  128.         sFlushableLayoutContextList.Remove(&context);
  129.     }
  130. }
  131.  
  132. void LayoutCacheMemoryFlusher(void)
  133. {
  134.     MWContext    *currentContext = NULL;
  135.     SInt32         bytesFreed = 0;
  136.     
  137.     LArrayIterator iter(sFlushableLayoutContextList);
  138.     
  139.     // go ahead and empty all the recycling bins
  140.     while ( iter.Previous(¤tContext) )
  141.         {
  142.         bytesFreed += LO_EmptyRecyclingBin(currentContext);
  143.         sFlushableLayoutContextList.RemoveItemsAt( 1, 1 );
  144.         }
  145. }
  146.  
  147. //##############################################################################
  148. //##############################################################################
  149. #pragma mark LIBNEO CACHE FLUSHING
  150.  
  151. void LibNeoCacheMemoryFlusher(void)
  152. {
  153.     // don't do this yet until we really understand how safe it is
  154. #if 0    
  155.     // we don't really know how much was freed here
  156.     NeoOutOfMemHandler();
  157. #endif
  158. }
  159.  
  160. //##############################################################################
  161. //##############################################################################
  162. #pragma mark NETLIB CACHE FLUSHING
  163.  
  164. void NetlibCacheMemoryFlusher(void)
  165. {
  166.     size_t    newCacheSize;
  167.     size_t    lastCacheSize;
  168.     
  169.     newCacheSize = NET_GetMemoryCacheSize();
  170.     
  171.     // shrink as much as we can
  172.     do    {
  173.         lastCacheSize = newCacheSize;
  174.         newCacheSize = NET_RemoveLastMemoryCacheObject();
  175.         }
  176.     while ( newCacheSize != lastCacheSize );
  177. }
  178.  
  179.  
  180. //##############################################################################
  181. //##############################################################################
  182. #pragma mark IMAGE CACHE FLUSHING
  183.  
  184. void ImageCacheMemoryFlusher(void)
  185. {
  186.     size_t    newCacheSize;
  187.     size_t    lastCacheSize;
  188.     
  189.     newCacheSize = IL_GetCacheSize();
  190.     
  191.     // shrink as much as we can
  192.     do    {
  193.         lastCacheSize = newCacheSize;
  194.         newCacheSize = IL_ShrinkCache();
  195.         }
  196.     while ( newCacheSize != lastCacheSize );
  197. }
  198.  
  199.  
  200. //##############################################################################
  201. //##############################################################################
  202. #pragma mark MALLOC HEAP LOW USER WARNING
  203.  
  204. void MallocIsLowWarning(void)
  205. {
  206.     gMallocWarner.WarnUserMallocLow();
  207. }
  208.  
  209. //##############################################################################
  210. //##############################################################################
  211. #pragma mark GROW ZONE LISTENER
  212.  
  213. LMemoryFlusherListener::
  214. LMemoryFlusherListener():LListener()
  215. {
  216.     (LGrowZone::GetGrowZone())->AddListener(this);
  217. }
  218.  
  219.  
  220. void
  221. LMemoryFlusherListener::ListenToMessage(MessageT inMessage, void *ioParam)
  222. {
  223.     Int32    *bytesToFree = (Int32 *)ioParam;
  224.     if (inMessage == msg_GrowZone) {
  225.         UInt8    successfulFlush = false;
  226.         
  227.         /* we don't do anything here yet */
  228.         if (!successfulFlush)
  229.             *bytesToFree = 0;
  230.     }
  231. }
  232.  
  233. //##############################################################################
  234. //##############################################################################
  235. #pragma mark MEMORY PERIODICAL
  236.  
  237. LLowMallocWarningPeriodical::LLowMallocWarningPeriodical() : LPeriodical()
  238. {
  239.     mWarnUser = false;
  240.     
  241.     StartIdling();
  242. }
  243.  
  244.  
  245. void
  246. LLowMallocWarningPeriodical::SpendTime(const EventRecord &inMacEvent)
  247. {
  248.     if ( mWarnUser )
  249.         {
  250.         StopIdling();
  251.         FE_Alert( NULL, GetPString ( MALLOC_HEAP_LOW_RESID ) );
  252.         }
  253. }
  254.  
  255.  
  256. LMallocFlusherPeriodical::LMallocFlusherPeriodical() : LPeriodical()
  257. {
  258.     mFlushMemory = false;
  259.  
  260.     StartIdling();
  261. }
  262.  
  263. void
  264. LMallocFlusherPeriodical::SpendTime(const EventRecord &inMacEvent)
  265. {
  266.     if ( mFlushMemory )
  267.         {
  268.         ImageCacheMemoryFlusher();
  269.         LayoutCacheMemoryFlusher();
  270.         NetlibCacheMemoryFlusher();    
  271.         LibNeoCacheMemoryFlusher();
  272.         
  273.         mFlushMemory = false;
  274.         }
  275.  
  276. }
  277.