Jump to content
Sign in to follow this  
animeranger

Calling Alloc From Main And Interrupts

Recommended Posts

I keep getting the following error when I link my current project:

 

Serious Warning: Possible sw stack corruption, function 'alloc' called by more than one asynchronous thread (main/Task, interrupt, interrupt low)

 

I understand why this is. I'm calling alloc from all three locations. I know of a workaround for this for my particular project. But, I am hoping to not have to use it.

 

I have created multiple subroutines as suggested in the manual such as function(), function_interrupt(), and function_interrupt_low() for all the other functions that have this conflict. However, to my knowledge, I can't do this for functions inside the BoostC libraries. Does anyone know how to create a function such as alloc_interrupt() ? If this is impossible, which I believe it is, I think I'll submit this as a feature request.

 

Thanks,

- Bill

Share this post


Link to post
Share on other sites

animeranger,

I keep getting the following error when I link my current project:

 

Serious Warning: Possible sw stack corruption, function 'alloc' called by more than one asynchronous thread (main/Task, interrupt, interrupt low)

This warning should definately not be ignored unless you have taken action to prevent corruption.

 

Background: The corruption can occur if any function is call from more than one asynchronous thread because before the function completes it could be re-entered which then corrupts the sofware stack. A software is used because PICs have very limited hardware stacks, so the linker uses regular general purpose registers (you can consider them RAM) and works out its own stack based on where the function is called in the function call tree.

 

Calling alloc in an interrupt routine is generally not a good idea, the time it takes to complete is variable and could be quite long. It you do want to do this then the corruption can be prevented by ensuring that interrupts are disabled before calling the alloc routine (and any other memory allocation related routines) in the main execution thread. Consider as an alternative having some pre-alloced buffers in the for of global arrays, this way you never need to call alloc in the first place, and the execution time of your code becomes more predictable.

 

You may also know from precisely understanding your program that the alloc call in the main execution thread is only called when interrupts can't occur (like before they are enabled). In this case no action needs to be taken.

 

The warning won't go away even if your code won't corrupt if a function is called from two asychronous execution threads. This is because its too difficult to detect that corruption will not occur, it requires full understanding of the program, the way its executed and the hardware. But if you have this warning you do need to think to make sure you have the bases covered or you will end up with some occassional unexpected behaviour.

 

Regards

Dave

Share this post


Link to post
Share on other sites

I understood what the problem was, but I didn't know that alloc can take a long time.

 

I generally don't use it. In the case of my current project, a message router, I am collecting messages from a network of devices I am building and delivering them to an interface I've created on my PC. I want to store these messages in the router if my PC is ever disconnected. When it reconnects, the router will deliver the messages.

 

Because the messages can vary in length, I thought that dynamic memory allocation may be useful. I normally just create a giant array of whatever memory I have left over (saving some for the heap, just in case I didn't see something that may need it), and use pointers to the beginnings of messages I keep inside of it, kind of like a memory map. Because I'm delivering the entire thing to one device and clearing it, fragmentation and other such things have never been a problem whenever I've done this.

 

I guess I'll go back to the memory map, or I'll just do everything in main and set flags in the interrupts for when main should alloc something. I'll play with both to find what is better.

 

Thanks, Dave.

 

- Bill

Share this post


Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...