malloc-simple.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /*
  2. * Copyright (c) 2009 Openmoko Inc.
  3. *
  4. * Authors Marek Lindner <marek@openmoko.com>
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (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. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <stdlib.h>
  20. #include <malloc-simple.h>
  21. #include <wikilib.h>
  22. #include <msg.h>
  23. #include <string.h>
  24. #ifndef __c33 // ugly hack but for now it works...
  25. void malloc_init_simple(void) { }
  26. void malloc_status_simple(void) { }
  27. void *malloc_simple(uint32_t size, uint32_t tag)
  28. {
  29. return malloc(size);
  30. }
  31. void free_simple(void *ptr, uint32_t tag)
  32. {
  33. free(ptr);
  34. }
  35. #else // __c33
  36. extern uint8_t __START_heap;
  37. extern uint8_t __END_heap;
  38. #define MEM_SIZE (&__END_heap - &__START_heap)
  39. #define RAM_START (&__START_heap)
  40. // page size should not be smaller than struct malloc_page
  41. #define PAGE_SIZE (256)
  42. #define MEMORY_DEBUG 1
  43. #define MAGIC_NUMBER 0x12345678
  44. #define NUM_PAGES (MEM_SIZE / PAGE_SIZE)
  45. #define CTRL_PAGE 1
  46. #define GET_PAGE(curr_page, num_pages) (((uint8_t *)(curr_page)) + ((num_pages) * PAGE_SIZE))
  47. enum mem_status {
  48. MEM_UNUSED,
  49. MEM_INUSE
  50. };
  51. struct malloc_page
  52. {
  53. uint32_t magicNumber;
  54. uint32_t filler[10];
  55. struct malloc_page *next;
  56. struct malloc_page *prev;
  57. uint32_t size; // number of pages
  58. uint32_t alloc_tag;
  59. uint32_t free_tag;
  60. enum mem_status status;
  61. };
  62. struct malloc_page *first_page = (struct malloc_page *)RAM_START;
  63. static void init_page(struct malloc_page *page)
  64. {
  65. ASSERT(page);
  66. page->magicNumber = MAGIC_NUMBER;
  67. page->prev = NULL;
  68. page->next = NULL;
  69. page->size = 0;
  70. page->status = MEM_UNUSED;
  71. page->alloc_tag = MEM_TAG_UNUSED;
  72. page->free_tag = MEM_TAG_UNUSED;
  73. }
  74. static struct malloc_page *page_new(struct malloc_page *curr_page, uint32_t needed_pages)
  75. {
  76. struct malloc_page *new_page = (struct malloc_page *)GET_PAGE(curr_page, needed_pages + 1);
  77. init_page(new_page);
  78. ASSERT(curr_page);
  79. ASSERT(curr_page->magicNumber == MAGIC_NUMBER);
  80. curr_page->next = new_page;
  81. new_page->prev = curr_page;
  82. // one more for the malloc control page
  83. new_page->size = curr_page->size - needed_pages - CTRL_PAGE;
  84. curr_page->size = needed_pages;
  85. return new_page;
  86. }
  87. void malloc_init_simple(void)
  88. {
  89. memset(first_page, 0, sizeof(struct malloc_page));
  90. init_page(first_page);
  91. first_page->size = NUM_PAGES;
  92. }
  93. void *malloc_simple(uint32_t size, uint32_t tag)
  94. {
  95. struct malloc_page *curr_page, *new_page;
  96. uint32_t needed_pages = size / PAGE_SIZE;
  97. if (size % PAGE_SIZE) {
  98. needed_pages++;
  99. }
  100. for (curr_page = first_page; curr_page != NULL; curr_page = curr_page->next) {
  101. ASSERT(curr_page);
  102. ASSERT(curr_page->magicNumber == MAGIC_NUMBER);
  103. if (curr_page->status != MEM_UNUSED) {
  104. continue;
  105. }
  106. // free memory but not enough space for us due to memory fragmentation
  107. if ((curr_page->next != NULL) && (curr_page->size < needed_pages)) {
  108. continue;
  109. }
  110. // end of memory reached - out of memory
  111. if ((curr_page->next == NULL) && (curr_page->size < needed_pages + CTRL_PAGE)) {
  112. break;
  113. }
  114. // the memory lying ahead has no control structure - we need to create it
  115. if (curr_page->next == NULL) {
  116. new_page = page_new(curr_page, needed_pages);
  117. }
  118. curr_page->alloc_tag = tag;
  119. curr_page->free_tag = MEM_TAG_UNUSED;
  120. curr_page->status = MEM_INUSE;
  121. ASSERT(curr_page);
  122. ASSERT(curr_page->magicNumber == MAGIC_NUMBER);
  123. return (void *)GET_PAGE(curr_page, 1);
  124. }
  125. // out of memory
  126. return NULL;
  127. }
  128. void free_simple(void *ptr, uint32_t tag)
  129. {
  130. struct malloc_page *my_page, *curr_page;
  131. if (!ptr) {
  132. return;
  133. }
  134. if ((uint8_t *)ptr < RAM_START + PAGE_SIZE){
  135. return;
  136. }
  137. my_page = (struct malloc_page *)GET_PAGE(ptr, -1);
  138. ASSERT(my_page);
  139. ASSERT(my_page->magicNumber == MAGIC_NUMBER);
  140. // double free
  141. if (my_page->status == MEM_UNUSED) {
  142. #if MEMORY_DEBUG
  143. msg(MSG_INFO, "MEMORY DEBUG: possible double free detected - trying to free %p again\n",
  144. ptr);
  145. msg(MSG_INFO, "MEMORY DEBUG: alloc tag: %d, old free tag: %d, curr free tag: %d)\n",
  146. my_page->alloc_tag, my_page->free_tag, tag);
  147. #endif
  148. return;
  149. }
  150. my_page->status = MEM_UNUSED;
  151. my_page->free_tag = tag;
  152. //msg(MSG_INFO, "freeing: alloc tag: %d, free tag %d\n", my_page->alloc_tag, tag);
  153. // we may be able to merge larger chunks of free space that follows the current chunk
  154. for (curr_page = my_page->next; curr_page != NULL; curr_page = curr_page->next) {
  155. ASSERT(curr_page->magicNumber == MAGIC_NUMBER);
  156. if (curr_page->status != MEM_UNUSED) {
  157. break;
  158. }
  159. my_page->size += curr_page->size + CTRL_PAGE;
  160. my_page->next = curr_page->next;
  161. if (curr_page->next) {
  162. ASSERT(curr_page->next->magicNumber == MAGIC_NUMBER);
  163. curr_page->next->prev = my_page;
  164. }
  165. }
  166. // merge free space that preceeds the current chunk
  167. for (curr_page = my_page->prev; curr_page != NULL; curr_page = curr_page->prev) {
  168. ASSERT(curr_page->magicNumber == MAGIC_NUMBER);
  169. if (curr_page->status != MEM_UNUSED) {
  170. break;
  171. }
  172. curr_page->size += curr_page->next->size + CTRL_PAGE;
  173. // save the tags from the item that is being deleted now
  174. curr_page->alloc_tag = curr_page->next->alloc_tag;
  175. curr_page->free_tag = curr_page->next->free_tag;
  176. ASSERT(curr_page->next);
  177. ASSERT(curr_page->next->magicNumber == MAGIC_NUMBER);
  178. curr_page->next = curr_page->next->next;
  179. curr_page->next->prev = curr_page;
  180. }
  181. }
  182. void malloc_status_simple(void)
  183. {
  184. struct malloc_page *curr_page;
  185. uint32_t mem_inuse = 0, mem_frag = 0, malloc_pages = 0, free_mem = 0;
  186. msg(MSG_INFO, "\nmemory status:\n");
  187. for (curr_page = first_page; curr_page != NULL; curr_page = curr_page->next) {
  188. malloc_pages++;
  189. ASSERT(curr_page);
  190. ASSERT(curr_page->magicNumber == MAGIC_NUMBER);
  191. if (curr_page->status == MEM_INUSE) {
  192. mem_inuse += curr_page->size;
  193. }
  194. if ((curr_page->status == MEM_UNUSED) && (curr_page->next != NULL)) {
  195. mem_frag += curr_page->size;
  196. }
  197. free_mem = curr_page->size;
  198. #if MEMORY_DEBUG
  199. if (curr_page->magicNumber != MAGIC_NUMBER) {
  200. msg(MSG_INFO,
  201. "MEMORY DEBUG: invalid magic number (%08x - expected %08x) - alloc tag: %d, prev alloc tag: %d\n",
  202. curr_page->magicNumber, MAGIC_NUMBER,
  203. curr_page->alloc_tag,
  204. (curr_page->prev != NULL ? curr_page->prev->alloc_tag : 0));
  205. }
  206. if (curr_page->status == MEM_INUSE) {
  207. msg(MSG_INFO, "MEMORY DEBUG: used mem - alloc tag: %d, size: %d\n",
  208. curr_page->alloc_tag, curr_page->size * PAGE_SIZE);
  209. }
  210. // do not show end of memory page
  211. else if (curr_page->next != NULL) {
  212. msg(MSG_INFO, "MEMORY DEBUG: fragmented mem - alloc tag: %d, size: %d, free tag: %d\n",
  213. curr_page->alloc_tag, curr_page->size * PAGE_SIZE, curr_page->free_tag);
  214. }
  215. #endif
  216. }
  217. msg(MSG_INFO, " * memory allocated: %d\n * memory fragmented: %d\n",
  218. mem_inuse * PAGE_SIZE, mem_frag * PAGE_SIZE);
  219. msg(MSG_INFO, " * num control pages: %d\n * free memory: %d\n * total memory: %d\n",
  220. malloc_pages, free_mem * PAGE_SIZE, MEM_SIZE);
  221. }
  222. #endif