Sign In

Don't have a Xilinx account yet?

  • Choose to receive important news and product information
  • Gain access to special content
  • Personalize your web experience on Xilinx.com

Create Account

Username

Password

Forgot your password?
XClose Panel
Xilinx Home
Reply
Contributor
swally
Posts: 40
Registered: ‎02-25-2008
0

best memory allocation to use, malloc?

Hi,

 

I was recently reading a bug report about xil_malloc and malloc from some time ago but the report said it's okay to use malloc if using the newlib library.  First off, if I'm running 10.1.2, are the newlib libraries already included?  Second, is malloc the best method to allocate memory in an embedded application?  I'm using xilkernel with a spartan 3E.  Thanks

 

Steve

Super Contributor
dorau
Posts: 127
Registered: ‎08-29-2008
0

Re: best memory allocation to use, malloc?

Hi,

 

First: The newlib library is a separate sourceware project maintained by some redhat guys! I assume, that it isn't delivered with 10.1.2!!!

Second: My opinion!!! I would avoid to use dynamic memory allocation in embedded systems.

Reason:

- Concepts regarding memory partitioning helps to write reliable software!You know what you do ;-)

- Dynamic memory allocation consumes execution time!

- Memory allocation could fail. This fact is avoided!

- Memory allocation must be freed, if it is not used!. This fact is avoided, too!

 

Kai

Xilinx Employee
vsiva
Posts: 495
Registered: ‎01-18-2008

Re: best memory allocation to use, malloc?

newlib libraries are included. That is the libc.a that is linked in to your project by default. You do not have to do anything special.

 

As far as malloc goes, personally, I think the point of view that you should not use malloc in an embedded system is quite extreme. To me, a design running uclinux on MicroBlaze is an embedded system. In such a system, regardless of what the user app does, there are tons of kmalloc calls going on in the kernel. These are the days of phones running Java VM's with garbage collection.

 

That said, you need to be aware of the limitations of the malloc library you are using. Probably the most important thing for you (because you are using xilkernel), is that malloc is not thread safe. That implies that you should not be calling malloc directly from multiple threads. Either call malloc from a single thread, or protect all calls to malloc with a semaphore. The simplest way to do it would be to write a wrapper function around malloc that protects it with a semaphore, and only call this wrapper function rather than malloc directly.

Contributor
swally
Posts: 40
Registered: ‎02-25-2008

Re: best memory allocation to use, malloc?

Hi, 

 

When you say it's not thread safe, do you mean that I can't have two threads using memory obtained at different times, or do you mean I can't call malloc from two threads at the same time - essentially trying to allocate two blocks simultaneously?  Also, do I need to worry about allocating at the same time xilkernel is allocating?

 

The reason I wrote this letter is because I've found malloc running on my microblaze to be very unreliable.  Both my stack and heap are set at 16K bytes but yet my malloc fails often.  Just today I counted 5 failures out of 30 attempts to allocate 32 bytes for a memory for a menu.  If I exit that menu and then go back in it usually works.  Is it normal to set up retries if allocation fails?  Is there an option to malloc?

 

Steve

 

 

Xilinx Employee
vsiva
Posts: 495
Registered: ‎01-18-2008
0

Re: best memory allocation to use, malloc?

You cannot have multiple threads call malloc. Check wikipedia for a good description of thread safety.

 

Your description of failures sounds quite odd. There shouldn't be any failures at all in such a case. And no, you shouldn't have to put in retries. If you can reproduce the issue in a small snippet of C program (use standalone instead of xilkernel), then you should file a webcase.