/source/SimpleHeap.h

https://github.com/infogulch/AutoHotkey_L · C Header · 62 lines · 22 code · 6 blank · 34 comment · 0 complexity · b2df1b33ea351b871f4c91cc93dcb0f9 MD5 · raw file

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