diff --git a/src/algo_utils.c b/src/algo_utils.c index 50e1336..2180345 100644 --- a/src/algo_utils.c +++ b/src/algo_utils.c @@ -59,13 +59,14 @@ struct buffer_packet* get_read_buffer(struct algo_ctx *app_ctx, struct evt_core_ // 3. Update state g_hash_table_insert(app_ctx->used_buffer, &(fdinfo->fd), bp); - // 4. Prepare packets - bp->mode = BP_READING; - bp->aread = 0; - bp->ap_count = 0; return bp; } +void __push_to_free(struct algo_ctx *app_ctx, struct buffer_packet* bp) { + memset(bp, 0, sizeof(struct buffer_packet)); + g_queue_push_tail (app_ctx->free_buffer, bp); +} + /** * Returns a buffer if available, NULL otherwise */ @@ -121,10 +122,9 @@ void mv_buffer_rtof(struct algo_ctx* app_ctx, struct evt_core_fdinfo* from) { if (bp == NULL) { fprintf(stderr, "Unable to find a buffer for fd=%d url=%s", from->fd, from->url); } - - memset(bp, 0, sizeof(struct buffer_packet)); - g_queue_push_tail (app_ctx->free_buffer, bp); g_hash_table_remove(app_ctx->used_buffer, &(from->fd)); + + __push_to_free (app_ctx, bp); } void mv_buffer_wtof(struct algo_ctx* app_ctx, struct evt_core_fdinfo* fdinfo) { @@ -132,9 +132,9 @@ void mv_buffer_wtof(struct algo_ctx* app_ctx, struct evt_core_fdinfo* fdinfo) { if (bp == NULL) { fprintf(stderr, "Unable to find a buffer for fd=%d url=%s", fdinfo->fd, fdinfo->url); } - memset(bp, 0, sizeof(struct buffer_packet)); - g_queue_push_tail (app_ctx->free_buffer, bp); g_hash_table_remove(app_ctx->used_buffer, &(fdinfo->fd)); + + __push_to_free (app_ctx, bp); } void mv_buffer_rtoa(struct algo_ctx* app_ctx, struct evt_core_fdinfo* from, void* to) { @@ -178,19 +178,16 @@ void mv_buffer_atow(struct algo_ctx* app_ctx, void* from, struct evt_core_fdinfo void mv_buffer_atof(struct algo_ctx* app_ctx, void* from) { struct buffer_packet* bp; - // 1. We get the buffer + // 1. Remove the buffer bp = g_hash_table_lookup (app_ctx->application_waiting, from); if (bp == NULL) { fprintf(stderr, "Unable to find this application buffer\n"); exit(EXIT_FAILURE); } - - // 2. Reset it - memset(bp, 0, sizeof(struct buffer_packet)); - - // 3. We move it g_hash_table_remove (app_ctx->application_waiting, from); - g_queue_push_tail (app_ctx->free_buffer, bp); + + // 2. Append it to free list + __push_to_free (app_ctx, bp); } struct buffer_packet* dup_buffer_tow(struct algo_ctx* app_ctx, struct buffer_packet* bp, struct evt_core_fdinfo* to) { diff --git a/src/proxy.c b/src/proxy.c index 6458ddf..6d4f998 100644 --- a/src/proxy.c +++ b/src/proxy.c @@ -146,20 +146,12 @@ int main_on_err(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo) { struct buffer_packet* bp; // 1. If has a "used" buffer, remove it - bp = g_hash_table_lookup (app_ctx->used_buffer, &(fdinfo->fd)); - if (bp != NULL) { - g_hash_table_remove (app_ctx->used_buffer, &(fdinfo->fd)); - memset(bp, 0, sizeof(struct buffer_packet)); - g_queue_push_tail(app_ctx->free_buffer, bp); - } + mv_buffer_rtof (app_ctx, fdinfo); // 2. If appears in the write waiting queue, remove it - GQueue* writew = g_hash_table_lookup (app_ctx->write_waiting, &(fdinfo->fd)); - while (writew != NULL && (bp = g_queue_pop_head (writew)) != NULL) { - memset(bp, 0, sizeof(struct buffer_packet)); - g_queue_push_tail(app_ctx->free_buffer, bp); + while (get_write_buffer (app_ctx, fdinfo) != NULL) { + mv_buffer_wtof(app_ctx, fdinfo); } - if (writew) g_hash_table_remove (app_ctx->write_waiting, &(fdinfo->fd)); // 3. If appears in the read waiting queue, remove it g_queue_remove_all (app_ctx->read_waiting, &(fdinfo->fd)); @@ -180,9 +172,7 @@ void algo_main_init(struct evt_core_ctx* evt, struct algo_params* ap) { ctx->is_rdy = 0; ctx->ap = *ap; for (int i = 0; i < sizeof(ctx->bps) / sizeof(ctx->bps[0]); i++) { - ctx->bps[i].mode = BP_READING; - ctx->bps[i].aread = 0; - ctx->bps[i].ap_count = 0; + memset(&(ctx->bps[i]), 0, sizeof(struct buffer_packet)); g_queue_push_tail(ctx->free_buffer, &(ctx->bps[i])); }