Skip to content

Commit 0fe0cac

Browse files
committed
MINOR: changed 'struct buffer' size allocation
To make buffer allocation more efficient, memory is assigned in 32-byte blocks. This is made possible using the new ALIGN_VALUE() macro.
1 parent 51213e8 commit 0fe0cac

File tree

3 files changed

+7
-5
lines changed

3 files changed

+7
-5
lines changed

include/common/define.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
#define STR_ADDRSIZE(a) (a), STR_SIZE(a)
6363
#define STR_BOOL(a) ((a) ? "true" : "false")
6464

65+
#define ALIGN_VALUE(v,b) (((v) == 0) ? 0 : (((((v) - 1) >> (b)) + 1) << (b)))
6566
#define CLAMP_VALUE(v,a,b) (((v) < (a)) ? (a) : (((v) > (b)) ? (b) : (v)))
6667

6768
#define IN_RANGE(v,a,b) (((v) >= (a)) && ((v) <= (b)))

src/.build-counter

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2411
1+
2412

src/util.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ void buffer_ptr_free(struct buffer **data)
199199
ssize_t buffer_grow(struct buffer *data, const void *src, size_t n)
200200
{
201201
uint8_t *ptr;
202+
size_t size = ALIGN_VALUE(n, 5);
202203
int retval = FUNC_RET_ERROR;
203204

204205
DBG_FUNC(NULL, "%p, %p, %zu", data, src, n);
@@ -222,18 +223,18 @@ ssize_t buffer_grow(struct buffer *data, const void *src, size_t n)
222223

223224
retval = data->len;
224225
}
225-
else if (_NULL(ptr = realloc(data->ptr, data->size + n))) {
226+
else if (_NULL(ptr = realloc(data->ptr, data->size + size))) {
226227
w_log(NULL, _E("Failed to allocate data: %m"));
227228
}
228229
else {
229230
W_DBG(NOTICE, NULL, " reallocating buffer { %p, %zu, %zu } -> { %p, %zu, %zu }",
230-
data->ptr, data->len, data->size, ptr, data->len + (_NULL(src) ? 0 : n), data->size + n);
231+
data->ptr, data->len, data->size, ptr, data->len + (_NULL(src) ? 0 : n), data->size + size);
231232

232233
if (_NULL(src)) {
233234
/* Clearing allocated buffer. */
234-
(void)memset(ptr + data->size, 0, data->size - n);
235+
(void)memset(ptr + data->size, 0, size);
235236

236-
data->size += n;
237+
data->size += size;
237238
} else {
238239
/* Copying src data to buffer. */
239240
(void)memcpy(ptr + data->len, src, n);

0 commit comments

Comments
 (0)