[U-Boot] Early malloc() summary

Graeme Russ
2012-08-17T01:15:18Z

Hi Marek,

On Fri, Aug 17, 2012 at 10:34 AM, Marek Vasut <marek.vasut@gmail.com> wrote:
> Dear Graeme Russ,
>
>> Hi Marek,
>>
>> On Fri, Aug 17, 2012 at 9:32 AM, Marek Vasut <marek.vasut@gmail.com> wrote:
>> > Dear Graeme Russ,
>> >
>> >> >> Hmm, I hadn't thought of that
>> >> >
>> >> > So, we're back to square 1 ?
>> >>
>> >> Nope - just a slight tweak is all
>> >
>> > What tweak ?
>>
>> Didn't you read the rest of my email?
>
> No :-)
>
> So every strdup will need a driver associated with it ?

No...

dm_malloc(bytes, driver *)
|
+-> early_malloc(bytes, reloc_helper *) /* Pre-Relocation */
| |
| +->register_helper(reloc_helper *)
| |
| +->pre_reloc_malloc(size_t bytes)
|
+-> malloc(bytes) /* Post-Relocation */

Drivers call dm_malloc(), helper functions call early_malloc()

dm_malloc() is implemented in the DM core code and checks for whether the
call is pre- or post- relocation. If pre-relocation, it checks for the
driver having a relocation helper (or the 'I don't need one' flag)

early_malloc() is implemented in the early malloc code seperate from the
DM code.

**** WARNING!!! STOP READING NOW!!! ****

early_malloc() registers a relocation function (if provided) which will be
called during relocation. DM core will strip this out as it will (for the
time being) handle the calling of the relocation helper for each of the
registered drivers. In the long term, I think that responsibility might be
able to be taken away from DM core (but there may be call-order issues that
might make that impossible)

The way I imagine it in the future, any code that might possible allocate
memory prior to relocation would do something like:

static int my_relocator(void *data)
{
struct foo *new_bar;

new_bar = malloc(sizeof(struct foo));
mem_cpy(new_bar, data, sizeof(struct foo));

/* Tweak internal new_bar members */

return 0;
}

int some_function()
{
struct foo *bar;

bar = malloc(sizeof(struct foo));
register_helper(bar, my_relocator);

return 0;
}

And behind the scenes we have:

data = malloc(bytes);
|
+->data = pre_reloc_malloc(size_t bytes) /* Pre-Relocation */
| |
| +->add_to_reloc_list(data)
| |
| +->return data;
|
+->malloc(size_t bytes); /* Post-Relocation */

register_helper(data, reloc_helper *)
|
+->update_reloc_list(data, reloc_helper *) /* Pre-Relocation */
|
+->Do Nothing /* Post-Relocation */

During relocation, the 'reloc list' is processed. Each 'data' entry with no
'reloc_helper' will elicite a (debug) warning to let you know about data
that was allocated but will not be relocated.

Regards,

Graeme