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

Sascha Hauer s.hauer at pengutronix.de
Tue Oct 28 02:42:15 PDT 2025


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.

> +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/

> + *
> + * @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?

> +/**
> + * 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)

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