tech-kern archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Temporary memory allocation from interrupt context



Hey folks,

I have the feeling this question is pretty stupid and I must be missing
something - so probably I will know a good solution immediately after
sending this.

I am working on removing malloc(9) calls from some code. We have a
great summary of options in memoryallocators(9), but for this situation
it has no clear advice (actually the currently working solution below is not
blessed by it at all).

Consider the following pseudo-code running in softint context:

void
softint_func(some_state *st, ....)
{
	LOCK(st);
	size_t n, max_n = st->num_items;
	some_state_item **tmp_list =
	    kmem_intr_alloc(max_n * sizeof(*tmp_list));
	n = 0;
	for (i : st->items) {
		if (!(i matches some predicate))
			continue;
		i->retain();
		tmp_list[n++] = i;
	}
	UNLOCK(st);
	/* do something with all elements in tmp_list */
	kmem_intr_free(tmp_list, max_n * sizeof(*tmp_list));
}

I don't want to alloca here (the list could be quite huge) and max_n could
vary a lot, so having a "manual" pool of a few common (preallocated)
list sizes hanging off the state does not go well either.

In a perfect world we would avoid the interrupt allocation all together, but
I have not found a way to rearrange things here to make this feasible.

Is kmem_intr_alloc(9) the best way forward?


Martin


Home | Main Index | Thread Index | Old Index