Skip to content

Commit c650d9d

Browse files
committed
refactor: Pass this pointer as first param to s11n callbacks.
1 parent e7fb91d commit c650d9d

File tree

9 files changed

+87
-60
lines changed

9 files changed

+87
-60
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
4384074ef96cf8f1ed3c420a58b7f67a49a4e22ad5b8fe1d66a4bddf61235fce /usr/local/bin/tox-bootstrapd
1+
c31bc1cd7249c62b4f1d2c9ea26c9e8528d9b897dc29afbdbe95894d30c92c8d /usr/local/bin/tox-bootstrapd

toxcore/DHT.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -420,21 +420,21 @@ static bool bin_pack_ip_port(Bin_Pack *bp, const Logger *logger, const IP_Port *
420420
}
421421

422422
non_null()
423-
static bool bin_pack_ip_port_handler(Bin_Pack *bp, const Logger *logger, const void *obj)
423+
static bool bin_pack_ip_port_handler(const void *obj, const Logger *logger, Bin_Pack *bp)
424424
{
425425
const IP_Port *ip_port = (const IP_Port *)obj;
426426
return bin_pack_ip_port(bp, logger, ip_port);
427427
}
428428

429429
int pack_ip_port(const Logger *logger, uint8_t *data, uint16_t length, const IP_Port *ip_port)
430430
{
431-
const uint32_t size = bin_pack_obj_size(bin_pack_ip_port_handler, logger, ip_port);
431+
const uint32_t size = bin_pack_obj_size(bin_pack_ip_port_handler, ip_port, logger);
432432

433433
if (size > length) {
434434
return -1;
435435
}
436436

437-
if (!bin_pack_obj(bin_pack_ip_port_handler, logger, ip_port, data, length)) {
437+
if (!bin_pack_obj(bin_pack_ip_port_handler, ip_port, logger, data, length)) {
438438
return -1;
439439
}
440440

@@ -543,7 +543,7 @@ int unpack_ip_port(IP_Port *ip_port, const uint8_t *data, uint16_t length, bool
543543
* @retval true on success.
544544
*/
545545
non_null()
546-
static bool bin_pack_node_handler(Bin_Pack *bp, const Logger *logger, const void *arr, uint32_t index)
546+
static bool bin_pack_node_handler(const void *arr, uint32_t index, const Logger *logger, Bin_Pack *bp)
547547
{
548548
const Node_format *nodes = (const Node_format *)arr;
549549
return bin_pack_ip_port(bp, logger, &nodes[index].ip_port)
@@ -552,8 +552,8 @@ static bool bin_pack_node_handler(Bin_Pack *bp, const Logger *logger, const void
552552

553553
int pack_nodes(const Logger *logger, uint8_t *data, uint16_t length, const Node_format *nodes, uint16_t number)
554554
{
555-
const uint32_t size = bin_pack_obj_array_b_size(bin_pack_node_handler, logger, nodes, number);
556-
if (!bin_pack_obj_array_b(bin_pack_node_handler, logger, nodes, number, data, length)) {
555+
const uint32_t size = bin_pack_obj_array_b_size(bin_pack_node_handler, nodes, number, logger);
556+
if (!bin_pack_obj_array_b(bin_pack_node_handler, nodes, number, logger, data, length)) {
557557
return -1;
558558
}
559559
return size;

toxcore/Messenger.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3191,7 +3191,7 @@ static void pack_groupchats(const GC_Session *c, Bin_Pack *bp)
31913191
}
31923192

31933193
non_null()
3194-
static bool pack_groupchats_handler(Bin_Pack *bp, const Logger *log, const void *obj)
3194+
static bool pack_groupchats_handler(const void *obj, const Logger *log, Bin_Pack *bp)
31953195
{
31963196
const GC_Session *session = (const GC_Session *)obj;
31973197
pack_groupchats(session, bp);
@@ -3201,8 +3201,8 @@ static bool pack_groupchats_handler(Bin_Pack *bp, const Logger *log, const void
32013201
non_null()
32023202
static uint32_t saved_groups_size(const Messenger *m)
32033203
{
3204-
const GC_Session *c = m->group_handler;
3205-
return bin_pack_obj_size(pack_groupchats_handler, m->log, c);
3204+
const GC_Session *session = m->group_handler;
3205+
return bin_pack_obj_size(pack_groupchats_handler, session, m->log);
32063206
}
32073207

32083208
non_null()
@@ -3224,7 +3224,7 @@ static uint8_t *groups_save(const Messenger *m, uint8_t *data)
32243224

32253225
data = state_write_section_header(data, STATE_COOKIE_TYPE, len, STATE_TYPE_GROUPS);
32263226

3227-
if (!bin_pack_obj(pack_groupchats_handler, m->log, c, data, len)) {
3227+
if (!bin_pack_obj(pack_groupchats_handler, c, m->log, data, len)) {
32283228
LOGGER_FATAL(m->log, "failed to pack group chats into buffer of length %u", len);
32293229
return data;
32303230
}
@@ -3237,7 +3237,7 @@ static uint8_t *groups_save(const Messenger *m, uint8_t *data)
32373237
}
32383238

32393239
non_null()
3240-
static bool handle_groups_load(Bin_Unpack *bu, void *obj)
3240+
static bool handle_groups_load(void *obj, Bin_Unpack *bu)
32413241
{
32423242
Messenger *m = (Messenger *)obj;
32433243

toxcore/bin_pack.c

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,41 +62,47 @@ static void bin_pack_init(Bin_Pack *bp, uint8_t *buf, uint32_t buf_size)
6262
cmp_init(&bp->ctx, bp, null_reader, null_skipper, buf_writer);
6363
}
6464

65-
uint32_t bin_pack_obj_size(bin_pack_cb *callback, const Logger *logger, const void *obj)
65+
uint32_t bin_pack_obj_size(bin_pack_cb *callback, const void *obj, const Logger *logger)
6666
{
6767
Bin_Pack bp;
6868
bin_pack_init(&bp, nullptr, 0);
69-
if (!callback(&bp, logger, obj)) {
69+
if (!callback(obj, logger, &bp)) {
7070
return UINT32_MAX;
7171
}
7272
return bp.bytes_pos;
7373
}
7474

75-
bool bin_pack_obj(bin_pack_cb *callback, const Logger *logger, const void *obj, uint8_t *buf, uint32_t buf_size)
75+
bool bin_pack_obj(bin_pack_cb *callback, const void *obj, const Logger *logger, uint8_t *buf, uint32_t buf_size)
7676
{
7777
Bin_Pack bp;
7878
bin_pack_init(&bp, buf, buf_size);
79-
return callback(&bp, logger, obj);
79+
return callback(obj, logger, &bp);
8080
}
8181

82-
uint32_t bin_pack_obj_array_b_size(bin_pack_array_cb *callback, const Logger *logger, const void *arr, uint32_t arr_size)
82+
uint32_t bin_pack_obj_array_b_size(bin_pack_array_cb *callback, const void *arr, uint32_t arr_size, const Logger *logger)
8383
{
8484
Bin_Pack bp;
8585
bin_pack_init(&bp, nullptr, 0);
86+
if (arr == nullptr) {
87+
assert(arr_size == 0);
88+
}
8689
for (uint32_t i = 0; i < arr_size; ++i) {
87-
if (!callback(&bp, logger, arr, i)) {
90+
if (!callback(arr, i, logger, &bp)) {
8891
return UINT32_MAX;
8992
}
9093
}
9194
return bp.bytes_pos;
9295
}
9396

94-
bool bin_pack_obj_array_b(bin_pack_array_cb *callback, const Logger *logger, const void *arr, uint32_t arr_size, uint8_t *buf, uint32_t buf_size)
97+
bool bin_pack_obj_array_b(bin_pack_array_cb *callback, const void *arr, uint32_t arr_size, const Logger *logger, uint8_t *buf, uint32_t buf_size)
9598
{
9699
Bin_Pack bp;
97100
bin_pack_init(&bp, buf, buf_size);
101+
if (arr == nullptr) {
102+
assert(arr_size == 0);
103+
}
98104
for (uint32_t i = 0; i < arr_size; ++i) {
99-
if (!callback(&bp, logger, arr, i)) {
105+
if (!callback(arr, i, logger, &bp)) {
100106
return false;
101107
}
102108
}
@@ -175,4 +181,3 @@ bool bin_pack_bin_b(Bin_Pack *bp, const uint8_t *data, uint32_t length)
175181
{
176182
return bp->ctx.write(&bp->ctx, data, length) == length;
177183
}
178-

toxcore/bin_pack.h

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,21 @@ extern "C" {
1616

1717
/**
1818
* @brief Binary serialisation object.
19+
*
20+
* Some notes on parameter order:
21+
*
22+
* - We pass the `obj` pointer as `this`-like pointer first to the callbacks.
23+
* - Any extra arguments passed to the callback follow the `obj` (and in case of
24+
* array packing, the `arr` and `arr_size` parameters).
25+
* - The packer is passed last.
26+
*
27+
* This roughly matches a curried lambda function:
28+
*
29+
* @code
30+
* bin_pack_obj([](const void *obj, const Logger *logger, Bin_Pack *bp) { ... }, obj, logger, buf, buf_size);
31+
* // Translates roughly to:
32+
* bin_pack_obj([obj, logger](Bin_Pack *bp) { ... }, buf, buf_size);
33+
* @endcode
1934
*/
2035
typedef struct Bin_Pack Bin_Pack;
2136

@@ -24,7 +39,7 @@ typedef struct Bin_Pack Bin_Pack;
2439
* This function would typically cast the `void *` to the actual object pointer type and then call
2540
* more appropriately typed packing functions.
2641
*/
27-
typedef bool bin_pack_cb(Bin_Pack *bp, const Logger *logger, const void *obj);
42+
typedef bool bin_pack_cb(const void *obj, const Logger *logger, Bin_Pack *bp);
2843

2944
/** @brief Function used to pack an array of objects.
3045
*
@@ -34,19 +49,19 @@ typedef bool bin_pack_cb(Bin_Pack *bp, const Logger *logger, const void *obj);
3449
* @param arr is the object array as void pointer.
3550
* @param index is the index in the object array that is currently being packed.
3651
*/
37-
typedef bool bin_pack_array_cb(Bin_Pack *bp, const Logger *logger, const void *arr, uint32_t index);
52+
typedef bool bin_pack_array_cb(const void *arr, uint32_t index, const Logger *logger, Bin_Pack *bp);
3853

3954
/** @brief Determine the serialised size of an object.
4055
*
4156
* @param callback The function called on the created packer and packed object.
42-
* @param logger Optional logger object to pass to the callback.
4357
* @param obj The object to be packed, passed as `obj` to the callback.
58+
* @param logger Optional logger object to pass to the callback.
4459
*
4560
* @return The packed size of the passed object according to the callback.
4661
* @retval UINT32_MAX in case of errors such as buffer overflow.
4762
*/
4863
non_null(1) nullable(2, 3)
49-
uint32_t bin_pack_obj_size(bin_pack_cb *callback, const Logger *logger, const void *obj);
64+
uint32_t bin_pack_obj_size(bin_pack_cb *callback, const void *obj, const Logger *logger);
5065

5166
/** @brief Pack an object into a buffer of a given size.
5267
*
@@ -56,32 +71,34 @@ uint32_t bin_pack_obj_size(bin_pack_cb *callback, const Logger *logger, const vo
5671
* You can use `bin_pack_obj_size` to determine the minimum required size of `buf`. If packing
5772
* overflows `uint32_t`, this function returns `false`.
5873
*
74+
* Passing NULL for `obj` is supported, but requires that the callback supports nullable inputs.
75+
*
5976
* @param callback The function called on the created packer and packed object.
60-
* @param logger Optional logger object to pass to the callback.
6177
* @param obj The object to be packed, passed as `obj` to the callback.
78+
* @param logger Optional logger object to pass to the callback.
6279
* @param buf A byte array large enough to hold the serialised representation of `obj`.
6380
* @param buf_size The size of the byte array. Can be `UINT32_MAX` to disable bounds checking.
6481
*
6582
* @retval false if an error occurred (e.g. buffer overflow).
6683
*/
6784
non_null(1, 4) nullable(2, 3)
68-
bool bin_pack_obj(bin_pack_cb *callback, const Logger *logger, const void *obj, uint8_t *buf, uint32_t buf_size);
85+
bool bin_pack_obj(bin_pack_cb *callback, const void *obj, const Logger *logger, uint8_t *buf, uint32_t buf_size);
6986

7087
/** @brief Determine the serialised size of an object array.
7188
*
7289
* Behaves exactly like `bin_pack_obj_b_array` but doesn't write.
7390
*
7491
* @param callback The function called on the created packer and each object to
7592
* be packed.
76-
* @param logger Optional logger object to pass to the callback.
7793
* @param arr The object array to be packed, passed as `arr` to the callback.
7894
* @param arr_size The number of elements in the object array.
95+
* @param logger Optional logger object to pass to the callback.
7996
*
8097
* @return The packed size of the passed object array according to the callback.
8198
* @retval UINT32_MAX in case of errors such as buffer overflow.
8299
*/
83-
non_null(1, 3) nullable(2)
84-
uint32_t bin_pack_obj_array_b_size(bin_pack_array_cb *callback, const Logger *logger, const void *arr, uint32_t arr_size);
100+
non_null(1) nullable(2, 4)
101+
uint32_t bin_pack_obj_array_b_size(bin_pack_array_cb *callback, const void *arr, uint32_t arr_size, const Logger *logger);
85102

86103
/** @brief Pack an object array into a buffer of a given size.
87104
*
@@ -93,18 +110,20 @@ uint32_t bin_pack_obj_array_b_size(bin_pack_array_cb *callback, const Logger *lo
93110
* Similar to `bin_pack_obj` but for arrays. Does not write the array length, so
94111
* if you need that, write it manually using `bin_pack_array`.
95112
*
113+
* Passing NULL for `arr` has no effect, but requires that `arr_size` is 0.
114+
*
96115
* @param callback The function called on the created packer and packed object
97116
* array.
98-
* @param logger Optional logger object to pass to the callback.
99117
* @param arr The object array to be packed, passed as `arr` to the callback.
100118
* @param arr_size The number of elements in the object array.
119+
* @param logger Optional logger object to pass to the callback.
101120
* @param buf A byte array large enough to hold the serialised representation of `arr`.
102121
* @param buf_size The size of the byte array. Can be `UINT32_MAX` to disable bounds checking.
103122
*
104123
* @retval false if an error occurred (e.g. buffer overflow).
105124
*/
106-
non_null(1, 3, 5) nullable(2)
107-
bool bin_pack_obj_array_b(bin_pack_array_cb *callback, const Logger *logger, const void *arr, uint32_t arr_size, uint8_t *buf, uint32_t buf_size);
125+
non_null(1, 5) nullable(2, 4)
126+
bool bin_pack_obj_array_b(bin_pack_array_cb *callback, const void *arr, uint32_t arr_size, const Logger *logger, uint8_t *buf, uint32_t buf_size);
108127

109128
/** @brief Start packing a MessagePack array.
110129
*

toxcore/bin_pack_test.cc

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,25 @@ TEST(BinPack, TooSmallBufferIsNotExceeded)
1515
const uint64_t orig = 1234567812345678LL;
1616
std::array<uint8_t, sizeof(orig) - 1> buf;
1717
EXPECT_FALSE(bin_pack_obj(
18-
[](Bin_Pack *bp, const Logger *logger, const void *obj) {
18+
[](const void *obj, const Logger *logger, Bin_Pack *bp) {
1919
return bin_pack_u64_b(bp, *static_cast<const uint64_t *>(obj));
2020
},
21-
nullptr, &orig, buf.data(), buf.size()));
21+
&orig, nullptr, buf.data(), buf.size()));
2222
}
2323

2424
TEST(BinPack, PackedUint64CanBeUnpacked)
2525
{
2626
const uint64_t orig = 1234567812345678LL;
2727
std::array<uint8_t, 8> buf;
2828
EXPECT_TRUE(bin_pack_obj(
29-
[](Bin_Pack *bp, const Logger *logger, const void *obj) {
29+
[](const void *obj, const Logger *logger, Bin_Pack *bp) {
3030
return bin_pack_u64_b(bp, *static_cast<const uint64_t *>(obj));
3131
},
32-
nullptr, &orig, buf.data(), buf.size()));
32+
&orig, nullptr, buf.data(), buf.size()));
3333

3434
uint64_t unpacked;
3535
EXPECT_TRUE(bin_unpack_obj(
36-
[](Bin_Unpack *bu, void *obj) {
36+
[](void *obj, Bin_Unpack *bu) {
3737
return bin_unpack_u64_b(bu, static_cast<uint64_t *>(obj));
3838
},
3939
&unpacked, buf.data(), buf.size()));
@@ -45,14 +45,14 @@ TEST(BinPack, MsgPackedUint8CanBeUnpackedAsUint32)
4545
const uint8_t orig = 123;
4646
std::array<uint8_t, 2> buf;
4747
EXPECT_TRUE(bin_pack_obj(
48-
[](Bin_Pack *bp, const Logger *logger, const void *obj) {
48+
[](const void *obj, const Logger *logger, Bin_Pack *bp) {
4949
return bin_pack_u08(bp, *static_cast<const uint8_t *>(obj));
5050
},
51-
nullptr, &orig, buf.data(), buf.size()));
51+
&orig, nullptr, buf.data(), buf.size()));
5252

5353
uint32_t unpacked;
5454
EXPECT_TRUE(bin_unpack_obj(
55-
[](Bin_Unpack *bu, void *obj) { return bin_unpack_u32(bu, static_cast<uint32_t *>(obj)); },
55+
[](void *obj, Bin_Unpack *bu) { return bin_unpack_u32(bu, static_cast<uint32_t *>(obj)); },
5656
&unpacked, buf.data(), buf.size()));
5757
EXPECT_EQ(unpacked, 123);
5858
}
@@ -62,14 +62,14 @@ TEST(BinPack, MsgPackedUint32CanBeUnpackedAsUint8IfSmallEnough)
6262
const uint32_t orig = 123;
6363
std::array<uint8_t, 2> buf;
6464
EXPECT_TRUE(bin_pack_obj(
65-
[](Bin_Pack *bp, const Logger *logger, const void *obj) {
65+
[](const void *obj, const Logger *logger, Bin_Pack *bp) {
6666
return bin_pack_u32(bp, *static_cast<const uint32_t *>(obj));
6767
},
68-
nullptr, &orig, buf.data(), buf.size()));
68+
&orig, nullptr, buf.data(), buf.size()));
6969

7070
uint8_t unpacked;
7171
EXPECT_TRUE(bin_unpack_obj(
72-
[](Bin_Unpack *bu, void *obj) { return bin_unpack_u08(bu, static_cast<uint8_t *>(obj)); },
72+
[](void *obj, Bin_Unpack *bu) { return bin_unpack_u08(bu, static_cast<uint8_t *>(obj)); },
7373
&unpacked, buf.data(), buf.size()));
7474

7575
EXPECT_EQ(unpacked, 123);
@@ -80,14 +80,14 @@ TEST(BinPack, LargeMsgPackedUint32CannotBeUnpackedAsUint8)
8080
const uint32_t orig = 1234567;
8181
std::array<uint8_t, 5> buf;
8282
EXPECT_TRUE(bin_pack_obj(
83-
[](Bin_Pack *bp, const Logger *logger, const void *obj) {
83+
[](const void *obj, const Logger *logger, Bin_Pack *bp) {
8484
return bin_pack_u32(bp, *static_cast<const uint32_t *>(obj));
8585
},
86-
nullptr, &orig, buf.data(), buf.size()));
86+
&orig, nullptr, buf.data(), buf.size()));
8787

8888
uint8_t unpacked;
8989
EXPECT_FALSE(bin_unpack_obj(
90-
[](Bin_Unpack *bu, void *obj) { return bin_unpack_u08(bu, static_cast<uint8_t *>(obj)); },
90+
[](void *obj, Bin_Unpack *bu) { return bin_unpack_u08(bu, static_cast<uint8_t *>(obj)); },
9191
&unpacked, buf.data(), buf.size()));
9292
}
9393

@@ -102,17 +102,17 @@ TEST(BinPack, BinCanHoldPackedInts)
102102

103103
std::array<uint8_t, 12> buf;
104104
EXPECT_TRUE(bin_pack_obj(
105-
[](Bin_Pack *bp, const Logger *logger, const void *obj) {
105+
[](const void *obj, const Logger *logger, Bin_Pack *bp) {
106106
const Stuff *self = static_cast<const Stuff *>(obj);
107107
return bin_pack_bin_marker(bp, packed_size) //
108108
&& bin_pack_u64_b(bp, self->u64) //
109109
&& bin_pack_u16_b(bp, self->u16);
110110
},
111-
nullptr, &orig, buf.data(), buf.size()));
111+
&orig, nullptr, buf.data(), buf.size()));
112112

113113
Stuff unpacked;
114114
EXPECT_TRUE(bin_unpack_obj(
115-
[](Bin_Unpack *bu, void *obj) {
115+
[](void *obj, Bin_Unpack *bu) {
116116
Stuff *stuff = static_cast<Stuff *>(obj);
117117
uint32_t size;
118118
return bin_unpack_bin_size(bu, &size) //
@@ -129,15 +129,15 @@ TEST(BinPack, BinCanHoldArbitraryData)
129129
{
130130
std::array<uint8_t, 7> buf;
131131
EXPECT_TRUE(bin_pack_obj(
132-
[](Bin_Pack *bp, const Logger *logger, const void *obj) {
132+
[](const void *obj, const Logger *logger, Bin_Pack *bp) {
133133
return bin_pack_bin_marker(bp, 5) //
134134
&& bin_pack_bin_b(bp, reinterpret_cast<const uint8_t *>("hello"), 5);
135135
},
136136
nullptr, nullptr, buf.data(), buf.size()));
137137

138138
std::array<uint8_t, 5> str;
139139
EXPECT_TRUE(bin_unpack_obj(
140-
[](Bin_Unpack *bu, void *obj) {
140+
[](void *obj, Bin_Unpack *bu) {
141141
uint8_t *data = static_cast<uint8_t *>(obj);
142142
return bin_unpack_bin_fixed(bu, data, 5);
143143
},
@@ -151,7 +151,7 @@ TEST(BinPack, OversizedArrayFailsUnpack)
151151

152152
uint32_t size;
153153
EXPECT_FALSE(bin_unpack_obj(
154-
[](Bin_Unpack *bu, void *obj) {
154+
[](void *obj, Bin_Unpack *bu) {
155155
uint32_t *size_ptr = static_cast<uint32_t *>(obj);
156156
return bin_unpack_array(bu, size_ptr);
157157
},

0 commit comments

Comments
 (0)