inline and static functions

A

Andrey Vul

I'm creating an object that looks like this:
file: reader.h
....
typedef struct reader { /* ... */ } reader_t;
....
reader_t *reader_xopen(const char * path, void *extbuf, int buflen);
reader_t *reader_fxopen(int fd, void *extbuf, int buflen);
reader_t *reader_open(const char *path);
reader_t *reader_fopen(int fd);
....

file: reader.c
static reader_t *
init_helper(const char *path, int fd, void *buf, int buflen, int ext)
{
reader_t *rdr = malloc(sizeof(reader_t));
...
return rdr;
}
inline reader_t *
reader_xopen(const char *path, void *extbuf, int buflen) {
return init_helper(path, -1, extbuf, buflen, extbuf != NULL);
}
inline reader_t *
reader_fxopen(int fd, void *extbuf, int buflen) {
return init_helper(NULL, fd, extbuf, buflen, extbuf != NULL);
}
inline reader_t *
reader_open(const char *path) {
return init_helper(path, -1, NULL, 0, 0);
}
inline reader_t *
reader_fopen(int fd) {
return init_helper(NULL, fd, NULL, 0, 0);
}

Will the following snippet cause undefined operation?
Should I un-inline the one-line wrapper functions?
 
A

Andrey Vul

I'm creating an object that looks like this:
file: reader.h
...
typedef struct reader { /* ... */ } reader_t;
...
reader_t *reader_xopen(const char * path, void *extbuf, int buflen);
reader_t *reader_fxopen(int fd, void *extbuf, int buflen);
reader_t *reader_open(const char *path);
reader_t *reader_fopen(int fd);
...

file: reader.c
static reader_t *
init_helper(const char *path, int fd, void *buf, int buflen, int ext)
{
        reader_t *rdr = malloc(sizeof(reader_t));
        ...
        return rdr;}

inline reader_t *
reader_xopen(const char *path, void *extbuf, int buflen) {
        return init_helper(path, -1, extbuf, buflen, extbuf != NULL);}

inline reader_t *
reader_fxopen(int fd, void *extbuf, int buflen) {
        return init_helper(NULL, fd, extbuf, buflen, extbuf != NULL);}

inline reader_t *
reader_open(const char *path) {
        return init_helper(path, -1, NULL, 0, 0);}

inline reader_t *
reader_fopen(int fd) {
        return init_helper(NULL, fd, NULL, 0, 0);

}

Will the following snippet cause undefined operation?
Should I un-inline the one-line wrapper functions?

Specifically, in this example:
file: foo.c
....
#include "reader.h"
....
int main(int argc, char **argv){
reader_t *rdr;
....
rdr = reader_open(argv[1]);
....
return 0;
}

Not including segfaults from going out-of-bounds in argv.
It's not important enough to use <assert.h> for this.
 
C

Chris McDonald

Andrey Vul said:
I'm creating an object that looks like this:
file: reader.h
...
typedef struct reader { /* ... */ } reader_t;
...
reader_t *reader_xopen(const char * path, void *extbuf, int buflen);
reader_t *reader_fxopen(int fd, void *extbuf, int buflen);
reader_t *reader_open(const char *path);
reader_t *reader_fopen(int fd);
...
file: reader.c
static reader_t *
init_helper(const char *path, int fd, void *buf, int buflen, int ext)
{
reader_t *rdr = malloc(sizeof(reader_t));
...
return rdr;
}
inline reader_t *
reader_xopen(const char *path, void *extbuf, int buflen) {
return init_helper(path, -1, extbuf, buflen, extbuf != NULL);
}
inline reader_t *
reader_fxopen(int fd, void *extbuf, int buflen) {
return init_helper(NULL, fd, extbuf, buflen, extbuf != NULL);
}
inline reader_t *
reader_open(const char *path) {
return init_helper(path, -1, NULL, 0, 0);
}
inline reader_t *
reader_fopen(int fd) {
return init_helper(NULL, fd, NULL, 0, 0);
}
Will the following snippet cause undefined operation?
Should I un-inline the one-line wrapper functions?


No snippets followed.

Is there a reason that the wrapper functions can't be preprocessor macros?
 
A

Andrey Vul

No snippets followed.
Snippet was second post.
Is there a reason that the wrapper functions can't be preprocessor macros?

Type safety.

Also, the wrapped function (init_helper()) is marked static in order
to encapsulate it.

OTOH, the linker would die saying it can't find a local function if
it's inlined to init_helper().
But the inline is only mentioned in the body, not the prototype.
The linker expects reader_open@4 (or whatever the C ABI uses). If
that's inlined, are results undefined when the function is called from
another file.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,151
Latest member
JaclynMarl
Top