Better handling of buffers

This commit is contained in:
Quentin 2019-05-24 14:05:42 +02:00
parent bf37e61e50
commit 504fa7f2df
2 changed files with 17 additions and 30 deletions

View file

@ -59,13 +59,14 @@ struct buffer_packet* get_read_buffer(struct algo_ctx *app_ctx, struct evt_core_
// 3. Update state // 3. Update state
g_hash_table_insert(app_ctx->used_buffer, &(fdinfo->fd), bp); 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; 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 * 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) { if (bp == NULL) {
fprintf(stderr, "Unable to find a buffer for fd=%d url=%s", from->fd, from->url); 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)); 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) { 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) { if (bp == NULL) {
fprintf(stderr, "Unable to find a buffer for fd=%d url=%s", fdinfo->fd, fdinfo->url); 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)); 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) { 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) { void mv_buffer_atof(struct algo_ctx* app_ctx, void* from) {
struct buffer_packet* bp; struct buffer_packet* bp;
// 1. We get the buffer // 1. Remove the buffer
bp = g_hash_table_lookup (app_ctx->application_waiting, from); bp = g_hash_table_lookup (app_ctx->application_waiting, from);
if (bp == NULL) { if (bp == NULL) {
fprintf(stderr, "Unable to find this application buffer\n"); fprintf(stderr, "Unable to find this application buffer\n");
exit(EXIT_FAILURE); 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_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) { struct buffer_packet* dup_buffer_tow(struct algo_ctx* app_ctx, struct buffer_packet* bp, struct evt_core_fdinfo* to) {

View file

@ -146,20 +146,12 @@ int main_on_err(struct evt_core_ctx* ctx, struct evt_core_fdinfo* fdinfo) {
struct buffer_packet* bp; struct buffer_packet* bp;
// 1. If has a "used" buffer, remove it // 1. If has a "used" buffer, remove it
bp = g_hash_table_lookup (app_ctx->used_buffer, &(fdinfo->fd)); mv_buffer_rtof (app_ctx, fdinfo);
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);
}
// 2. If appears in the write waiting queue, remove it // 2. If appears in the write waiting queue, remove it
GQueue* writew = g_hash_table_lookup (app_ctx->write_waiting, &(fdinfo->fd)); while (get_write_buffer (app_ctx, fdinfo) != NULL) {
while (writew != NULL && (bp = g_queue_pop_head (writew)) != NULL) { mv_buffer_wtof(app_ctx, fdinfo);
memset(bp, 0, sizeof(struct buffer_packet));
g_queue_push_tail(app_ctx->free_buffer, bp);
} }
if (writew) g_hash_table_remove (app_ctx->write_waiting, &(fdinfo->fd));
// 3. If appears in the read waiting queue, remove it // 3. If appears in the read waiting queue, remove it
g_queue_remove_all (app_ctx->read_waiting, &(fdinfo->fd)); 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->is_rdy = 0;
ctx->ap = *ap; ctx->ap = *ap;
for (int i = 0; i < sizeof(ctx->bps) / sizeof(ctx->bps[0]); i++) { for (int i = 0; i < sizeof(ctx->bps) / sizeof(ctx->bps[0]); i++) {
ctx->bps[i].mode = BP_READING; memset(&(ctx->bps[i]), 0, sizeof(struct buffer_packet));
ctx->bps[i].aread = 0;
ctx->bps[i].ap_count = 0;
g_queue_push_tail(ctx->free_buffer, &(ctx->bps[i])); g_queue_push_tail(ctx->free_buffer, &(ctx->bps[i]));
} }