GithubHelp home page GithubHelp logo

Comments (9)

SG7 avatar SG7 commented on July 19, 2024

I made the following changes to the code:

#define CIRC_GBUF_DEF(t,x,s)                   \
  CIRC_GBUF_V_DEF(t,x,s)                     \
  static inline int x##_push_refd(t * pt)    \
  {                                          \
      return circ_gbuf_push(&x,pt);          \
  }                                          \
  static inline int x##_pop_refd(t * pt)     \
  {                                          \
      return circ_gbuf_pop(&x,pt,0);         \
  }                                          \
  static inline int x##_peek_refd(t * pt)    \
  {                                          \
      return circ_gbuf_pop(&x,pt,1);         \
  }

However, those changes prevents me to declare the circular buffer on the stack:

int main()
{
    CIRC_GBUF_DEF(struct my_struct, my_circ_buf, 10);
...

because of:

Compilation failed due to following error(s). main.c: In function ‘zero_pad’:
main.c:63:13: warning: implicit declaration of function ‘strnlen’ [-Wimplicit-function-declaration]
     for(i = strnlen(s,len); i<len; i++)
             ^
main.c: In function ‘main’:
main.c:148:37: error: invalid storage class for function ‘my_circ_buf_push_refd’
     CIRC_GBUF_DEF(struct my_struct, my_circ_buf, 10);
                                     ^
main.c:30:23: note: in definition of macro ‘CIRC_GBUF_DEF’
     static inline int x##_push_refd(t * pt)    \
                       ^
main.c:148:37: error: invalid storage class for function ‘my_circ_buf_pop_refd’
     CIRC_GBUF_DEF(struct my_struct, my_circ_buf, 10);
                                     ^
main.c:34:23: note: in definition of macro ‘CIRC_GBUF_DEF’
     static inline int x##_pop_refd(t * pt)     \
                       ^
main.c:148:37: error: invalid storage class for function ‘my_circ_buf_peek_refd’
     CIRC_GBUF_DEF(struct my_struct, my_circ_buf, 10);
                                     ^
main.c:38:23: note: in definition of macro ‘CIRC_GBUF_DEF’
     static inline int x##_peek_refd(t * pt)    \
                       ^

BTW, I am not found of any macros in the first place. Maybe an alternative solution (without any macros) could be proposed.

from c-utils.

sidcha avatar sidcha commented on July 19, 2024

@fovea1959, Yes. static inline is required when you compile with -std=c99 but prevents generated methods from being extern-ed. The better approach is to get rid of the inline itself.

@SG7, The macro CIRC_GBUF_DEF is never meant to be used declare circular buffers in stack (they generate functions; functions cannot be within functions in C). These macros are designed for a specific purpose: allow type checking before push/pop. Contrary to the common belief, macros aren't entirely bad; this is actually a very good usage of macros to make code safe and readable. It is very easy to split CIRC_GBUF_DEF into 2 parts, one that defines the functions and another that defines the data members (which you can use in stack).

But, just wondering, what would you do with a circular buffer in stack? in most use cases, you would need them to be available to two (or more) distinct code paths.

from c-utils.

fovea1959 avatar fovea1959 commented on July 19, 2024

remove the 'inline' altogether?

from c-utils.

sidcha avatar sidcha commented on July 19, 2024

Yes. That or add static if you don't plan on extern-ing the circular buffer elsewhere.

from c-utils.

SG7 avatar SG7 commented on July 19, 2024

The macro CIRC_GBUF_DEF works very well on the stack:

...
struct my_struct {
    int a;
    unsigned int b;
};

int main()
{
    CIRC_GBUF_DEF(struct my_struct, my_circ_buf, 10);
    circ_gbuf_t *p = &my_circ_buf;
    
/* Now, I can pass pointer to my_circ_buf to other functions or threads */
 ....   
}

That is not possible if we add static to the inline functions. The program would not compile.

from c-utils.

sidcha avatar sidcha commented on July 19, 2024

Okay. I think this is some compiler extension. I'm pretty sure C standards don't allow it. In any case, it's not recommended as it's not potable.

from c-utils.

SG7 avatar SG7 commented on July 19, 2024

@cbsiddharth You are very right.

The CIRC_GBUF_DEF(struct my_struct, my_circ_buf, 10) on the stack is not portable, gcc compilers may tolerate it with the warning: ISO C forbids nested functions [-Wpedantic] .

The macro CIRC_GBUF_V_DEF works better (no warnings):

struct my_struct {
    int a;
};

int main()
{
    CIRC_GBUF_V_DEF(struct my_struct, my_circ_buf, 10)
    struct my_struct m;

    circ_gbuf_t *p = &my_circ_buf;
    m.a = 1;

    circ_gbuf_push(p, &m); /* CIRC_GBUF_PUSH(my_circ_buf,&m) cannot be used */

   return 0;
}

Unfortunately, the macros cannot be used anymore with this approach.

It is very easy to split CIRC_GBUF_DEF into 2 parts, one that defines the functions and another that defines the data members (which you can use in stack).

That would be very beneficial!

Could you please show how to do it?

from c-utils.

sidcha avatar sidcha commented on July 19, 2024

Sure. I will implement and then close this issue.

from c-utils.

sidcha avatar sidcha commented on July 19, 2024

@SG7, to allow circular buffers to be declared in stack, I must discard type checking of the structure being pushed into the circular buffer. IMHO, type checking is a far more important feature to loose.

from c-utils.

Related Issues (15)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.