Custom memory allocator for running Lua on CM4

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
13 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Custom memory allocator for running Lua on CM4

Rahul
Let me confess i am newbee on Lua and embedded. I am trying to port Lua on custom freertos running on CM4 and which does not have reallocation. is there any standard custom memory allocator available?

I was thinking to request a bigger chunk of memory in one shot from the system and later custom allocator will keep allocating and free memory chunk from that. 

Looking forward to hear from you guys.

-Rahul
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Custom memory allocator for running Lua on CM4

Dhaval Kapil
Let me confess i am newbee on Lua and embedded. I am trying to port Lua on custom freertos running on CM4 and which does not have reallocation. is there any standard custom memory allocator available?

I understand that 'realloc' is unavailable on the custom freertos.

If you want a 'hack' to get it running, you could probably implement the 'realloc' function as a wrapper over 'malloc' and 'free'?

On Wed, Aug 2, 2017 at 4:39 PM, RAHUL KUMAR <[hidden email]> wrote:
Let me confess i am newbee on Lua and embedded. I am trying to port Lua on custom freertos running on CM4 and which does not have reallocation. is there any standard custom memory allocator available?

I was thinking to request a bigger chunk of memory in one shot from the system and later custom allocator will keep allocating and free memory chunk from that. 

Looking forward to hear from you guys.

-Rahul



--
Dhaval Kapil
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Custom memory allocator for running Lua on CM4

Rahul
Right but problem is that system free is not available /restricted and even if it is then it will lead to fragmentation. So i was thinking to manage the memory myself after system malloc. Just wanted to check if there is already allocator available ?
 

On Wed, Aug 2, 2017 at 4:47 PM, Dhaval Kapil <[hidden email]> wrote:
Let me confess i am newbee on Lua and embedded. I am trying to port Lua on custom freertos running on CM4 and which does not have reallocation. is there any standard custom memory allocator available?

I understand that 'realloc' is unavailable on the custom freertos.

If you want a 'hack' to get it running, you could probably implement the 'realloc' function as a wrapper over 'malloc' and 'free'?

On Wed, Aug 2, 2017 at 4:39 PM, RAHUL KUMAR <[hidden email]> wrote:
Let me confess i am newbee on Lua and embedded. I am trying to port Lua on custom freertos running on CM4 and which does not have reallocation. is there any standard custom memory allocator available?

I was thinking to request a bigger chunk of memory in one shot from the system and later custom allocator will keep allocating and free memory chunk from that. 

Looking forward to hear from you guys.

-Rahul



--
Dhaval Kapil

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Custom memory allocator for running Lua on CM4

KHMan
On 8/2/2017 7:23 PM, RAHUL KUMAR wrote:
> Right but problem is that system free is not available /restricted
> and even if it is then it will lead to fragmentation. So i was
> thinking to manage the memory myself after system malloc. Just
> wanted to check if there is already allocator available ?

If you have enough memory/performance, Doug Lea's malloc seems
pretty popular, it's like a de facto standard. Even Cygwin uses
it, only grumblings I recall relating to Cygwin's malloc are some
talk of wishing to maximize performance with some sort of
multi-threaded malloc.

uClibc has some malloc implementations. There are a number of
other small libc projects in the style of uClibc, all of which
should offer some sort of malloc.

> On Wed, Aug 2, 2017 at 4:47 PM, Dhaval Kapil wrote:
>
>     > Let me confess i am newbee on Lua and embedded. I am trying to
>     port Lua on custom freertos running on CM4 and which does not
>     have reallocation. is there any standard custom memory
>     allocator available?
>
>     I understand that 'realloc' is unavailable on the custom freertos.
>
>     If you want a 'hack' to get it running, you could probably
>     implement the 'realloc' function as a wrapper over 'malloc'
>     and 'free'?
>
>     On Wed, Aug 2, 2017 at 4:39 PM, RAHUL KUMAR
>     <[hidden email] <mailto:[hidden email]>> wrote:
>
>         Let me confess i am newbee on Lua and embedded. I am
>         trying to port Lua on custom freertos running on CM4 and
>         which does not have reallocation. is there any standard
>         custom memory allocator available?
>
>         I was thinking to request a bigger chunk of memory in one
>         shot from the system and later custom allocator will keep
>         allocating and free memory chunk from that.
>
>         Looking forward to hear from you guys.
>
>         -Rahul


--
Cheers,
Kein-Hong Man (esq.)
Selangor, Malaysia


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Custom memory allocator for running Lua on CM4

Luiz Henrique de Figueiredo
In reply to this post by Rahul
> is there any standard custom memory allocator available?
>
> I was thinking to request a bigger chunk of memory in one shot from the
> system and later custom allocator will keep allocating and free memory
> chunk from that.

Try the K&R allocator. Google found a copy at
        https://github.com/BoiseState/CS453-resources/tree/master/examples/memory-management/simple-malloc

If you want to find other copies, search for
        "morecore: ask system for more memory"

You'll have to adapt morecore to your needs: for instance, by replacing sbrk.

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Custom memory allocator for running Lua on CM4

Rahul
Thanks Luiz and Kein. I will try and keep you guys posted.

On Wed, Aug 2, 2017 at 5:21 PM, Luiz Henrique de Figueiredo <[hidden email]> wrote:
> is there any standard custom memory allocator available?
>
> I was thinking to request a bigger chunk of memory in one shot from the
> system and later custom allocator will keep allocating and free memory
> chunk from that.

Try the K&R allocator. Google found a copy at
        https://github.com/BoiseState/CS453-resources/tree/master/examples/memory-management/simple-malloc

If you want to find other copies, search for
        "morecore: ask system for more memory"

You'll have to adapt morecore to your needs: for instance, by replacing sbrk.


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Custom memory allocator for running Lua on CM4

Luiz Henrique de Figueiredo
> Thanks Luiz and Kein. I will try and keep you guys posted.

Yes, please consider sharing your allocator here.

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Custom memory allocator for running Lua on CM4

kamicc olo
In reply to this post by Luiz Henrique de Figueiredo


On Wed, Aug 2, 2017 at 2:51 PM, Luiz Henrique de Figueiredo <[hidden email]> wrote:
You'll have to adapt morecore to your needs: for instance, by replacing sbrk.

I guess, this problem was already discussed in this mailling list at least couple of times :}

I'd like to expand on that a little bit. In general, You'll have to deal with linker scripts and mapping properly the RAM.
Also, there are plenty of MCUs with terribly segmented RAM, in chunks of 16-32 whatever kbytes. Which... makes
sbrk() implementation a bit tricky. 

All in all, that's the generic way dealing with more or less custom or just various embedded devices like ARM boards.

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Custom memory allocator for running Lua on CM4

Luiz Henrique de Figueiredo
> Which... makes sbrk() implementation a bit tricky.

I was suggesting that sbrk simply returned a single, perhaps preallocated,
block in the first call and fail in subsequent calls.

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Custom memory allocator for running Lua on CM4

kamicc olo

On Wed, Aug 2, 2017 at 3:52 PM, Luiz Henrique de Figueiredo <[hidden email]> wrote:
> Which... makes sbrk() implementation a bit tricky.

I was suggesting that sbrk simply returned a single, perhaps preallocated,
block in the first call and fail in subsequent calls.


Well, if the memory is highly segmented... I guess it was some STM MCU with like... 5 or 8 separated
chunks of RAM available with no contiguous addressing. I can't see how single block would work on
this specific case.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Custom memory allocator for running Lua on CM4

Rahul
In reply to this post by kamicc olo
I have subscribed recently so not aware of previous discussions. If you can pull the old mailing list or conclusion that would be helpful :) 

Currently i have following options

1.       dlmalloc(http://g.oswego.edu/dl/html/malloc.html)

2.        K &R ( like https://github.com/BoiseState/CS453-resources/tree/master/examples/memory-management/simple-malloc)

3.       uClibc (https://github.com/kraj/uClibc/blob/master/libc/stdlib/malloc/malloc.h)

4. last not the least the OpenRTOS malloc implemenation  (http://www.freertos.org/a00111.html#heap_5)

Not sure which one to start with.



On Wed, Aug 2, 2017 at 6:19 PM, Ką Mykolas <[hidden email]> wrote:


On Wed, Aug 2, 2017 at 2:51 PM, Luiz Henrique de Figueiredo <[hidden email]> wrote:
You'll have to adapt morecore to your needs: for instance, by replacing sbrk.

I guess, this problem was already discussed in this mailling list at least couple of times :}

I'd like to expand on that a little bit. In general, You'll have to deal with linker scripts and mapping properly the RAM.
Also, there are plenty of MCUs with terribly segmented RAM, in chunks of 16-32 whatever kbytes. Which... makes
sbrk() implementation a bit tricky. 

All in all, that's the generic way dealing with more or less custom or just various embedded devices like ARM boards.


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Custom memory allocator for running Lua on CM4

Doug Currie

Not sure which one to start with.

The FreeRTOS heap_5 implementation has two advantages: it is thread safe, and it handles discontiguous regions. I'd start there.

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Custom memory allocator for running Lua on CM4

Enrico Colombini
In reply to this post by Rahul
On 02-Aug-17 17:57, RAHUL KUMAR wrote:

> Currently i have following options
>
> 1.dlmalloc(http://g.oswego.edu/dl/html/malloc.html)
>
> 2. K &R ( like
> https://github.com/BoiseState/CS453-resources/tree/master/examples/memory-management/simple-malloc)
>
> 3.uClibc
> (https://github.com/kraj/uClibc/blob/master/libc/stdlib/malloc/malloc.h)
>
> 4. last not the least the OpenRTOS malloc implemenation
>   (http://www.freertos.org/a00111.html#heap_5)
>
> Not sure which one to start with.

Not sure if it would be the best choice in this case, but there is also
TLSF (Two-Level Segregate Fit), a bounded-time allocator useful in time-
and/or resource-constrained systems. I used version 1.9 to manage 1 MB
of RAM and it worked very well with Lua:

http://www.gii.upv.es/tlsf/

If you search the Lua-list archives looking for "tlsf" and my name, you
should find a few precious hints I got from Luiz and/or Roberto.

--
   Enrico

Loading...