* @brief This function will initialize a memory pool object, normally which is used * for static object. * * @param mp is the memory pool object. * * @param name is the name of the memory pool. * * @param start is the start address of the memory pool. * * @param size is the total size of the memory pool. * * @param block_size is the size for each block.. * * @return RT_E
| 82 | * @return RT_EOK |
| 83 | */ |
| 84 | rt_err_t rt_mp_init(struct rt_mempool *mp, |
| 85 | const char *name, |
| 86 | void *start, |
| 87 | rt_size_t size, |
| 88 | rt_size_t block_size) |
| 89 | { |
| 90 | rt_uint8_t *block_ptr; |
| 91 | rt_size_t offset; |
| 92 | |
| 93 | /* parameter check */ |
| 94 | RT_ASSERT(mp != RT_NULL); |
| 95 | RT_ASSERT(name != RT_NULL); |
| 96 | RT_ASSERT(start != RT_NULL); |
| 97 | RT_ASSERT(size > 0 && block_size > 0); |
| 98 | |
| 99 | /* initialize object */ |
| 100 | rt_object_init(&(mp->parent), RT_Object_Class_MemPool, name); |
| 101 | |
| 102 | /* initialize memory pool */ |
| 103 | mp->start_address = start; |
| 104 | mp->size = RT_ALIGN_DOWN(size, RT_ALIGN_SIZE); |
| 105 | |
| 106 | /* align the block size */ |
| 107 | block_size = RT_ALIGN(block_size, RT_ALIGN_SIZE); |
| 108 | mp->block_size = block_size; |
| 109 | |
| 110 | /* align to align size byte */ |
| 111 | mp->block_total_count = mp->size / (mp->block_size + sizeof(rt_uint8_t *)); |
| 112 | mp->block_free_count = mp->block_total_count; |
| 113 | |
| 114 | /* initialize suspended thread list */ |
| 115 | rt_list_init(&(mp->suspend_thread)); |
| 116 | |
| 117 | /* initialize free block list */ |
| 118 | block_ptr = (rt_uint8_t *)mp->start_address; |
| 119 | for (offset = 0; offset < mp->block_total_count; offset ++) |
| 120 | { |
| 121 | *(rt_uint8_t **)(block_ptr + offset * (block_size + sizeof(rt_uint8_t *))) = |
| 122 | (rt_uint8_t *)(block_ptr + (offset + 1) * (block_size + sizeof(rt_uint8_t *))); |
| 123 | } |
| 124 | |
| 125 | *(rt_uint8_t **)(block_ptr + (offset - 1) * (block_size + sizeof(rt_uint8_t *))) = |
| 126 | RT_NULL; |
| 127 | |
| 128 | mp->block_list = block_ptr; |
| 129 | rt_spin_lock_init(&(mp->spinlock)); |
| 130 | |
| 131 | return RT_EOK; |
| 132 | } |
| 133 | RTM_EXPORT(rt_mp_init); |
| 134 | |
| 135 | /** |