mirror of
https://github.com/torvalds/linux.git
synced 2024-12-29 22:31:32 +00:00
fa226f1d81
The current codebase makes use of the zero-length array language
extension to the C90 standard, but the preferred mechanism to declare
variable-length types such as these ones is a flexible array member[1][2],
introduced in C99:
struct foo {
int stuff;
struct boo array[];
};
By making use of the mechanism above, we will get a compiler warning
in case the flexible array does not occur last in the structure, which
will help us prevent some kind of undefined behavior bugs from being
inadvertently introduced[3] to the codebase from now on.
Also, notice that, dynamic memory allocations won't be affected by
this change:
"Flexible array members have incomplete type, and so the sizeof operator
may not be applied. As a quirk of the original implementation of
zero-length arrays, sizeof evaluates to zero."[1]
This issue was found with the help of Coccinelle.
[1] https://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html
[2] https://github.com/KSPP/linux/issues/21
[3] commit 7649773293
("cxgb3/l2t: Fix undefined behaviour")
Link: https://lkml.kernel.org/r/20200221150612.GA9717@embeddedor
Signed-off-by: Gustavo A. R. Silva <gustavo@embeddedor.com>
Signed-off-by: Vasily Gorbik <gor@linux.ibm.com>
113 lines
2.5 KiB
C
113 lines
2.5 KiB
C
// SPDX-License-Identifier: GPL-2.0
|
|
/*
|
|
* Copyright IBM Corp. 2007, 2012
|
|
* Author(s): Peter Oberparleiter <peter.oberparleiter@de.ibm.com>
|
|
*/
|
|
|
|
#include <linux/vmalloc.h>
|
|
#include <linux/bitmap.h>
|
|
#include <linux/bitops.h>
|
|
#include "idset.h"
|
|
#include "css.h"
|
|
|
|
struct idset {
|
|
int num_ssid;
|
|
int num_id;
|
|
unsigned long bitmap[];
|
|
};
|
|
|
|
static inline unsigned long bitmap_size(int num_ssid, int num_id)
|
|
{
|
|
return BITS_TO_LONGS(num_ssid * num_id) * sizeof(unsigned long);
|
|
}
|
|
|
|
static struct idset *idset_new(int num_ssid, int num_id)
|
|
{
|
|
struct idset *set;
|
|
|
|
set = vmalloc(sizeof(struct idset) + bitmap_size(num_ssid, num_id));
|
|
if (set) {
|
|
set->num_ssid = num_ssid;
|
|
set->num_id = num_id;
|
|
memset(set->bitmap, 0, bitmap_size(num_ssid, num_id));
|
|
}
|
|
return set;
|
|
}
|
|
|
|
void idset_free(struct idset *set)
|
|
{
|
|
vfree(set);
|
|
}
|
|
|
|
void idset_fill(struct idset *set)
|
|
{
|
|
memset(set->bitmap, 0xff, bitmap_size(set->num_ssid, set->num_id));
|
|
}
|
|
|
|
static inline void idset_add(struct idset *set, int ssid, int id)
|
|
{
|
|
set_bit(ssid * set->num_id + id, set->bitmap);
|
|
}
|
|
|
|
static inline void idset_del(struct idset *set, int ssid, int id)
|
|
{
|
|
clear_bit(ssid * set->num_id + id, set->bitmap);
|
|
}
|
|
|
|
static inline int idset_contains(struct idset *set, int ssid, int id)
|
|
{
|
|
return test_bit(ssid * set->num_id + id, set->bitmap);
|
|
}
|
|
|
|
static inline int idset_get_first(struct idset *set, int *ssid, int *id)
|
|
{
|
|
int bitnum;
|
|
|
|
bitnum = find_first_bit(set->bitmap, set->num_ssid * set->num_id);
|
|
if (bitnum >= set->num_ssid * set->num_id)
|
|
return 0;
|
|
*ssid = bitnum / set->num_id;
|
|
*id = bitnum % set->num_id;
|
|
return 1;
|
|
}
|
|
|
|
struct idset *idset_sch_new(void)
|
|
{
|
|
return idset_new(max_ssid + 1, __MAX_SUBCHANNEL + 1);
|
|
}
|
|
|
|
void idset_sch_add(struct idset *set, struct subchannel_id schid)
|
|
{
|
|
idset_add(set, schid.ssid, schid.sch_no);
|
|
}
|
|
|
|
void idset_sch_del(struct idset *set, struct subchannel_id schid)
|
|
{
|
|
idset_del(set, schid.ssid, schid.sch_no);
|
|
}
|
|
|
|
/* Clear ids starting from @schid up to end of subchannel set. */
|
|
void idset_sch_del_subseq(struct idset *set, struct subchannel_id schid)
|
|
{
|
|
int pos = schid.ssid * set->num_id + schid.sch_no;
|
|
|
|
bitmap_clear(set->bitmap, pos, set->num_id - schid.sch_no);
|
|
}
|
|
|
|
int idset_sch_contains(struct idset *set, struct subchannel_id schid)
|
|
{
|
|
return idset_contains(set, schid.ssid, schid.sch_no);
|
|
}
|
|
|
|
int idset_is_empty(struct idset *set)
|
|
{
|
|
return bitmap_empty(set->bitmap, set->num_ssid * set->num_id);
|
|
}
|
|
|
|
void idset_add_set(struct idset *to, struct idset *from)
|
|
{
|
|
int len = min(to->num_ssid * to->num_id, from->num_ssid * from->num_id);
|
|
|
|
bitmap_or(to->bitmap, to->bitmap, from->bitmap, len);
|
|
}
|