wm_mem.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*****************************************************************************
  2. *
  3. * File Name : wm_mem.h
  4. *
  5. * Description: memory manager Module
  6. *
  7. * Copyright (c) 2014 Winner Micro Electronic Design Co., Ltd.
  8. * All rights reserved.
  9. *
  10. * Author : dave
  11. *
  12. * Date : 2014-6-12
  13. *****************************************************************************/
  14. #ifndef WM_MEM_H
  15. #define WM_MEM_H
  16. #include "csi_config.h"
  17. #include "wm_type_def.h"
  18. #if 1//for doxygen
  19. //#ifdef CONFIG_KERNEL_FREERTOS
  20. //#define WM_MEM_DEBUG 1
  21. #if WM_MEM_DEBUG
  22. #include "../list.h"
  23. #define MEM_HEADER_PATTERN 0x76028412
  24. #define MEM_TAILER_PATTERN 0x83395627
  25. #define MEM_FREED_PATTERN 0x82962503
  26. extern u32 alloc_heap_mem_bytes;
  27. extern u32 alloc_heap_mem_blk_cnt;
  28. extern u32 alloc_heap_mem_max_size;
  29. //
  30. // Note: it's important that the size of MP_MEMORY_BLOCK structure
  31. // be multiple of 16 bytes.
  32. //
  33. typedef struct _MEMORY_BLOCK {
  34. struct dl_list list; /**< Pointer to next and previous blocks */
  35. char *file; /**< name of the file which is doing the allocation */
  36. u32 pad; /**< pad to make the size of whole structure multiple of 16 bytes */
  37. u32 line; /**< line number where allocated */
  38. u32 length; /**< ulong index of trailer (=(length/4)-1 relative to data start */
  39. u32 header_pattern; /**< To help detect underflows. A trailer is also added to find overflows */
  40. } MEMORY_BLOCK, *PMEMORY_BLOCK;
  41. typedef struct _MEMORY_PATTERN{
  42. u32 pattern0;
  43. //u32 pattern1;
  44. //u32 pattern2;
  45. //u32 pattern3;
  46. }MEMORY_PATTERN, *PMEMORY_PATTERN;
  47. void mem_free_debug(void *p, char* file, int line);
  48. #define tls_mem_free(p) mem_free_debug( p, __FILE__, __LINE__)
  49. void *mem_alloc_debug(u32 size, char* file, int line);
  50. void mem_free_allocated_blocks(void);
  51. #define tls_mem_alloc(size) mem_alloc_debug(size, __FILE__, __LINE__)
  52. void * mem_realloc_debug(void *mem_address, u32 size, char* file, int line);
  53. #define tls_mem_realloc(mem_address, size) mem_realloc_debug(mem_address, size, __FILE__, __LINE__)
  54. void *mem_calloc_debug(u32 n,u32 size,char * file,int line);
  55. #define tls_mem_calloc(n, size) mem_calloc_debug(n, size, __FILE__, __LINE__)
  56. void tls_mem_alloc_info(void);
  57. int is_safe_addr_debug(void* p, u32 len, char* file, int line);
  58. #define tls_is_safe_addr(p, len) is_safe_addr_debug(p, len, __FILE__, __LINE__)
  59. #if 1
  60. #define MEMCPY memcpy
  61. #define SMEMCPY MEMCPY
  62. #else
  63. #define MEMCPY(dst,src,len) do { \
  64. if(tls_is_safe_addr(dst, len)){ \
  65. memcpy(dst,src,len);}}while(0)
  66. #define SMEMCPY(dst,src,len) do { \
  67. if(tls_is_safe_addr(dst, len)){ \
  68. memcpy(dst,src,len);}}while(0)
  69. #endif
  70. #else /* WM_MEM_DEBUG */
  71. void * mem_alloc_debug(u32 size);
  72. void mem_free_debug(void *p);
  73. void * mem_realloc_debug(void *mem_address, u32 size);
  74. void *mem_calloc_debug(u32 length, u32 size);
  75. /**
  76. * @defgroup System_APIs System APIs
  77. * @brief System APIs
  78. */
  79. /**
  80. * @addtogroup System_APIs
  81. * @{
  82. */
  83. /**
  84. * @defgroup MEM_APIs MEM APIs
  85. * @brief memory manager APIs
  86. */
  87. /**
  88. * @addtogroup MEM_APIs
  89. * @{
  90. */
  91. /**
  92. * @brief This function is called by memory allocation
  93. *
  94. * @param[in] size
  95. *
  96. * @retval NULL malloc failed
  97. * @retval pointer pointer to the address of the allocated memory
  98. *
  99. * @note None
  100. */
  101. #define tls_mem_alloc(size) mem_alloc_debug(size)
  102. /**
  103. * @brief This function is used to free memory
  104. *
  105. * @param None
  106. *
  107. * @return None
  108. *
  109. * @note None
  110. */
  111. #define tls_mem_free mem_free_debug
  112. /**
  113. * @brief This function is used to realloc memory
  114. *
  115. * @param None
  116. *
  117. * @retval NULL realloc failed
  118. * @retval Pointer pointer to the address of the allocated memory
  119. *
  120. * @note None
  121. */
  122. #define tls_mem_realloc mem_realloc_debug
  123. /**
  124. * @brief This function is used to calloc memory
  125. *
  126. * @param None
  127. *
  128. * @retval NULL realloc failed
  129. * @retval Pointer pointer to the address of the allocated memory
  130. *
  131. * @note None
  132. */
  133. #define tls_mem_calloc mem_calloc_debug
  134. /**
  135. * @brief This function is used to copy memory content from one address to another address
  136. *
  137. * @param[in] dst pointer to destination address
  138. * @param[in] src pointer to source address
  139. * @param[in] len length to copy
  140. *
  141. * @retval dst
  142. *
  143. * @note None
  144. */
  145. #define MEMCPY(dst,src,len) memcpy(dst,src,len)
  146. //#define MEMCPY(dst,src,len) do{extern void delay_cnt(int count); delay_cnt(10); memcpy(dst,src,len); delay_cnt(100); }while(0)
  147. /**
  148. * @brief This function is used to copy memory content from one address to another address
  149. *
  150. * @param[in] dst pointer to destination address
  151. * @param[in] src pointer to source address
  152. * @param[in] len length to copy
  153. *
  154. * @retval dst
  155. *
  156. * @note None
  157. */
  158. #define SMEMCPY(dst,src,len) memcpy(dst,src,len)
  159. /**
  160. * @}
  161. */
  162. /**
  163. * @}
  164. */
  165. #endif /* WM_MEM_DEBUG */
  166. #else /* CONFIG_KERNEL_FREERTOS */
  167. #define tls_mem_alloc(size) malloc(size)
  168. #define tls_mem_free free
  169. #define tls_mem_realloc realloc
  170. #define tls_mem_calloc calloc
  171. #define MEMCPY(dst,src,len) memcpy(dst,src,len)
  172. #define SMEMCPY(dst,src,len) memcpy(dst,src,len)
  173. #endif /* CONFIG_KERNEL_FREERTOS */
  174. #endif /* TLS_MEM_H */