| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include <aws/io/message_pool.h> |
| 7 |
|
| 8 |
#include <aws/common/thread.h> |
| 9 |
|
| 10 |
int aws_memory_pool_init( |
| 11 |
struct aws_memory_pool *mempool, |
| 12 |
struct aws_allocator *alloc, |
| 13 |
uint16_t ideal_segment_count, |
| 14 |
size_t segment_size) { |
| 15 |
|
| 16 |
mempool->alloc = alloc; |
| 17 |
mempool->ideal_segment_count = ideal_segment_count; |
| 18 |
mempool->segment_size = segment_size; |
| 19 |
mempool->data_ptr = aws_mem_calloc(alloc, ideal_segment_count, sizeof(void *)); |
| 20 |
if (!mempool->data_ptr) { |
| 21 |
return AWS_OP_ERR; |
| 22 |
} |
| 23 |
|
| 24 |
aws_array_list_init_static(&mempool->stack, mempool->data_ptr, ideal_segment_count, sizeof(void *)); |
| 25 |
|
| 26 |
for (uint16_t i = 0; i < ideal_segment_count; ++i) { |
| 27 |
void *memory = aws_mem_acquire(alloc, segment_size); |
| 28 |
if (memory) { |
| 29 |
aws_array_list_push_back(&mempool->stack, &memory); |
| 30 |
} else { |
| 31 |
goto clean_up; |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
return AWS_OP_SUCCESS; |
| 36 |
|
| 37 |
clean_up: |
| 38 |
aws_memory_pool_clean_up(mempool); |
| 39 |
return AWS_OP_ERR; |
| 40 |
} |
| 41 |
|
| 42 |
void aws_memory_pool_clean_up(struct aws_memory_pool *mempool) { |
| 43 |
void *cur = NULL; |
| 44 |
|
| 45 |
while (aws_array_list_length(&mempool->stack) > 0) { |
| 46 |
|
| 47 |
aws_array_list_back(&mempool->stack, &cur); |
| 48 |
aws_array_list_pop_back(&mempool->stack); |
| 49 |
aws_mem_release(mempool->alloc, cur); |
| 50 |
} |
| 51 |
|
| 52 |
aws_array_list_clean_up(&mempool->stack); |
| 53 |
aws_mem_release(mempool->alloc, mempool->data_ptr); |
| 54 |
} |
| 55 |
|
| 56 |
void *aws_memory_pool_acquire(struct aws_memory_pool *mempool) { |
| 57 |
void *back = NULL; |
| 58 |
if (aws_array_list_length(&mempool->stack) > 0) { |
| 59 |
aws_array_list_back(&mempool->stack, &back); |
| 60 |
aws_array_list_pop_back(&mempool->stack); |
| 61 |
|
| 62 |
return back; |
| 63 |
} |
| 64 |
|
| 65 |
void *mem = aws_mem_acquire(mempool->alloc, mempool->segment_size); |
| 66 |
return mem; |
| 67 |
} |
| 68 |
|
| 69 |
void aws_memory_pool_release(struct aws_memory_pool *mempool, void *to_release) { |
| 70 |
size_t pool_size = aws_array_list_length(&mempool->stack); |
| 71 |
|
| 72 |
if (pool_size >= mempool->ideal_segment_count) { |
| 73 |
aws_mem_release(mempool->alloc, to_release); |
| 74 |
return; |
| 75 |
} |
| 76 |
|
| 77 |
aws_array_list_push_back(&mempool->stack, &to_release); |
| 78 |
} |
| 79 |
|
| 80 |
struct message_pool_allocator { |
| 81 |
struct aws_allocator base_allocator; |
| 82 |
struct aws_message_pool *msg_pool; |
| 83 |
}; |
| 84 |
|
| 85 |
void *s_message_pool_mem_acquire(struct aws_allocator *allocator, size_t size) { |
| 86 |
(void)allocator; |
| 87 |
(void)size; |
| 88 |
|
| 89 |
|
| 90 |
AWS_ASSERT(0); |
| 91 |
return NULL; |
| 92 |
} |
| 93 |
|
| 94 |
void s_message_pool_mem_release(struct aws_allocator *allocator, void *ptr) { |
| 95 |
struct message_pool_allocator *msg_pool_alloc = allocator->impl; |
| 96 |
|
| 97 |
aws_message_pool_release(msg_pool_alloc->msg_pool, (struct aws_io_message *)ptr); |
| 98 |
} |
| 99 |
|
| 100 |
static size_t MSG_OVERHEAD = sizeof(struct aws_io_message) + sizeof(struct message_pool_allocator); |
| 101 |
|
| 102 |
int aws_message_pool_init( |
| 103 |
struct aws_message_pool *msg_pool, |
| 104 |
struct aws_allocator *alloc, |
| 105 |
struct aws_message_pool_creation_args *args) { |
| 106 |
|
| 107 |
msg_pool->alloc = alloc; |
| 108 |
|
| 109 |
size_t msg_data_size = args->application_data_msg_data_size + MSG_OVERHEAD; |
| 110 |
|
| 111 |
if (aws_memory_pool_init( |
| 112 |
&msg_pool->application_data_pool, alloc, args->application_data_msg_count, msg_data_size)) { |
| 113 |
return AWS_OP_ERR; |
| 114 |
} |
| 115 |
|
| 116 |
size_t small_blk_data_size = args->small_block_msg_data_size + MSG_OVERHEAD; |
| 117 |
|
| 118 |
if (aws_memory_pool_init(&msg_pool->small_block_pool, alloc, args->small_block_msg_count, small_blk_data_size)) { |
| 119 |
aws_memory_pool_clean_up(&msg_pool->application_data_pool); |
| 120 |
return AWS_OP_ERR; |
| 121 |
} |
| 122 |
|
| 123 |
return AWS_OP_SUCCESS; |
| 124 |
} |
| 125 |
|
| 126 |
void aws_message_pool_clean_up(struct aws_message_pool *msg_pool) { |
| 127 |
aws_memory_pool_clean_up(&msg_pool->application_data_pool); |
| 128 |
aws_memory_pool_clean_up(&msg_pool->small_block_pool); |
| 129 |
AWS_ZERO_STRUCT(*msg_pool); |
| 130 |
} |
| 131 |
|
| 132 |
struct message_wrapper { |
| 133 |
struct aws_io_message message; |
| 134 |
struct message_pool_allocator msg_allocator; |
| 135 |
uint8_t buffer_start[1]; |
| 136 |
}; |
| 137 |
|
| 138 |
struct aws_io_message *aws_message_pool_acquire( |
| 139 |
struct aws_message_pool *msg_pool, |
| 140 |
enum aws_io_message_type message_type, |
| 141 |
size_t size_hint) { |
| 142 |
|
| 143 |
struct message_wrapper *message_wrapper = NULL; |
| 144 |
size_t max_size = 0; |
| 145 |
switch (message_type) { |
| 146 |
case AWS_IO_MESSAGE_APPLICATION_DATA: |
| 147 |
if (size_hint > msg_pool->small_block_pool.segment_size - MSG_OVERHEAD) { |
| 148 |
message_wrapper = aws_memory_pool_acquire(&msg_pool->application_data_pool); |
| 149 |
max_size = msg_pool->application_data_pool.segment_size - MSG_OVERHEAD; |
| 150 |
} else { |
| 151 |
message_wrapper = aws_memory_pool_acquire(&msg_pool->small_block_pool); |
| 152 |
max_size = msg_pool->small_block_pool.segment_size - MSG_OVERHEAD; |
| 153 |
} |
| 154 |
break; |
| 155 |
default: |
| 156 |
AWS_ASSERT(0); |
| 157 |
break; |
| 158 |
} |
| 159 |
|
| 160 |
AWS_FATAL_ASSERT(message_wrapper); |
| 161 |
|
| 162 |
message_wrapper->message.message_type = message_type; |
| 163 |
message_wrapper->message.message_tag = 0; |
| 164 |
message_wrapper->message.user_data = NULL; |
| 165 |
message_wrapper->message.copy_mark = 0; |
| 166 |
message_wrapper->message.on_completion = NULL; |
| 167 |
|
| 168 |
message_wrapper->message.message_data.buffer = message_wrapper->buffer_start; |
| 169 |
message_wrapper->message.message_data.len = 0; |
| 170 |
message_wrapper->message.message_data.capacity = size_hint <= max_size ? size_hint : max_size; |
| 171 |
|
| 172 |
|
| 173 |
message_wrapper->msg_allocator.base_allocator.impl = &message_wrapper->msg_allocator; |
| 174 |
message_wrapper->msg_allocator.base_allocator.mem_acquire = s_message_pool_mem_acquire; |
| 175 |
message_wrapper->msg_allocator.base_allocator.mem_realloc = NULL; |
| 176 |
message_wrapper->msg_allocator.base_allocator.mem_release = s_message_pool_mem_release; |
| 177 |
message_wrapper->msg_allocator.msg_pool = msg_pool; |
| 178 |
|
| 179 |
message_wrapper->message.allocator = &message_wrapper->msg_allocator.base_allocator; |
| 180 |
return &message_wrapper->message; |
| 181 |
} |
| 182 |
|
| 183 |
void aws_message_pool_release(struct aws_message_pool *msg_pool, struct aws_io_message *message) { |
| 184 |
|
| 185 |
memset(message->message_data.buffer, 0, message->message_data.len); |
| 186 |
message->allocator = NULL; |
| 187 |
|
| 188 |
struct message_wrapper *wrapper = AWS_CONTAINER_OF(message, struct message_wrapper, message); |
| 189 |
|
| 190 |
switch (message->message_type) { |
| 191 |
case AWS_IO_MESSAGE_APPLICATION_DATA: |
| 192 |
if (message->message_data.capacity > msg_pool->small_block_pool.segment_size - MSG_OVERHEAD) { |
| 193 |
aws_memory_pool_release(&msg_pool->application_data_pool, wrapper); |
| 194 |
} else { |
| 195 |
aws_memory_pool_release(&msg_pool->small_block_pool, wrapper); |
| 196 |
} |
| 197 |
break; |
| 198 |
default: |
| 199 |
AWS_ASSERT(0); |
| 200 |
aws_raise_error(AWS_IO_CHANNEL_UNKNOWN_MESSAGE_TYPE); |
| 201 |
} |
| 202 |
} |