-1

I have a macro as follow.

#define WR_BLK_SIZE_REGINFO 123

Am using this macro in a library project(.a). Library projects are pre-compiled where the size of "WR_BLK_SIZE_REGINFO" cannot be changed as it is statically allocated in the library project.

Now how can I dynamically allocate preprocessor directives code?

I want something like below. Is that possible?

#define WR_BLK_SIZE_REGINFO malloc

Let me brief out more if confused.

- What for this macro is used?

Ans: This macro is used in various files of my library project. For reducing code complexity I am using a macro to fix a size

  • Why do you want to malloc the macro?

Ans: Am doing a library project, where this library projects will be used in many controllers. Where each controller has its own memory set. So I can't fix the size of that macro statically. As it needs to be varied based on controllers memory.

Demonstrate some code?

Sample.h

#define WR_BLK_SIZE_REGINFO 123

sample.c

if( !((numItems >= 1) && (numItems <= WR_BLK_SIZE_REGINFO)) )
{
    // logic            
}

sample1.c

if( !((numItemsWrite >= 1) && (numItemsWrite <= WR_BLK_SIZE_REGINFO)) )
{
    // logic    
}

Note1: all the above files are library files which are pre-compiled and "123" is fixed here, which I don't want it to be fixed. I want to allocate dynamically.

Note2: Am not sure whether malloc is the right choice. All I need is I wanted to input "123" at run time in the library project or during compile time of application project(Input 123 from application project to library project)

Note3: Library project are built using MPLAB X IDE, basically it is a microchip project.

kaviarasan
  • 115
  • 2
  • 13
  • Why not just `int WR_BLK_SIZE_REGINFO = 123;`? – David Schwartz Jan 21 '17 at 08:09
  • I need macro since WR_BLK_SIZE_REGINFO has been used at 1000's of place and into different files at least approx 10-15 files – kaviarasan Jan 21 '17 at 08:10
  • 2
    That works the same as the macro, doesn't it? That sample code you showed should still work. Of course any code that assumes WR_BLK_SIZE_REGINFO is a compile time constant will have to rewritten, but that's unavoidable. – David Schwartz Jan 21 '17 at 08:13
  • This makes sense. Still, what's your take on malloc w.r.t. int WR_BLK_SIZE_REGINFO = 123; can't we dynamically allocate this 123 using a compile time? – kaviarasan Jan 21 '17 at 08:19
  • 1
    Honestly, I have no idea what you're talking about. You have some space that stores the old value where code looks to get that value. You want it to look the same way and get the new value. So you need to change the value stored (in the place where they're looking) from the old value to the new value. Where does allocation come into it? – David Schwartz Jan 21 '17 at 08:21

1 Answers1

1

In sample.h:

int get_wr_blk_size(void);
#define WR_BLK_SIZE_REGINFO get_wr_blk_size()

In some C file somewhere

static int wr_blk_size = 123;
static int set_wr_blk_size(int j) { wr_blk_size = j; }
int get_wr_blk_size(void) { return wr_blk_size; }

Note that any code that assumes WR_BLK_SIZE_REGINFO is a compile-time constant will have to be rewritten since it isn't anymore.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278