<commit_before>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>

#include "xyz_event2.h"

#define XYZ_EVENT2_FDMAX 65535 

struct xyz_event2_node_t {
    int rdtype;
    int wttype;
    xyz_ev2_func rdfunc;
    xyz_ev2_func wtfunc;
    void *rdarg;
    void *wtarg;
};

struct xyz_event2_t {
    int evfd;
    struct xyz_event2_node_t *nodes;
    int isstop;
    xyz_ev2_call call;
};

#ifdef __linux__

#include <sys/epoll.h>

int __xyz_event2_init(struct xyz_event2_t *ev2)
{
    if(ev2 == NULL) {
        return -1;
    }

    ev2->evfd = epoll_create(XYZ_EVENT2_FDMAX);

    if(ev2->evfd == -1) {
        return -1;
    }

    return 0;
}

int __xyz_event2_add(struct xyz_event2_t *ev2, int fd, int type)
{
    struct epoll_event ev;

    if(ev2 == NULL || fd < 0 || fd > XYZ_EVENT2_FDMAX) {
        return -1;
    }

    ev.data.fd = fd;
    if(type == XYZ_EVENT2_RD) {
        ev.events=EPOLLIN;
    } else if(type == XYZ_EVENT2_WT) {
        ev.events=EPOLLOUT;
    } else {
        return -1;
    }

    return epoll_ctl(ev2->evfd, EPOLL_CTL_ADD, fd, &ev);
}

int __xyz_event2_del(struct xyz_event2_t *ev2, int fd, int type)
{
    struct epoll_event ev;

    if(ev2 == NULL || fd < 0 || fd > XYZ_EVENT2_FDMAX) {
        return -1;
    }

    ev.data.fd = fd;
    if(type == XYZ_EVENT2_RD) {
        ev.events=EPOLLIN;
    } else if(type == XYZ_EVENT2_WT) {
        ev.events=EPOLLOUT;
    } else {
        return -1;
    }

    return epoll_ctl(ev2->evfd, EPOLL_CTL_DEL, fd, &ev);
}

int __xyz_event2_run(struct xyz_event2_t *ev2)
{
    int i;
    struct epoll_event events[XYZ_EVENT2_FDMAX];

    if(ev2 == NULL) {
        return -1;
    }

    int nfds=epoll_wait(ev2->evfd, events, XYZ_EVENT2_FDMAX, 500);

    for(i=0; i < nfds; ++i)
    {
        if(events[i].events & EPOLLIN)  {
            ev2->nodes[events[i].data.fd].rdfunc(events[i].data.fd, ev2->nodes[events[i].data.fd].rdarg);
        } else if(events[i].events & EPOLLOUT) {
            ev2->nodes[events[i].data.fd].wtfunc(events[i].data.fd, ev2->nodes[events[i].data.fd].wtarg);<commit_msg>modify event2<commit_after>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>

#include "xyz_event2.h"

#define XYZ_EVENT2_FDMAX 65535 

struct xyz_event2_node_t {
    int rdtype;
    int wttype;
    xyz_ev2_func rdfunc;
    xyz_ev2_func wtfunc;
    void *rdarg;
    void *wtarg;
};

struct xyz_event2_t {
    int evfd;
    struct xyz_event2_node_t *nodes;
    int isstop;
    xyz_ev2_call call;
};

#ifdef __linux__

#include <sys/epoll.h>

int __xyz_event2_init(struct xyz_event2_t *ev2)
{
    if(ev2 == NULL) {
        return -1;
    }

    ev2->evfd = epoll_create(XYZ_EVENT2_FDMAX);

    if(ev2->evfd == -1) {
        return -1;
    }

    return 0;
}

int __xyz_event2_add(struct xyz_event2_t *ev2, int fd, int type)
{
    struct epoll_event ev;

    if(ev2 == NULL || fd < 0 || fd > XYZ_EVENT2_FDMAX) {
        return -1;
    }

    ev.data.fd = fd;
    if(type == XYZ_EVENT2_RD) {
        ev.events = EPOLLIN;
        if(ev2->nodes[fd].wttype == 1) {
            ev.events |= EPOLLOUT;
            return epoll_ctl(ev2->evfd, EPOLL_CTL_MOD, fd, &ev);
        } else {
            return epoll_ctl(ev2->evfd, EPOLL_CTL_ADD, fd, &ev);
        }
    } 
    if(type == XYZ_EVENT2_WT) {
        ev.events = EPOLLOUT;
        if(ev2->nodes[fd].rdtype == 1) {
            ev.events |= EPOLLIN;
            return epoll_ctl(ev2->evfd, EPOLL_CTL_MOD, fd, &ev);
        } else {
            return epoll_ctl(ev2->evfd, EPOLL_CTL_ADD, fd, &ev);
        }
    } 

    return -1;
}

int __xyz_event2_del(struct xyz_event2_t *ev2, int fd, int type)
{
    struct epoll_event ev;

    if(ev2 == NULL || fd < 0 || fd > XYZ_EVENT2_FDMAX) {
        return -1;
    }

    ev.data.fd = fd;
    if(type == XYZ_EVENT2_RD) {
        if(ev2.nodes[fd].wttype == 1) {
            ev.events=EPOLLOUT;
            return epoll_ctl(ev2->evfd, EPOLL_CTL_MOD, fd, &ev);
        } else {
            return epoll_ctl(ev2->evfd, EPOLL_CTL_DEL, fd, &ev);
        }
    }
    if(type == XYZ_EVENT2_WT) {
        if(ev2.nodes[fd].rdtype == 1) {
            ev.events=EPOLLIN;
            return epoll_ctl(ev2->evfd, EPOLL_CTL_MOD, fd, &ev);
        } else {
            return epoll_ctl(ev2->evfd, EPOLL_CTL_DEL, fd, &ev);
        }
    } 

    return -1;
}

int __xyz_event2_run(struct xyz_event2_t *ev2)
{
    int i;
    struct epoll_event events[XYZ_EVENT2_FDMAX];

    if(ev2 == NULL) {
        return -1;
    }

    int nfds=epoll_wait(ev2->evfd, events, XYZ_EVENT2_FDMAX, 500);

    for(i=0; i < nfds; ++i)
    {
        if(events[i].events & EPOLLIN)  {
            ev2->nodes[events[i].data.fd].rdfunc(events[i].data.fd, ev2->nodes[events[i].data.fd].rdarg);
        } else if(events[i].events & EPOLLOUT) {
            ev2->nodes[events[i].data.fd].wtfunc(events[i].data.fd, ev2->nodes[events[i].data.fd].wtarg);
{'fraction_non_alphanumeric': 0.14245359160613397, 'fraction_numerical': 0.02804681194511703, 'mean_word_length': 2.3652410047522063, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5353119', 'n_tokens_mistral': 2199, 'n_tokens_neox': 2085, 'n_words': 372}