All of lore.kernel.org
 help / color / mirror / Atom feed
From: Tomas Hlavacek <tmshlvck@gmail.com>
To: u-boot@lists.denx.de
Subject: [U-Boot] [PATCH v8] [RFC] early_malloc for DM added.
Date: Wed, 26 Sep 2012 12:16:07 +0200	[thread overview]
Message-ID: <CAEB7QLCidcMuVSZDoeL1ODzoqf-Jpcs=7K+iL7+YHA0Zn_go=g@mail.gmail.com> (raw)
In-Reply-To: <CALButCKSu_sAr2Y3xugNgja2LfHzQG7-aePJOEq+3nSUi6oR_A@mail.gmail.com>

Hello Graeme,

On Wed, Sep 26, 2012 at 1:04 AM, Graeme Russ <graeme.russ@gmail.com> wrote:
> Hi Tomas
>
> On Tue, Sep 25, 2012 at 7:09 PM, Graeme Russ <graeme.russ@gmail.com> wrote:
>
>> We should implement each of malloc(), free(), calloc(), and realloc().
>>
>> Don't worry about reclaiming and reusing space with a proper free()
>> implementation. Remember, all memory allocated on the early heap must be
>> relocated anyway.
>>
>> Maybe if you add a size_t value immediately before the allocated space which
>> stores the block size. So:
>>
>> size_t *bytes_ptr = ptr;
>> bytes_ptr--;
>> size_t bytes = *bytes_ptr;
>>
>> gives you the block size
>
> I've been thinking about this a bit more, and for the sake of 4 bytes,
> this additional 'size' member could be quite handy:
>  - We effectively end up with a linked-list of allocated blocks

Yes, this makes sense. Knowing size of allocated blocks enables the
early_malloc to do the whole set of malloc* functions.

Do you think it would be also useful to have a basic check that the
pointer passed to new early_free() and/or early_realloc() is valid? I
mean check that the pointer really points at the start of the block
and there is a valid header preceding the block. We can have a magic
number in the header for example. Or I can store a list of allocated
blocks as a variable-sized array in the early_heap_header.

>  - free() could set the high bit to tag the block as 'de-allocated'

Well, we would end up with malloc doing:

1) scan through all heaps to find best fit into previously free()-ed block
2) scan through all heaps to find enough free space using the heap header
3) call early_brk to obtain more space

Data structures would be:

The scanning two different header types for free space seems to me
being a bit redundant. What do you think about replacing point (2) and
using block headers instead of free space pointer in the heap header
and just split the block when there is not exact size match. I mean:

1) scan through all heaps to find best fit block
2) scan through all heaps to find greater block and split it
3) call early_brk to obtain more space

Data structures would be:

struct early_heap_header {
    int heap_size;
    phys_addr_t reloc_addr_old;
}

struct early_block_header {
    int magic;
    int size;
}

The brand new early_heap right after it's creation would consist of a
early_heap_header followed by a early_block_header with the free bit
set to true.

>  - When a block is relocated into the permanent heap, free() should be
>    called on the source (early heap) block

Well, that time we would have only a copy of the early_heap on certain
platforms. Assuming that first we set off drivers and DM using
early_heap(s), then we would copy each early_heap to some computed
address and jump to board_init_r. In board_init_r we would access the
early_heap copy via translation function using the address offset
(early_heap_copy - early_heap_copy->reloc_addr_old).

To do the early_free correctly in this mid-relocation state I would
have to allow early_* code to operate during this period. It is not
that hard to do, but maybe it is conceptually wrong because it means
operating early_malloc/realloc/free code on a copy of it's own data
after relocation.

>  - We can call a 'cleanup' function after early heap is no longer needed
>    and check that every block has the high bit set

Yes, it would help. When each block has to be free()-ed or relocated
and then free()-ed, it could help find memory leaks during early init.

>  - We can re-use blocks by scanning for a tagged block with the same size
>    (usefull for drivers that allocate temporary buffers which are always
>    the same size)
>  - If there are no early heaps with enough space for a given malloc
>    operation, but there is a tagged block that is larger than the
>    requested size, we can split tagged blocks
>
> Remebering back to when I suggested a list of relocation helpers (one for
> each allocated block), I think we can implement that as an additional field
> in the block header (stretching the header to 8 bytes). This can come later.

Do you mean we should place a pointer pointing at a relocation
function into each block header? I think it would end up in situation
like: Somebody is allocating a tree for example, he would set the
helper function into the root element and NULL pointers to other
nodes, because it makes more sense to relocate tree recursively from
the root.

Tomas

  reply	other threads:[~2012-09-26 10:16 UTC|newest]

Thread overview: 48+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-08-27 12:12 [U-Boot] [PATCH 1/1] [RFC] DM: early_malloc for DM added Tomas Hlavacek
2012-08-27 12:18 ` Marek Vasut
2012-08-27 12:37   ` Tomas Hlavacek
2012-08-27 12:42 ` [U-Boot] [PATCHv2 " Tomas Hlavacek
2012-08-27 23:02   ` Graeme Russ
2012-08-28  0:11     ` Graeme Russ
2012-08-28  0:18     ` Graeme Russ
2012-09-18  7:13 ` [U-Boot] [PATCHv4] " Tomas Hlavacek
2012-09-18 10:57   ` Marek Vasut
2012-09-18 23:29     ` Graeme Russ
2012-09-18 23:33       ` Marek Vasut
2012-09-18 23:44         ` Graeme Russ
2012-09-18 23:53           ` Marek Vasut
2012-09-19 18:29       ` Tom Rini
2012-09-22  0:37       ` Tomas Hlavacek
2012-09-18 23:25   ` Graeme Russ
2012-09-22  0:25 ` [U-Boot] [PATCH v5] [RFC] " Tomas Hlavacek
2012-09-22  0:28   ` Marek Vasut
2012-09-22  7:52     ` Tomas Hlavacek
2012-09-22 13:19       ` Marek Vasut
2012-09-22 22:09 ` [U-Boot] [PATCH v6] " Tomas Hlavacek
2012-09-23 13:06   ` Graeme Russ
2012-09-23 15:30     ` Tomas Hlavacek
2012-09-23 15:38 ` [U-Boot] [PATCH v7] " Tomas Hlavacek
2012-09-23 16:15 ` [U-Boot] [PATCH v8] " Tomas Hlavacek
2012-09-23 16:32   ` Wolfgang Denk
2012-09-23 16:47     ` Tomas Hlavacek
2012-09-24 21:48       ` Tom Rini
2012-09-23 23:11   ` Marek Vasut
2012-09-24 14:16     ` Tomas Hlavacek
2012-09-24 14:19       ` Marek Vasut
2012-09-25  0:37         ` Graeme Russ
2012-09-25  8:43           ` Tomas Hlavacek
2012-09-25  9:09             ` Graeme Russ
2012-09-25 23:04               ` Graeme Russ
2012-09-26 10:16                 ` Tomas Hlavacek [this message]
2012-09-26 23:03                   ` Graeme Russ
2012-09-24  0:00   ` Graeme Russ
2012-09-24  0:35     ` Tomas Hlavacek
2012-09-24  0:46       ` Graeme Russ
2012-10-24 23:49 ` [U-Boot] [PATCH v9] [RFC] Add dmmalloc module for DM Tomas Hlavacek
2012-10-25  1:40   ` Graeme Russ
2012-10-25 19:16     ` Tomas Hlavacek
2012-10-25 23:04       ` Graeme Russ
2012-10-28 23:20 ` [U-Boot] [PATCH v10] " Tomas Hlavacek
2013-11-05 15:26   ` Mateusz Zalega
2013-11-05 17:17     ` Tom Rini
2013-11-05 17:26       ` Mateusz Zalega

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='CAEB7QLCidcMuVSZDoeL1ODzoqf-Jpcs=7K+iL7+YHA0Zn_go=g@mail.gmail.com' \
    --to=tmshlvck@gmail.com \
    --cc=u-boot@lists.denx.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.