Skip to content

Commit 0973dd7

Browse files
committed
wip: queue dynamic resizing
1 parent a2d685f commit 0973dd7

File tree

5 files changed

+52
-15
lines changed

5 files changed

+52
-15
lines changed

source/means-to-an-end/client-session.c

+5-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <stdlib.h>
77
#include <assert.h>
88

9+
#include "utils/queue.h"
910
#include "means-to-an-end/asset-prices.h"
1011
#include "means-to-an-end/client-session.h"
1112

@@ -17,9 +18,12 @@ void clients_session_init(struct clients_session **pca, int client_id)
1718
*pca = malloc(sizeof(struct clients_session));
1819
assert(*pca != NULL);
1920

20-
asset_prices_init(&(*pca)->asset, 64);
21+
asset_prices_init(&(*pca)->asset, 16);
2122
assert((*pca)->asset != NULL);
2223

24+
queue_init(&((*pca)->recv_qu), 512);
25+
assert((*pca)->recv_qu != NULL);
26+
2327
(*pca)->next = NULL;
2428
(*pca)->prev = NULL;
2529
(*pca)->client_id = client_id;

source/means-to-an-end/client-session.h

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ extern "C" {
88
struct clients_session {
99
int client_id; // Client's file descriptor
1010
struct asset_prices *asset;
11+
struct queue *recv_qu;
1112
struct clients_session *next;
1213
struct clients_session *prev;
1314
};

source/utils/queue.c

+43-12
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,38 @@
1313
#include "utils/queue.h"
1414
#include "log/log.h"
1515

16+
void queue_init_data(struct queue* qu, size_t capacity)
17+
{
18+
assert(qu != NULL);
19+
assert(capacity > 0);
20+
assert(capacity > qu->capacity);
21+
22+
char *new_data = NULL;
23+
new_data = realloc(qu->data, capacity);
24+
assert(new_data != NULL);
25+
26+
qu->data = new_data;
27+
qu->capacity = capacity;
28+
qu->free_capacity = capacity - qu->size;
29+
}
30+
31+
void queue_expand_capacity(struct queue* qu, size_t size)
32+
{
33+
assert(qu != NULL);
34+
if (size ==0)
35+
return;
36+
37+
if (qu->size + size < qu->free_capacity)
38+
return;
39+
40+
size_t new_cap = qu->capacity * 2, new_free_cap = new_cap - qu->size;
41+
while (qu->size + size >= new_free_cap) {
42+
new_cap *= 2;
43+
new_free_cap = new_cap - qu->size;
44+
}
45+
queue_init_data(qu, new_cap);
46+
}
47+
1648
/**
1749
* @brief Initializes a queue with the specified capacity.
1850
*
@@ -35,12 +67,11 @@ void queue_init(struct queue** qu, size_t capacity)
3567
*qu = malloc(sizeof(struct queue));
3668
assert(qu != NULL);
3769

38-
(*qu)->data = malloc(capacity);
39-
assert((*qu)->data != NULL);
40-
41-
(*qu)->capacity = capacity;
42-
(*qu)->free_capacity = capacity;
70+
(*qu)->capacity = 0;
4371
(*qu)->size = 0;
72+
(*qu)->data = NULL;
73+
queue_init_data(*qu, capacity);
74+
4475
(*qu)->head = (*qu)->data;
4576
}
4677

@@ -81,13 +112,13 @@ void queue_push(struct queue* qu, char* data, size_t size)
81112
assert(data != NULL);
82113

83114
if (size == 0) {
84-
log_warn("queue_push: size is zero... noop");
115+
log_trace("queue_push: size is zero... noop");
85116
return;
86117
}
87118

88-
if (qu->size + size >= qu->capacity) {
89-
log_error("queue_push: pushing over queue capacity... noop");
90-
return;
119+
if (qu->size + size >= qu->free_capacity) {
120+
queue_expand_capacity(qu, size);
121+
log_info("queue_push: expanding current capacity to %d\n", qu->capacity);
91122
}
92123

93124
memcpy(qu->head, data, size);
@@ -115,9 +146,9 @@ void queue_push_ex(struct queue* qu, size_t size)
115146
return;
116147
}
117148

118-
if (qu->size + size >= qu->capacity) {
119-
log_error("queue_push: pushing over queue capacity... noop");
120-
return;
149+
if (qu->size + size >= qu->free_capacity) {
150+
queue_expand_capacity(qu, size);
151+
log_info("queue_push: expanding current capacity to %d\n", qu->capacity);
121152
}
122153

123154
qu->head += size;

test/means-to-an-end/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22
add_executable(messages-prices-test
33
"${CMAKE_SOURCE_DIR}/source/log/log.c"
4+
"${CMAKE_SOURCE_DIR}/source/utils/queue.c"
45
"${CMAKE_SOURCE_DIR}/source/means-to-an-end/asset-prices.c"
56
"${CMAKE_SOURCE_DIR}/source/means-to-an-end/client-session.c"
67
messages-prices-test.cpp

test/queue/queue-test.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ TEST_CASE("Queue Edge Cases")
8989
queue_push(qu, const_cast<char*>("world"), 5);
9090

9191
// As it should over push the capacity, size should remain the same.
92-
REQUIRE(qu->size == 4);
93-
REQUIRE(qu->free_capacity == 1);
92+
REQUIRE(qu->size == 9);
93+
REQUIRE(qu->capacity == 20);
9494
}
9595

9696
SECTION("Pop Empty Queue")

0 commit comments

Comments
 (0)