| 214 | } |
| 215 | |
| 216 | static int |
| 217 | handle_alloc_request(const struct malloc_mp_req *m, |
| 218 | struct mp_request *req) |
| 219 | { |
| 220 | struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config; |
| 221 | const struct malloc_req_alloc *ar = &m->alloc_req; |
| 222 | struct malloc_heap *heap; |
| 223 | struct malloc_elem *elem; |
| 224 | struct rte_memseg **ms; |
| 225 | size_t alloc_sz; |
| 226 | int n_segs; |
| 227 | void *map_addr; |
| 228 | |
| 229 | /* this is checked by the API, but we need to prevent divide by zero */ |
| 230 | if (ar->page_sz == 0 || !rte_is_power_of_2(ar->page_sz)) { |
| 231 | RTE_LOG(ERR, EAL, "Attempting to allocate with invalid page size\n"); |
| 232 | return -1; |
| 233 | } |
| 234 | |
| 235 | /* heap idx is index into the heap array, not socket ID */ |
| 236 | if (ar->malloc_heap_idx >= RTE_MAX_HEAPS) { |
| 237 | RTE_LOG(ERR, EAL, "Attempting to allocate from invalid heap\n"); |
| 238 | return -1; |
| 239 | } |
| 240 | |
| 241 | heap = &mcfg->malloc_heaps[ar->malloc_heap_idx]; |
| 242 | |
| 243 | /* |
| 244 | * for allocations, we must only use internal heaps, but since the |
| 245 | * rte_malloc_heap_socket_is_external() is thread-safe and we're already |
| 246 | * read-locked, we'll have to take advantage of the fact that internal |
| 247 | * socket ID's are always lower than RTE_MAX_NUMA_NODES. |
| 248 | */ |
| 249 | if (heap->socket_id >= RTE_MAX_NUMA_NODES) { |
| 250 | RTE_LOG(ERR, EAL, "Attempting to allocate from external heap\n"); |
| 251 | return -1; |
| 252 | } |
| 253 | |
| 254 | alloc_sz = RTE_ALIGN_CEIL(RTE_ALIGN_CEIL(ar->elt_size, ar->align) + |
| 255 | MALLOC_ELEM_OVERHEAD, ar->page_sz); |
| 256 | n_segs = alloc_sz / ar->page_sz; |
| 257 | |
| 258 | /* we can't know in advance how many pages we'll need, so we malloc */ |
| 259 | ms = malloc(sizeof(*ms) * n_segs); |
| 260 | if (ms == NULL) { |
| 261 | RTE_LOG(ERR, EAL, "Couldn't allocate memory for request state\n"); |
| 262 | return -1; |
| 263 | } |
| 264 | memset(ms, 0, sizeof(*ms) * n_segs); |
| 265 | |
| 266 | elem = alloc_pages_on_heap(heap, ar->page_sz, ar->elt_size, ar->socket, |
| 267 | ar->flags, ar->align, ar->bound, ar->contig, ms, |
| 268 | n_segs); |
| 269 | |
| 270 | if (elem == NULL) |
| 271 | goto fail; |
| 272 | |
| 273 | map_addr = ms[0]->addr; |
no test coverage detected