Dynamic Allocation in ISR 
Author Message
 Dynamic Allocation in ISR

Hi all,

I am learning how to write ISR. I was just wondering if we could
dynamically allocate memory in an ISR (i.e. calling malloc or calloc)?
 If not, why?
Also, I was told not to call printf() in ISR since it is not
re-entrant. I am still not quite sure of what re-entrant meant? and
How do you tell whether a function is re-entrant or not?

Thanks in advance,
Kaede



Fri, 22 Apr 2005 11:20:57 GMT  
 Dynamic Allocation in ISR

Quote:

> I am learning how to write ISR. I was just wondering if we could
> dynamically allocate memory in an ISR (i.e. calling malloc or calloc)?
>  If not, why?
> Also, I was told not to call printf() in ISR since it is not
> re-entrant. I am still not quite sure of what re-entrant meant? and
> How do you tell whether a function is re-entrant or not?

This is all terribly operating system and implementation
dependent.  As a result, you should ask your question in a
newsgroup dedicated to your particular OS and/or implementation.


Fri, 22 Apr 2005 11:21:07 GMT  
 Dynamic Allocation in ISR
*** post for FREE via your newsreader at post.newsfeed.com ***

Quote:


> > I am learning how to write ISR. I was just wondering if we could
> > dynamically allocate memory in an ISR (i.e. calling malloc or calloc)?
> >  If not, why?
> > Also, I was told not to call printf() in ISR since it is not
> > re-entrant. I am still not quite sure of what re-entrant meant? and
> > How do you tell whether a function is re-entrant or not?

> This is all terribly operating system and implementation
> dependent.

    Why, not *all*. ISR code implies running in freestanding environment,
for which malloc(), calloc() are not defined (and printf() as well). OTOH,
if they do exist and work, they are implementation's extensions, as you say.

To OP: printf() being non-reentrant apparently means that it can't handle a
call while the previous call haven't returned yet, e.g. if you receive
another interrupt while the previous is inside printf() or another interrupt
from a different CPU while the precious is inside printf(). ISRs are usually
designed to be reentrant.

/BP

 -----= Posted via Newsfeed.Com, Uncensored Usenet News =-----
http://www.newsfeed.com - The #1 Newsgroup Service in the World!
-----== 100,000 Groups! - 19 Servers! - Unlimited Download! =-----



Sat, 23 Apr 2005 01:30:09 GMT  
 Dynamic Allocation in ISR

Quote:

> Hi all,

> I am learning how to write ISR. I was just wondering if we could
> dynamically allocate memory in an ISR (i.e. calling malloc or calloc)?
>  If not, why?

The C language has no concept of ``ISR''. There is an interrupt-like
feature in standard C called signals. A signal handler cannot do very
much; calling the allocator functions is off-limits.

Why? What if the execution of malloc is interrupted by that very
handler? The library function would have to be capable of guarding
against such reentrancy; if it is arbitrarily interrupted, its
internal variables could be in such a state that they are not ready to
handle a new malloc request. Linked list updates may be half done,
etc.

Embedded systems that support the writing of interrupt service
routines will provide you a documented set of rules for obtaining
memory. For example, in the Linux kernel, you can use special flags
when calling the kernel allocator to request ``atomic memory''; this
memory comes from a special, usually small pool reserved for
interrupts. Requests to that memory do not block; if the pool is
temporarily exhausted, the allocation fails even if there is lots of
memory elsewhere. The size of the pool can be adjusted dynamically.

Quote:
> Also, I was told not to call printf() in ISR since it is not
> re-entrant. I am still not quite sure of what re-entrant meant? and
> How do you tell whether a function is re-entrant or not?

You define what reentrant means, and then you evaluate the semantics
of the code with respect to those criteria.

Reentrancy is rarely absolute; it means that the execution of
something can be interleaved with, or happen concurrently with,
something else, meeting some criteria that you care about. At the
heart of all reentrancy is some lower level that is non-reentrant: for
instance, your CPU probably has only one global set of registers,
which have to be multiplexed by saving and restoring to create the
illusion of reentrancy at a higher programming level.



Sat, 23 Apr 2005 03:53:05 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. dynamic allocation

2. Multi-dimensional array using dynamic memory allocation

3. C Dynamic allocation of Pointers

4. Dynamic array allocation;

5. char **info; /*dynamic allocation*/

6. Need help with dynamic memory allocation (malloc) in C

7. Dynamic Allocation of Multi-Dimensional Array (Simple Question)

8. dynamic memory allocation

9. memory allocation / "dynamic arrays" / portability

10. Help Please!(linked lists/dynamic memory allocation etc.)

11. Dynamic Allocation

12. Dynamic allocation!

 

 
Powered by phpBB® Forum Software