ers.hpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*****************************************************************************\
  2. * Copyright (c) rAthena Dev Teams - Licensed under GNU GPL *
  3. * For more information, see LICENCE in the main folder *
  4. * *
  5. * <H1>Entry Reusage System</H1> *
  6. * *
  7. * There are several root entry managers, each with a different entry size. *
  8. * Each manager will keep track of how many instances have been 'created'. *
  9. * They will only automatically destroy themselves after the last instance *
  10. * is destroyed. *
  11. * *
  12. * Entries can be allocated from the managers. *
  13. * If it has reusable entries (freed entry), it uses one. *
  14. * So no assumption should be made about the data of the entry. *
  15. * Entries should be freed in the manager they where allocated from. *
  16. * Failure to do so can lead to unexpected behaviors. *
  17. * *
  18. * <H2>Advantages:</H2> *
  19. * - The same manager is used for entries of the same size. *
  20. * So entries freed in one instance of the manager can be used by other *
  21. * instances of the manager. *
  22. * - Much less memory allocation/deallocation - program will be faster. *
  23. * - Avoids memory fragmentation - program will run better for longer. *
  24. * *
  25. * <H2>Disadvantages:</H2> *
  26. * - Unused entries are almost inevitable - memory being wasted. *
  27. * - A manager will only auto-destroy when all of its instances are *
  28. * destroyed so memory will usually only be recovered near the end. *
  29. * - Always wastes space for entries smaller than a pointer. *
  30. * *
  31. * WARNING: The system is not thread-safe at the moment. *
  32. * *
  33. * HISTORY: *
  34. * 0.1 - Initial version *
  35. * *
  36. * @version 0.1 - Initial version *
  37. * @author Flavio @ Amazon Project *
  38. * @encoding US-ASCII *
  39. \*****************************************************************************/
  40. #ifndef _ERS_HPP_
  41. #define _ERS_HPP_
  42. #include "cbasetypes.hpp"
  43. /*****************************************************************************\
  44. * (1) All public parts of the Entry Reusage System. *
  45. * DISABLE_ERS - Define to disable this system. *
  46. * ERS_ALIGNED - Alignment of the entries in the blocks. *
  47. * ERS - Entry manager. *
  48. * ers_new - Allocate an instance of an entry manager. *
  49. * ers_report - Print a report about the current state. *
  50. * ers_final - Clears the remainder of the managers. *
  51. \*****************************************************************************/
  52. /**
  53. * Define this to disable the Entry Reusage System.
  54. * All code except the typedef of ERInterface will be disabled.
  55. * To allow a smooth transition,
  56. */
  57. //#define DISABLE_ERS
  58. /**
  59. * Entries are aligned to ERS_ALIGNED bytes in the blocks of entries.
  60. * By default it aligns to one byte, using the "natural order" of the entries.
  61. * This should NEVER be set to zero or less.
  62. * If greater than one, some memory can be wasted. This should never be needed
  63. * but is here just in case some alignment issues arise.
  64. */
  65. #ifndef ERS_ALIGNED
  66. # define ERS_ALIGNED 1
  67. #endif /* not ERS_ALIGN_ENTRY */
  68. enum ERSOptions {
  69. ERS_OPT_NONE = 0x00,
  70. ERS_OPT_CLEAR = 0x01,/* silently clears any entries left in the manager upon destruction */
  71. ERS_OPT_WAIT = 0x02,/* wait for entries to come in order to list! */
  72. ERS_OPT_FREE_NAME = 0x04,/* name is dynamic memory, and should be freed */
  73. ERS_OPT_CLEAN = 0x08,/* clears used memory upon ers_free so that its all new to be reused on the next alloc */
  74. ERS_OPT_FLEX_CHUNK = 0x10,/* signs that it should look for its own cache given it'll have a dynamic chunk size, so that it doesn't affect the other ERS it'd otherwise be sharing */
  75. /* Compound, is used to determine whether it should be looking for a cache of matching options */
  76. ERS_CACHE_OPTIONS = ERS_OPT_CLEAN|ERS_OPT_FLEX_CHUNK,
  77. ERS_CLEAN_OPTIONS = ERS_OPT_CLEAN|ERS_OPT_CLEAR,
  78. ERS_DBN_OPTIONS = ERS_OPT_CLEAN|ERS_OPT_WAIT|ERS_OPT_FREE_NAME,
  79. };
  80. /**
  81. * Public interface of the entry manager.
  82. * @param alloc Allocate an entry from this manager
  83. * @param free Free an entry allocated from this manager
  84. * @param entry_size Return the size of the entries of this manager
  85. * @param destroy Destroy this instance of the manager
  86. */
  87. typedef struct eri {
  88. /**
  89. * Allocate an entry from this entry manager.
  90. * If there are reusable entries available, it reuses one instead.
  91. * @param self Interface of the entry manager
  92. * @return An entry
  93. */
  94. void *(*alloc)(struct eri *self);
  95. /**
  96. * Free an entry allocated from this manager.
  97. * WARNING: Does not check if the entry was allocated by this manager.
  98. * Freeing such an entry can lead to unexpected behavior.
  99. * @param self Interface of the entry manager
  100. * @param entry Entry to be freed
  101. */
  102. void (*free)(struct eri *self, void *entry);
  103. /**
  104. * Return the size of the entries allocated from this manager.
  105. * @param self Interface of the entry manager
  106. * @return Size of the entries of this manager in bytes
  107. */
  108. size_t (*entry_size)(struct eri *self);
  109. /**
  110. * Destroy this instance of the manager.
  111. * The manager is actually only destroyed when all the instances are destroyed.
  112. * When destroying the manager a warning is shown if the manager has
  113. * missing/extra entries.
  114. * @param self Interface of the entry manager
  115. */
  116. void (*destroy)(struct eri *self);
  117. /* */
  118. void (*chunk_size) (struct eri *self, unsigned int new_size);
  119. } ERS;
  120. #ifdef DISABLE_ERS
  121. // Use memory manager to allocate/free and disable other interface functions
  122. # define ers_alloc(obj,type) (type *)aMalloc(sizeof(type))
  123. # define ers_free(obj,entry) aFree(entry)
  124. # define ers_entry_size(obj) (size_t)0
  125. # define ers_destroy(obj)
  126. # define ers_chunk_size(obj,size)
  127. // Disable the public functions
  128. # define ers_new(size,name,options) NULL
  129. # define ers_report()
  130. # define ers_final()
  131. #else /* not DISABLE_ERS */
  132. // These defines should be used to allow the code to keep working whenever
  133. // the system is disabled
  134. # define ers_alloc(obj,type) ((type *)(obj)->alloc(obj))
  135. # define ers_free(obj,entry) ((obj)->free((obj),(entry)))
  136. # define ers_entry_size(obj) ((obj)->entry_size(obj))
  137. # define ers_destroy(obj) ((obj)->destroy(obj))
  138. # define ers_chunk_size(obj,size) ((obj)->chunk_size((obj),(size)))
  139. /**
  140. * Get a new instance of the manager that handles the specified entry size.
  141. * Size has to greater than 0.
  142. * If the specified size is smaller than a pointer, the size of a pointer is
  143. * used instead.
  144. * It's also aligned to ERS_ALIGNED bytes, so the smallest multiple of
  145. * ERS_ALIGNED that is greater or equal to size is what's actually used.
  146. * @param The requested size of the entry in bytes
  147. * @return Interface of the object
  148. */
  149. ERS *ers_new(uint32 size, const char *name, enum ERSOptions options);
  150. /**
  151. * Print a report about the current state of the Entry Reusage System.
  152. * Shows information about the global system and each entry manager.
  153. * The number of entries are checked and a warning is shown if extra reusable
  154. * entries are found.
  155. * The extra entries are included in the count of reusable entries.
  156. */
  157. void ers_report(void);
  158. /**
  159. * Clears the remainder of the managers
  160. **/
  161. void ers_final(void);
  162. #endif /* DISABLE_ERS / not DISABLE_ERS */
  163. #endif /* _ERS_HPP_ */