* wasting some space this way, but it's much nicer than looping around
* trying to reserve each and every page size.
*
- * However, since size calculation will produce page-aligned sizes, it
- * makes sense to first try and see if we can reserve the entire memzone
- * in one contiguous chunk as well (otherwise we might end up wasting a
- * 1G page on a 10MB memzone). If we fail to get enough contiguous
- * memory, then we'll go and reserve space page-by-page.
+ * If we fail to get enough contiguous memory, then we'll go and
+ * reserve space in smaller chunks.
*
* We also have to take into account the fact that memory that we're
* going to allocate from can belong to an externally allocated memory
* @param[out] align
* Location for required memory chunk alignment.
* @return
- * Required memory size aligned at page boundary.
+ * Required memory size.
*/
typedef ssize_t (*rte_mempool_calc_mem_size_t)(const struct rte_mempool *mp,
uint32_t obj_num, uint32_t pg_shift,
* that pages are grouped in subsets of physically continuous pages big
* enough to store at least one object.
*
- * Minimum size of memory chunk is a maximum of the page size and total
- * element size.
- *
- * Required memory chunk alignment is a maximum of page size and cache
- * line size.
+ * Minimum size of memory chunk is the total element size.
+ * Required memory chunk alignment is the cache line size.
*/
ssize_t rte_mempool_op_calc_mem_size_default(const struct rte_mempool *mp,
uint32_t obj_num, uint32_t pg_shift,
return ops->get_count(mp);
}
-/* wrapper to notify new memory area to external mempool */
+/* wrapper to calculate the memory size required to store given number
+ * of objects
+ */
ssize_t
rte_mempool_ops_calc_mem_size(const struct rte_mempool *mp,
uint32_t obj_num, uint32_t pg_shift,
size_t *min_chunk_size, size_t *align)
{
size_t total_elt_sz;
- size_t obj_per_page, pg_num, pg_sz;
+ size_t obj_per_page, pg_sz, objs_in_last_page;
size_t mem_size;
total_elt_sz = mp->header_size + mp->elt_size + mp->trailer_size;
mem_size =
RTE_ALIGN_CEIL(total_elt_sz, pg_sz) * obj_num;
} else {
- pg_num = (obj_num + obj_per_page - 1) / obj_per_page;
- mem_size = pg_num << pg_shift;
+ /* In the best case, the allocator will return a
+ * page-aligned address. For example, with 5 objs,
+ * the required space is as below:
+ * | page0 | page1 | page2 (last) |
+ * |obj0 |obj1 |xxx|obj2 |obj3 |xxx|obj4|
+ * <------------- mem_size ------------->
+ */
+ objs_in_last_page = ((obj_num - 1) % obj_per_page) + 1;
+ /* room required for the last page */
+ mem_size = objs_in_last_page * total_elt_sz;
+ /* room required for other pages */
+ mem_size += ((obj_num - objs_in_last_page) /
+ obj_per_page) << pg_shift;
+
+ /* In the worst case, the allocator returns a
+ * non-aligned pointer, wasting up to
+ * total_elt_sz. Add a margin for that.
+ */
+ mem_size += total_elt_sz - 1;
}
}
- *min_chunk_size = RTE_MAX((size_t)1 << pg_shift, total_elt_sz);
-
- *align = RTE_MAX((size_t)RTE_CACHE_LINE_SIZE, (size_t)1 << pg_shift);
+ *min_chunk_size = total_elt_sz;
+ *align = RTE_CACHE_LINE_SIZE;
return mem_size;
}