Once again, Im not a developer but a wood worker so my questions might be, well, stupid.
I forgot something really important I've to use gcc-3.8 to compile as the original code I'm working with can't compile with newer version. I totaly forgot to talk about that, sorry.
I'm sending data from a tool to an autonomous robot. Robot receive data as unsigned char* I read a lot and it seems that malloc isn't interrupt safe.
As the robot could do bad & dangerous things, I try to make every parts safe (at least as much as I can).
This malloc happens after an interrupt raised by data received. This code segment is now giving me an hard time to make it safe, also my syntax is probably bad..
char* _Xp = (char*) malloc(strlen((char*)_X)*sizeof(char));
strcpy(_Xp, (char*)_X);
1) is malloc really not interrupt safe? information I found are from 2004.
2) is there a more efficient way to initialize the buffer?
3) why are unsigned char "bad"? (read something about that).
4) last question is strcpy also not interrupt safe? sources I read differ on that point.
===== answering some questions:
Robot doesn't have operating system, target is an STR911FFM44 at 25Mhz (if it helps)
Input arrays are all nul-terminated.
Code is not in the interruption handler but in the infinite loop and processed only if the IrHandler as set the flag for it.
I don't know the rate of the data stream to "hard code" the safety. but interruption should be in [500ms to 1500ms].
_Xis nul-terminated, do you? Also, there is NO need to cast the return ofmalloc, it is unnecessary. See: Do I cast the result of malloc? for thorough explanation. - David C. Rankinmalloc()is one of the least interrupt-safe routines around. (2) Don't do the memory allocation in the interrupt handler. In your main loop, spot that an interrupt occurred so there is data to read and then allocate the buffer and read the data. (3) There's no particular reason to avoidunsigned charif that's the type you need to work with. The only reason for caution is that the library functions almost all takechar *and notunsigned char *, so usingunsigned charcan make things verbose. - Jonathan Leffler:)and if @A.albin writes the first bit of code to auto-square my Beismeir Fence System, I'll be the first to use it... - David C. Rankin