Quality RTOS & Embedded Software

 Real time embedded FreeRTOS RSS feed 
Real time embedded FreeRTOS mailing list 
Quick Start Supported MCUs PDF Books Trace Tools Ecosystem TCP & FAT Training


XML parser

Posted by Marcel van Lieshout on August 31, 2013
Can anyone help me to find a small, simple XML-parser that can read and write from/to file AND does not use realloc() or alloca()?

RE: XML parser

Posted by Richard on August 31, 2013
In order that we can maintain the quality of this support service I would respectfully request that this forum is only used to post questions that are directly related to FreeRTOS.


RE: XML parser

Posted by Marcel van Lieshout on August 31, 2013
Of course. The absence of realloc() and alloca() is because FreeRTOS has currently no support for them. That's the link to FreeRTOS.

RE: XML parser

Posted by MEdwards on August 31, 2013
Use heap_3.c, it just wraps the standard library functions. You can wrap the calloc function in the same way.

RE: XML parser

Posted by Marcel van Lieshout on August 31, 2013
That would work. But degrades overall performance of all other tasks. I heavily use malloc() and free().

RE: XML parser

Posted by Richard Damon on September 1, 2013
If you use malloc and free in a multi-thread/task system, then malloc/free MUST be made aware of this as the wrapper does, otherwise you run the real risk of corrupting your heap. Some implementations will define hooks inside of these functions that you can define that might make the cost a bit smaller, but it still needs to be there.

RE: XML parser

Posted by Marcel van Lieshout on September 3, 2013
You are right. I forgot to mention that I use heap4.c for dynamice memory (de-)allocations.

RE: XML parser

Posted by Richard Damon on September 3, 2013
i\Is you code calling pvPortMalloc or malloc? If the latter, you are at risk of corrupting the heap unless you are guarding all calls yourself.

To get a calloc/realloc for the first case, either switch to heap3.c and add the simple to define functions, or write the corresponding functions for the heap4 implementation. calloc should be fairly simple, as it just needs a little math, call malloc, and then clear the buffer. realloc will take a bit more work, but there are plenty of examples of how to write a realloc around.

RE: XML parser

Posted by Marcel van Lieshout on September 3, 2013
I use the heap4 functions, not the standard malloc and free (by including the "-Dmalloc pvPortMalloc "-Dfree pvPortFree" on the commandline). My app is running nicely, so no problems there.

Calloc() is no issue. realloc() is doable, but what about alloca()? alloca() allocates the (variable length) memory on the stack instead of the FreeRTOS-heap.

RE: XML parser

Posted by Richard Damon on September 3, 2013
alloca() is a somewhat non-standard function (I don't think it is defined in the ISO standards), but by its general nature, is should be thread safe as it allocates from the stack and each thread has its own stack. The one danger is that you would need to know if the version of alloca (assuming it is provided by your implementation) is compatible with the way FreeRTOS moves the stack.

If your implementation does NOT provide an alloca(), then it may be possible to write one yourself, but it will need to be written in assembly, as it can not be written in C, and needs a fairly good understanding on how your compiler works, snd there will likely be strange restrictions on you code to make sure your program still works (declaring new variables after the alloca likely will not work right unless the compiler knows about alloca.)

Most programs that use alloca can be changed to use malloc, the biggest issue is finding where to put the free call, which is the best choice if alloca isn't supported for you.

RE: XML parser

Posted by Marcel van Lieshout on September 3, 2013
The GCC based compiler I use for PIC32 (XC32) has a alloca.h header which contains the following:
#define alloca(x)__builtin_alloca(x)

For temporary (variable size/number) allocations, alloca() would be very useful. It can help to eliminate fragmentation of the heap because these allocations are task-local. Long life allocations would still use the heap.

Any suggestions on how to determine whether the default alloca() is 100% compatible to FreeRTOS?

RE: XML parser

Posted by Richard Damon on September 3, 2013
The one thing I can think of is look at the generated assembly, and make sure that the code it generates doesn't include a check for the stack pointer compared to a global variable, if it does, then it likely won't work with FreeRTOS, if not it should. The problem is that alloca is often very simple, and if you ask for too much memory it just over runs the stack. This sort of operation should be compatible with FreeRTOS, as it doesn't us any knowledge of the programs memory layout that is changed by FreeRTOS. "Nicer" versions might detect the stack overflow, but these will get confused by FreeRTOS as the stack for a task isn't in the space the stack started out to be. The good news is that most alloca()s are the quick and dirty versions that don't error check.

It would make sense, at least for debugging, to make sure that you have turned on the FreeRTOS stack checks if you use alloca(), as it does make it much harder to estimate stack usage.

RE: XML parser

Posted by Marcel van Lieshout on September 4, 2013
Thank you!

I will look into this the next few days and report back.

[ Back to the top ]    [ About FreeRTOS ]    [ Sitemap ]    [ ]

Copyright (C) 2004-2010 Richard Barry. Copyright (C) 2010-2016 Real Time Engineers Ltd.
Any and all data, files, source code, html content and documentation included in the FreeRTOSTM distribution or available on this site are the exclusive property of Real Time Engineers Ltd.. See the files license.txt (included in the distribution) and this copyright notice for more information. FreeRTOSTM and FreeRTOS.orgTM are trade marks of Real Time Engineers Ltd.

Latest News:

FreeRTOS V9.0.0 is now available for download.

Free TCP/IP and file system demos for the RTOS

Sponsored Links

⇓ Now With No Code Size Limit! ⇓
⇑ Free Download Without Registering ⇑

FreeRTOS Partners

ARM Connected RTOS partner for all ARM microcontroller cores

Renesas Electronics Gold Alliance RTOS Partner.jpg

Microchip Premier RTOS Partner

RTOS partner of NXP for all NXP ARM microcontrollers

Atmel RTOS partner supporting ARM Cortex-M3 and AVR32 microcontrollers

STMicro RTOS partner supporting ARM7, ARM Cortex-M3, ARM Cortex-M4 and ARM Cortex-M0

Xilinx Microblaze and Zynq partner

Silicon Labs low power RTOS partner

Altera RTOS partner for Nios II and Cortex-A9 SoC

Freescale Alliance RTOS Member supporting ARM and ColdFire microcontrollers

Infineon ARM Cortex-M microcontrollers

Texas Instruments MCU Developer Network RTOS partner for ARM and MSP430 microcontrollers

Cypress RTOS partner supporting ARM Cortex-M3

Fujitsu RTOS partner supporting ARM Cortex-M3 and FM3

Microsemi (previously Actel) RTOS partner supporting ARM Cortex-M3

Atollic Partner

IAR Partner

Keil ARM Partner

Embedded Artists