Quality RTOS & Embedded Software

 Real time embedded FreeRTOS RSS feed 
Quick Start Supported MCUs PDF Books Trace Tools Ecosystem


question: macro or function?

Posted by Steven on August 6, 2010
Why is portSAVE_CONTEXT() implemented as a macro, and not as a function?

I am going to use a PIC18F4550 but I was thinking: if I use a function instead of a macro, I can save a little bit RAM (pic18f4550 doesnt have much RAM), what do you think?

RE: question: macro or function?

Posted by woops_ on August 6, 2010
Saving and restoring must usually be inline code to get the stack frame right. Function calls include hidden code.

RE: question: macro or function?

Posted by Richard Damon on August 6, 2010
If portSAVE_CONTEXT was a function, then when it returned it would have to "unsave" the context to get to the return address, in one sense, portYIELD does that, save context, swap context, restore new context, and all works just fine. It is some what tricky programming (if it is possible) to make a function that you can call that returns to you and in the process saves the processor state on the stack, and even if you could write it, changing the stack in this manner in arbitrary places could give the compiler problems. This is one reason why the rules for how you right your ISRs can be very precise and tricky, as the save context macro needs to know the conditions it is being used to make things work.

Another issue is that it really wouldn't save that much program memory, as there are normally only a few calls to this macro. As I mentioned, portYEILD does, and in some port, the interrupt handlers will call it. (on others the interrupt handlers call portYIELD to save the context).

RE: question: macro or function?

Posted by Steven on August 7, 2010
ok, now i see, thank you all!

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

Copyright (C) Amazon Web Services, Inc. or its affiliates. All rights reserved.

Latest News

NXP tweet showing LPC5500 (ARMv8-M Cortex-M33) running FreeRTOS.

Version 10.1.1 of the FreeRTOS kernel is available for immediate download. MIT licensed.

View a recording of the "OTA Update Security and Reliability" webinar, presented by TI and AWS.


FreeRTOS and other embedded software careers at AWS.

FreeRTOS Partners

ARM Connected RTOS partner for all ARM microcontroller cores

Espressif ESP32

IAR Partner

Microchip Premier RTOS Partner

RTOS partner of NXP for all NXP ARM microcontrollers


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

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

OpenRTOS and SafeRTOS

Xilinx Microblaze and Zynq partner