diff --git a/tunnels/adapters/connector/connector.c b/tunnels/adapters/connector/connector.c index f4fd5e1d..e532f9f7 100644 --- a/tunnels/adapters/connector/connector.c +++ b/tunnels/adapters/connector/connector.c @@ -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; @@ -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; diff --git a/ww/buffer_pool.c b/ww/buffer_pool.c index 622506f9..3d949e2c 100644 --- a/ww/buffer_pool.c +++ b/ww/buffer_pool.c @@ -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); @@ -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) @@ -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); } @@ -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; } @@ -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; } diff --git a/ww/buffer_pool.h b/ww/buffer_pool.h index 0582b756..b2679b6c 100644 --- a/ww/buffer_pool.h +++ b/ww/buffer_pool.h @@ -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; diff --git a/ww/managers/node_manager.c b/ww/managers/node_manager.c index 6647aaa3..b3a54e8b 100644 --- a/ww/managers/node_manager.c +++ b/ww/managers/node_manager.c @@ -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); @@ -196,6 +196,22 @@ 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() { @@ -203,8 +219,7 @@ static void startParsingFiles() 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", @@ -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); diff --git a/ww/managers/node_manager.h b/ww/managers/node_manager.h index e19a5899..0ca41089 100644 --- a/ww/managers/node_manager.h +++ b/ww/managers/node_manager.h @@ -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();