* @brief This function will initialize small memory management algorithm. * * @param name is the name of the small memory management object. * * @param begin_addr the beginning address of memory. * * @param size is the size of the memory. * * @return Return a pointer to the memory object. When the return value is RT_NULL, it means the init failed. */
| 168 | * @return Return a pointer to the memory object. When the return value is RT_NULL, it means the init failed. |
| 169 | */ |
| 170 | rt_smem_t rt_smem_init(const char *name, |
| 171 | void *begin_addr, |
| 172 | rt_size_t size) |
| 173 | { |
| 174 | struct rt_small_mem_item *mem; |
| 175 | struct rt_small_mem *small_mem; |
| 176 | rt_uintptr_t start_addr, begin_align, end_align, mem_size; |
| 177 | |
| 178 | small_mem = (struct rt_small_mem *)RT_ALIGN((rt_uintptr_t)begin_addr, RT_ALIGN_SIZE); |
| 179 | start_addr = (rt_uintptr_t)small_mem + sizeof(*small_mem); |
| 180 | begin_align = RT_ALIGN((rt_uintptr_t)start_addr, RT_ALIGN_SIZE); |
| 181 | end_align = RT_ALIGN_DOWN((rt_uintptr_t)begin_addr + size, RT_ALIGN_SIZE); |
| 182 | |
| 183 | /* alignment addr */ |
| 184 | if ((end_align > (2 * SIZEOF_STRUCT_MEM)) && |
| 185 | ((end_align - 2 * SIZEOF_STRUCT_MEM) >= start_addr)) |
| 186 | { |
| 187 | /* calculate the aligned memory size */ |
| 188 | mem_size = end_align - begin_align - 2 * SIZEOF_STRUCT_MEM; |
| 189 | } |
| 190 | else |
| 191 | { |
| 192 | rt_kprintf("mem init, error begin address 0x%x, and end address 0x%x\n", |
| 193 | (rt_uintptr_t)begin_addr, (rt_uintptr_t)begin_addr + size); |
| 194 | |
| 195 | return RT_NULL; |
| 196 | } |
| 197 | |
| 198 | rt_memset(small_mem, 0, sizeof(*small_mem)); |
| 199 | /* initialize small memory object */ |
| 200 | rt_object_init(&(small_mem->parent.parent), RT_Object_Class_Memory, name); |
| 201 | small_mem->parent.algorithm = "small"; |
| 202 | small_mem->parent.address = begin_align; |
| 203 | small_mem->parent.total = mem_size; |
| 204 | small_mem->mem_size_aligned = mem_size; |
| 205 | |
| 206 | /* point to begin address of heap */ |
| 207 | small_mem->heap_ptr = (rt_uint8_t *)begin_align; |
| 208 | |
| 209 | LOG_D("mem init, heap begin address 0x%x, size %d", |
| 210 | (rt_uintptr_t)small_mem->heap_ptr, small_mem->mem_size_aligned); |
| 211 | |
| 212 | /* initialize the start of the heap */ |
| 213 | mem = (struct rt_small_mem_item *)small_mem->heap_ptr; |
| 214 | mem->pool_ptr = MEM_FREED(small_mem); |
| 215 | mem->next = small_mem->mem_size_aligned + SIZEOF_STRUCT_MEM; |
| 216 | mem->prev = 0; |
| 217 | #ifdef RT_USING_MEMTRACE |
| 218 | rt_smem_setname(mem, "INIT"); |
| 219 | #endif /* RT_USING_MEMTRACE */ |
| 220 | |
| 221 | /* initialize the end of the heap */ |
| 222 | small_mem->heap_end = (struct rt_small_mem_item *)&small_mem->heap_ptr[mem->next]; |
| 223 | small_mem->heap_end->pool_ptr = MEM_USED(small_mem); |
| 224 | small_mem->heap_end->next = small_mem->mem_size_aligned + SIZEOF_STRUCT_MEM; |
| 225 | small_mem->heap_end->prev = small_mem->mem_size_aligned + SIZEOF_STRUCT_MEM; |
| 226 | #ifdef RT_USING_MEMTRACE |
| 227 | rt_smem_setname(small_mem->heap_end, "INIT"); |
no test coverage detected