ers.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. /*****************************************************************************\
  2. * Copyright (c) Athena 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 behaviours. *
  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 fragmentaion - program will run better for longer. *
  24. * *
  25. * <H2>Disavantages:</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. * @see common#ers.h *
  40. \*****************************************************************************/
  41. #include <stdlib.h>
  42. #include "../common/cbasetypes.h"
  43. #include "../common/malloc.h" // CREATE, RECREATE, aMalloc, aFree
  44. #include "../common/showmsg.h" // ShowMessage, ShowError, ShowFatalError, CL_BOLD, CL_NORMAL
  45. #include "ers.h"
  46. #ifndef DISABLE_ERS
  47. /*****************************************************************************\
  48. * (1) Private defines, structures and global variables. *
  49. * ERS_BLOCK_ENTRIES - Number of entries in each block. *
  50. * ERS_ROOT_SIZE - Maximum number of root entry managers. *
  51. * ERLinkedList - Structure of a linked list of reusable entries. *
  52. * ERS_impl - Class of an entry manager. *
  53. * ers_root - Array of root entry managers. *
  54. * ers_num - Number of root entry managers in the array. *
  55. \*****************************************************************************/
  56. /**
  57. * Number of entries in each block.
  58. * @see #ers_obj_alloc_entry(ERS eri)
  59. */
  60. #define ERS_BLOCK_ENTRIES 4096
  61. /**
  62. * Maximum number of root entry managers.
  63. * @private
  64. * @see #ers_root
  65. * @see #ers_num
  66. */
  67. #define ERS_ROOT_SIZE 256
  68. /**
  69. * Linked list of reusable entries.
  70. * The minimum size of the entries is the size of this structure.
  71. * @private
  72. * @see ERS_impl#reuse
  73. */
  74. typedef struct ers_ll {
  75. struct ers_ll *next;
  76. } *ERLinkedList;
  77. /**
  78. * Class of the object that manages entries of a certain size.
  79. * @param eri Public interface of the object
  80. * @param reuse Linked list of reusable data entries
  81. * @param blocks Array with blocks of entries
  82. * @param free Number of unused entries in the last block
  83. * @param num Number of blocks in the array
  84. * @param max Current maximum capacity of the array
  85. * @param destroy Destroy lock
  86. * @param size Size of the entries of the manager
  87. * @private
  88. */
  89. typedef struct ers_impl {
  90. /**
  91. * Public interface of the entry manager.
  92. * @param alloc Allocate an entry from this manager
  93. * @param free Free an entry allocated from this manager
  94. * @param entry_size Return the size of the entries of this manager
  95. * @param destroy Destroy this instance of the manager
  96. * @public
  97. */
  98. struct eri vtable;
  99. /**
  100. * Linked list of reusable entries.
  101. */
  102. ERLinkedList reuse;
  103. /**
  104. * Array with blocks of entries.
  105. */
  106. uint8 **blocks;
  107. /**
  108. * Number of unused entries in the last block.
  109. */
  110. uint32 free;
  111. /**
  112. * Number of blocks in the array.
  113. */
  114. uint32 num;
  115. /**
  116. * Current maximum capacity of the array.
  117. */
  118. uint32 max;
  119. /**
  120. * Destroy lock.
  121. */
  122. uint32 destroy;
  123. /**
  124. * Size of the entries of the manager.
  125. */
  126. size_t size;
  127. } *ERS_impl;
  128. /**
  129. * Root array with entry managers.
  130. * @private
  131. * @static
  132. * @see #ERS_ROOT_SIZE
  133. * @see #ers_num
  134. */
  135. static ERS_impl ers_root[ERS_ROOT_SIZE];
  136. /**
  137. * Number of entry managers in the root array.
  138. * @private
  139. * @static
  140. * @see #ERS_ROOT_SIZE
  141. * @see #ers_root
  142. */
  143. static uint32 ers_num = 0;
  144. /*****************************************************************************\
  145. * (2) Object functions. *
  146. * ers_obj_alloc_entry - Allocate an entry from the manager. *
  147. * ers_obj_free_entry - Free an entry allocated from the manager. *
  148. * ers_obj_entry_size - Return the size of the entries of the manager. *
  149. * ers_obj_destroy - Destroy the instance of the manager. *
  150. \*****************************************************************************/
  151. /**
  152. * Allocate an entry from this entry manager.
  153. * If there are reusable entries available, it reuses one instead.
  154. * @param self Interface of the entry manager
  155. * @return An entry
  156. * @see #ERS_BLOCK_ENTRIES
  157. * @see #ERLinkedList
  158. * @see ERS_impl::vtable#alloc
  159. */
  160. static void *ers_obj_alloc_entry(ERS self)
  161. {
  162. ERS_impl obj = (ERS_impl)self;
  163. void *ret;
  164. if (obj == NULL) {
  165. ShowError("ers::alloc : NULL object, aborting entry allocation.\n");
  166. return NULL;
  167. }
  168. if (obj->reuse) { // Reusable entry
  169. ret = obj->reuse;
  170. obj->reuse = obj->reuse->next;
  171. } else if (obj->free) { // Unused entry
  172. obj->free--;
  173. ret = &obj->blocks[obj->num -1][obj->free*obj->size];
  174. } else { // allocate a new block
  175. if (obj->num == obj->max) { // expand the block array
  176. if (obj->max == UINT32_MAX) { // No more space for blocks
  177. ShowFatalError("ers::alloc : maximum number of blocks reached, increase ERS_BLOCK_ENTRIES.\n"
  178. "exiting the program...\n");
  179. exit(EXIT_FAILURE);
  180. }
  181. obj->max = (obj->max*4)+3; // left shift bits '11' - overflow won't happen
  182. RECREATE(obj->blocks, uint8 *, obj->max);
  183. }
  184. CREATE(obj->blocks[obj->num], uint8, obj->size*ERS_BLOCK_ENTRIES);
  185. obj->free = ERS_BLOCK_ENTRIES -1;
  186. ret = &obj->blocks[obj->num][obj->free*obj->size];
  187. obj->num++;
  188. }
  189. return ret;
  190. }
  191. /**
  192. * Free an entry allocated from this manager.
  193. * WARNING: Does not check if the entry was allocated by this manager.
  194. * Freeing such an entry can lead to unexpected behaviour.
  195. * @param self Interface of the entry manager
  196. * @param entry Entry to be freed
  197. * @see #ERLinkedList
  198. * @see ERS_impl#reuse
  199. * @see ERS_impl::vtable#free
  200. */
  201. static void ers_obj_free_entry(ERS self, void *entry)
  202. {
  203. ERS_impl obj = (ERS_impl)self;
  204. ERLinkedList reuse;
  205. if (obj == NULL) {
  206. ShowError("ers::free : NULL object, aborting entry freeing.\n");
  207. return;
  208. } else if (entry == NULL) {
  209. ShowError("ers::free : NULL entry, nothing to free.\n");
  210. return;
  211. }
  212. reuse = (ERLinkedList)entry;
  213. reuse->next = obj->reuse;
  214. obj->reuse = reuse;
  215. }
  216. /**
  217. * Return the size of the entries allocated from this manager.
  218. * @param self Interface of the entry manager
  219. * @return Size of the entries of this manager in bytes
  220. * @see ERS_impl#size
  221. * @see ERS_impl::vtable#entry_size
  222. */
  223. static size_t ers_obj_entry_size(ERS self)
  224. {
  225. ERS_impl obj = (ERS_impl)self;
  226. if (obj == NULL) {
  227. ShowError("ers::entry_size : NULL object, returning 0.\n");
  228. return 0;
  229. }
  230. return obj->size;
  231. }
  232. /**
  233. * Destroy this instance of the manager.
  234. * The manager is actually only destroyed when all the instances are destroyed.
  235. * When destroying the manager a warning is shown if the manager has
  236. * missing/extra entries.
  237. * @param self Interface of the entry manager
  238. * @see #ERLinkedList
  239. * @see ERS_impl::vtable#destroy
  240. */
  241. static void ers_obj_destroy(ERS self)
  242. {
  243. ERS_impl obj = (ERS_impl)self;
  244. ERLinkedList reuse,old;
  245. uint32 i;
  246. uint32 count;
  247. if (obj == NULL) {
  248. ShowError("ers::destroy: NULL object, aborting instance destruction.\n");
  249. return;
  250. }
  251. obj->destroy--;
  252. if (obj->destroy)
  253. return; // Not last instance
  254. // Remove manager from root array
  255. for (i = 0; i < ers_num; i++) {
  256. if (ers_root[i] == obj) {
  257. ers_num--;
  258. if (i < ers_num) // put the last manager in the free slot
  259. ers_root[i] = ers_root[ers_num];
  260. break;
  261. }
  262. }
  263. reuse = obj->reuse;
  264. count = 0;
  265. // Check for missing/extra entries
  266. for (i = 0; i < obj->num; i++) {
  267. if (i == 0) {
  268. count = ERS_BLOCK_ENTRIES -obj->free;
  269. } else if (count > UINT32_MAX -ERS_BLOCK_ENTRIES) {
  270. count = UINT32_MAX;
  271. break;
  272. } else {
  273. count += ERS_BLOCK_ENTRIES;
  274. }
  275. while (reuse && count) {
  276. count--;
  277. old = reuse;
  278. reuse = reuse->next;
  279. old->next = NULL; // this makes duplicate frees report as missing entries
  280. }
  281. }
  282. if (count) { // missing entries
  283. ShowWarning("ers::destroy : %u entries missing (possible double free), continuing destruction (entry size=%u).\n",
  284. count, obj->size);
  285. } else if (reuse) { // extra entries
  286. while (reuse && count != UINT32_MAX) {
  287. count++;
  288. reuse = reuse->next;
  289. }
  290. ShowWarning("ers::destroy : %u extra entries found, continuing destruction (entry size=%u).\n",
  291. count, obj->size);
  292. }
  293. // destroy the entry manager
  294. if (obj->max) {
  295. for (i = 0; i < obj->num; i++)
  296. aFree(obj->blocks[i]); // release block of entries
  297. aFree(obj->blocks); // release array of blocks
  298. }
  299. aFree(obj); // release manager
  300. }
  301. /*****************************************************************************\
  302. * (3) Public functions. *
  303. * ers_new - Get a new instance of an entry manager. *
  304. * ers_report - Print a report about the current state. *
  305. * ers_force_destroy_all - Force the destruction of all the managers. *
  306. \*****************************************************************************/
  307. /**
  308. * Get a new instance of the manager that handles the specified entry size.
  309. * Size has to greater than 0.
  310. * If the specified size is smaller than a pointer, the size of a pointer is
  311. * used instead.
  312. * It's also aligned to ERS_ALIGNED bytes, so the smallest multiple of
  313. * ERS_ALIGNED that is greater or equal to size is what's actually used.
  314. * @param The requested size of the entry in bytes
  315. * @return Interface of the object
  316. * @see #ERS_impl
  317. * @see #ers_root
  318. * @see #ers_num
  319. */
  320. ERS ers_new(uint32 size)
  321. {
  322. ERS_impl obj;
  323. uint32 i;
  324. if (size == 0) {
  325. ShowError("ers_new: invalid size %u, aborting instance creation.\n",
  326. size);
  327. return NULL;
  328. }
  329. if (size < sizeof(struct ers_ll)) // Minimum size
  330. size = sizeof(struct ers_ll);
  331. if (size%ERS_ALIGNED) // Align size
  332. size += ERS_ALIGNED -size%ERS_ALIGNED;
  333. for (i = 0; i < ers_num; i++) {
  334. obj = ers_root[i];
  335. if (obj->size == size) {
  336. // found a manager that handles the entry size
  337. obj->destroy++;
  338. return &obj->vtable;
  339. }
  340. }
  341. // create a new manager to handle the entry size
  342. if (ers_num == ERS_ROOT_SIZE) {
  343. ShowFatalError("ers_alloc: too many root objects, increase ERS_ROOT_SIZE.\n"
  344. "exiting the program...\n");
  345. exit(EXIT_FAILURE);
  346. }
  347. obj = (ERS_impl)aMalloc(sizeof(struct ers_impl));
  348. // Public interface
  349. obj->vtable.alloc = ers_obj_alloc_entry;
  350. obj->vtable.free = ers_obj_free_entry;
  351. obj->vtable.entry_size = ers_obj_entry_size;
  352. obj->vtable.destroy = ers_obj_destroy;
  353. // Block reusage system
  354. obj->reuse = NULL;
  355. obj->blocks = NULL;
  356. obj->free = 0;
  357. obj->num = 0;
  358. obj->max = 0;
  359. obj->destroy = 1;
  360. // Properties
  361. obj->size = size;
  362. ers_root[ers_num++] = obj;
  363. return &obj->vtable;
  364. }
  365. /**
  366. * Print a report about the current state of the Entry Reusage System.
  367. * Shows information about the global system and each entry manager.
  368. * The number of entries are checked and a warning is shown if extra reusable
  369. * entries are found.
  370. * The extra entries are included in the count of reusable entries.
  371. * @see #ERLinkedList
  372. * @see #ERS_impl
  373. * @see #ers_root
  374. * @see #ers_num
  375. */
  376. void ers_report(void)
  377. {
  378. uint32 i;
  379. uint32 j;
  380. uint32 used;
  381. uint32 reusable;
  382. uint32 extra;
  383. ERLinkedList reuse;
  384. ERS_impl obj;
  385. // Root system report
  386. ShowMessage(CL_BOLD"Entry Reusage System report:\n"CL_NORMAL);
  387. ShowMessage("root array size : %u\n", ERS_ROOT_SIZE);
  388. ShowMessage("root entry managers : %u\n", ers_num);
  389. ShowMessage("entries per block : %u\n", ERS_BLOCK_ENTRIES);
  390. for (i = 0; i < ers_num; i++) {
  391. obj = ers_root[i];
  392. reuse = obj->reuse;
  393. used = 0;
  394. reusable = 0;
  395. // Count used and reusable entries
  396. for (j = 0; j < obj->num; j++) {
  397. if (j == 0) { // take into acount the free entries
  398. used = ERS_BLOCK_ENTRIES -obj->free;
  399. } else if (reuse) { // counting reusable entries
  400. used = ERS_BLOCK_ENTRIES;
  401. } else { // no more reusable entries, count remaining used entries
  402. for (; j < obj->num; j++) {
  403. if (used > UINT32_MAX -ERS_BLOCK_ENTRIES) { // overflow
  404. used = UINT32_MAX;
  405. break;
  406. }
  407. used += ERS_BLOCK_ENTRIES;
  408. }
  409. break;
  410. }
  411. while (used && reuse) { // count reusable entries
  412. used--;
  413. if (reusable != UINT32_MAX)
  414. reusable++;
  415. reuse = reuse->next;
  416. }
  417. }
  418. // Count extra reusable entries
  419. extra = 0;
  420. while (reuse && extra != UINT32_MAX) {
  421. extra++;
  422. reuse = reuse->next;
  423. }
  424. // Entry manager report
  425. ShowMessage(CL_BOLD"[Entry manager #%u report]\n"CL_NORMAL, i);
  426. ShowMessage("\tinstances : %u\n", obj->destroy);
  427. ShowMessage("\tentry size : %u\n", obj->size);
  428. ShowMessage("\tblock array size : %u\n", obj->max);
  429. ShowMessage("\tallocated blocks : %u\n", obj->num);
  430. ShowMessage("\tentries being used : %u\n", used);
  431. ShowMessage("\tunused entries : %u\n", obj->free);
  432. ShowMessage("\treusable entries : %u\n", reusable);
  433. if (extra)
  434. ShowMessage("\tWARNING - %u extra reusable entries were found.\n", extra);
  435. }
  436. ShowMessage("End of report\n");
  437. }
  438. /**
  439. * Forcibly destroy all the entry managers, checking for nothing.
  440. * The system is left as if no instances or entries had ever been allocated.
  441. * All previous entries and instances of the managers become invalid.
  442. * The use of this is NOT recommended.
  443. * It should only be used in extreme situations to make shure all the memory
  444. * allocated by this system is released.
  445. * @see #ERS_impl
  446. * @see #ers_root
  447. * @see #ers_num
  448. */
  449. void ers_force_destroy_all(void)
  450. {
  451. uint32 i;
  452. uint32 j;
  453. ERS_impl obj;
  454. for (i = 0; i < ers_num; i++) {
  455. obj = ers_root[i];
  456. if (obj->max) {
  457. for (j = 0; j < obj->num; j++)
  458. aFree(obj->blocks[j]); // block of entries
  459. aFree(obj->blocks); // array of blocks
  460. }
  461. aFree(obj); // entry manager object
  462. }
  463. ers_num = 0;
  464. }
  465. #endif /* not DISABLE_ERS */