rhempel / umm_malloc

Memory Manager For Small(ish) Microprocessors
MIT License
395 stars 105 forks source link

Heap initialization value other than zero. #68

Open user2222222222 opened 4 months ago

user2222222222 commented 4 months ago

It would be nice to be able to initialize the heap to something other than zero (such as a checker pattern: 0x55). #define UMM_HEAPINIT 0x55

It looks like these are the only changes needed in umm_multi_init_heap(), but thought I'd bring it up here in case you think it's worth adding, but mainly to make sure I'm not missing something:

    memset(UMM_HEAP, UMM_HEAPINIT, UMM_HEAPSIZE);

    UMM_NBLOCK(0) = 1;
    UMM_PBLOCK(0) = 0;
    UMM_NFREE(0) = 1;
    UMM_PFREE(0) = 1;

    UMM_NBLOCK(1) = UMM_BLOCK_LAST | UMM_FREELIST_MASK;
    UMM_PBLOCK(1) = 0;
    UMM_NFREE(1) = 0;
    UMM_PFREE(1) = 0;

    UMM_NBLOCK(UMM_BLOCK_LAST) = 0;
    UMM_PBLOCK(UMM_BLOCK_LAST) = 1;
    UMM_NFREE(UMM_BLOCK_LAST) = 0;
    UMM_PFREE(UMM_BLOCK_LAST) = 0;
rhempel commented 3 months ago

We have a compile time switch to add some safety padding around memory blocks already.

Do you have a use case for initializing the entire heap to some specific value?

user2222222222 commented 3 months ago

Not one useful in any kind of new design, but something I needed for virtualizing an old system. Some old embedded devices initialize the heap with a checker pattern. Needed to keep it the same when wrapping it with umm and that was the easiest thing to do without changing anything else. What I did is working fine, but didn't investigate the changes I made: just did what looked logical.

rhempel commented 3 months ago

Here is what I can do - there are a couple of options.

  1. We can make the init function WEAK, so that by default it is initialized to 0 as it is today. If the user supplies a custom initializer, then it is called
  2. We can make a #define that is defaulted to 0 and is overrideable either on the build command line or in a config file

I am leaning to option 2 since we already have a nice mechanism for overriding values :-)

user2222222222 commented 3 months ago

Option 2 is what works for me and considering the rarity of it's application, seems like the best thing to do. In the unlikely case someone needed a dynamic option, it would be an easy and thoughtless modification. And that's the only reason I brought it up at all: it would have been nice for it to be there so I didn't have to wonder whether I might be breaking some obscure special case (we've all been there before.)