/* Description: Free an sg table previously allocated and setup with __sg_alloc_table(). The @max_ents value must be identical to that previously used with __sg_alloc_table().  */
void __sg_free_table(struct sg_table *table, unsigned int max_ents, sg_free_fn *free_fn)
/* Description: Free an sg table previously allocated and setup with __sg_alloc_table(). The @max_ents value must be identical to that previously used with __sg_alloc_table().  */
void __sg_free_table(struct sg_table *table, unsigned int max_ents, sg_free_fn *free_fn)
{
	struct scatterlist *sgl, *next;
	if (unlikely(!table->sgl))
		return;
	sgl = table->sgl;
	while (table->orig_nents) {
		unsigned int alloc_size = table->orig_nents;
		unsigned int sg_size;
		if (alloc_size > max_ents) {
			next = sg_chain_ptr(&sgl[max_ents - 1]);
			alloc_size = max_ents;
			sg_size = alloc_size - 1;
		} else {
			sg_size = alloc_size;
			next = NULL;
		}
		table->orig_nents -= sg_size;
		free_fn(sgl, alloc_size);
		sgl = next;
	}
	table->sgl = NULL;
}