<commit_before>/**
 * xmp3 - XMPP Proxy
 * event.{c,h} - Simple event loop
 * Copyright (c) 2011 Drexel University
 */

#include "event.h"

#include <errno.h>
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include <sys/queue.h>
#include <sys/select.h>
#include <sys/socket.h>

#include "log.h"

/* Contains data for one callback function */
struct event_item {
    /* The file descriptor to monitor */
    int fd;

    /* Function to call when the descriptor fires */
    event_callback func;

    /* Miscelaneous data to send to the callback function */
    void *data;

    /* Linked-list entries to more event_items */
    LIST_ENTRY(event_item) list_entry;
};

/* Contains data about the whole event loop */
struct event_loop {
    /* Flag to stop the event loop. */
    volatile sig_atomic_t stop_loop;

    /* File descriptors for select */
    fd_set readfds;

    /* For select, highest-numbered file descriptor in readfds plus 1 */
    int nfds;

    /* Linked-list of callback functions */
    LIST_HEAD(event_items, event_item) list_head;
};

struct event_loop* event_new_loop(void) {
    struct event_loop *loop = calloc(1, sizeof(struct event_loop));
    check_mem(loop);

    LIST_INIT(&loop->list_head);

    return loop;

error:
    return NULL;
}

void event_del_loop(struct event_loop *loop) {
    // Free the memory for all the callback entries
    struct event_item *item;
    LIST_FOREACH(item, &loop->list_head, list_entry) {
        free(item);
    }
    free(loop);
}

void event_register_callback(struct event_loop *loop, int fd,
                             event_callback func, void *data) {
    struct event_item *item = calloc(1, sizeof(struct event_item));
    check_mem(item);

    // Add the item to the list
    item->fd = fd;
    item->func = func;
    item->data = data;

    LIST_INSERT_HEAD(&loop->list_head, item, list_entry);

    // Select needs nfds, one plus the highest numbered fd in readfds
    if (fd + 1 > loop->nfds) {
        loop->nfds = fd + 1;
    }

error:
    // TODO: Make this return a bool or something.
    return;
}

void event_deregister_callback(struct event_loop *loop, int fd) {
    // Find the entry in the list, and free it
    struct event_item *item;
    LIST_FOREACH(item, &loop->list_head, list_entry) {
        if (item->fd == fd) {
            LIST_REMOVE(item, list_entry);
            free(item);
            break;
        }
    }

    if (fd + 1 == loop->nfds) {
        // We need to find a new maximum
        LIST_FOREACH(item, &loop->list_head, list_entry) {
            if (item->fd + 1 > loop->nfds) {
                loop->nfds = item->fd + 1;
            }
        }
    }
}<commit_msg>Remove extraneous error goto labels in event.c.<commit_after>/**
 * xmp3 - XMPP Proxy
 * event.{c,h} - Simple event loop
 * Copyright (c) 2011 Drexel University
 */

#include "event.h"

#include <errno.h>
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include <sys/queue.h>
#include <sys/select.h>
#include <sys/socket.h>

#include "log.h"

/* Contains data for one callback function */
struct event_item {
    /* The file descriptor to monitor */
    int fd;

    /* Function to call when the descriptor fires */
    event_callback func;

    /* Miscelaneous data to send to the callback function */
    void *data;

    /* Linked-list entries to more event_items */
    LIST_ENTRY(event_item) list_entry;
};

/* Contains data about the whole event loop */
struct event_loop {
    /* Flag to stop the event loop. */
    volatile sig_atomic_t stop_loop;

    /* File descriptors for select */
    fd_set readfds;

    /* For select, highest-numbered file descriptor in readfds plus 1 */
    int nfds;

    /* Linked-list of callback functions */
    LIST_HEAD(event_items, event_item) list_head;
};

struct event_loop* event_new_loop(void) {
    struct event_loop *loop = calloc(1, sizeof(struct event_loop));
    check_mem(loop);

    LIST_INIT(&loop->list_head);

    return loop;
}

void event_del_loop(struct event_loop *loop) {
    // Free the memory for all the callback entries
    struct event_item *item;
    LIST_FOREACH(item, &loop->list_head, list_entry) {
        free(item);
    }
    free(loop);
}

void event_register_callback(struct event_loop *loop, int fd,
                             event_callback func, void *data) {
    struct event_item *item = calloc(1, sizeof(struct event_item));
    check_mem(item);

    // Add the item to the list
    item->fd = fd;
    item->func = func;
    item->data = data;

    LIST_INSERT_HEAD(&loop->list_head, item, list_entry);

    // Select needs nfds, one plus the highest numbered fd in readfds
    if (fd + 1 > loop->nfds) {
        loop->nfds = fd + 1;
    }
}

void event_deregister_callback(struct event_loop *loop, int fd) {
    // Find the entry in the list, and free it
    struct event_item *item;
    LIST_FOREACH(item, &loop->list_head, list_entry) {
        if (item->fd == fd) {
            LIST_REMOVE(item, list_entry);
            free(item);
            break;
        }
    }

    if (fd + 1 == loop->nfds) {
        // We need to find a new maximum
        LIST_FOREACH(item, &loop->list_head, list_entry) {
            if (item->fd + 1 > loop->nfds) {
                loop->nfds = item->fd + 1;
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.11659858893427405, 'fraction_numerical': 0.004827330115113257, 'mean_word_length': 2.534776902887139, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 57, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '167392', 'n_tokens_mistral': 1814, 'n_tokens_neox': 1740, 'n_words': 519}