* calculate the starting point of where data of the requested size * and alignment would fit in the current element. If the data doesn't * fit, return NULL. */
| 216 | * fit, return NULL. |
| 217 | */ |
| 218 | static void * |
| 219 | elem_start_pt(struct malloc_elem *elem, size_t size, unsigned align, |
| 220 | size_t bound, bool contig) |
| 221 | { |
| 222 | size_t elem_size = elem->size; |
| 223 | |
| 224 | /* |
| 225 | * we're allocating from the end, so adjust the size of element by |
| 226 | * alignment size. |
| 227 | */ |
| 228 | while (elem_size >= size) { |
| 229 | const size_t bmask = ~(bound - 1); |
| 230 | uintptr_t end_pt = (uintptr_t)elem + |
| 231 | elem_size - MALLOC_ELEM_TRAILER_LEN; |
| 232 | uintptr_t new_data_start = RTE_ALIGN_FLOOR((end_pt - size), |
| 233 | align); |
| 234 | uintptr_t new_elem_start; |
| 235 | |
| 236 | /* check boundary */ |
| 237 | if ((new_data_start & bmask) != ((end_pt - 1) & bmask)) { |
| 238 | end_pt = RTE_ALIGN_FLOOR(end_pt, bound); |
| 239 | new_data_start = RTE_ALIGN_FLOOR((end_pt - size), |
| 240 | align); |
| 241 | end_pt = new_data_start + size; |
| 242 | |
| 243 | if (((end_pt - 1) & bmask) != (new_data_start & bmask)) |
| 244 | return NULL; |
| 245 | } |
| 246 | |
| 247 | new_elem_start = new_data_start - MALLOC_ELEM_HEADER_LEN; |
| 248 | |
| 249 | /* if the new start point is before the exist start, |
| 250 | * it won't fit |
| 251 | */ |
| 252 | if (new_elem_start < (uintptr_t)elem) |
| 253 | return NULL; |
| 254 | |
| 255 | if (contig) { |
| 256 | size_t new_data_size = end_pt - new_data_start; |
| 257 | |
| 258 | /* |
| 259 | * if physical contiguousness was requested and we |
| 260 | * couldn't fit all data into one physically contiguous |
| 261 | * block, try again with lower addresses. |
| 262 | */ |
| 263 | if (!elem_check_phys_contig(elem->msl, |
| 264 | (void *)new_data_start, |
| 265 | new_data_size)) { |
| 266 | elem_size -= align; |
| 267 | continue; |
| 268 | } |
| 269 | } |
| 270 | return (void *)new_elem_start; |
| 271 | } |
| 272 | return NULL; |
| 273 | } |
| 274 | |
| 275 | /* |
no test coverage detected