Newer
Older
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
template <typename In_It, typename Fwd_It>
Error Decoder<In_It, Fwd_It>::add_packet (In_It &start, const In_It end)
{
// RFC packet, section 4.4.2 page 11
using T = typename std::iterator_traits<Fwd_It>::value_type;
// each packet has an header of 32 bits. We can not start writing the
// encoded symbols in the middle of an iterator yet.
if (sizeof(T) > sizeof(uint32_t) || sizeof(T) == 3) {
assert (false && "libRaptorQ: sorry, encde_packets can only be used "
"with types of at most 32 bits for now\n");
return Error::INITIALIZATION;
}
// first of all, check the packet length
size_t pkt_len;
if (std::is_same<typename std::iterator_traits<Fwd_It>::iterator_category,
std::random_access_iterator_tag>::value) {
// we were lucky with a random iterator.
pkt_len = sizeof(T) * (end - start);
} else {
pkt_len = 0;
auto start_copy = start;
while (start_copy != end) {
pkt_len += sizeof(T);
++start_copy;
}
}
if (pkt_len < (sizeof(uint32_t) + 1))
return Error::NEED_DATA;
const uint8_t *p = reinterpret_cast<const uint8_t*> (&*start);
uint32_t symbol_id;
uint8_t *p_out = reinterpret_cast<uint8_t*> (&symbol_id);
// manual loop unrolling ftw
*(p_out++) = *(p++);
if (sizeof(T) == 1) {
++start;
p_out = reinterpret_cast<uint8_t*> (&*start);
}
*(p_out++) = *(p++);
if (sizeof(T) == 2 || sizeof(T) == 1) {
++start;
p_out = reinterpret_cast<uint8_t*> (&*start);
}
*(p_out++) = *(p++);
if (sizeof(T) == 1) {
++start;
p_out = reinterpret_cast<uint8_t*> (&*start);
}
*(p_out++) = *(p++);
// sizeof(T) can only be 1,2,4, so we can safely just increment start here
++start;
constexpr uint32_t mask = ~(static_cast<uint32_t>(0xFF) << 24);
const uint32_t host_symbol_id = RaptorQ__v1::Impl::Endian::b_to_h<uint32_t>(
symbol_id);
const uint8_t sbn = host_symbol_id >> 24;
uint32_t symbol = host_symbol_id & mask;
bool only_source = symbol >= symbols (sbn);
pkt_len -= sizeof(uint32_t);
while (pkt_len > 0) {
// we can finally start reading the symbols
uint32_t symbol_length = _symbol_size;
if (only_source && sbn == (blocks() - 1) &&
symbol == (symbols (sbn) - 1)) {
// the last symbol might have much shorter size
// get the real symbol size
symbol_length = block_size (sbn) % _symbol_size;
if (symbol_length == 0)
symbol_length = _symbol_size;
}
if (pkt_len < symbol_length)
break;
auto cp_start = start;
auto err = add_symbol (cp_start, end, symbol, sbn);
if (err != Error::NONE && err != Error::NOT_NEEDED)
return Error::NONE;
start = cp_start;
pkt_len -= symbol_length;
++symbol;
if (only_source && symbol >= symbols (sbn))
return Error::NONE;
}
return Error::WRONG_INPUT;
}
template <typename In_It, typename Fwd_It>
std::vector<bool> Decoder<In_It, Fwd_It>::end_of_input (
const Fill_With_Zeros fill)
std::vector<bool> ret;
if (!operator bool() ||
(fill != Fill_With_Zeros::YES && fill != Fill_With_Zeros::NO)) {
return ret;
}
if (fill == Fill_With_Zeros::YES)
ret.resize (_size, false);
size_t ret_idx = 0;
std::unique_lock<std::mutex> pool_lock (*_pool_mtx);
std::unique_lock<std::mutex> dec_lock (_mtx);
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
for (uint8_t sbn = 0; sbn < blocks(); ++sbn) {
auto it = decoders.find (sbn);
if (fill == Fill_With_Zeros::YES) {
if (it == decoders.end()) {
// we might not even have he block for our end_of_input
bool success;
const uint16_t syms = this->symbols (sbn);
const Block_Size b_size = this->extended_symbols (sbn);
// we might have padding symbols. add thse to the esi.
const uint16_t padding = static_cast<uint16_t> (b_size) - syms;
std::tie (it, success) = decoders.emplace (std::make_pair(sbn,
Dec (b_size, _symbol_size, padding)));
assert (success);
}
auto real_symbols = it->second.dec->fill_with_zeros();
Impl::De_Interleaver<Fwd_It> de_interleaving (
it->second.dec->get_symbols(),
_sub_blocks,
symbols (sbn),
_alignment);
uint32_t block_bytes = block_size (sbn);
auto block_bitmask = de_interleaving.symbols_to_bytes (block_bytes,
std::move(real_symbols));
assert (ret.size() - ret_idx >= block_bitmask.size());
for (const auto block_bit : block_bitmask)
ret[ret_idx++] = block_bit;
}
if (it != decoders.end())
it->second.dec->end_of_input = true;
}
dec_lock.unlock();
pool_lock.unlock();
_pool_notify->notify_all();
}
template <typename In_It, typename Fwd_It>
std::vector<bool> Decoder<In_It, Fwd_It>::end_of_input (
const Fill_With_Zeros fill,
const uint8_t block)
std::vector<bool> ret;
if (!operator bool() ||
(fill != Fill_With_Zeros::YES && fill != Fill_With_Zeros::NO)) {
return ret;
}
std::unique_lock<std::mutex> pool_lock (*_pool_mtx);
std::unique_lock<std::mutex> dec_lock (_mtx);
auto it = decoders.find(block);
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
if (it == decoders.end()) {
// we might not even have he block for our end_of_input
bool success;
const uint16_t syms = this->symbols (block);
const Block_Size b_size = this->extended_symbols (block);
// we might have padding symbols. add thse to the esi.
const uint16_t padding = static_cast<uint16_t> (b_size) - syms;
std::tie (it, success) = decoders.emplace (std::make_pair(block,
Dec (b_size, _symbol_size, padding)));
assert (success);
}
std::vector<bool> symbol_bitmask;
if (fill == Fill_With_Zeros::YES)
symbol_bitmask = it->second.dec->fill_with_zeros();
it->second.dec->end_of_input = true;
Impl::De_Interleaver<Fwd_It> de_interleaving (
it->second.dec->get_symbols(),
_sub_blocks,
symbols (block),
_alignment);
uint32_t block_bytes = block_size (block);
ret = de_interleaving.symbols_to_bytes (block_bytes,
std::move(symbol_bitmask));
dec_lock.unlock();
pool_lock.unlock();
_pool_notify->notify_all();
return ret;
template <typename In_It, typename Fwd_It>
Decoder<In_It, Fwd_It>::Block_Work::~Block_Work()
{
// have we been called before the computation finished?
auto locked_dec = work.lock();
auto locked_notify = notify.lock();
auto locked_mtx = lock.lock();
std::unique_lock<std::mutex> p_lock (*locked_mtx);
RQ_UNUSED(p_lock);
template <typename In_It, typename Fwd_It>
Work_Exit_Status Decoder<In_It, Fwd_It>::Block_Work::do_work (
auto locked_notify = notify.lock();
auto locked_mtx = lock.lock();
std::unique_lock<std::mutex> p_lock (*locked_mtx, std::defer_lock);
p_lock.lock();
if (locked_dec->can_decode()) {
// check again to avoid race between threads
return Work_Exit_Status::REQUEUE;
} else {
locked_dec->drop_concurrent();
if (locked_dec->end_of_input && locked_dec->threads() == 0)
locked_notify->notify_all();
p_lock.unlock();
work.reset();
return Work_Exit_Status::DONE;
}
if (locked_dec->ready()) { // did an other thread stop us?
locked_dec->drop_concurrent();
work.reset();
return Work_Exit_Status::DONE;
return Work_Exit_Status::REQUEUE;
}
}
return Work_Exit_Status::DONE;
std::future<std::pair<Error, uint8_t>> Decoder<In_It, Fwd_It>::compute (
using ret_t = std::pair<Error, uint8_t>;
std::promise<ret_t> p;
bool error = !operator bool(); // test correct class initialization
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
// need some flags
if (flags == Compute::NONE)
error = true;
// flag incompatibilities
if (Compute::NONE != (flags & Compute::PARTIAL_FROM_BEGINNING) &&
(Compute::NONE != (flags & (Compute::PARTIAL_ANY |
Compute::COMPLETE |
Compute::NO_POOL)))) {
error = true;
} else if (Compute::NONE != (flags & Compute::PARTIAL_ANY) &&
(Compute::NONE != (flags & (Compute::PARTIAL_FROM_BEGINNING |
Compute::COMPLETE |
Compute::NO_POOL)))) {
error = true;
} else if (Compute::NONE != (flags & Compute::COMPLETE) &&
Compute::NONE != (flags &(Compute::PARTIAL_FROM_BEGINNING |
Compute::PARTIAL_ANY |
Compute::NO_POOL))) {
error = true;
}
if (Compute::NONE != (flags & Compute::NO_POOL)) {
std::unique_lock<std::mutex> lock (_mtx);
RQ_UNUSED(lock);
if (decoders.size() != 0) {
// You can only say you won't use the pool *before* you start
// decoding something!
error = true;
} else {
use_pool = false;
p.set_value ({Error::NONE, 0});
return p.get_future();
}
}
if (error) {
p.set_value ({Error::WRONG_INPUT, 0});
return p.get_future();
}
// do not add work to the pool to save up memory.
// let "add_symbol craete the Decoders as needed.
// spawn thread waiting for other thread exit.
// this way we can set_value to the future when needed.
auto future = p.get_future();
if (Compute::NONE != (flags & Compute::NO_BACKGROUND)) {
wait_threads (this, flags, std::move(p));
} else {
std::unique_lock<std::mutex> pool_wait_lock (*_pool_mtx);
RQ_UNUSED(pool_wait_lock);
pool_wait.emplace_back (wait_threads, this, flags, std::move(p));
}
return future;
void Decoder<In_It, Fwd_It>::wait_threads (Decoder<In_It, Fwd_It> *obj,
const Compute flags,
std::promise<std::pair<Error, uint8_t>> p)
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
std::unique_lock<std::mutex> lock (*obj->_pool_mtx);
if (obj->exiting) { // make sure we can exit
p.set_value ({Error::EXITING, 0});
break;
}
auto status = obj->get_report (flags);
if (Error::WORKING != status.first) {
p.set_value (status);
break;
}
_notify->wait (lock);
lock.unlock();
}
// delete ourselves from the waiting thread vector.
std::unique_lock<std::mutex> lock (*obj->_pool_mtx);
for (auto it = obj->pool_wait.begin(); it != obj->pool_wait.end(); ++it) {
if (it->get_id() == std::this_thread::get_id()) {
it->detach();
obj->pool_wait.erase (it);
break;
}
}
}
template <typename In_It, typename Fwd_It>
std::pair<Error, uint8_t> Decoder<In_It, Fwd_It>::get_report (
if (decoders.size() == 0)
return {Error::WORKING, 0};
if (Compute::COMPLETE == (flags & Compute::COMPLETE) ||
Compute::PARTIAL_FROM_BEGINNING ==
(flags & Compute::PARTIAL_FROM_BEGINNING)) {
uint16_t reportable = 0;
uint16_t next_expected = static_cast<uint16_t> (pool_last_reported + 1);
std::unique_lock<std::mutex> dec_lock (_mtx);
auto it = decoders.lower_bound (static_cast<uint8_t> (next_expected));
// get last reportable block
for (; it != decoders.end(); ++it) {
auto id = it->first;
if (id != next_expected)
break; // not consecutive
if (ptr == nullptr) {
assert(false && "RFC6330: decoder should never be nullptr.");
break;
}
if (!ptr->ready()) {
if (ptr->is_stopped())
return {Error::EXITING, 0};
if (ptr->end_of_input && ptr->threads() == 0)
return {Error::NEED_DATA, 0};
break; // still working
}
if (reportable > 0) {
pool_last_reported += reportable;
if (Compute::PARTIAL_FROM_BEGINNING ==
(flags & Compute::PARTIAL_FROM_BEGINNING)) {
return {Error::NONE, static_cast<uint8_t>(pool_last_reported)};
} else {
// complete
if (pool_last_reported == _blocks - 1)
return {Error::NONE,
}
}
} else if (Compute::PARTIAL_ANY == (flags & Compute::PARTIAL_ANY)) {
// invalidate other pointers.
auto undecodable = decoders.end();
std::unique_lock<std::mutex> dec_lock (_mtx);
RQ_UNUSED(dec_lock);
for (auto it = decoders.begin(); it != decoders.end(); ++it) {
if (!it->second.reported) {
auto ptr = it->second.dec;
if (ptr == nullptr) {
assert(false && "RFC6330: decoder should never be nullptr");
break;
}
if (ptr->ready()) {
it->second.reported = true;
return {Error::NONE, it->first};
}
if (ptr->is_stopped())
return {Error::EXITING, 0};
// first return all decodable blocks
// then return the ones we can not decode.
if (ptr->end_of_input && ptr->threads() == 0)
undecodable = it;
if (undecodable != decoders.end()) {
undecodable->second.reported = true;
return {Error::NEED_DATA, undecodable->first};
}
}
// can be reached if computing thread was stopped
return {Error::WORKING, 0};
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
template <typename In_It, typename Fwd_It>
uint64_t Decoder<In_It, Fwd_It>::decode_symbol (Fwd_It &start, const Fwd_It end,
const uint16_t esi,
const uint8_t sbn)
{
if (!operator bool() || sbn > blocks() || esi > symbols (sbn))
return 0;
std::shared_ptr<RaptorQ__v1::Impl::Raw_Decoder<In_It>> dec_ptr = nullptr;
std::unique_lock<std::mutex> lock (_mtx);
auto it = decoders.find (sbn);
if (it == decoders.end())
return 0; // did not receiveany data yet.
if (use_pool) {
dec_ptr = it->second.dec;
lock.unlock();
if (!dec_ptr->ready())
return 0; // did not receive enough data, or could not decode yet.
} else {
dec_ptr = it->second.dec;
lock.unlock();
if (!dec_ptr->ready()) {
if (!dec_ptr->can_decode())
return 0;
RaptorQ__v1::Work_State keep_working =
RaptorQ__v1::Work_State::KEEP_WORKING;
dec_ptr->decode (&keep_working);
if (!dec_ptr->ready())
return 0;
}
}
// decoder has decoded the block
Impl::De_Interleaver<Fwd_It> de_interleaving (dec_ptr->get_symbols(),
_sub_blocks,
symbols (sbn),
_alignment);
// find the end:
auto real_end = start;
size_t fwd_iter_for_symbol = symbol_size() /
sizeof(typename std::iterator_traits<Fwd_It>::value_type);
// be sure that 'end' points AT MAX to the end of the symbol
if (std::is_same<typename std::iterator_traits<Fwd_It>::iterator_category,
std::random_access_iterator_tag>::value) {
real_end += fwd_iter_for_symbol;
if (real_end > end)
real_end = end;
} else {
// sory, fwd_iterators do not have comparison operators :(
while (real_end != end && fwd_iter_for_symbol != 0)
++real_end;
}
return de_interleaving (start, end, max_bytes, 0, esi);
}
template <typename In_It, typename Fwd_It>
uint64_t Decoder<In_It, Fwd_It>::decode_bytes (Fwd_It &start, const Fwd_It end,
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
// Decode from the beginning, up untill we can.
// return number of BYTES written, starting at "start + skip" bytes
//
// in case the last iterator is only half written, "start" will
// point to the half-written iterator.
uint64_t written = 0;
uint8_t new_skip = skip;
for (uint8_t sbn = 0; sbn < blocks(); ++sbn) {
std::unique_lock<std::mutex> block_lock (_mtx);
auto it = decoders.find (sbn);
if (it == decoders.end())
return written;
auto dec_ptr = it->second.dec;
block_lock.unlock();
if (!dec_ptr->ready()) {
if (!use_pool && dec_ptr->can_decode()) {
RaptorQ__v1::Work_State state =
RaptorQ__v1::Work_State::KEEP_WORKING;
auto ret = dec_ptr->decode (&state);
return written;
}
} else {
return written;
}
}
Impl::De_Interleaver<Fwd_It> de_interleaving (dec_ptr->get_symbols(),
_sub_blocks,
symbols (sbn),
_alignment);
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
auto tmp_start = start;
uint64_t bytes_written = de_interleaving (tmp_start, end, max_bytes,
new_skip);
written += bytes_written;
uint64_t bytes_and_skip = new_skip + bytes_written;
new_skip = bytes_and_skip %
sizeof(typename std::iterator_traits<Fwd_It>::value_type);
if (bytes_written == 0)
return written;
//new_skip = block_size (sbn) %
// sizeof(typename std::iterator_traits<Fwd_It>::value_type);
// if we ended decoding in the middle of a Fwd_It, do not advance
// start too much, or we will end up having additional zeros.
if (new_skip == 0) {
start = tmp_start;
} else {
uint64_t it_written = bytes_and_skip /
sizeof(typename std::iterator_traits<Fwd_It>::value_type);
// RaptorQ handles at most 881GB per rfc, so
// casting uint64 to int64 is safe
// we can not do "--start" since it's a forward iterator
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wshorten-64-to-32"
#pragma clang diagnostic ignored "-Wsign-conversion"
start += std::max (static_cast<uint64_t>(0), it_written - 1);
#pragma clang diagnostic pop
}
}
return written;
size_t Decoder<In_It, Fwd_It>::decode_block_bytes (Fwd_It &start,
const Fwd_It end,
const uint8_t skip,
const uint8_t sbn)
return 0;
std::shared_ptr<RaptorQ__v1::Impl::Raw_Decoder<In_It>> dec_ptr = nullptr;
std::unique_lock<std::mutex> lock (_mtx);
auto it = decoders.find (sbn);
if (it == decoders.end())
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
if (use_pool) {
dec_ptr = it->second.dec;
lock.unlock();
if (!dec_ptr->ready())
return 0; // did not receive enough data, or could not decode yet.
} else {
dec_ptr = it->second.dec;
lock.unlock();
if (!dec_ptr->ready()) {
if (!dec_ptr->can_decode())
return 0;
RaptorQ__v1::Work_State keep_working =
RaptorQ__v1::Work_State::KEEP_WORKING;
dec_ptr->decode (&keep_working);
if (!dec_ptr->ready())
return 0;
}
}
// decoder has decoded the block
Impl::De_Interleaver<Fwd_It> de_interleaving (dec_ptr->get_symbols(),
_sub_blocks,
symbols (sbn),
_alignment);
}
template <typename In_It, typename Fwd_It>
Decoder_written Decoder<In_It, Fwd_It>::decode_aligned (Fwd_It &start,
const Fwd_It end,
const uint8_t skip)
const uint64_t bytes = decode_bytes (start, end, skip);
const uint64_t skip_and_bytes = skip + bytes;
const uint64_t iterators = skip_and_bytes /
sizeof(typename std::iterator_traits<Fwd_It>::value_type);
const uint8_t new_skip = skip_and_bytes %
sizeof(typename std::iterator_traits<Fwd_It>::value_type);
return {iterators, new_skip};
}
template <typename In_It, typename Fwd_It>
Decoder_written Decoder<In_It, Fwd_It>::decode_block_aligned (
Fwd_It &start,
const Fwd_It end,
const uint8_t skip,
const uint8_t sbn)
const size_t bytes = decode_block_bytes (start, end, skip, sbn);
const size_t skip_and_bytes = skip + bytes;
const size_t iterators = skip_and_bytes /
sizeof(typename std::iterator_traits<Fwd_It>::value_type);
const uint8_t new_skip = skip_and_bytes %
sizeof(typename std::iterator_traits<Fwd_It>::value_type);
return {iterators, new_skip};
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
template <typename In_It, typename Fwd_It>
uint8_t Decoder<In_It, Fwd_It>::blocks_ready()
{
uint8_t blocks_ready = 0;
for (uint8_t sbn = 0; sbn < blocks(); ++sbn) {
if (is_block_ready (sbn))
++blocks_ready;
}
return blocks_ready;
}
template <typename In_It, typename Fwd_It>
bool Decoder<In_It, Fwd_It>::is_ready()
{ return blocks_ready() == blocks(); }
template <typename In_It, typename Fwd_It>
bool Decoder<In_It, Fwd_It>::is_block_ready (const uint8_t block)
{
std::unique_lock<std::mutex> block_lock (_mtx);
auto it = decoders.find (block);
if (it == decoders.end())
return false;
auto dec_ptr = it->second.dec;
block_lock.unlock();
if (dec_ptr->ready())
return true;
return false;
}
template <typename In_It, typename Fwd_It>
uint64_t Decoder<In_It, Fwd_It>::bytes() const
template <typename In_It, typename Fwd_It>
uint8_t Decoder<In_It, Fwd_It>::blocks() const
return static_cast<uint8_t> (part.num (0) + part.num (1));
template <typename In_It, typename Fwd_It>
uint32_t Decoder<In_It, Fwd_It>::block_size (const uint8_t sbn) const
if (ret != 0 && sbn == (part.num (0) + part.num (1) - 1)) {
// the size of the data (_size) is different from the sum of the size of
// all blocks. Get the real size, so we do not write more.
// we obviously need to consider this only for the last block.
size_t left = ret;
left -= part.num (0) * part.size (0) * _symbol_size;
if (part.num (1) > 1)
left -= (part.num (1) - 1) * part.size (1) * _symbol_size;
ret = static_cast<uint32_t> (left);
}
return ret;
template <typename In_It, typename Fwd_It>
uint16_t Decoder<In_It, Fwd_It>::symbol_size() const
template <typename In_It, typename Fwd_It>
uint16_t Decoder<In_It, Fwd_It>::symbols (const uint8_t sbn) const
if (sbn < part.num (0)) {
return part.size (0);
} else if (sbn - part.num (0) < part.num (1)) {
return part.size (1);
}
return 0;
template <typename Rnd_It, typename Fwd_It>
Block_Size Decoder<Rnd_It, Fwd_It>::extended_symbols (const uint8_t sbn) const
{
const uint16_t symbols = this->symbols (sbn);
if (symbols == 0)
return static_cast<Block_Size> (0);
uint16_t idx;
for (idx = 0; idx < (*RFC6330__v1::blocks).size(); ++idx) {
if (static_cast<uint16_t> ((*RFC6330__v1::blocks)[idx]) >= symbols)
break;
}
// check that the user did not try some cast trickery,
// and maximum size is ssize_t::max. But ssize_t is not standard,
// so we search the maximum ourselves.
if (idx == (*RFC6330__v1::blocks).size())
return static_cast<Block_Size> (0);
return (*RFC6330__v1::blocks)[idx];
}