Showing posts with label c program. Show all posts
Showing posts with label c program. Show all posts

Saturday, January 14, 2023

(Programming) Combining the constant and volatile keywords in C

Sometimes asked as an interview question in embedded programming, this summary article covers an interesting use case of combining two seemingly opposite keywords in C [1].

Here is a TLDR of what this means:
1. int volatile x; Tells the compiler that x can change independent of the code e.g a register change that happens in Hardware.
2. uint16_t const x; Is used to tell the compiler that the variable x is immutable.
3. So when can we end up using these together? The article [1] talks about 3 cases, but they are essentially the same thing. We can use these keywords together for a pointer where the address of the pointer does not change (constant) but the value contained at that address is volatile (e.g. a register, shared memory etc). 
e.g. Constant address of volatile hardware register / memory. uint8_t volatile * const p_ptr; i.e the p_ptr has a constant address for a volatile (register).
 
Reference:
[1] Embedded.com. Michael Barr Combining C's volatile and constant 

Monday, May 3, 2021

Linux: Why do we need an executable stack with nested functions in GCC

 

  • The stack needs to be writable because the trampoline code is written on the stack which needs to be executed (to jump to the nested function). 
A nested function has no linkage by itself. So the trampoline code actually ensures two things:
  1. The outer () functions stack frame is available in the nested function
  2. Jump and execute code from the nested function.
Some other notes:
  • Nested functions are not a part of ANSI C, however, they are part of Gnu C.
  • The only reason nested functions are possibly useful because they use common stack variables.

Friday, April 30, 2021

Math: Simple technique to implement moving averages in Java, C or C++

 The oneline solution is:

accumulator = (alpha * new_value) + (1.0 - alpha) * accumulator
Here,

Accummulator - holds the value being tracked
alpha - value between 0 and 1.

The more aggressive the alpha (closer to 1) the  faster the moving average adapts to the recent values. This is an exponential moving average.

Monday, November 23, 2020

Linux: Comma separated arguments in an If Statement

 What happens when you write code like this:

    if ((x,y) == true) {

Is it even a legal condition to put in? If yes, why would you use it?

We have an example you could try out:

bash-4.1$ cat test.cc 
#include <iostream>
using namespace std;
int main() {
    int x = 1, y =0;
    if ((x,y) == true) {
        cout << “X TRUE PATH" << endl;
    } else {
        cout << “Y FALSE PATH" << endl;
    }
}

Thursday, June 4, 2020

Linux: Why is skb recycling done

Why is this done?
* Saves the cost of allocating and de-allocating memory repeatedly.
* Savings are significant because this is a very frequent operation (usually skb alloc and de-alloc is done on a per-packet basis).

Recent changes to SKB recycling:

"- Make skb recycling available to all drivers, without needing driver
  modifications.

- Allow recycling skbuffs in more cases, by having the recycle check
  in __kfree_skb() instead of in the ethernet driver transmit
  completion routine.  This also allows for example recycling locally
  destined skbuffs, instead of only recycling forwarded skbuffs as
  the transmit completion-time check does.

- Allow more consumers of skbuffs in the system use recycled skbuffs,
  and not just the rx refill process in the driver.

Saturday, May 23, 2020

Linux: Where to use packed structures

Not sure if the structure should be packed or not? When to use one vs the other? What is the default behavior? What do they do?

For answering these questions, please download this PDF tutorial.

Typically this stems from the question, "what is the size of this struct"?

struct test_A
{
char a;
int b;
char c;
} x;
PDF Download

Citation:
Gautam Bhanage, "Padding versus Packing in Embedded Systems Programming", Published online at www.bhanage.com. May 2020. [PDF]

Sunday, January 5, 2020

Different ways to print Linux kernel symbols instead of addresses

Cheatsheet:

%pF versatile_init+0x0/0x110
%pf versatile_init
%pS versatile_init+0x0/0x110
%pSR versatile_init+0x9/0x110
(with __builtin_extract_return_addr() translation)
%ps versatile_init
%pB prev_fn_of_versatile_init+0x88/0x88

Monday, October 28, 2019

How to use IS_ERR and PTR_ERR? What do they mean?

From the kernel definition there are three macros:
  1. IS_ERR - used to check, Returns non-0 value if the ptr is an error. Otherwise 0 if it’s not an error.
  2. PTR_ERR - used to print. Current value of the pointer.
  3. IS_ERR_VALUE - is explained a little bit more detail here1.
I find this the most useful for kernel space programming. Used as follows- if ptr is the pointer you want to check then use it as follows:
if (IS_ERR(ptr))
     printk("Error here: %ld", PTR_ERR(ptr));

Tuesday, June 18, 2019

Linux: Sleeping while atomic during kmalloc solved

So you ran into a crash where the kernel complains that you were sleeping while atomic.
You were either able to run objdump or gdb and track down that the crash is caused due
to a kmalloc? How is that possible?
Did you make recent changes which included putting in a kmalloc?