Skip to content

Commit

Permalink
buffer tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
radkesvat committed Jul 19, 2024
1 parent d655508 commit d806fcf
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 35 deletions.
4 changes: 2 additions & 2 deletions core/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,19 @@ int main(void)
{
c_foreach(k, vec_config_path_t, getCoreSettings()->config_paths)
{
// read config file
LOGD("Core: begin parsing config file \"%s\"", *k.ref);
config_file_t *cfile = parseConfigFile(*k.ref);

/*
in case of error in config file, the details is already printed out and the
program will not reach this line
program will not reach this line.
*/

LOGI("Core: parsing config file \"%s\" complete", *k.ref);
runConfigFile(cfile);
}
}

LOGD("Core: starting workers ...");
startSocketManager();
runMainThread();
Expand Down
17 changes: 7 additions & 10 deletions tunnels/adapters/listener/tcp/tcp_listener.c
Original file line number Diff line number Diff line change
Expand Up @@ -206,21 +206,18 @@ static void downStream(tunnel_t *self, context_t *c)
}
else
{

if (c->est)
{
assert(! cstate->established);
cstate->established = true;
hio_set_keepalive_timeout(cstate->io, kEstablishedKeepAliveTimeOutMs);
destroyContext(c);
return;
}
if (c->fin)
{
CSTATE_DROP(c);
cleanup(cstate, true);
destroyContext(c);
return;
}
else if (c->est)
{
assert(! cstate->established);
cstate->established = true;
hio_set_keepalive_timeout(cstate->io, kEstablishedKeepAliveTimeOutMs);
destroyContext(c);
}
}
}
Expand Down
8 changes: 5 additions & 3 deletions ww/eventloop/event/nio.c
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ static int __nio_write(hio_t* io, const void* buf, int len) {
static void nio_read(hio_t* io) {
// printd("nio_read fd=%d\n", io->fd);
int nread = 0, err = 0;
// read:;
// read:;

// #if defined(OS_LINUX) && defined(HAVE_PIPE)
// if(io->pfd_w){
Expand All @@ -195,7 +195,10 @@ static void nio_read(hio_t* io) {
// #endif
shift_buffer_t* buf = popBuffer(io->loop->bufpool);
unsigned int available = rCap(buf);
if (WW_UNLIKELY(available < 1024)) {
if (available > (1U << 15)) {
available = (1U << 15);
}
else if (WW_UNLIKELY(available < 1024)) {
reserveBufSpace(buf, 1024);
}
nread = __nio_read(io, rawBufMut(buf), available);
Expand Down Expand Up @@ -488,7 +491,6 @@ int hio_close(hio_t* io) {
// return hio_close_async(io); /* tid lost its meaning, its now ww tid */
// }


if (io->closed) {

return 0;
Expand Down
6 changes: 3 additions & 3 deletions ww/managers/socket_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,15 @@ typedef struct udpsock_s

} udpsock_t;

// if you asked for udp, youll get such struct when a udp packet received and passed all filters
// if you asked for udp, you'll get such struct when a udp packet received and passed all filters
typedef struct udp_payload_s
{
udpsock_t *sock;
tunnel_t *tunnel;
uint8_t tid;
shift_buffer_t *buf;
sockaddr_u peer_addr;
uint16_t real_localport;
shift_buffer_t *buf;
uint8_t tid;

} udp_payload_t;

Expand Down
33 changes: 17 additions & 16 deletions ww/shiftbuffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
#include <stdint.h>
#include <string.h>

#define PREPADDING (ram_profile >= kRamProfileS2Memory ? (1U << 13) : ((1U << 8) + 512))
#define LEFTPADDING (ram_profile >= kRamProfileS2Memory ? (1U << 13) : ((1U << 8) + 512))
#define RIGHTPADDING (ram_profile >= kRamProfileS2Memory ? (1U << 10) : (1U << 8))

pool_item_t *allocShiftBufferPoolHandle(struct generic_pool_s *pool)
{
(void) pool;
shift_buffer_t *self = wwmGlobalMalloc(sizeof(shift_buffer_t));

*self = (shift_buffer_t){
*self = (shift_buffer_t) {
.refc = wwmGlobalMalloc(sizeof(self->refc[0])),
};
return self;
Expand Down Expand Up @@ -53,14 +54,14 @@ shift_buffer_t *newShiftBuffer(uint8_t tid, unsigned int pre_cap) // NOLINT
pre_cap = (unsigned int) pow(2, ceil(log2((double) max(16, pre_cap))));
}

unsigned int real_cap = pre_cap + (PREPADDING);
unsigned int real_cap = pre_cap + LEFTPADDING + RIGHTPADDING;

// shift_buffer_t *self = wwmGlobalMalloc(sizeof(shift_buffer_t));
shift_buffer_t *self = (shift_buffer_t *) popPoolItem(shift_buffer_pools[tid]);

self->calc_len = 0;
self->offset = 0;
self->curpos = PREPADDING;
self->offset = 0;
self->curpos = LEFTPADDING;
self->full_cap = real_cap;
self->pbuf = wwmGlobalMalloc(real_cap);
// self->refc = wwmGlobalMalloc(sizeof(self->refc[0])),
Expand Down Expand Up @@ -99,7 +100,7 @@ void reset(shift_buffer_t *self, unsigned int pre_cap)
pre_cap = (unsigned int) pow(2, ceil(log2(((double) max(16, pre_cap)))));
}

unsigned int real_cap = pre_cap + (PREPADDING);
unsigned int real_cap = pre_cap + LEFTPADDING + RIGHTPADDING;

if (self->full_cap != real_cap)
{
Expand All @@ -109,8 +110,8 @@ void reset(shift_buffer_t *self, unsigned int pre_cap)
// memset(self->pbuf, 0, real_cap);
}
self->calc_len = 0;
self->offset = 0;
self->curpos = PREPADDING;
self->offset = 0;
self->curpos = LEFTPADDING;
}

void unShallow(shift_buffer_t *self)
Expand All @@ -123,7 +124,7 @@ void unShallow(shift_buffer_t *self)
*(self->refc) = 1;
char *old_buf = self->pbuf;
self->pbuf = wwmGlobalMalloc(self->full_cap);
self->offset = 0;
self->offset = 0;
memcpy(&(self->pbuf[self->curpos]), &(old_buf[self->curpos]), (self->calc_len));
}

Expand All @@ -139,7 +140,7 @@ void expand(shift_buffer_t *self, unsigned int increase)
*(self->refc) = 1;
char *old_buf = self->pbuf;
self->pbuf = wwmGlobalMalloc(new_realcap);
self->offset = 0;
self->offset = 0;
unsigned int dif = (new_realcap - self->full_cap) / 2;
memcpy(&(self->pbuf[self->curpos + dif]), &(old_buf[self->curpos]), self->calc_len);
self->curpos += dif;
Expand Down Expand Up @@ -206,19 +207,19 @@ shift_buffer_t *sliceBuffer(const uint8_t tid, shift_buffer_t *const self, const
return newbuf;
}

void *tmp_pbuf = self->pbuf;
void *tmp_refc = self->refc;
void *tmp_pbuf = self->pbuf;
void *tmp_refc = self->refc;
unsigned int tmpoffset = self->offset;

self->refc = newbuf->refc;
self->pbuf = newbuf->pbuf;
self->refc = newbuf->refc;
self->pbuf = newbuf->pbuf;
self->offset = newbuf->offset;
*newbuf = (struct shift_buffer_s){.calc_len = self->calc_len,
*newbuf = (struct shift_buffer_s) {.calc_len = self->calc_len,
.curpos = self->curpos,
.full_cap = self->full_cap,
.pbuf = tmp_pbuf,
.refc = tmp_refc,
.offset = tmpoffset};
.offset = tmpoffset};

memcpy(rawBufMut(self), &(((const char *) rawBuf(newbuf))[bytes]), bufLen(newbuf) - bytes);
shiftr(self, bytes);
Expand Down
2 changes: 1 addition & 1 deletion ww/ww.c
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ void createWW(const ww_construction_data_t init_data)
// [Section] workers and pools heap allocation
{
workers_count = init_data.workers_count;
if (workers_count <= 0 || workers_count > (256 - kAdditionalReservedWorkers))
if (workers_count <= 0 || workers_count > (255 - kAdditionalReservedWorkers))
{
fprintf(stderr, "workers count was not in valid range, value: %u range:[1 - 255]\n", workers_count);
}
Expand Down

0 comments on commit d806fcf

Please sign in to comment.