mem.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /* Memory handling, malloc wrapers, etc */
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "mem.h"
  5. /* Calls exit(EXIT_FAILURE) if ptr is NULL. Is message is not null, prints this on stderr. */
  6. void * exit_if_null(void * ptr, char * message) {
  7. if(!ptr) {
  8. if(message) {
  9. fprintf(stderr, "%s\n", message);
  10. }
  11. exit(EXIT_FAILURE);
  12. }
  13. return ptr;
  14. }
  15. #define EXIT_ON_NOMEMORY(PTR) exit_if_null(PTR, "Out of memory!")
  16. /** Wrapper for calloc/malloc */
  17. void * mem_calloc(size_t amount, size_t size) {
  18. void * ptr = calloc(amount, size);
  19. return EXIT_ON_NOMEMORY(ptr);
  20. }
  21. /** Wrapper for calloc/malloc */
  22. void * mem_alloc(size_t size) {
  23. void * ptr = mem_calloc(1, size);
  24. return EXIT_ON_NOMEMORY(ptr);
  25. }
  26. /** Wrapper for free */
  27. void * mem_free(void * ptr) {
  28. free(ptr);
  29. return NULL;
  30. }
  31. /** Wrapper for realloc */
  32. void * mem_realloc(void *ptr, size_t newsize) {
  33. void * aid = realloc(ptr, newsize);
  34. return EXIT_ON_NOMEMORY(aid);
  35. }
  36. /** Resizes memory, taking care not to lose ptr* if the reallocation
  37. fails. */
  38. void * mem_resize(void ** ptr, size_t newsize) {
  39. if(*ptr) {
  40. void * res = mem_realloc((*ptr), newsize);
  41. if(res) {
  42. (*ptr) = res;
  43. return res;
  44. }
  45. }
  46. return NULL;
  47. }
  48. /** Wrapper for memmove, for consistency */
  49. void * mem_move(void * dest, void * src, size_t size) {
  50. return memmove(dest , src, size);
  51. }
  52. /* Thoughts on hierarchical data structures.
  53. * Often, in C data is allocatedin a hierarchical way.
  54. * It's useful to have functions that take this into consideration...
  55. */
  56. typedef struct Memtree_ Memtree;
  57. struct Memtree_ {
  58. void * memory;
  59. size_t size;
  60. Memtree * next, * parent, * child;
  61. };
  62. #ifdef COMMENT_
  63. /* Tree memory allocator. */
  64. void * mem_tree_alloc(void * vparent, size_t size) {
  65. char * aid;
  66. Memtree * me, * next;
  67. Memtree * parent = vparent;
  68. aid = mem_calloc(1, size + sizeof(Memtree));
  69. if (!aid) return NULL;
  70. me = (Memtree *) aid;
  71. me->memory = aid + sizeof(Memtree);
  72. me->size = size;
  73. if (parent) {
  74. me->parent = parent;
  75. if (parent->child) {
  76. next = parent->child;
  77. } else {
  78. next = NULL
  79. }
  80. parent->child = me;
  81. me->next = next;
  82. } else {
  83. me->parent = NULL;
  84. me->next = NULL;
  85. me->child = NULL;
  86. }
  87. return me->memory;
  88. }
  89. void mem_tree_free(void * vparent) {
  90. char * aid;
  91. Memtree * me, * next;
  92. Memtree * parent = vparent;
  93. aid = vparent;
  94. if (!aid) return;
  95. me = (Memtree *) (aid + sizeof(Memtree));
  96. if (!me) return;
  97. me->memory = NULL;
  98. me->size = 0;
  99. if (me->child) {
  100. mem_tree_free(me->child);
  101. } else if (me->next) {
  102. for (next = me->next; next; next = next->next) {
  103. mem_tree_free(next);
  104. }
  105. } else {
  106. next = NULL
  107. }
  108. mem_free(me);
  109. }
  110. #endif
  111. /*
  112. * Thoughts on memory allocation in C.
  113. *
  114. * In theory, memory allocations could just use malloc an free and
  115. * that would be enough, however, in practise there are several factors that
  116. * mean that just calling free() on any pointer won't free all allocated
  117. * memory and resources associated to it. In general, when a pointer to
  118. * a resource needs to be cleaned up, there are 3 concens to take care of:
  119. *
  120. * 1) Cleaning up of non-memory resources like OS handles, API haandles,
  121. * FILEs, etc. that must be closed or cleaned up before the pointer/struct
  122. * that refers to them may be deallocated. In general, this could be
  123. * called deinitialization.
  124. *
  125. * 2) The second problem is that the pointer may point to a complex data
  126. * structure that may have been allocated my several mallocs. In this case
  127. * multiple frees are needed. This could be called deconstruction.
  128. *
  129. * 3) Finally the pointer will usually point to allocated memory that must
  130. * be deallocated. For this, free could suffice, but not in all cases.
  131. * This could be deallocation.
  132. *
  133. * These 3 steps are of course mirrored when the resource is created, as can
  134. * be seen in the diagram below.
  135. *
  136. * step state of resource
  137. *
  138. * Before creation: : VOID
  139. *
  140. * /- allocation : ALLOCATED
  141. * Creation {-- construction : CONSTRUCTED
  142. * \- initialization : INITIALIZED
  143. *
  144. * Use of the resource....
  145. *
  146. * /- deinitialization : CONSTRUCTED
  147. * Destruction {-- deconstruction : ALLLOCATED
  148. * \- deallocation : VOID
  149. *
  150. * It should be noticed that with proper care an object that has not been
  151. * intialized, or that merely has been deinitialized (i.e. in the CONSTRUCTED
  152. * state) could be used or reused simply by initializing or reinitializing it.
  153. * It could be useful for performance or practical reasons or to enable such
  154. * recycling of already allocated and constructed resources.
  155. *
  156. * On the other hand a resource that has been deallocated or not allocated
  157. * cannnot be used correctly at all. What then about a resource that has not
  158. * been constructed yet or one that has already been deconstructed? The barrier
  159. * is more fluid and uncrear here. However, in many progaming languages, these
  160. * 3 steps get simplified ito 2 or even 1 step. However, experience shows that
  161. * having only one step is too gross, especially in the face of reference
  162. * counting or on-GC deinitialization. Therefore, it's best to have 2
  163. * steps on construction and 2 steps on deconstruction namely
  164. *
  165. * construction by XXX_new : XXX_build XXX_init.
  166. * destruction by XXX_free : XXX_done XXX_demolish
  167. *
  168. * The rule is that XXX_done and XXX_init should be complementary,
  169. * any resource RES that has XXX_init(XXX_done(RES), params...) called
  170. * will be usable again. Furthemore XXX_done should be idempotent,
  171. * that is XXX_done(XXX_done(RES)), etc is allowed.
  172. * XXX_init need not be idempotent as long as it can undo XXX_done correctly.
  173. * XXX_free is guaranteed to call XXX_done and XXX_demolish.
  174. * XXX_demolish must not neccesarily be exposed on the public API or
  175. * even exist.
  176. * XXX_new calls XXX_build and XXX_init in turn.
  177. *
  178. *
  179. * The problem remains then : how to know which destructor to call?
  180. * In case it's known at compile-time which other destructors will be needed
  181. * it's simple. However, some containers may want to store objects whose type
  182. * is only known at run time. For them it is neccesary to either not accept
  183. * ownership and let some other routine deallocate them) which is
  184. * bothersome, certainy for, say, hash tables, or to properly devise a system
  185. * where the correct destructors can be passed at run time.
  186. *
  187. * And then we arrive at interfaces that use and/or consist of virtual tables,
  188. * that is tablew with function pointers. For the destructor a single pointer
  189. * could suffice, but while we're going virtual, allowing interface/class like
  190. * tables will be better since it alllows more generality and saves space.
  191. *
  192. *
  193. * Another point is what to do if memory allocation fails. While for a critical
  194. * application, it woule make sense to try to free ups some space and try
  195. * again, for non-critical ones like Eruta, the best thing to do is to
  196. * simply end the program, since there's nothing sensible that could be done,
  197. * apart from trying to restore the sceen to it's previous resolution if possible.
  198. *
  199. *
  200. */
  201. /** Thoughts on memory management and garbage collection.
  202. * The probelm in C is that it is not always clear who is the owner
  203. * for an object, and and who is just using a reference.
  204. * If ownership is well defined, then correct cleanup is easier to arrange.
  205. *
  206. * Here's an idea on reference linking for shared ownership:
  207. */
  208. typedef struct Memref_ Memref;
  209. struct Memref_ {
  210. void * pointer;
  211. Memref * next;
  212. Memref * previous;
  213. };
  214. Memref memref(void * pointer) {
  215. Memref ref = { pointer, 0, 0 };
  216. return ref;
  217. }