res_mempool.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. #include <os_common_api.h>
  2. #include <mem_manager.h>
  3. #include <ui_mem.h>
  4. #include <string.h>
  5. #include "res_manager_api.h"
  6. #include "res_mempool.h"
  7. #define MAX_INNER_BITMAP_SIZE 64
  8. #define MAX_ARRAY_INNER_SIZE 32*1024
  9. #define MAX_ARRAY_INNER_BLOCK 32
  10. #define MAX_PRELOAD_BITMAP_SIZE 32
  11. #define MAX_ARRAY_PRELOAD_SIZE 16*1024
  12. #define MAX_ARRAY_PRELOAD_BLOCK 16
  13. #define MAX_COMPACT_BITMAP_SIZE 16
  14. #define MAX_ARRAY_COMPACT_SIZE 8*1024
  15. #define MAX_ARRAY_COMPACT_BLOCK 8
  16. #define MAX_ARRAY_BUFFER_BLOCK (MAX_ARRAY_INNER_BLOCK+MAX_ARRAY_PRELOAD_BLOCK+MAX_ARRAY_COMPACT_BLOCK)
  17. #define MAX_ARRAY_BUFFER_SIZE (MAX_ARRAY_INNER_SIZE+MAX_ARRAY_PRELOAD_SIZE+MAX_ARRAY_COMPACT_SIZE)
  18. #define RES_MEM_MAX_RESOURCE_SETS 6
  19. #define RES_DEBUG_LOAD_BMP 0
  20. #define RES_PRELOAD_MAX_BLOCK_SIZE 100*1024
  21. typedef struct _mem_info
  22. {
  23. void* ptr;
  24. size_t size;
  25. struct _mem_info* next;
  26. }mem_info_t;
  27. static uint32_t res_mem_peak = 0;
  28. static uint8_t* block_mem = NULL;
  29. static uint32_t block_size = 0;
  30. static uint32_t block_num = 0;
  31. static uint32_t block_stat = 0;
  32. #ifdef RES_MEM_PEAK_STATISTIC
  33. static mem_info_t* mem_info_list = NULL;
  34. static uint32_t res_mem_total = 0;
  35. void _add_mem_info(void* ptr, size_t size)
  36. {
  37. mem_info_t* prev = NULL;
  38. mem_info_t* listp = NULL;
  39. mem_info_t* item;
  40. //item = mem_malloc(sizeof(mem_info_t));
  41. item = (mem_info_t*)res_array_alloc(RES_MEM_SIMPLE_INNER, sizeof(mem_info_t));
  42. if(item == NULL)
  43. {
  44. SYS_LOG_ERR("no ram for res mem info\n");
  45. return;
  46. }
  47. item->ptr = ptr;
  48. item->size = size;
  49. item->next = NULL;
  50. listp = mem_info_list;
  51. if(mem_info_list == NULL)
  52. {
  53. mem_info_list = item;
  54. }
  55. else
  56. {
  57. while(listp != NULL)
  58. {
  59. prev = listp;
  60. listp = listp->next;
  61. }
  62. prev->next = item;
  63. }
  64. res_mem_total += size;
  65. if(res_mem_total > res_mem_peak)
  66. {
  67. res_mem_peak = res_mem_total;
  68. }
  69. //SYS_LOG_INF("testalloc alloc == ptr 0x%x, size %d, total %d, peak %d\n", ptr, size, res_mem_total, res_mem_peak);
  70. }
  71. void _remove_mem_info(void* ptr)
  72. {
  73. mem_info_t* prev;
  74. mem_info_t* listp;
  75. mem_info_t* item;
  76. listp = mem_info_list;
  77. prev = NULL;
  78. while(listp != NULL)
  79. {
  80. if(listp->ptr == ptr)
  81. {
  82. break;
  83. }
  84. prev = listp;
  85. listp = listp->next;
  86. }
  87. if(listp == NULL)
  88. {
  89. SYS_LOG_ERR("testalloc remove mem info not found %p\n", ptr);
  90. return;
  91. }
  92. item = listp;
  93. if(prev == NULL)
  94. {
  95. mem_info_list = listp->next;
  96. res_mem_total -= item->size;
  97. //SYS_LOG_INF("testalloc free == item 0x%x, ptr 0x%x size %d, total %d, peak %d\n", item, item->ptr, item->size, res_mem_total, res_mem_peak);
  98. res_array_free(item);
  99. }
  100. else
  101. {
  102. prev->next = listp->next;
  103. res_mem_total -= item->size;
  104. //SYS_LOG_INF("testalloc free == item 0x%x, ptr 0x%x size %d, total %d, peak %d\n", item, item->ptr, item->size, res_mem_total, res_mem_peak);
  105. res_array_free(item);
  106. }
  107. }
  108. #endif
  109. void res_mem_init(void)
  110. {
  111. if(CONFIG_RES_MANAGER_BLOCK_SIZE > 0 && CONFIG_RES_MANAGER_BLOCK_NUM > 0)
  112. {
  113. block_mem = ui_mem_alloc(MEM_RES, CONFIG_RES_MANAGER_BLOCK_SIZE*CONFIG_RES_MANAGER_BLOCK_NUM, __func__);
  114. if(!block_mem)
  115. {
  116. return;
  117. }
  118. block_size = CONFIG_RES_MANAGER_BLOCK_SIZE;
  119. block_num = CONFIG_RES_MANAGER_BLOCK_NUM;
  120. block_stat = 0;
  121. }
  122. }
  123. void res_mem_deinit(void)
  124. {
  125. }
  126. uint32_t _get_simple_size(int32_t type)
  127. {
  128. switch (type) {
  129. case RES_MEM_SIMPLE_INNER:
  130. return MAX_ARRAY_INNER_SIZE;
  131. case RES_MEM_SIMPLE_PRELOAD:
  132. return MAX_ARRAY_PRELOAD_SIZE;
  133. case RES_MEM_SIMPLE_COMPACT:
  134. return MAX_ARRAY_COMPACT_SIZE;
  135. default:
  136. return 0;
  137. }
  138. }
  139. uint32_t _get_simple_bitmap_size(int32_t type)
  140. {
  141. switch (type) {
  142. case RES_MEM_SIMPLE_INNER:
  143. return MAX_INNER_BITMAP_SIZE;
  144. case RES_MEM_SIMPLE_PRELOAD:
  145. return MAX_PRELOAD_BITMAP_SIZE;
  146. case RES_MEM_SIMPLE_COMPACT:
  147. return MAX_COMPACT_BITMAP_SIZE;
  148. default:
  149. return 0;
  150. }
  151. }
  152. void* res_mem_alloc_block(size_t size, const char* func)
  153. {
  154. int i;
  155. void* ptr = NULL;
  156. size_t align = 4;
  157. if(block_num > 0 && block_size > 0 && size <= block_size)
  158. {
  159. for(i=0;i<block_num;i++)
  160. {
  161. if((block_stat & (1<<i)) == 0)
  162. {
  163. ptr = (void*)((uint32_t)block_mem + block_size*i);
  164. block_stat = block_stat | (1<<i);
  165. SYS_LOG_DBG("block alloc : %d, %p, stat 0x%x\n", i, ptr, block_stat);
  166. return ptr;
  167. }
  168. }
  169. }
  170. align = res_mem_get_align();
  171. ptr = res_mem_aligned_alloc_debug(RES_MEM_POOL_BMP, align, size, func);
  172. if(ptr == NULL)
  173. {
  174. SYS_LOG_ERR("block alloc: no res block or mem available, stat %d\n", block_stat);
  175. res_manager_dump_info();
  176. return NULL;
  177. }
  178. SYS_LOG_DBG("block alloc: alloc from heap, stat 0x%x\n", block_stat);
  179. return ptr;
  180. }
  181. void res_mem_free_block(void* ptr)
  182. {
  183. int off = -1;
  184. if(block_mem == NULL || (uint32_t)ptr < (uint32_t)block_mem || (uint32_t)ptr >= (uint32_t)(block_mem+block_size*block_num))
  185. {
  186. SYS_LOG_DBG("block free: ptr %p not in block mem, stat 0x%x \n", ptr, block_stat);
  187. res_mem_free(RES_MEM_POOL_BMP, ptr);
  188. return;
  189. }
  190. off = ((uint32_t)ptr - (uint32_t)block_mem)/block_size;
  191. if(off < 0)
  192. {
  193. SYS_LOG_DBG("block free: ptr %p invalid\n", ptr);
  194. return;
  195. }
  196. if((block_stat & (1<<off)) != 0)
  197. {
  198. block_stat = block_stat & ~(1<<off);
  199. SYS_LOG_DBG("block free: %d, %p, stat 0x%x\n", off, ptr, block_stat);
  200. }
  201. else
  202. {
  203. SYS_LOG_INF("block freed: %d, %p, stat 0x%x\n", off, ptr, block_stat);
  204. }
  205. }
  206. int res_mem_is_block(void* ptr)
  207. {
  208. if(block_mem == NULL || (uint32_t)ptr < (uint32_t)block_mem || (uint32_t)ptr >= (uint32_t)(block_mem+block_size*block_num))
  209. {
  210. return 0;
  211. }
  212. return 1;
  213. }
  214. void* res_array_alloc_debug(int32_t type, size_t size, const char *func)
  215. {
  216. return ui_mem_alloc(MEM_RES, size, func);
  217. }
  218. uint32_t res_array_free(void* ptr)
  219. {
  220. ui_mem_free(MEM_RES, ptr);
  221. os_strace_u32(SYS_TRACE_ID_RES_PRELOAD_FREE, (uint32_t)ptr);
  222. return 0;
  223. }
  224. void *res_mem_alloc_debug(uint32_t type, size_t size, const char *func)
  225. {
  226. void* ptr;
  227. ptr = ui_mem_alloc(MEM_RES, size, func);
  228. #ifdef RES_MEM_PEAK_STATISTIC
  229. if(ptr != NULL)
  230. {
  231. _add_mem_info(ptr, size);
  232. }
  233. #endif
  234. return ptr;
  235. }
  236. void res_mem_free(uint32_t type, void *ptr)
  237. {
  238. ui_mem_free(MEM_RES, ptr);
  239. #ifdef RES_MEM_PEAK_STATISTIC
  240. _remove_mem_info(ptr);
  241. #endif
  242. }
  243. void *res_mem_realloc_debug(uint32_t type, void *ptr, size_t requested_size, const char* func)
  244. {
  245. return ui_mem_realloc(MEM_RES, ptr, requested_size, func);
  246. }
  247. void * res_mem_aligned_alloc_debug(uint8_t type, size_t align, size_t size, const void* caller)
  248. {
  249. void* ptr;
  250. ptr = ui_mem_aligned_alloc(MEM_RES, align, size, caller);
  251. #ifdef RES_MEM_PEAK_STATISTIC
  252. if(ptr != NULL)
  253. {
  254. _add_mem_info(ptr, size);
  255. }
  256. #endif
  257. return ptr;
  258. }
  259. size_t res_mem_get_align(void)
  260. {
  261. #ifdef CONFIG_RES_MANAGER_ALIGN
  262. return CONFIG_RES_MANAGER_ALIGN;
  263. #else
  264. return 4;
  265. #endif
  266. }
  267. void res_mem_dump(void)
  268. {
  269. ui_mem_dump(MEM_RES);
  270. }
  271. int res_is_auto_search_files(void)
  272. {
  273. #ifndef CONFIG_RES_MANAGER_DISABLE_AUTO_SEARCH_FILES
  274. return 1;
  275. #else
  276. return 0;
  277. #endif
  278. }
  279. int res_debug_load_bitmap_is_on(void)
  280. {
  281. #if RES_DEBUG_LOAD_BMP == 1
  282. return 1;
  283. #else
  284. return 0;
  285. #endif
  286. }
  287. int res_mem_get_max_resource_sets(void)
  288. {
  289. return RES_MEM_MAX_RESOURCE_SETS;
  290. }
  291. uint32_t res_mem_get_max_compact_block_size(void)
  292. {
  293. return RES_PRELOAD_MAX_BLOCK_SIZE;
  294. }
  295. uint32_t res_mem_get_mem_peak(void)
  296. {
  297. return res_mem_peak;
  298. }