"Fossies" - the Fresh Open Source Software Archive

Member "AutoHotkey_L-1.1.33.09/source/SimpleHeap.h" (8 May 2021, 3053 Bytes) of package /windows/misc/AutoHotkey_L-1.1.33.09.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "SimpleHeap.h" see the Fossies "Dox" file reference documentation.

    1 /*
    2 AutoHotkey
    3 
    4 Copyright 2003-2009 Chris Mallett (support@autohotkey.com)
    5 
    6 This program is free software; you can redistribute it and/or
    7 modify it under the terms of the GNU General Public License
    8 as published by the Free Software Foundation; either version 2
    9 of the License, or (at your option) any later version.
   10 
   11 This program is distributed in the hope that it will be useful,
   12 but WITHOUT ANY WARRANTY; without even the implied warranty of
   13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   14 GNU General Public License for more details.
   15 */
   16 
   17 #ifndef SimpleHeap_h
   18 #define SimpleHeap_h
   19 
   20 // In a large script (200 KB of text) of a typical nature, using SimpleHeap rather than malloc() saves
   21 // nearly 200 KB of memory as shown by Task Manager's "VM Size" column (2384 vs. 2580 KB).
   22 // This is because many callers allocate chunks of memory that are very small on average.  If each
   23 // such chunk were allocated with malloc (or worse, "new"), the percentage of system overhead
   24 // compared to the memory actually used for such blocks would be enormous (perhaps 40 bytes of
   25 // overhead for each malloc(), even if it's only for 3 or 4 bytes).  In addition, SimpleHeap improves
   26 // performance to the extent that it is faster than malloc(), which it almost certainly is.  Finally,
   27 // the OS's overall memory fragmentation may be reduced, especially if the app uses this class over
   28 // a long period of time (hours or days).
   29 
   30 // The size of each block in bytes.  Use a size that's a good compromise
   31 // of avg. wastage vs. reducing memory fragmentation and overhead.
   32 // But be careful never to reduce it to something less than LINE_SIZE
   33 // (the maximum line length that can be loaded -- currently 16K), otherwise,
   34 // memory for that line might be impossible to allocate.
   35 // Update: reduced it from 64K to 32K since many scripts tend to be small.
   36 // Update 2 (fincs): Use twice as big blocks when compiling for Unicode because
   37 // Unicode strings are twice as large.
   38 #define BLOCK_SIZE (32 * 1024 * sizeof(TCHAR)) // Relied upon by Malloc() to be a multiple of 4.
   39 
   40 class SimpleHeap
   41 {
   42 private:
   43     char *mBlock; // This object's memory block.  Although private, its contents are public.
   44     char *mFreeMarker;  // Address inside the above block of the first unused byte.
   45     size_t mSpaceAvailable;
   46     static UINT sBlockCount;
   47     static SimpleHeap *sFirst, *sLast;  // The first and last objects in the linked list.
   48     static char *sMostRecentlyAllocated; // For use with Delete().
   49     SimpleHeap *mNextBlock;  // The object after this one in the linked list; NULL if none.
   50 
   51     static SimpleHeap *CreateBlock();
   52     SimpleHeap();  // Private constructor, since we want only the static methods to be able to create new objects.
   53     ~SimpleHeap();
   54 public:
   55 //  static UINT GetBlockCount() {return sBlockCount;}
   56     static LPTSTR Malloc(LPTSTR aBuf, size_t aLength = -1); // Return a block of memory to the caller and copy aBuf into it.
   57     static void* Malloc(size_t aSize); // Return a block of memory to the caller.
   58     static void Delete(void *aPtr);
   59     //static void DeleteAll();
   60 };
   61 
   62 #endif