Skip to content

Commit 259de48

Browse files
committed
cleanup: Remove bin_pack_{new,free}.
We should only ever use `bin_pack_obj` and friends, which stack-allocate the packer and pass it to callbacks.
1 parent 21a8ff5 commit 259de48

File tree

11 files changed

+185
-198
lines changed

11 files changed

+185
-198
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
738c98673260593fc150b8d5b0cb770cd521f469b4eb04c873f19d89bb7238cf /usr/local/bin/tox-bootstrapd
1+
189633f3accb67886c402bf242616d9b3e8258f8050dbd00a10b7c6147ed28aa /usr/local/bin/tox-bootstrapd

toxcore/DHT.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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_size(bin_pack_node_handler, logger, nodes, number);
556-
if (!bin_pack_obj_array(bin_pack_node_handler, logger, nodes, number, data, length)) {
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)) {
557557
return -1;
558558
}
559559
return size;

toxcore/Messenger.c

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3237,22 +3237,17 @@ static uint8_t *groups_save(const Messenger *m, uint8_t *data)
32373237
}
32383238

32393239
non_null()
3240-
static State_Load_Status groups_load(Messenger *m, const uint8_t *data, uint32_t length)
3240+
static bool handle_groups_load(Bin_Unpack *bu, void *obj)
32413241
{
3242-
Bin_Unpack *bu = bin_unpack_new(data, length);
3243-
if (bu == nullptr) {
3244-
LOGGER_ERROR(m->log, "failed to allocate binary unpacker");
3245-
return STATE_LOAD_STATUS_ERROR;
3246-
}
3242+
Messenger *m = (Messenger *)obj;
32473243

32483244
uint32_t num_groups;
32493245
if (!bin_unpack_array(bu, &num_groups)) {
32503246
LOGGER_ERROR(m->log, "msgpack failed to unpack groupchats array: expected array");
3251-
bin_unpack_free(bu);
3252-
return STATE_LOAD_STATUS_ERROR;
3247+
return false;
32533248
}
32543249

3255-
LOGGER_DEBUG(m->log, "Loading %u groups (length %u)", num_groups, length);
3250+
LOGGER_DEBUG(m->log, "Loading %u groups", num_groups);
32563251

32573252
for (uint32_t i = 0; i < num_groups; ++i) {
32583253
const int group_number = gc_group_load(m->group_handler, bu);
@@ -3266,7 +3261,16 @@ static State_Load_Status groups_load(Messenger *m, const uint8_t *data, uint32_t
32663261

32673262
LOGGER_DEBUG(m->log, "Successfully loaded %u groups", gc_count_groups(m->group_handler));
32683263

3269-
bin_unpack_free(bu);
3264+
return true;
3265+
}
3266+
3267+
non_null()
3268+
static State_Load_Status groups_load(Messenger *m, const uint8_t *data, uint32_t length)
3269+
{
3270+
if (!bin_unpack_obj(handle_groups_load, m, data, length)) {
3271+
LOGGER_ERROR(m->log, "msgpack failed to unpack groupchats array");
3272+
return STATE_LOAD_STATUS_ERROR;
3273+
}
32703274

32713275
return STATE_LOAD_STATUS_CONTINUE;
32723276
}

toxcore/bin_pack.c

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
#include "bin_pack.h"
66

77
#include <assert.h>
8-
#include <stdlib.h>
98
#include <string.h>
109

1110
#include "../third_party/cmp/cmp.h"
@@ -34,11 +33,11 @@ static bool null_skipper(cmp_ctx_t *ctx, size_t limit)
3433
}
3534

3635
non_null()
37-
static size_t buf_writer(cmp_ctx_t *ctx, const void *data, size_t count)
36+
static size_t buf_writer(cmp_ctx_t *ctx, const void *data, size_t data_size)
3837
{
3938
Bin_Pack *bp = (Bin_Pack *)ctx->buf;
4039
assert(bp != nullptr);
41-
const uint32_t new_pos = bp->bytes_pos + count;
40+
const uint32_t new_pos = bp->bytes_pos + data_size;
4241
if (new_pos < bp->bytes_pos) {
4342
// 32 bit overflow.
4443
return 0;
@@ -48,10 +47,10 @@ static size_t buf_writer(cmp_ctx_t *ctx, const void *data, size_t count)
4847
// Buffer too small.
4948
return 0;
5049
}
51-
memcpy(bp->bytes + bp->bytes_pos, data, count);
50+
memcpy(&bp->bytes[bp->bytes_pos], data, data_size);
5251
}
53-
bp->bytes_pos += count;
54-
return count;
52+
bp->bytes_pos += data_size;
53+
return data_size;
5554
}
5655

5756
non_null(1) nullable(2)
@@ -80,45 +79,30 @@ bool bin_pack_obj(bin_pack_cb *callback, const Logger *logger, const void *obj,
8079
return callback(&bp, logger, obj);
8180
}
8281

83-
uint32_t bin_pack_obj_array_size(bin_pack_array_cb *callback, const Logger *logger, const void *arr, uint32_t count)
82+
uint32_t bin_pack_obj_array_b_size(bin_pack_array_cb *callback, const Logger *logger, const void *arr, uint32_t arr_size)
8483
{
8584
Bin_Pack bp;
8685
bin_pack_init(&bp, nullptr, 0);
87-
for (uint32_t i = 0; i < count; ++i) {
86+
for (uint32_t i = 0; i < arr_size; ++i) {
8887
if (!callback(&bp, logger, arr, i)) {
8988
return UINT32_MAX;
9089
}
9190
}
9291
return bp.bytes_pos;
9392
}
9493

95-
bool bin_pack_obj_array(bin_pack_array_cb *callback, const Logger *logger, const void *arr, uint32_t count, uint8_t *buf, uint32_t buf_size)
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)
9695
{
9796
Bin_Pack bp;
9897
bin_pack_init(&bp, buf, buf_size);
99-
for (uint32_t i = 0; i < count; ++i) {
98+
for (uint32_t i = 0; i < arr_size; ++i) {
10099
if (!callback(&bp, logger, arr, i)) {
101100
return false;
102101
}
103102
}
104103
return true;
105104
}
106105

107-
Bin_Pack *bin_pack_new(uint8_t *buf, uint32_t buf_size)
108-
{
109-
Bin_Pack *bp = (Bin_Pack *)calloc(1, sizeof(Bin_Pack));
110-
if (bp == nullptr) {
111-
return nullptr;
112-
}
113-
bin_pack_init(bp, buf, buf_size);
114-
return bp;
115-
}
116-
117-
void bin_pack_free(Bin_Pack *bp)
118-
{
119-
free(bp);
120-
}
121-
122106
bool bin_pack_array(Bin_Pack *bp, uint32_t size)
123107
{
124108
return cmp_write_array(&bp->ctx, size);

toxcore/bin_pack.h

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -69,27 +69,24 @@ bool bin_pack_obj(bin_pack_cb *callback, const Logger *logger, const void *obj,
6969

7070
/** @brief Determine the serialised size of an object array.
7171
*
72-
* Calls the callback `count` times with increasing `index` argument from 0 to
73-
* `count`. This function is here just so we don't need to write the same
74-
* trivial loop many times and so we don't need an extra struct just to contain
75-
* an array with size so it can be passed to `bin_pack_obj_size`.
72+
* Behaves exactly like `bin_pack_obj_b_array` but doesn't write.
7673
*
7774
* @param callback The function called on the created packer and each object to
7875
* be packed.
7976
* @param logger Optional logger object to pass to the callback.
8077
* @param arr The object array to be packed, passed as `arr` to the callback.
81-
* @param count The number of elements in the object array.
78+
* @param arr_size The number of elements in the object array.
8279
*
8380
* @return The packed size of the passed object array according to the callback.
8481
* @retval UINT32_MAX in case of errors such as buffer overflow.
8582
*/
8683
non_null(1, 3) nullable(2)
87-
uint32_t bin_pack_obj_array_size(bin_pack_array_cb *callback, const Logger *logger, const void *arr, uint32_t count);
84+
uint32_t bin_pack_obj_array_b_size(bin_pack_array_cb *callback, const Logger *logger, const void *arr, uint32_t arr_size);
8885

8986
/** @brief Pack an object array into a buffer of a given size.
9087
*
91-
* Calls the callback `count` times with increasing `index` argument from 0 to
92-
* `count`. This function is here just so we don't need to write the same
88+
* Calls the callback `arr_size` times with increasing `index` argument from 0 to
89+
* `arr_size`. This function is here just so we don't need to write the same
9390
* trivial loop many times and so we don't need an extra struct just to contain
9491
* an array with size so it can be passed to `bin_pack_obj`.
9592
*
@@ -100,33 +97,14 @@ uint32_t bin_pack_obj_array_size(bin_pack_array_cb *callback, const Logger *logg
10097
* array.
10198
* @param logger Optional logger object to pass to the callback.
10299
* @param arr The object array to be packed, passed as `arr` to the callback.
103-
* @param count The number of elements in the object array.
100+
* @param arr_size The number of elements in the object array.
104101
* @param buf A byte array large enough to hold the serialised representation of `arr`.
105102
* @param buf_size The size of the byte array. Can be `UINT32_MAX` to disable bounds checking.
106103
*
107104
* @retval false if an error occurred (e.g. buffer overflow).
108105
*/
109106
non_null(1, 3, 5) nullable(2)
110-
bool bin_pack_obj_array(bin_pack_array_cb *callback, const Logger *logger, const void *arr, uint32_t count, uint8_t *buf, uint32_t buf_size);
111-
112-
/** @brief Allocate a new packer object.
113-
*
114-
* This is the only function that allocates memory in this module.
115-
*
116-
* @param buf A byte array large enough to hold the serialised representation of `obj`.
117-
* @param buf_size The size of the byte array. Can be `UINT32_MAX` to disable bounds checking.
118-
*
119-
* @retval nullptr on allocation failure.
120-
*/
121-
non_null()
122-
Bin_Pack *bin_pack_new(uint8_t *buf, uint32_t buf_size);
123-
124-
/** @brief Deallocates a packer object.
125-
*
126-
* Does not deallocate the buffer inside.
127-
*/
128-
nullable(1)
129-
void bin_pack_free(Bin_Pack *bp);
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);
130108

131109
/** @brief Start packing a MessagePack array.
132110
*

0 commit comments

Comments
 (0)