mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: Sascha Hauer <s.hauer@pengutronix.de>,
	Ahmad Fatoum <a.fatoum@barebox.org>
Cc: barebox@lists.infradead.org
Subject: Re: [PATCH RFC 1/3] lib: add talloc for overlaying a tree onto allocations
Date: Tue, 28 Oct 2025 11:26:02 +0100	[thread overview]
Message-ID: <94867b83-cde8-449f-8ed4-d97855831d09@pengutronix.de> (raw)
In-Reply-To: <aQCP95IqfhGo1zJg@pengutronix.de>

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 |




      reply	other threads:[~2025-10-28 10:26 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-10-27  7:54 Ahmad Fatoum
2025-10-27  7:54 ` [PATCH RFC 2/3] test: self: add talloc selftest Ahmad Fatoum
2025-10-27  7:54 ` [PATCH RFC 3/3] hush: fix memory leaks Ahmad Fatoum
2025-10-28  9:42 ` [PATCH RFC 1/3] lib: add talloc for overlaying a tree onto allocations Sascha Hauer
2025-10-28 10:26   ` Ahmad Fatoum [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=94867b83-cde8-449f-8ed4-d97855831d09@pengutronix.de \
    --to=a.fatoum@pengutronix.de \
    --cc=a.fatoum@barebox.org \
    --cc=barebox@lists.infradead.org \
    --cc=s.hauer@pengutronix.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox