Skip to content

Commit 2a01e41

Browse files
andreittrunikraft-bot
authored andcommitted
{include,lib/*}: Split refcount initializers from values
Previously refcount types defined in uk/refcount.h and uk/swrefcount.h provided initializer macros in the form of anonymous struct values (i.e. designated initializers cast to the struct type). This prevented them from being used to initialize static variables with nested structs on some compilers, most notably GCC up to version 12. This change separates initializers from initial values as two different macros, the first to be used for initializing (static) variables, with the second meant for direct assignment to already declared structs. In addition, all uses of refcount initializers in variable assignments have been changed to correctly use initial value macros. Signed-off-by: Andrei Tatar <[email protected]> Reviewed-by: Maria Sfiraiala <[email protected]> Reviewed-by: Marco Schlumpp <[email protected]> Approved-by: Razvan Deaconescu <[email protected]> GitHub-Closes: unikraft#1245
1 parent 24e3d8a commit 2a01e41

File tree

8 files changed

+23
-9
lines changed

8 files changed

+23
-9
lines changed

include/uk/refcount.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,13 @@ extern "C" {
5656
} while (0)
5757
#endif /* CONFIG_LIBUKDEBUG */
5858

59-
#define UK_REFCOUNT_INITIALIZER(val) ((__atomic){ .counter = (val) })
59+
/*
60+
* We define initializers separate from an initial values.
61+
* The former can only be used in (static) variable initializations, while the
62+
* latter is meant for assigning to variables or as anonymous data structures.
63+
*/
64+
#define UK_REFCOUNT_INITIALIZER(val) { .counter = (val) }
65+
#define UK_REFCOUNT_INIT_VALUE(val) ((__atomic)UK_REFCOUNT_INITIALIZER(val))
6066

6167
/**
6268
* Initialize the atomic reference.

include/uk/weak_refcount.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,17 @@ struct uk_swrefcount {
1717
__atomic strong; /* Number of strong references; <= .refcount */
1818
};
1919

20-
#define UK_SWREFCOUNT_INITIALIZER(r, s) ((struct uk_swrefcount){ \
20+
/*
21+
* We define initializers separate from an initial values.
22+
* The former can only be used in (static) variable initializations, while the
23+
* latter is meant for assigning to variables or as anonymous data structures.
24+
*/
25+
#define UK_SWREFCOUNT_INITIALIZER(r, s) { \
2126
.refcount = UK_REFCOUNT_INITIALIZER((r)), \
2227
.strong = UK_REFCOUNT_INITIALIZER((s)), \
23-
})
28+
}
29+
#define UK_SWREFCOUNT_INIT_VALUE(r, s) \
30+
((struct uk_swrefcount)UK_SWREFCOUNT_INITIALIZER((r), (s)))
2431

2532
/**
2633
* Initialize refcount with `ref` references, of which `strong` are strong.

lib/posix-eventfd/eventfd.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ struct uk_file *uk_eventfile_create(unsigned int count, int flags)
137137
al->alloc = a;
138138
al->counter = count;
139139
al->fstate = UK_FILE_STATE_INITIALIZER(al->fstate);
140-
al->frefcnt = UK_FILE_REFCNT_INITIALIZER;
140+
al->frefcnt = UK_FILE_REFCNT_INIT_VALUE;
141141
al->f = (struct uk_file){
142142
.vol = vol,
143143
.node = &al->counter,

lib/posix-pipe/pipe.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -292,8 +292,8 @@ int uk_pipefile_create(struct uk_file *pipes[2], int flags)
292292
al->node.whead = 0;
293293
memset(al->node.buf, 0, sizeof(al->node.buf));
294294
al->fstate = UK_FILE_STATE_INITIALIZER(al->fstate);
295-
al->rref = UK_FILE_REFCNT_INITIALIZER;
296-
al->wref = UK_FILE_REFCNT_INITIALIZER;
295+
al->rref = UK_FILE_REFCNT_INIT_VALUE;
296+
al->wref = UK_FILE_REFCNT_INIT_VALUE;
297297
al->rf = (struct uk_file){
298298
.vol = PIPE_VOLID,
299299
.node = &al->node,

lib/posix-poll/epoll.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ struct uk_file *uk_epollfile_create(void)
368368
al->alloc = a;
369369
al->list = NULL;
370370
al->fstate = UK_FILE_STATE_INITIALIZER(al->fstate);
371-
al->frefcnt = UK_FILE_REFCNT_INITIALIZER;
371+
al->frefcnt = UK_FILE_REFCNT_INIT_VALUE;
372372
al->f = (struct uk_file){
373373
.vol = EPOLL_VOLID,
374374
.node = &al->list,

lib/posix-socket/socket.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ static void _socket_init(struct socket_alloc *al,
230230
.driver = d
231231
};
232232
al->fstate = UK_FILE_STATE_INITIALIZER(al->fstate);
233-
al->fref = UK_FILE_REFCNT_INITIALIZER;
233+
al->fref = UK_FILE_REFCNT_INIT_VALUE;
234234
al->f = (struct uk_file){
235235
.vol = POSIX_SOCKET_VOLID,
236236
.node = &al->node,

lib/posix-timerfd/timerfd.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ struct uk_file *uk_timerfile_create(clockid_t id)
225225
.upthread = NULL
226226
};
227227
al->fstate = UK_FILE_STATE_INITIALIZER(al->fstate);
228-
al->frefcnt = UK_FILE_REFCNT_INITIALIZER;
228+
al->frefcnt = UK_FILE_REFCNT_INIT_VALUE;
229229
al->f = (struct uk_file){
230230
.vol = TIMERFD_VOLID,
231231
.node = &al->node,

lib/ukfile/include/uk/file.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ struct uk_file {
133133

134134
/* Files always get created with one strong reference held */
135135
#define UK_FILE_REFCNT_INITIALIZER UK_SWREFCOUNT_INITIALIZER(1, 1)
136+
#define UK_FILE_REFCNT_INIT_VALUE UK_SWREFCOUNT_INIT_VALUE(1, 1)
136137

137138
/* Operations inlines */
138139
static inline

0 commit comments

Comments
 (0)