Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
radkesvat committed Apr 25, 2024
1 parent 27f7456 commit bb0b08a
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 29 deletions.
8 changes: 3 additions & 5 deletions tunnels/adapters/connector/connector.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ static void upStream(tunnel_t *self, context_t *c)
default:
case kSapTcp:
state->tcp_connector->upStream(state->tcp_connector, c);

break;

case kSapUdp:
state->udp_connector->upStream(state->udp_connector, c);
break;
Expand All @@ -47,10 +47,8 @@ tunnel_t *newConnector(node_instance_context_t *instance_info)
LOGF("JSON Error: Connector->settings (object field) : The object was empty or invalid");
return NULL;
}
node_t *tcp_outbound_node = malloc(sizeof(node_t));
node_t *udp_outbound_node = malloc(sizeof(node_t));
memset(tcp_outbound_node, 0, sizeof(node_t));
memset(udp_outbound_node, 0, sizeof(node_t));
node_t *tcp_outbound_node = newNode();
node_t *udp_outbound_node = newNode();
tcp_outbound_node->name = concat(instance_info->node->name, "_tcp_outbound");
tcp_outbound_node->type = "TcpConnector";
tcp_outbound_node->version = instance_info->node->version;
Expand Down
23 changes: 12 additions & 11 deletions ww/buffer_pool.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ static void firstCharge(buffer_pool_t *pool)

static void reCharge(buffer_pool_t *pool)
{
const size_t increase = pool->cap / 2;
const size_t increase = min((pool->cap - pool->len) - 1, pool->cap / 2);

for (size_t i = pool->len; i < (pool->len + increase); i++)
{
pool->available[i] = newShiftBuffer(pool->buffers_size);
Expand Down Expand Up @@ -65,13 +66,12 @@ shift_buffer_t *popBuffer(buffer_pool_t *pool)
{
reCharge(pool);
}
--(pool->len);
shift_buffer_t *result = pool->available[pool->len];

#ifdef DEBUG
pool->in_use += 1;
#endif
return result;
--(pool->len);
return pool->available[pool->len];
}

void reuseBuffer(buffer_pool_t *pool, shift_buffer_t *b)
Expand All @@ -87,9 +87,8 @@ void reuseBuffer(buffer_pool_t *pool, shift_buffer_t *b)
pool->in_use -= 1;
#endif
reset(b, pool->buffers_size);
pool->available[pool->len] = b;
++(pool->len);
if (pool->len > (pool->cap * 2) / 3)
pool->available[(pool->len)++] = b;
if (pool->len > pool->free_threshould)
{
giveMemBackToOs(pool);
}
Expand Down Expand Up @@ -121,8 +120,9 @@ buffer_pool_t *createBufferPool()
memset(pool, 0xEE, sizeof(buffer_pool_t) + container_len);
#endif
memset(pool, 0, sizeof(buffer_pool_t));
pool->cap = count_max;
pool->buffers_size = BUFFER_SIZE;
pool->cap = count_max;
pool->buffers_size = BUFFER_SIZE;
pool->free_threshould = (pool->cap * 2) / 3;
firstCharge(pool);
return pool;
}
Expand All @@ -136,8 +136,9 @@ buffer_pool_t *createSmallBufferPool()
memset(pool, 0xEE, sizeof(buffer_pool_t) + container_len);
#endif
memset(pool, 0, sizeof(buffer_pool_t));
pool->cap = count_max;
pool->buffers_size = 1024;
pool->cap = count_max;
pool->buffers_size = 1024;
pool->free_threshould = (pool->cap * 2) / 3;
firstCharge(pool);
return pool;
}
1 change: 1 addition & 0 deletions ww/buffer_pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ struct buffer_pool_s
{
unsigned int len;
unsigned int cap;
unsigned int free_threshould;
unsigned int buffers_size;
#ifdef DEBUG
atomic_size_t in_use;
Expand Down
31 changes: 18 additions & 13 deletions ww/managers/node_manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ void registerNode(node_t *new_node, cJSON *node_settings)

new_node_ctx.node_json = NULL;
new_node_ctx.node_settings_json = node_settings;
new_node_ctx.node = new_node;
new_node_ctx.node = new_node;
new_node_ctx.node_file_handle = state->config_file;
new_node->instance_context = new_node_ctx;
map_node_t *map = &(state->node_map);
Expand All @@ -196,15 +196,30 @@ void registerNode(node_t *new_node, cJSON *node_settings)
map_node_t_insert(map, new_node->hash_name, new_node);
}

node_t *getNode(hash_t hash_node_name)
{
map_node_t_iter iter = map_node_t_find(&(state->node_map), hash_node_name);
if (iter.ref == map_node_t_end(&(state->node_map)).ref)
{
return NULL;
}
return (iter.ref->second);
}

node_t *newNode()
{
node_t *new_node = malloc(sizeof(node_t));
memset(new_node, 0, sizeof(node_t));
return new_node;
}

static void startParsingFiles()
{
cJSON *nodes_json = state->config_file->nodes;
cJSON *node_json = NULL;
cJSON_ArrayForEach(node_json, nodes_json)
{
node_t *new_node = malloc(sizeof(node_t));
memset(new_node, 0, sizeof(node_t));
node_t *new_node = newNode();
if (! getStringFromJsonObject(&(new_node->name), node_json, "name"))
{
LOGF("JSON Error: config file \"%s\" -> nodes[x]->name (string field) was empty or invalid",
Expand All @@ -227,16 +242,6 @@ static void startParsingFiles()
runNodes();
}

node_t *getNode(hash_t hash_node_name)
{
map_node_t_iter iter = map_node_t_find(&(state->node_map), hash_node_name);
if (iter.ref == map_node_t_end(&(state->node_map)).ref)
{
return NULL;
}
return (iter.ref->second);
}

static tunnel_t *getTunnel(hash_t hash_node_name)
{
map_node_t_iter iter = map_node_t_find(&(state->node_map), hash_node_name);
Expand Down
1 change: 1 addition & 0 deletions ww/managers/node_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ struct node_manager_s;

void runNode(node_t *n1, size_t chain_index);
node_t * getNode(hash_t hash_node_name);
node_t * newNode();
void registerNode(node_t *new_node, cJSON *node_settings);
void runConfigFile(config_file_t *config_file);
struct node_manager_s *getNodeManager();
Expand Down

0 comments on commit bb0b08a

Please sign in to comment.