2015-06-09 12:10:20 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2015 Kaspar Schleiser <kaspar@schleiser.de>
|
|
|
|
*
|
|
|
|
* This file is subject to the terms and conditions of the GNU Lesser
|
|
|
|
* General Public License v2.1. See the file LICENSE in the top level
|
|
|
|
* directory for more details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @{
|
|
|
|
*
|
|
|
|
* @file
|
2019-09-14 15:47:10 +02:00
|
|
|
* @brief Bitfield auxiliary functions
|
2015-06-09 12:10:20 +02:00
|
|
|
*
|
|
|
|
* @author Kaspar Schleiser <kaspar@schleiser.de>
|
|
|
|
*
|
|
|
|
* @}
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include "bitfield.h"
|
|
|
|
#include "irq.h"
|
|
|
|
|
2022-03-01 00:33:51 +01:00
|
|
|
int bf_get_unset(uint8_t field[], size_t size)
|
2015-06-09 12:10:20 +02:00
|
|
|
{
|
|
|
|
int result = -1;
|
|
|
|
int nbytes = (size + 7) / 8;
|
2022-03-01 00:33:51 +01:00
|
|
|
size_t i = 0;
|
2015-06-09 12:10:20 +02:00
|
|
|
|
2016-03-19 09:25:47 +01:00
|
|
|
unsigned state = irq_disable();
|
2015-06-09 12:10:20 +02:00
|
|
|
|
|
|
|
/* skip full bytes */
|
|
|
|
for (int j = 0; (j < nbytes) && (field[j] == 255); j++) {
|
|
|
|
i += 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (; i < size; i++) {
|
|
|
|
if (!bf_isset(field, i)) {
|
|
|
|
bf_set(field, i);
|
|
|
|
result = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-19 09:25:47 +01:00
|
|
|
irq_restore(state);
|
2022-03-01 00:33:51 +01:00
|
|
|
return result;
|
2015-06-09 12:10:20 +02:00
|
|
|
}
|