Home > database >  What is the exact usage of custom code sections in memory apart from default sections (bss, data, et
What is the exact usage of custom code sections in memory apart from default sections (bss, data, et

Time:07-13

I could see code snippets having #pragma ghs section bss=".mysection". I heard that these kind of custom code segments will improve execution/performance in a way. But I don't get it exactly how it functions internally & also how it contributes to the execution/performance. Thanks in advance.

CodePudding user response:

This is a micro-optimization which is rarely beneficial.

In short, caches can benefit from "locality of reference". Data that is used together should be stored together, for faster speed of access. The same goes for code. You can override the compilers choice by putting some functions together in a single code segment. If you guess better than the compiler, you could see a performance increase. But if your guess is worse, the performance will decrease. And compiler vendors generally have more experience than you do.

CodePudding user response:

Here are some examples of why I've used custom sections on my embedded microcontroller projects. It's not always for improved execution/performance.

  1. Microcontrollers often execute code from flash and sometimes access to flash is slower than RAM so I have created a custom section in RAM to contain a copy of a portion of code (such as an interrupt handler) that I want to execute as fast as possible.
  2. Flash can be erased and reprogrammed by the microcontroller but sometimes the microcontroller cannot execute code from the flash while the flash is being erased/reprogrammed. So I create a custom section to contain a RAM copy of the code that performs the flash erasing/reprogramming.
  3. Sometimes I want to store configuration or log data in nonvolatile flash memory. I want the data located at a fixed known location so that subsequent application revisions always know where to read the data from. I create a custom section to contain the configuration/log data at a known fixed location.
  4. Sometimes I want to store some data at a known fixed location so that the data can be accessed by multiple programs such as the bootloader and the main application. I'll create a custom section to contain the shared data at a fixed location.
  5. Sometimes microcontroller peripherals such as a DMA controller have strict alignment requirements such as the buffer must be 256-byte aligned or whatever. Or maybe the microcontroller has multiple RAM regions with different performance levels or peripheral accessibility. I'll create a custom section to locate the DMA buffer in desired RAM region with the required alignment.

There are probably more examples that I'm not thinking of. Basically anytime I want to control the location of some code or data, and I don't want to leave it up to the whim of the linker, then I create a custom section to force the code/data into the location that I want.

  • Related