1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
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
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
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
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
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
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
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
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
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
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
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
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
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
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
/*
* Ceph - scalable distributed file system
*
* Copyright (C) 2016 Allen Samuels <allen.samuels@sandisk.com>
*
* This is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1, as published by the Free Software
* Foundation. See file COPYING.
*
*/
// If you #include "include/encoding.h" you get the old-style *and*
// the new-style definitions. (The old-style needs denc_traits<> in
// order to disable the container helpers when new-style traits are
// present.)
// You can also just #include "include/denc.h" and get only the
// new-style helpers. The eventual goal is to drop the legacy
// definitions.
#ifndef _ENC_DEC_H
#define _ENC_DEC_H
#include <array>
#include <cstring>
#include <map>
#include <optional>
#include <set>
#include <string>
#include <type_traits>
#include <vector>
#include <boost/container/flat_map.hpp>
#include <boost/container/flat_set.hpp>
#include <boost/intrusive/set.hpp>
#include <boost/optional.hpp>
#include "include/ceph_assert.h" // boost clobbers this
#include "include/intarith.h"
#include "include/int_types.h"
#include "buffer.h"
#include "byteorder.h"
#include "common/convenience.h"
template<typename T, typename=void>
struct denc_traits {
static constexpr bool supported = false;
static constexpr bool featured = false;
static constexpr bool bounded = false;
static constexpr bool need_contiguous = true;
};
template<typename T>
inline constexpr bool denc_supported = denc_traits<T>::supported;
// hack for debug only; FIXME
//#include <iostream>
//using std::cout;
// Define this to compile in a dump of all encoded objects to disk to
// populate ceph-object-corpus. Note that there is an almost
// identical implementation in encoding.h, but you only need to define
// ENCODE_DUMP_PATH here.
//
// See src/test/encoding/generate-corpus-objects.sh.
//
//#define ENCODE_DUMP_PATH /tmp/something
#ifdef ENCODE_DUMP_PATH
# include <cstdio>
# include <sys/types.h>
# include <sys/stat.h>
# include <fcntl.h>
# define ENCODE_STR(x) #x
# define ENCODE_STRINGIFY(x) ENCODE_STR(x)
# define DENC_DUMP_PRE(Type) \
char *__denc_dump_pre = p.get_pos();
// this hackery with bits below is just to get a semi-reasonable
// distribution across time. it is somewhat exponential but not
// quite.
# define DENC_DUMP_POST(Type) \
do { \
static int i = 0; \
i++; \
int bits = 0; \
for (unsigned t = i; t; bits++) \
t &= t - 1; \
if (bits > 2) \
break; \
char fn[PATH_MAX]; \
snprintf(fn, sizeof(fn), \
ENCODE_STRINGIFY(ENCODE_DUMP_PATH) "/%s__%d.%x", #Type, \
getpid(), i++); \
int fd = ::open(fn, O_WRONLY|O_TRUNC|O_CREAT|O_CLOEXEC, 0644); \
if (fd >= 0) { \
size_t len = p.get_pos() - __denc_dump_pre; \
int r = ::write(fd, __denc_dump_pre, len); \
(void)r; \
::close(fd); \
} \
} while (0)
#else
# define DENC_DUMP_PRE(Type)
# define DENC_DUMP_POST(Type)
#endif
/*
top level level functions look like so
======================================
inline void denc(const T& o, size_t& p, uint64_t features=0);
inline void denc(const T& o, buffer::list::contiguous_appender& p,
uint64_t features=0);
inline void denc(T& o, buffer::ptr::const_iterator& p, uint64_t features=0);
or (for featured objects)
inline void denc(const T& o, size_t& p, uint64_t features);
inline void denc(const T& o, buffer::list::contiguous_appender& p,
uint64_t features);
inline void denc(T& o, buffer::ptr::const_iterator& p, uint64_t features);
- These are symmetrical, so that they can be used from the magic DENC
method of writing the bound_encode/encode/decode methods all in one go;
they differ only in the type of p.
- These are automatically fabricated via a template that calls into
the denc_traits<> methods (see below), provided denc_traits<T>::supported
is defined and true. They never need to be written explicitly.
static denc_traits<> definitions look like so
=============================================
template<>
struct denc_traits<T> {
static constexpr bool supported = true;
static constexpr bool bounded = false;
static constexpr bool featured = false;
static constexpr bool need_contiguous = true;
static void bound_encode(const T &o, size_t& p, uint64_t f=0);
static void encode(const T &o, buffer::list::contiguous_appender& p,
uint64_t f=0);
static void decode(T& o, buffer::ptr::const_iterator &p, uint64_t f=0);
};
or (for featured objects)
template<>
struct denc_traits<T> {
static constexpr bool supported = true;
static constexpr bool bounded = false;
static constexpr bool featured = true;
static constexpr bool need_contiguous = true;
static void bound_encode(const T &o, size_t& p, uint64_t f);
static void encode(const T &o, buffer::list::contiguous_appender& p,
uint64_t f);
static void decode(T& o, buffer::ptr::const_iterator &p, uint64_t f=0);
};
- denc_traits<T> is normally declared via the WRITE_CLASS_DENC(type) macro,
which is used in place of the old-style WRITE_CLASS_ENCODER(type) macro.
There are _FEATURED and _BOUNDED variants. The class traits simply call
into class methods of the same name (see below).
- denc_traits<T> can also be written explicitly for some type to indicate
how it should be encoded. This is the "source of truth" for how a type
is encoded.
- denc_traits<T> are declared for the base integer types, string, bufferptr,
and bufferlist base types.
- denc_traits<std::foo<T>>-like traits are declared for standard container
types.
class methods look like so
==========================
void bound_encode(size_t& p) const;
void encode(buffer::list::contiguous_appender& p) const;
void decode(buffer::ptr::const_iterator &p);
or (for featured objects)
void bound_encode(size_t& p, uint64_t f) const;
void encode(buffer::list::contiguous_appender& p, uint64_t f) const;
void decode(buffer::ptr::const_iterator &p);
- These are normally invoked by the denc_traits<> methods that are
declared via WRITE_CLASS_DENC, although you can also invoke them explicitly
in your code.
- These methods are optimised for contiguous buffer, but denc() will try
rebuild a contigous one if the decoded bufferlist is segmented. If you are
concerned about the cost, you might want to define yet another method:
void decode(buffer::list::iterator &p);
- These can be defined either explicitly (as above), or can be "magically"
defined all in one go using the DENC macro and DENC_{START,FINISH} helpers
(which work like the legacy {ENCODE,DECODE}_{START,FINISH} macros):
class foo_t {
...
DENC(foo_t, v, p) {
DENC_START(1, 1, p);
denc(v.foo, p);
denc(v.bar, p);
denc(v.baz, p);
DENC_FINISH(p);
}
...
};
WRITE_CLASS_DENC(foo_t)
*/
// ---------------------------------------------------------------------
// raw types
namespace _denc {
template<typename T, typename... Us>
inline constexpr bool is_any_of = (... || std::is_same_v<T, Us>);
template<typename T, typename=void> struct underlying_type {
using type = T;
};
template<typename T>
struct underlying_type<T, std::enable_if_t<std::is_enum_v<T>>> {
using type = std::underlying_type_t<T>;
};
template<typename T>
using underlying_type_t = typename underlying_type<T>::type;
}
template<class It>
struct is_const_iterator
: std::conditional_t<std::is_const_v<std::remove_pointer_t<typename It::pointer>>,
std::true_type,
std::false_type>
{};
template<>
struct is_const_iterator<size_t> : std::false_type {};
template<>
struct is_const_iterator<buffer::list::contiguous_appender> : std::false_type {
// appender is used for *changing* the buffer
};
template<class It>
inline constexpr bool is_const_iterator_v = is_const_iterator<It>::value;
template<typename T, class It>
std::enable_if_t<is_const_iterator_v<It>, const T&>
get_pos_add(It& i) {
return *reinterpret_cast<const T*>(i.get_pos_add(sizeof(T)));
}
template<typename T, class It>
std::enable_if_t<!is_const_iterator_v<It>, T&>
get_pos_add(It& i) {
return *reinterpret_cast<T*>(i.get_pos_add(sizeof(T)));
}
template<typename T>
struct denc_traits<
T,
std::enable_if_t<
_denc::is_any_of<_denc::underlying_type_t<T>,
ceph_le64, ceph_le32, ceph_le16, uint8_t
#ifndef _CHAR_IS_SIGNED
, int8_t
#endif
>>> {
static constexpr bool supported = true;
static constexpr bool featured = false;
static constexpr bool bounded = true;
static constexpr bool need_contiguous = false;
static void bound_encode(const T &o, size_t& p, uint64_t f=0) {
p += sizeof(T);
}
template<class It>
static std::enable_if_t<!is_const_iterator_v<It>>
encode(const T &o, It& p, uint64_t f=0) {
get_pos_add<T>(p) = o;
}
template<class It>
static std::enable_if_t<is_const_iterator_v<It>>
decode(T& o, It& p, uint64_t f=0) {
o = get_pos_add<T>(p);
}
static void decode(T& o, buffer::list::const_iterator &p) {
p.copy(sizeof(T), reinterpret_cast<char*>(&o));
}
};
// -----------------------------------------------------------------------
// integer types
// itype == internal type
// otype == external type, i.e., the type on the wire
// NOTE: the overload resolution ensures that the legacy encode/decode methods
// defined for int types is preferred to the ones defined using the specialized
// template, and hence get selected. This machinery prevents these these from
// getting glued into the legacy encode/decode methods; the overhead of setting
// up a contiguous_appender etc is likely to be slower.
namespace _denc {
template<typename T, typename=void> struct ExtType {
using type = void;
};
template<typename T>
struct ExtType<T, std::enable_if_t<std::is_same_v<T, int16_t> ||
std::is_same_v<T, uint16_t>>> {
using type = ceph_le16;
};
template<typename T>
struct ExtType<T, std::enable_if_t<std::is_same_v<T, int32_t> ||
std::is_same_v<T, uint32_t>>> {
using type = ceph_le32;
};
template<typename T>
struct ExtType<T, std::enable_if_t<std::is_same_v<T, int64_t> ||
std::is_same_v<T, uint64_t>>> {
using type = ceph_le64;
};
template<>
struct ExtType<bool> {
using type = uint8_t;
};
template<typename T>
using ExtType_t = typename ExtType<T>::type;
} // namespace _denc
template<typename T>
struct denc_traits<T, std::enable_if_t<!std::is_void_v<_denc::ExtType_t<T>>>>
{
static constexpr bool supported = true;
static constexpr bool featured = false;
static constexpr bool bounded = true;
static constexpr bool need_contiguous = false;
using etype = _denc::ExtType_t<T>;
static void bound_encode(const T &o, size_t& p, uint64_t f=0) {
p += sizeof(etype);
}
template<class It>
static std::enable_if_t<!is_const_iterator_v<It>>
encode(const T &o, It& p, uint64_t f=0) {
get_pos_add<etype>(p) = o;
}
template<class It>
static std::enable_if_t<is_const_iterator_v<It>>
decode(T& o, It &p, uint64_t f=0) {
o = get_pos_add<etype>(p);
}
static void decode(T& o, buffer::list::const_iterator &p) {
etype e;
p.copy(sizeof(etype), reinterpret_cast<char*>(&e));
o = e;
}
};
// varint
//
// high bit of each byte indicates another byte follows.
template<typename T>
inline void denc_varint(T v, size_t& p) {
p += sizeof(T) + 1;
}
template<typename T>
inline void denc_varint(T v, bufferlist::contiguous_appender& p) {
uint8_t byte = v & 0x7f;
v >>= 7;
while (v) {
byte |= 0x80;
get_pos_add<__u8>(p) = byte;
byte = (v & 0x7f);
v >>= 7;
}
get_pos_add<__u8>(p) = byte;
}
template<typename T>
inline void denc_varint(T& v, bufferptr::const_iterator& p) {
uint8_t byte = *(__u8*)p.get_pos_add(1);
v = byte & 0x7f;
int shift = 7;
while (byte & 0x80) {
byte = get_pos_add<__u8>(p);
v |= (T)(byte & 0x7f) << shift;
shift += 7;
}
}
// signed varint encoding
//
// low bit = 1 = negative, 0 = positive
// high bit of every byte indicates whether another byte follows.
inline void denc_signed_varint(int64_t v, size_t& p) {
p += sizeof(v) + 2;
}
template<class It>
inline std::enable_if_t<!is_const_iterator_v<It>>
denc_signed_varint(int64_t v, It& p) {
if (v < 0) {
v = (-v << 1) | 1;
} else {
v <<= 1;
}
denc_varint(v, p);
}
template<typename T, class It>
inline std::enable_if_t<is_const_iterator_v<It>>
denc_signed_varint(T& v, It& p)
{
int64_t i = 0;
denc_varint(i, p);
if (i & 1) {
v = -(i >> 1);
} else {
v = i >> 1;
}
}
// varint + lowz encoding
//
// first(low) 2 bits = how many low zero bits (nibbles)
// high bit of each byte = another byte follows
// (so, 5 bits data in first byte, 7 bits data thereafter)
inline void denc_varint_lowz(uint64_t v, size_t& p) {
p += sizeof(v) + 2;
}
inline void denc_varint_lowz(uint64_t v, bufferlist::contiguous_appender& p) {
int lowznib = v ? (ctz(v) / 4) : 0;
if (lowznib > 3)
lowznib = 3;
v >>= lowznib * 4;
v <<= 2;
v |= lowznib;
denc_varint(v, p);
}
template<typename T>
inline void denc_varint_lowz(T& v, bufferptr::const_iterator& p)
{
uint64_t i = 0;
denc_varint(i, p);
int lowznib = (i & 3);
i >>= 2;
i <<= lowznib * 4;
v = i;
}
// signed varint + lowz encoding
//
// first low bit = 1 for negative, 0 for positive
// next 2 bits = how many low zero bits (nibbles)
// high bit of each byte = another byte follows
// (so, 4 bits data in first byte, 7 bits data thereafter)
inline void denc_signed_varint_lowz(int64_t v, size_t& p) {
p += sizeof(v) + 2;
}
template<class It>
inline std::enable_if_t<!is_const_iterator_v<It>>
denc_signed_varint_lowz(int64_t v, It& p) {
bool negative = false;
if (v < 0) {
v = -v;
negative = true;
}
unsigned lowznib = v ? (ctz(v) / 4) : 0u;
if (lowznib > 3)
lowznib = 3;
v >>= lowznib * 4;
v <<= 3;
v |= lowznib << 1;
v |= (int)negative;
denc_varint(v, p);
}
template<typename T, class It>
inline std::enable_if_t<is_const_iterator_v<It>>
denc_signed_varint_lowz(T& v, It& p)
{
int64_t i = 0;
denc_varint(i, p);
int lowznib = (i & 6) >> 1;
if (i & 1) {
i >>= 3;
i <<= lowznib * 4;
v = -i;
} else {
i >>= 3;
i <<= lowznib * 4;
v = i;
}
}
// LBA
//
// first 1-3 bits = how many low zero bits
// *0 = 12 (common 4 K alignment case)
// *01 = 16
// *011 = 20
// *111 = byte
// then 28-30 bits of data
// then last bit = another byte follows
// high bit of each subsequent byte = another byte follows
inline void denc_lba(uint64_t v, size_t& p) {
p += sizeof(v) + 2;
}
template<class It>
inline std::enable_if_t<!is_const_iterator_v<It>>
denc_lba(uint64_t v, It& p) {
int low_zero_nibbles = v ? (int)(ctz(v) / 4) : 0;
int pos;
uint32_t word;
int t = low_zero_nibbles - 3;
if (t < 0) {
pos = 3;
word = 0x7;
} else if (t < 3) {
v >>= (low_zero_nibbles * 4);
pos = t + 1;
word = (1 << t) - 1;
} else {
v >>= 20;
pos = 3;
word = 0x3;
}
word |= (v << pos) & 0x7fffffff;
v >>= 31 - pos;
if (!v) {
*(ceph_le32*)p.get_pos_add(sizeof(uint32_t)) = word;
return;
}
word |= 0x80000000;
*(ceph_le32*)p.get_pos_add(sizeof(uint32_t)) = word;
uint8_t byte = v & 0x7f;
v >>= 7;
while (v) {
byte |= 0x80;
*(__u8*)p.get_pos_add(1) = byte;
byte = (v & 0x7f);
v >>= 7;
}
*(__u8*)p.get_pos_add(1) = byte;
}
template<class It>
inline std::enable_if_t<is_const_iterator_v<It>>
denc_lba(uint64_t& v, It& p) {
uint32_t word = *(ceph_le32*)p.get_pos_add(sizeof(uint32_t));
int shift;
switch (word & 7) {
case 0:
case 2:
case 4:
case 6:
v = (uint64_t)(word & 0x7ffffffe) << (12 - 1);
shift = 12 + 30;
break;
case 1:
case 5:
v = (uint64_t)(word & 0x7ffffffc) << (16 - 2);
shift = 16 + 29;
break;
case 3:
v = (uint64_t)(word & 0x7ffffff8) << (20 - 3);
shift = 20 + 28;
break;
case 7:
v = (uint64_t)(word & 0x7ffffff8) >> 3;
shift = 28;
}
uint8_t byte = word >> 24;
while (byte & 0x80) {
byte = *(__u8*)p.get_pos_add(1);
v |= (uint64_t)(byte & 0x7f) << shift;
shift += 7;
}
}
// ---------------------------------------------------------------------
// denc top-level methods that call into denc_traits<T> methods
template<typename T, typename traits=denc_traits<T>>
inline std::enable_if_t<traits::supported> denc(
const T& o,
size_t& p,
uint64_t f=0)
{
if constexpr (traits::featured) {
traits::bound_encode(o, p, f);
} else {
traits::bound_encode(o, p);
}
}
template<typename T, class It, typename traits=denc_traits<T>>
inline std::enable_if_t<traits::supported && !is_const_iterator_v<It>>
denc(const T& o,
It& p,
uint64_t features=0)
{
if constexpr (traits::featured) {
traits::encode(o, p, features);
} else {
traits::encode(o, p);
}
}
template<typename T, class It, typename traits=denc_traits<T>>
inline std::enable_if_t<traits::supported && is_const_iterator_v<It>>
denc(T& o,
It& p,
uint64_t features=0)
{
if constexpr (traits::featured) {
traits::decode(o, p, features);
} else {
traits::decode(o, p);
}
}
namespace _denc {
template<typename T, typename = void>
struct has_legacy_denc : std::false_type {};
template<typename T>
struct has_legacy_denc<T, decltype(std::declval<T&>()
.decode(std::declval<
bufferlist::const_iterator&>()))>
: std::true_type {
static void decode(T& v, bufferlist::const_iterator& p) {
v.decode(p);
}
};
template<typename T>
struct has_legacy_denc<T,
std::enable_if_t<
!denc_traits<T>::need_contiguous>> : std::true_type {
static void decode(T& v, bufferlist::const_iterator& p) {
denc_traits<T>::decode(v, p);
}
};
}
template<typename T,
typename traits=denc_traits<T>,
typename has_legacy_denc=_denc::has_legacy_denc<T>>
inline std::enable_if_t<traits::supported &&
has_legacy_denc::value> denc(
T& o,
buffer::list::const_iterator& p)
{
has_legacy_denc::decode(o, p);
}
// ---------------------------------------------------------------------
// base types and containers
//
// std::string
//
template<typename A>
struct denc_traits<std::basic_string<char,std::char_traits<char>,A>> {
private:
using value_type = std::basic_string<char,std::char_traits<char>,A>;
public:
static constexpr bool supported = true;
static constexpr bool featured = false;
static constexpr bool bounded = false;
static constexpr bool need_contiguous = false;
static void bound_encode(const value_type& s, size_t& p, uint64_t f=0) {
p += sizeof(uint32_t) + s.size();
}
template<class It>
static void encode(const value_type& s,
It& p,
uint64_t f=0) {
denc((uint32_t)s.size(), p);
memcpy(p.get_pos_add(s.size()), s.data(), s.size());
}
template<class It>
static void decode(value_type& s,
It& p,
uint64_t f=0) {
uint32_t len;
denc(len, p);
decode_nohead(len, s, p);
}
static void decode(value_type& s, buffer::list::const_iterator& p)
{
uint32_t len;
denc(len, p);
decode_nohead(len, s, p);
}
template<class It>
static void decode_nohead(size_t len, value_type& s, It& p) {
s.clear();
if (len) {
s.append(p.get_pos_add(len), len);
}
}
static void decode_nohead(size_t len, value_type& s,
buffer::list::const_iterator& p) {
if (len) {
if constexpr (std::is_same_v<value_type, std::string>) {
s.clear();
p.copy(len, s);
} else {
s.resize(len);
p.copy(len, s.data());
}
} else {
s.clear();
}
}
template<class It>
static std::enable_if_t<!is_const_iterator_v<It>>
encode_nohead(const value_type& s, It& p) {
auto len = s.length();
maybe_inline_memcpy(p.get_pos_add(len), s.data(), len, 16);
}
};
//
// bufferptr
//
template<>
struct denc_traits<bufferptr> {
static constexpr bool supported = true;
static constexpr bool featured = false;
static constexpr bool bounded = false;
static constexpr bool need_contiguous = false;
static void bound_encode(const bufferptr& v, size_t& p, uint64_t f=0) {
p += sizeof(uint32_t) + v.length();
}
template <class It>
static std::enable_if_t<!is_const_iterator_v<It>>
encode(const bufferptr& v, It& p, uint64_t f=0) {
denc((uint32_t)v.length(), p);
p.append(v);
}
template <class It>
static std::enable_if_t<is_const_iterator_v<It>>
decode(bufferptr& v, It& p, uint64_t f=0) {
uint32_t len;
denc(len, p);
v = p.get_ptr(len);
}
static void decode(bufferptr& v, buffer::list::const_iterator& p) {
uint32_t len;
denc(len, p);
bufferlist s;
p.copy(len, s);
if (len) {
if (s.get_num_buffers() == 1)
v = s.front();
else
v = buffer::copy(s.c_str(), s.length());
}
}
};
//
// bufferlist
//
template<>
struct denc_traits<bufferlist> {
static constexpr bool supported = true;
static constexpr bool featured = false;
static constexpr bool bounded = false;
static constexpr bool need_contiguous = false;
static void bound_encode(const bufferlist& v, size_t& p, uint64_t f=0) {
p += sizeof(uint32_t) + v.length();
}
static void encode(const bufferlist& v, buffer::list::contiguous_appender& p,
uint64_t f=0) {
denc((uint32_t)v.length(), p);
p.append(v);
}
static void decode(bufferlist& v, buffer::ptr::const_iterator& p, uint64_t f=0) {
uint32_t len;
denc(len, p);
v.clear();
v.push_back(p.get_ptr(len));
}
static void decode(bufferlist& v, buffer::list::const_iterator& p) {
uint32_t len;
denc(len, p);
v.clear();
p.copy(len, v);
}
static void encode_nohead(const bufferlist& v,
buffer::list::contiguous_appender& p) {
p.append(v);
}
static void decode_nohead(size_t len, bufferlist& v,
buffer::ptr::const_iterator& p) {
v.clear();
if (len) {
v.append(p.get_ptr(len));
}
}
static void decode_nohead(size_t len, bufferlist& v,
buffer::list::const_iterator& p) {
v.clear();
p.copy(len, v);
}
};
//
// std::pair<A, B>
//
template<typename A, typename B>
struct denc_traits<
std::pair<A, B>,
std::enable_if_t<denc_supported<A> && denc_supported<B>>> {
typedef denc_traits<A> a_traits;
typedef denc_traits<B> b_traits;
static constexpr bool supported = true;
static constexpr bool featured = a_traits::featured || b_traits::featured ;
static constexpr bool bounded = a_traits::bounded && b_traits::bounded;
static constexpr bool need_contiguous = (a_traits::need_contiguous ||
b_traits::need_contiguous);
static void bound_encode(const std::pair<A,B>& v, size_t& p, uint64_t f = 0) {
if constexpr (featured) {
denc(v.first, p, f);
denc(v.second, p, f);
} else {
denc(v.first, p);
denc(v.second, p);
}
}
static void encode(const std::pair<A,B>& v, bufferlist::contiguous_appender& p,
uint64_t f = 0) {
if constexpr (featured) {
denc(v.first, p, f);
denc(v.second, p, f);
} else {
denc(v.first, p);
denc(v.second, p);
}
}
static void decode(std::pair<A,B>& v, buffer::ptr::const_iterator& p, uint64_t f=0) {
denc(v.first, p, f);
denc(v.second, p, f);
}
template<typename AA=A>
static std::enable_if_t<!!sizeof(AA) && !need_contiguous>
decode(std::pair<A,B>& v, buffer::list::const_iterator& p,
uint64_t f = 0) {
denc(v.first, p);
denc(v.second, p);
}
};
namespace _denc {
template<template<class...> class C, typename Details, typename ...Ts>
struct container_base {
private:
using container = C<Ts...>;
using T = typename Details::T;
public:
using traits = denc_traits<T>;
static constexpr bool supported = true;
static constexpr bool featured = traits::featured;
static constexpr bool bounded = false;
static constexpr bool need_contiguous = traits::need_contiguous;
template<typename U=T>
static void bound_encode(const container& s, size_t& p, uint64_t f = 0) {
p += sizeof(uint32_t);
if constexpr (traits::bounded) {
if (!s.empty()) {
// STL containers use weird element types like std::pair<const K, V>;
// cast to something we have denc_traits for.
size_t elem_size = 0;
if constexpr (traits::featured) {
denc(static_cast<const T&>(*s.begin()), elem_size, f);
} else {
denc(static_cast<const T&>(*s.begin()), elem_size);
}
p += sizeof(uint32_t) + elem_size * s.size();
}
} else {
for (const T& e : s) {
if constexpr (traits::featured) {
denc(e, p, f);
} else {
denc(e, p);
}
}
}
}
template<typename U=T>
static void encode(const container& s, buffer::list::contiguous_appender& p,
uint64_t f = 0) {
denc((uint32_t)s.size(), p);
if constexpr (traits::featured) {
encode_nohead(s, p, f);
} else {
encode_nohead(s, p);
}
}
static void decode(container& s, buffer::ptr::const_iterator& p, uint64_t f = 0) {
uint32_t num;
denc(num, p);
decode_nohead(num, s, p, f);
}
template<typename U=T>
static std::enable_if_t<!!sizeof(U) && !need_contiguous>
decode(container& s, buffer::list::const_iterator& p) {
uint32_t num;
denc(num, p);
decode_nohead(num, s, p);
}
// nohead
static void encode_nohead(const container& s, buffer::list::contiguous_appender& p,
uint64_t f = 0) {
for (const T& e : s) {
if constexpr (traits::featured) {
denc(e, p, f);
} else {
denc(e, p);
}
}
}
static void decode_nohead(size_t num, container& s,
buffer::ptr::const_iterator& p, uint64_t f=0) {
s.clear();
Details::reserve(s, num);
while (num--) {
T t;
denc(t, p, f);
Details::insert(s, std::move(t));
}
}
template<typename U=T>
static std::enable_if_t<!!sizeof(U) && !need_contiguous>
decode_nohead(size_t num, container& s,
buffer::list::const_iterator& p) {
s.clear();
Details::reserve(s, num);
while (num--) {
T t;
denc(t, p);
Details::insert(s, std::move(t));
}
}
};
template<typename T>
class container_has_reserve {
template<typename U, U> struct SFINAE_match;
template<typename U>
static std::true_type test(SFINAE_match<T(*)(typename T::size_type),
&U::reserve>*);
template<typename U>
static std::false_type test(...);
public:
static constexpr bool value = decltype(
test<denc_traits<T>>(0))::value;
};
template<typename T>
inline constexpr bool container_has_reserve_v =
container_has_reserve<T>::value;
template<typename Container>
struct container_details_base {
using T = typename Container::value_type;
static void reserve(Container& c, size_t s) {
if constexpr (container_has_reserve_v<Container>) {
c.reserve(s);
}
}
};
template<typename Container>
struct pushback_details : public container_details_base<Container> {
template<typename ...Args>
static void insert(Container& c, Args&& ...args) {
c.emplace_back(std::forward<Args>(args)...);
}
};
}
template<typename T, typename ...Ts>
struct denc_traits<
std::list<T, Ts...>,
typename std::enable_if_t<denc_traits<T>::supported>>
: public _denc::container_base<std::list,
_denc::pushback_details<std::list<T, Ts...>>,
T, Ts...> {};
template<typename T, typename ...Ts>
struct denc_traits<
std::vector<T, Ts...>,
typename std::enable_if_t<denc_traits<T>::supported>>
: public _denc::container_base<std::vector,
_denc::pushback_details<std::vector<T, Ts...>>,
T, Ts...> {};
namespace _denc {
template<typename Container>
struct setlike_details : public container_details_base<Container> {
using T = typename Container::value_type;
template<typename ...Args>
static void insert(Container& c, Args&& ...args) {
c.emplace_hint(c.cend(), std::forward<Args>(args)...);
}
};
}
template<typename T, typename ...Ts>
struct denc_traits<
std::set<T, Ts...>,
std::enable_if_t<denc_traits<T>::supported>>
: public _denc::container_base<std::set,
_denc::setlike_details<std::set<T, Ts...>>,
T, Ts...> {};
template<typename T, typename ...Ts>
struct denc_traits<
boost::container::flat_set<T, Ts...>,
std::enable_if_t<denc_traits<T>::supported>>
: public _denc::container_base<
boost::container::flat_set,
_denc::setlike_details<boost::container::flat_set<T, Ts...>>,
T, Ts...> {};
namespace _denc {
template<typename Container>
struct maplike_details : public container_details_base<Container> {
using T = std::pair<typename Container::key_type,
typename Container::mapped_type>;
template<typename ...Args>
static void insert(Container& c, Args&& ...args) {
c.emplace_hint(c.cend(), std::forward<Args>(args)...);
}
};
}
template<typename A, typename B, typename ...Ts>
struct denc_traits<
std::map<A, B, Ts...>,
std::enable_if_t<denc_traits<A>::supported &&
denc_traits<B>::supported>>
: public _denc::container_base<std::map,
_denc::maplike_details<std::map<A, B, Ts...>>,
A, B, Ts...> {};
template<typename A, typename B, typename ...Ts>
struct denc_traits<
boost::container::flat_map<A, B, Ts...>,
std::enable_if_t<denc_traits<A>::supported &&
denc_traits<B>::supported>>
: public _denc::container_base<
boost::container::flat_map,
_denc::maplike_details<boost::container::flat_map<
A, B, Ts...>>,
A, B, Ts...> {};
template<typename T, size_t N>
struct denc_traits<
std::array<T, N>,
std::enable_if_t<denc_traits<T>::supported>> {
private:
using container = std::array<T, N>;
public:
using traits = denc_traits<T>;
static constexpr bool supported = true;
static constexpr bool featured = traits::featured;
static constexpr bool bounded = traits::bounded;
static constexpr bool need_contiguous = traits::need_contiguous;
static void bound_encode(const container& s, size_t& p, uint64_t f = 0) {
if constexpr (traits::bounded) {
if constexpr (traits::featured) {
if (!s.empty()) {
size_t elem_size = 0;
denc(*s.begin(), elem_size, f);
p += elem_size * s.size();
}
} else {
size_t elem_size = 0;
denc(*s.begin(), elem_size);
p += elem_size * N;
}
} else {
for (const auto& e : s) {
if constexpr (traits::featured) {
denc(e, p, f);
} else {
denc(e, p);
}
}
}
}
static void encode(const container& s, buffer::list::contiguous_appender& p,
uint64_t f = 0) {
for (const auto& e : s) {
if constexpr (traits::featured) {
denc(e, p, f);
} else {
denc(e, p);
}
}
}
static void decode(container& s, buffer::ptr::const_iterator& p, uint64_t f = 0) {
for (auto& e : s)
denc(e, p, f);
}
template<typename U=T>
static std::enable_if_t<!!sizeof(U) &&
!need_contiguous>
decode(container& s, buffer::list::const_iterator& p) {
for (auto& e : s) {
denc(e, p);
}
}
};
template<typename... Ts>
struct denc_traits<
std::tuple<Ts...>,
std::enable_if_t<(denc_traits<Ts>::supported && ...)>> {
private:
static_assert(sizeof...(Ts) > 0,
"Zero-length tuples are not supported.");
using container = std::tuple<Ts...>;
public:
static constexpr bool supported = true;
static constexpr bool featured = (denc_traits<Ts>::featured || ...);
static constexpr bool bounded = (denc_traits<Ts>::bounded && ...);
static constexpr bool need_contiguous =
(denc_traits<Ts>::need_contiguous || ...);
template<typename U = container>
static std::enable_if_t<denc_traits<U>::featured>
bound_encode(const container& s, size_t& p, uint64_t f) {
ceph::for_each(s, [&p, f] (const auto& e) {
if constexpr (denc_traits<std::decay_t<decltype(e)>>::featured) {
denc(e, p, f);
} else {
denc(e, p);
}
});
}
template<typename U = container>
static std::enable_if_t<!denc_traits<U>::featured>
bound_encode(const container& s, size_t& p) {
ceph::for_each(s, [&p] (const auto& e) {
denc(e, p);
});
}
template<typename U = container>
static std::enable_if_t<denc_traits<U>::featured>
encode(const container& s, buffer::list::contiguous_appender& p, uint64_t f) {
ceph::for_each(s, [&p, f] (const auto& e) {
if constexpr (denc_traits<std::decay_t<decltype(e)>>::featured) {
denc(e, p, f);
} else {
denc(e, p);
}
});
}
template<typename U = container>
static std::enable_if_t<!denc_traits<U>::featured>
encode(const container& s, buffer::list::contiguous_appender& p) {
ceph::for_each(s, [&p] (const auto& e) {
denc(e, p);
});
}
static void decode(container& s, buffer::ptr::const_iterator& p, uint64_t f = 0) {
ceph::for_each(s, [&p] (auto& e) {
denc(e, p);
});
}
template<typename U = container>
static std::enable_if_t<!denc_traits<U>::need_contiguous>
decode(container& s, buffer::list::const_iterator& p, uint64_t f = 0) {
ceph::for_each(s, [&p] (auto& e) {
denc(e, p);
});
}
};
//
// boost::optional<T>
//
template<typename T>
struct denc_traits<
boost::optional<T>,
std::enable_if_t<denc_traits<T>::supported>> {
using traits = denc_traits<T>;
static constexpr bool supported = true;
static constexpr bool featured = traits::featured;
static constexpr bool bounded = false;
static constexpr bool need_contiguous = traits::need_contiguous;
static void bound_encode(const boost::optional<T>& v, size_t& p,
uint64_t f = 0) {
p += sizeof(bool);
if (v) {
if constexpr (featured) {
denc(*v, p, f);
} else {
denc(*v, p);
}
}
}
static void encode(const boost::optional<T>& v,
bufferlist::contiguous_appender& p,
uint64_t f = 0) {
denc((bool)v, p);
if (v) {
if constexpr (featured) {
denc(*v, p, f);
} else {
denc(*v, p);
}
}
}
static void decode(boost::optional<T>& v, buffer::ptr::const_iterator& p,
uint64_t f = 0) {
bool x;
denc(x, p, f);
if (x) {
v = T{};
denc(*v, p, f);
} else {
v = boost::none;
}
}
template<typename U = T>
static std::enable_if_t<!!sizeof(U) && !need_contiguous>
decode(boost::optional<T>& v, buffer::list::const_iterator& p) {
bool x;
denc(x, p);
if (x) {
v = T{};
denc(*v, p);
} else {
v = boost::none;
}
}
template<typename U = T>
static void encode_nohead(const boost::optional<T>& v,
bufferlist::contiguous_appender& p,
uint64_t f = 0) {
if (v) {
if constexpr (featured) {
denc(*v, p, f);
} else {
denc(*v, p);
}
}
}
static void decode_nohead(bool num, boost::optional<T>& v,
buffer::ptr::const_iterator& p, uint64_t f = 0) {
if (num) {
v = T();
denc(*v, p, f);
} else {
v = boost::none;
}
}
};
template<>
struct denc_traits<boost::none_t> {
static constexpr bool supported = true;
static constexpr bool featured = false;
static constexpr bool bounded = true;
static constexpr bool need_contiguous = false;
static void bound_encode(const boost::none_t& v, size_t& p) {
p += sizeof(bool);
}
static void encode(const boost::none_t& v,
bufferlist::contiguous_appender& p) {
denc(false, p);
}
};
//
// std::optional<T>
//
template<typename T>
struct denc_traits<
std::optional<T>,
std::enable_if_t<denc_traits<T>::supported>> {
using traits = denc_traits<T>;
static constexpr bool supported = true;
static constexpr bool featured = traits::featured;
static constexpr bool bounded = false;
static constexpr bool need_contiguous = traits::need_contiguous;
static void bound_encode(const std::optional<T>& v, size_t& p,
uint64_t f = 0) {
p += sizeof(bool);
if (v) {
if constexpr (featured) {
denc(*v, p, f);
} else {
denc(*v, p);
}
}
}
static void encode(const std::optional<T>& v,
bufferlist::contiguous_appender& p,
uint64_t f = 0) {
denc((bool)v, p);
if (v) {
if constexpr (featured) {
denc(*v, p, f);
} else {
denc(*v, p);
}
}
}
static void decode(std::optional<T>& v, buffer::ptr::const_iterator& p,
uint64_t f = 0) {
bool x;
denc(x, p, f);
if (x) {
v = T{};
denc(*v, p, f);
} else {
v = std::nullopt;
}
}
template<typename U = T>
static std::enable_if_t<!!sizeof(U) && !need_contiguous>
decode(std::optional<T>& v, buffer::list::const_iterator& p) {
bool x;
denc(x, p);
if (x) {
v = T{};
denc(*v, p);
} else {
v = std::nullopt;
}
}
static void encode_nohead(const std::optional<T>& v,
bufferlist::contiguous_appender& p,
uint64_t f = 0) {
if (v) {
if constexpr (featured) {
denc(*v, p, f);
} else {
denc(*v, p);
}
}
}
static void decode_nohead(bool num, std::optional<T>& v,
buffer::ptr::const_iterator& p, uint64_t f = 0) {
if (num) {
v = T();
denc(*v, p, f);
} else {
v = std::nullopt;
}
}
};
template<>
struct denc_traits<std::nullopt_t> {
static constexpr bool supported = true;
static constexpr bool featured = false;
static constexpr bool bounded = true;
static constexpr bool need_contiguous = false;
static void bound_encode(const std::nullopt_t& v, size_t& p) {
p += sizeof(bool);
}
static void encode(const std::nullopt_t& v,
bufferlist::contiguous_appender& p) {
denc(false, p);
}
};
// ----------------------------------------------------------------------
// class helpers
// Write denc_traits<> for a class that defines bound_encode/encode/decode
// methods.
#define WRITE_CLASS_DENC(T) _DECLARE_CLASS_DENC(T, false)
#define WRITE_CLASS_DENC_BOUNDED(T) _DECLARE_CLASS_DENC(T, true)
#define _DECLARE_CLASS_DENC(T, b) \
template<> struct denc_traits<T> { \
static constexpr bool supported = true; \
static constexpr bool featured = false; \
static constexpr bool bounded = b; \
static constexpr bool need_contiguous = !_denc::has_legacy_denc<T>::value;\
static void bound_encode(const T& v, size_t& p, uint64_t f=0) { \
v.bound_encode(p); \
} \
static void encode(const T& v, buffer::list::contiguous_appender& p, \
uint64_t f=0) { \
v.encode(p); \
} \
static void decode(T& v, buffer::ptr::const_iterator& p, uint64_t f=0) { \
v.decode(p); \
} \
};
#define WRITE_CLASS_DENC_FEATURED(T) _DECLARE_CLASS_DENC_FEATURED(T, false)
#define WRITE_CLASS_DENC_FEATURED_BOUNDED(T) _DECLARE_CLASS_DENC_FEATURED(T, true)
#define _DECLARE_CLASS_DENC_FEATURED(T, b) \
template<> struct denc_traits<T> { \
static constexpr bool supported = true; \
static constexpr bool featured = true; \
static constexpr bool bounded = b; \
static constexpr bool need_contiguous = !_denc::has_legacy_denc<T>::value;\
static void bound_encode(const T& v, size_t& p, uint64_t f) { \
v.bound_encode(p, f); \
} \
static void encode(const T& v, buffer::list::contiguous_appender& p, \
uint64_t f) { \
v.encode(p, f); \
} \
static void decode(T& v, buffer::ptr::const_iterator& p, uint64_t f=0) { \
v.decode(p, f); \
} \
};
// ----------------------------------------------------------------------
// encode/decode wrappers
// These glue the new-style denc world into old-style calls to encode
// and decode by calling into denc_traits<> methods (when present).
namespace ceph {
template<typename T, typename traits=denc_traits<T>>
inline std::enable_if_t<traits::supported && !traits::featured> encode(
const T& o,
bufferlist& bl,
uint64_t features_unused=0)
{
size_t len = 0;
traits::bound_encode(o, len);
auto a = bl.get_contiguous_appender(len);
traits::encode(o, a);
}
template<typename T, typename traits=denc_traits<T>>
inline std::enable_if_t<traits::supported && traits::featured> encode(
const T& o, bufferlist& bl,
uint64_t features)
{
size_t len = 0;
traits::bound_encode(o, len, features);
auto a = bl.get_contiguous_appender(len);
traits::encode(o, a, features);
}
template<typename T,
typename traits=denc_traits<T>>
inline std::enable_if_t<traits::supported && !traits::need_contiguous> decode(
T& o,
bufferlist::const_iterator& p)
{
if (p.end())
throw buffer::end_of_buffer();
const auto& bl = p.get_bl();
const auto remaining = bl.length() - p.get_off();
// it is expensive to rebuild a contigous buffer and drop it, so avoid this.
if (!p.is_pointing_same_raw(bl.back()) && remaining > CEPH_PAGE_SIZE) {
traits::decode(o, p);
} else {
// ensure we get a contigous buffer... until the end of the
// bufferlist. we don't really know how much we'll need here,
// unfortunately. hopefully it is already contiguous and we're just
// bumping the raw ref and initializing the ptr tmp fields.
bufferptr tmp;
auto t = p;
t.copy_shallow(remaining, tmp);
auto cp = std::cbegin(tmp);
traits::decode(o, cp);
p.advance(cp.get_offset());
}
}
template<typename T,
typename traits=denc_traits<T>>
inline std::enable_if_t<traits::supported && traits::need_contiguous> decode(
T& o,
bufferlist::const_iterator& p)
{
if (p.end())
throw buffer::end_of_buffer();
// ensure we get a contigous buffer... until the end of the
// bufferlist. we don't really know how much we'll need here,
// unfortunately. hopefully it is already contiguous and we're just
// bumping the raw ref and initializing the ptr tmp fields.
bufferptr tmp;
auto t = p;
t.copy_shallow(p.get_bl().length() - p.get_off(), tmp);
auto cp = std::cbegin(tmp);
traits::decode(o, cp);
p.advance(cp.get_offset());
}
// nohead variants
template<typename T, typename traits=denc_traits<T>>
inline std::enable_if_t<traits::supported &&
!traits::featured> encode_nohead(
const T& o,
bufferlist& bl)
{
size_t len = 0;
traits::bound_encode(o, len);
auto a = bl.get_contiguous_appender(len);
traits::encode_nohead(o, a);
}
template<typename T, typename traits=denc_traits<T>>
inline std::enable_if_t<traits::supported && !traits::featured> decode_nohead(
size_t num,
T& o,
bufferlist::const_iterator& p)
{
if (!num)
return;
if (p.end())
throw buffer::end_of_buffer();
if constexpr (traits::need_contiguous) {
bufferptr tmp;
auto t = p;
if constexpr (denc_traits<typename T::value_type>::bounded) {
size_t element_size = 0;
typename T::value_type v;
denc_traits<typename T::value_type>::bound_encode(v, element_size);
t.copy_shallow(num * element_size, tmp);
} else {
t.copy_shallow(p.get_bl().length() - p.get_off(), tmp);
}
auto cp = std::cbegin(tmp);
traits::decode_nohead(num, o, cp);
p.advance(cp.get_offset());
} else {
traits::decode_nohead(num, o, p);
}
}
}
// ----------------------------------------------------------------
// DENC
// These are some class methods we need to do the version and length
// wrappers for DENC_{START,FINISH} for inter-version
// interoperability.
#define DENC_HELPERS \
/* bound_encode */ \
static void _denc_start(size_t& p, \
__u8 *struct_v, \
__u8 *struct_compat, \
char **, uint32_t *) { \
p += 2 + 4; \
} \
static void _denc_finish(size_t& p, \
__u8 *struct_v, \
__u8 *struct_compat, \
char **, uint32_t *) { } \
/* encode */ \
static void _denc_start(bufferlist::contiguous_appender& p, \
__u8 *struct_v, \
__u8 *struct_compat, \
char **len_pos, \
uint32_t *start_oob_off) { \
denc(*struct_v, p); \
denc(*struct_compat, p); \
*len_pos = p.get_pos_add(4); \
*start_oob_off = p.get_out_of_band_offset(); \
} \
static void _denc_finish(bufferlist::contiguous_appender& p, \
__u8 *struct_v, \
__u8 *struct_compat, \
char **len_pos, \
uint32_t *start_oob_off) { \
*(ceph_le32*)*len_pos = p.get_pos() - *len_pos - sizeof(uint32_t) + \
p.get_out_of_band_offset() - *start_oob_off; \
} \
/* decode */ \
static void _denc_start(buffer::ptr::const_iterator& p, \
__u8 *struct_v, \
__u8 *struct_compat, \
char **start_pos, \
uint32_t *struct_len) { \
denc(*struct_v, p); \
denc(*struct_compat, p); \
denc(*struct_len, p); \
*start_pos = const_cast<char*>(p.get_pos()); \
} \
static void _denc_finish(buffer::ptr::const_iterator& p, \
__u8 *struct_v, __u8 *struct_compat, \
char **start_pos, \
uint32_t *struct_len) { \
const char *pos = p.get_pos(); \
char *end = *start_pos + *struct_len; \
ceph_assert(pos <= end); \
if (pos < end) { \
p.advance(end - pos); \
} \
}
// Helpers for versioning the encoding. These correspond to the
// {ENCODE,DECODE}_{START,FINISH} macros.
#define DENC_START(v, compat, p) \
__u8 struct_v = v; \
__u8 struct_compat = compat; \
char *_denc_pchar; \
uint32_t _denc_u32; \
_denc_start(p, &struct_v, &struct_compat, &_denc_pchar, &_denc_u32); \
do {
#define DENC_FINISH(p) \
} while (false); \
_denc_finish(p, &struct_v, &struct_compat, &_denc_pchar, &_denc_u32);
// ----------------------------------------------------------------------
// Helpers for writing a unified bound_encode/encode/decode
// implementation that won't screw up buffer size estimations.
#define DENC(Type, v, p) \
DENC_HELPERS \
void bound_encode(size_t& p) const { \
_denc_friend(*this, p); \
} \
void encode(bufferlist::contiguous_appender& p) const { \
DENC_DUMP_PRE(Type); \
_denc_friend(*this, p); \
DENC_DUMP_POST(Type); \
} \
void decode(buffer::ptr::const_iterator& p) { \
_denc_friend(*this, p); \
} \
template<typename T, typename P> \
friend std::enable_if_t<std::is_same_v<T, Type> || \
std::is_same_v<T, const Type>> \
_denc_friend(T& v, P& p)
#define DENC_FEATURED(Type, v, p, f) \
DENC_HELPERS \
void bound_encode(size_t& p, uint64_t f) const { \
_denc_friend(*this, p, f); \
} \
void encode(bufferlist::contiguous_appender& p, uint64_t f) const { \
DENC_DUMP_PRE(Type); \
_denc_friend(*this, p, f); \
DENC_DUMP_POST(Type); \
} \
void decode(buffer::ptr::const_iterator& p, uint64_t f=0) { \
_denc_friend(*this, p, f); \
} \
template<typename T, typename P> \
friend std::enable_if_t<std::is_same_v<T, Type> || \
std::is_same_v<T, const Type>> \
_denc_friend(T& v, P& p, uint64_t f)
#endif
|