[PATCH RFC 1/3] lib: add talloc for overlaying a tree onto allocations

Ahmad Fatoum a.fatoum at pengutronix.de
Tue Oct 28 03:26:02 PDT 2025


Hi,

On 10/28/25 10:42 AM, Sascha Hauer wrote:
> On Mon, Oct 27, 2025 at 08:54:32AM +0100, Ahmad Fatoum wrote:
>> +#ifndef __TALLOC_H__
>> +#define __TALLOC_H__
>> +
>> +#include <linux/types.h>
>> +
>> +struct talloc {
>> +	struct talloc *child;
>> +	struct talloc *next;
>> +	union {
>> +		struct talloc *prev;
>> +		struct talloc *parent; /* Valid only when is_first(mem) */
>> +	};
>> +};
> 
> Currently tlsf allocations are 8 byte aligned. On 32bit architectures
> struct talloc will be 12 bytes, so talloc allocations will only have 4
> byte alignment.

Good catch. On 32-bit, there is 4 bytes of unused padding inside TLSF
and feels bad sacrificing 4 more bytes here. :/

Will have to give it a thought.

>> +static void *talloc_ctx_init(struct talloc *hdr, const void *parent)
>> +{
>> +	void *mem;
>> +
>> +	if (!hdr)
>> +		return NULL;
>> +
>> +	memset(hdr, 0, sizeof(*hdr));
>> +
>> +	mem = hdr2mem(hdr);
>> +	talloc_steal(parent, mem);
>> +	return mem;
>> +}
>> +
>> +/**
>> + * talloc_usable_size() - Report the size of the talloation
> 
> s/talloation/tallocation/

Will fix for v2.

> 
>> + *
>> + * @mem: pointer to previously talloc'ed memory chunk.
>> + *
>> + * Return: size of tallocation
>> + */
>> +size_t talloc_usable_size(void *mem)
>> +{
>> +	return malloc_usable_size(mem) - sizeof(struct talloc);
>> +}
>> +EXPORT_SYMBOL(talloc_usable_size);
> 
> This function is unused. Do we need it?

To make it easier to retrofit normal malloc code for talloc, it's useful
to have already, I think.

>> +/**
>> + * talloc_free() - Deallocate a talloc'ed memory chunk and all the chunks depending on it.
>> + *
>> + * @mem: pointer to previously talloc'ed memory chunk.
>> + */
>> +void talloc_free(void *mem)
>> +{
>> +	struct talloc *hdr = mem2hdr(mem);
>> +
>> +	if (ZERO_OR_NULL_PTR(hdr))
>> +		return;
> 
> For correctness I believe you have to check mem here, not hdr.
> 
> (Although the above works as long as sizeof(struct talloc) <= ZERO_SIZE_PTR)

Thanks. This used to be container_of_safe of a flexible data member,
which I dropped in the meantime.

Will fix for v2.

Cheers,
Ahmad

> 
> Sascha
> 

-- 
Pengutronix e.K.                  |                             |
Steuerwalder Str. 21              | http://www.pengutronix.de/  |
31137 Hildesheim, Germany         | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686  | Fax:   +49-5121-206917-5555 |




More information about the barebox mailing list