Skip to content

Commit

Permalink
reformat
Browse files Browse the repository at this point in the history
  • Loading branch information
radkesvat committed Aug 3, 2024
1 parent 1a83904 commit ba6bbdf
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 43 deletions.
1 change: 1 addition & 0 deletions ww/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ add_library(ww STATIC
config_file.c
buffer_pool.c
generic_pool.c
master_pool.c
http_def.c
cacert.c
sync_dns.c
Expand Down
28 changes: 15 additions & 13 deletions ww/buffer_pool.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ static void firstCharge(buffer_pool_t *pool)
{
for (size_t i = 0; i < (pool->cap / 2); i++)
{
pool->available[i] = newShiftBuffer(pool->tid,pool->buffers_size);
pool->available[i] = newShiftBuffer(pool->tid, pool->buffers_size);
}
pool->len = pool->cap / 2;
}
Expand All @@ -64,7 +64,7 @@ static void reCharge(buffer_pool_t *pool)

for (size_t i = pool->len; i < (pool->len + increase); i++)
{
pool->available[i] = newShiftBuffer(pool->tid,pool->buffers_size);
pool->available[i] = newShiftBuffer(pool->tid, pool->buffers_size);
}
pool->len += increase;
#if defined(DEBUG) && defined(BUFFER_POOL_DEBUG)
Expand All @@ -78,7 +78,7 @@ static void giveMemBackToOs(buffer_pool_t *pool)

for (size_t i = pool->len - decrease; i < pool->len; i++)
{
destroyShiftBuffer(pool->tid,pool->available[i]);
destroyShiftBuffer(pool->tid, pool->available[i]);
}
pool->len -= decrease;

Expand All @@ -95,15 +95,17 @@ shift_buffer_t *popBuffer(buffer_pool_t *pool)
#if defined(DEBUG) && defined(BYPASS_BUFFERPOOL)
return newShiftBuffer(pool->buffers_size);
#endif
#if defined(DEBUG) && defined(BUFFER_POOL_DEBUG)
pool->in_use += 1;
#endif

if (pool->len <= 0)
if (pool->len > 0)
{
reCharge(pool);
--(pool->len);
return pool->available[pool->len];
}
reCharge(pool);

#if defined(DEBUG) && defined(BUFFER_POOL_DEBUG)
pool->in_use += 1;
#endif
--(pool->len);
return pool->available[pool->len];
}
Expand All @@ -117,7 +119,7 @@ void reuseBuffer(buffer_pool_t *pool, shift_buffer_t *b)

if (isShallow(b))
{
destroyShiftBuffer(pool->tid,b);
destroyShiftBuffer(pool->tid, b);
return;
}
#if defined(DEBUG) && defined(BUFFER_POOL_DEBUG)
Expand Down Expand Up @@ -147,7 +149,7 @@ shift_buffer_t *appendBufferMerge(buffer_pool_t *pool, shift_buffer_t *restrict
return b2;
}

static buffer_pool_t *allocBufferPool(uint8_t tid,unsigned long bufcount, unsigned int buffer_size) // NOLINT
static buffer_pool_t *allocBufferPool(uint8_t tid, unsigned long bufcount, unsigned int buffer_size) // NOLINT
{
// stop using pool if you want less, simply uncomment lines in popbuffer and reuseBuffer
assert(bufcount >= 1);
Expand All @@ -164,17 +166,17 @@ static buffer_pool_t *allocBufferPool(uint8_t tid,unsigned long bufcount, unsign
pool->cap = bufcount;
pool->buffers_size = buffer_size;
pool->free_threshould = max(pool->cap / 2, (pool->cap * 2) / 3);
pool->tid = tid;
pool->tid = tid;
firstCharge(pool);
return pool;
}

buffer_pool_t *createBufferPool(uint8_t tid)
{
return allocBufferPool(tid,BUFFERPOOL_CONTAINER_LEN, BUFFER_SIZE);
return allocBufferPool(tid, BUFFERPOOL_CONTAINER_LEN, BUFFER_SIZE);
}

buffer_pool_t *createSmallBufferPool(uint8_t tid)
{
return allocBufferPool(tid,BUFFERPOOL_SMALL_CONTAINER_LEN, BUFFER_SIZE_SMALL);
return allocBufferPool(tid, BUFFERPOOL_SMALL_CONTAINER_LEN, BUFFER_SIZE_SMALL);
}
3 changes: 2 additions & 1 deletion ww/generic_pool.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ static generic_pool_t *allocateGenericPool(unsigned int item_size, unsigned int
PoolItemCreateHandle create_h, PoolItemDestroyHandle destroy_h)
{

pool_width = (unsigned long) pow(2, floor(log2((double) (max(1, (ssize_t) pool_width)))));
pool_width = (max(1,pool_width) + 15) & ~0x0F;

// half of the pool is used, other half is free at startup
pool_width = 2 * pool_width;

Expand Down
14 changes: 9 additions & 5 deletions ww/generic_pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,20 @@ static inline pool_item_t *popPoolItem(generic_pool_t *pool)
return pool->create_item_handle(pool);
#endif

if (pool->len <= 0)
{
poolReCharge(pool);
}

#if defined(DEBUG) && defined(POOL_DEBUG)
pool->in_use += 1;
#endif

if (pool->len > 0)
{
--(pool->len);
return pool->available[pool->len];
}

poolReCharge(pool);
--(pool->len);
return pool->available[pool->len];

}

static inline void reusePoolItem(generic_pool_t *pool, pool_item_t *b)
Expand Down
23 changes: 10 additions & 13 deletions ww/pipe_line.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

/*
<--------------- Down Line ------------------>
----------------------------|------------------|
<- con -> Pipe 1 thread 1 |
----------------------------|------------------|
Expand Down Expand Up @@ -47,14 +47,11 @@ typedef struct pipe_line_s pipe_line_t;

pool_item_t *allocPipeLineMsgPoolHandle(struct generic_pool_s *pool);
void destroyPipeLineMsgPoolHandle(struct generic_pool_s *pool, pool_item_t *item);

void pipeOnUpLinePaused(void *state);
void pipeOnUpLineResumed(void *state);
void pipeOnDownLineResumed(void *state);
void pipeOnDownLinePaused(void *state);

bool pipeSendToUpStream(pipe_line_t *pl, context_t *c);
bool pipeSendToDownStream(pipe_line_t *pl, context_t *c);

void newPipeLine(tunnel_t *self, line_t *left_line, uint8_t dest_tid,
PipeLineFlowRoutine local_up_stream, PipeLineFlowRoutine local_down_stream);
void pipeOnUpLinePaused(void *state);
void pipeOnUpLineResumed(void *state);
void pipeOnDownLineResumed(void *state);
void pipeOnDownLinePaused(void *state);
bool pipeSendToUpStream(pipe_line_t *pl, context_t *c);
bool pipeSendToDownStream(pipe_line_t *pl, context_t *c);
void newPipeLine(tunnel_t *self, line_t *left_line, uint8_t dest_tid, PipeLineFlowRoutine local_up_stream,
PipeLineFlowRoutine local_down_stream);
1 change: 1 addition & 0 deletions ww/shiftbuffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pool_item_t *allocShiftBufferPoolHandle(struct generic_pool_s *pool)
*self = (shift_buffer_t) {0};
return self;
}

void destroyShiftBufferPoolHandle(struct generic_pool_s *pool, pool_item_t *item)
{
(void) pool;
Expand Down
6 changes: 6 additions & 0 deletions ww/tunnel.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ void chainUp(tunnel_t *from, tunnel_t *to)
{
from->up = to;
}

// `to` downstreams to `from`
void chainDown(tunnel_t *from, tunnel_t *to)
{
Expand All @@ -18,6 +19,7 @@ void chainDown(tunnel_t *from, tunnel_t *to)
// but the cyclic refrence detection is already done in node map
to->dw = from;
}

// `from` <-> `to`
void chain(tunnel_t *from, tunnel_t *to)
{
Expand Down Expand Up @@ -45,6 +47,7 @@ pool_item_t *allocLinePoolHandle(struct generic_pool_s *pool)
(void) pool;
return globalMalloc(sizeof(line_t));
}

void destroyLinePoolHandle(struct generic_pool_s *pool, pool_item_t *item)
{
(void) pool;
Expand Down Expand Up @@ -90,6 +93,7 @@ void pipeUpStream(context_t *c)
destroyContext(c);
}
}

void pipeDownStream(context_t *c)
{
if (! pipeSendToDownStream((pipe_line_t *) c->line->dw_state, c))
Expand All @@ -114,6 +118,7 @@ static void defaultPipeLocalUpStream(struct tunnel_s *self, struct context_s *c,
self->upStream(self, c);
}
}

static void defaultPipeLocalDownStream(struct tunnel_s *self, struct context_s *c, struct pipe_line_s *pl)
{
(void) pl;
Expand All @@ -126,6 +131,7 @@ static void defaultPipeLocalDownStream(struct tunnel_s *self, struct context_s *
self->dw->downStream(self->dw, c);
}
}

void pipeTo(tunnel_t *self, line_t *l, uint8_t tid)
{
assert(l->up_state == NULL);
Expand Down
21 changes: 10 additions & 11 deletions ww/ww.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@
enum
{
kSocketManagerWorkerId = 0,
kTunManagerWorkerId = 1,
kAdditionalReservedWorkers = 2
kAdditionalReservedWorkers = 1
};

ww_global_state_t global_ww_state = {0};
Expand Down Expand Up @@ -53,13 +52,13 @@ static void initalizeSocketManagerWorker(worker_t *worker, tid_t tid)

worker->shift_buffer_pool =
newGenericPoolWithCap((64) + GSTATE.ram_profile, allocShiftBufferPoolHandle, destroyShiftBufferPoolHandle);
GSTATE.shortcut_shift_buffer_pools[tid] = (getWorker(tid)->shift_buffer_pool);
GSTATE.shortcut_shift_buffer_pools[tid] = getWorker(tid)->shift_buffer_pool;

worker->buffer_pool = createSmallBufferPool(worker->tid);
GSTATE.shortcut_buffer_pools[tid] = (getWorker(tid)->buffer_pool);
GSTATE.shortcut_buffer_pools[tid] = getWorker(tid)->buffer_pool;

worker->loop = hloop_new(HLOOP_FLAG_AUTO_FREE, worker->buffer_pool, 0);
GSTATE.shortcut_loops[tid] = (getWorker(tid)->loop);
GSTATE.shortcut_loops[tid] = getWorker(tid)->loop;
}

static void initalizeWorker(worker_t *worker, tid_t tid)
Expand All @@ -68,24 +67,24 @@ static void initalizeWorker(worker_t *worker, tid_t tid)

worker->shift_buffer_pool =
newGenericPoolWithCap((64) + GSTATE.ram_profile, allocShiftBufferPoolHandle, destroyShiftBufferPoolHandle);
GSTATE.shortcut_shift_buffer_pools[tid] = (getWorker(tid)->shift_buffer_pool);
GSTATE.shortcut_shift_buffer_pools[tid] = getWorker(tid)->shift_buffer_pool;

worker->buffer_pool = createBufferPool(worker->tid);
GSTATE.shortcut_buffer_pools[tid] = (getWorker(tid)->buffer_pool);
GSTATE.shortcut_buffer_pools[tid] = getWorker(tid)->buffer_pool;

worker->loop = hloop_new(HLOOP_FLAG_AUTO_FREE, worker->buffer_pool, 0);
GSTATE.shortcut_loops[tid] = (getWorker(tid)->loop);
GSTATE.shortcut_loops[tid] = getWorker(tid)->loop;

worker->context_pool =
newGenericPoolWithCap((16) + GSTATE.ram_profile, allocContextPoolHandle, destroyContextPoolHandle);
GSTATE.shortcut_context_pools[tid] = (getWorker(tid)->context_pool);
GSTATE.shortcut_context_pools[tid] = getWorker(tid)->context_pool;

worker->line_pool = newGenericPoolWithCap((8) + GSTATE.ram_profile, allocLinePoolHandle, destroyLinePoolHandle);
GSTATE.shortcut_line_pools[tid] = (getWorker(tid)->line_pool);
GSTATE.shortcut_line_pools[tid] = getWorker(tid)->line_pool;

worker->pipeline_msg_pool =
newGenericPoolWithCap((8) + GSTATE.ram_profile, allocPipeLineMsgPoolHandle, destroyPipeLineMsgPoolHandle);
GSTATE.shortcut_pipeline_msg_pools[tid] = (getWorker(tid)->pipeline_msg_pool);
GSTATE.shortcut_pipeline_msg_pools[tid] = getWorker(tid)->pipeline_msg_pool;
}

static void runWorker(worker_t *worker)
Expand Down

0 comments on commit ba6bbdf

Please sign in to comment.