Comments (9)
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.
@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.
remove the 'inline' altogether?
from c-utils.
Yes. That or add static if you don't plan on extern-ing the circular buffer elsewhere.
from c-utils.
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.
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.
@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.
Sure. I will implement and then close this issue.
from c-utils.
@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)
- doesn't compile HOT 1
- This is test issue
- utils.c/hexdup() does not print string when length is exactly 16
- hash_map_free bug HOT 3
- The description for CIRC_GBUF_PEEK and CIRC_GBUF_POP is interchanged HOT 1
- FILO.c how to make it Circular HOT 1
- Using circular buff with a typedef HOT 1
- push_count and pop_count overflow HOT 10
- Are structures properly stored and retrieved? HOT 1
- warning: ISO C forbids nested functions HOT 2
- Purpose of zero_pad(tail, circ_buf->element_size); HOT 2
- circ_gbuf_free_space incorrect HOT 1
- Since `circ_gbuf_free_space` is not thread safe than`circ_gbuf_pop` and `circ_gbuf_pop` are also not thread safe. HOT 4
- Length Bug, or documentation bug HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from c-utils.