<commit_before>/*
 * Copyright (C) 2014  Mozilla Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <assert.h>
#include <errno.h>
#include <sys/epoll.h>
#include <unistd.h>
#include <fdio/loop.h>
#include "log.h"

#define MAXNFDS 64

#define ARRAYLEN(x) \
  (sizeof(x) / sizeof(x[0]))

struct fd_state {
  struct epoll_event event;
  void (*func)(int, uint32_t, void*);
  void* data;
};

static struct fd_state fd_state[MAXNFDS];
static int epfd;

static int
fd_is_valid(int fd)
{
  if (fd < 0) {
    ALOGE("invalid file descriptor %d", fd);
    return 0;
  } else if (fd >= (ssize_t)ARRAYLEN(fd_state)) {
    ALOGE("file descriptors %d out of range", fd);
    return 0;
  }
  return 1;
}

int
add_fd_to_epoll_loop(int fd, uint32_t epoll_events,
                     void (*func)(int, uint32_t, void*), void* data)
{
  int enabled;
  int res;

  if (!fd_is_valid(fd))
    return -1;
  if (!func) {
    ALOGE("no callback function specified");
    return -1;
  }

  enabled = !!fd_state[fd].event.events;

  fd_state[fd].event.events = epoll_events;
  fd_state[fd].event.data.fd = fd;

  if (enabled)
    res = epoll_ctl(epfd, EPOLL_CTL_MOD, fd, &fd_state[fd].event);
  else
    res = epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &fd_state[fd].event);

  if (res < 0) {
    ALOGE_ERRNO("epoll_ctl");
    return -1;
  }

  fd_state[fd].func = func;
  fd_state[fd].data = data;

  return 0;
}

/* returns event bits at position 'i' or above */
static int
events_above(uint32_t epoll_events, unsigned int i)
{
  assert(i < 32);
  return epoll_events & ~((1<<i)-1);
}

static void
fd_events_func(int fd, uint32_t epoll_events, const struct fd_events* evfuncs)
{
  uint32_t bit;

  assert(evfuncs);

  for (bit = 0; events_above(epoll_events, bit); ++bit) {
    void (*func)(int, void*);
    switch (epoll_events & (1<<bit)) {
      case 0:
        /* no event at current bit */
        continue;
      case EPOLLIN:
        func = evfuncs->epollin;
        break;
      case EPOLLPRI:
        func = evfuncs->epollpri;
        break;
      case EPOLLOUT:
        func = evfuncs->epollout;
        break;
      case EPOLLERR:
        func = evfuncs->epollerr;
        break;
      case EPOLLHUP:
        func = evfuncs->epollhup;
        break;
      default:
        ALOGE("unknown event bit 0x%x", bit);
        continue;
    }
    if (func) {
      func(fd, evfuncs->data);
    } else {
      ALOGE("unhandled event bit 0x%x", bit);
    }
  }
}

static void
fd_events_func_cb(int fd, uint32_t epoll_events, void* data)
{
  return fd_events_func(fd, epoll_events, data);
}

int
add_fd_events_to_epoll_loop(int fd, uint32_t epoll_events,
                            const struct fd_events* evfuncs)
{
  if (!evfuncs) {
    ALOGE("no callback structure specified");
    return -1;
  }
  return add_fd_to_epoll_loop(fd, epoll_events, fd_events_func_cb,
                              (void*)evfuncs);
}

void
remove_fd_from_epoll_loop(int fd)
{
  int res;

  if (!fd_is_valid(fd))
    return;

  assert(fd_state[fd].events);

  res = epoll_ctl(epfd, EPOLL_CTL_DEL, fd, NULL);
  if (res < 0)
    ALOGW_ERRNO("epoll_ctl");
}

static int
epoll_loop_iteration(void)
{
  struct epoll_event events[MAXNFDS];
  int nevents, i;<commit_msg>Bug 1031122: Cleanup |fd_state| entry after fd removal from epoll<commit_after>/*
 * Copyright (C) 2014  Mozilla Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <assert.h>
#include <errno.h>
#include <sys/epoll.h>
#include <unistd.h>
#include <fdio/loop.h>
#include "log.h"

#define MAXNFDS 64

#define ARRAYLEN(x) \
  (sizeof(x) / sizeof(x[0]))

struct fd_state {
  struct epoll_event event;
  void (*func)(int, uint32_t, void*);
  void* data;
};

static struct fd_state fd_state[MAXNFDS];
static int epfd;

static int
fd_is_valid(int fd)
{
  if (fd < 0) {
    ALOGE("invalid file descriptor %d", fd);
    return 0;
  } else if (fd >= (ssize_t)ARRAYLEN(fd_state)) {
    ALOGE("file descriptors %d out of range", fd);
    return 0;
  }
  return 1;
}

int
add_fd_to_epoll_loop(int fd, uint32_t epoll_events,
                     void (*func)(int, uint32_t, void*), void* data)
{
  int enabled;
  int res;

  if (!fd_is_valid(fd))
    return -1;
  if (!func) {
    ALOGE("no callback function specified");
    return -1;
  }

  enabled = !!fd_state[fd].event.events;

  fd_state[fd].event.events = epoll_events;
  fd_state[fd].event.data.fd = fd;

  if (enabled)
    res = epoll_ctl(epfd, EPOLL_CTL_MOD, fd, &fd_state[fd].event);
  else
    res = epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &fd_state[fd].event);

  if (res < 0) {
    ALOGE_ERRNO("epoll_ctl");
    return -1;
  }

  fd_state[fd].func = func;
  fd_state[fd].data = data;

  return 0;
}

/* returns event bits at position 'i' or above */
static int
events_above(uint32_t epoll_events, unsigned int i)
{
  assert(i < 32);
  return epoll_events & ~((1<<i)-1);
}

static void
fd_events_func(int fd, uint32_t epoll_events, const struct fd_events* evfuncs)
{
  uint32_t bit;

  assert(evfuncs);

  for (bit = 0; events_above(epoll_events, bit); ++bit) {
    void (*func)(int, void*);
    switch (epoll_events & (1<<bit)) {
      case 0:
        /* no event at current bit */
        continue;
      case EPOLLIN:
        func = evfuncs->epollin;
        break;
      case EPOLLPRI:
        func = evfuncs->epollpri;
        break;
      case EPOLLOUT:
        func = evfuncs->epollout;
        break;
      case EPOLLERR:
        func = evfuncs->epollerr;
        break;
      case EPOLLHUP:
        func = evfuncs->epollhup;
        break;
      default:
        ALOGE("unknown event bit 0x%x", bit);
        continue;
    }
    if (func) {
      func(fd, evfuncs->data);
    } else {
      ALOGE("unhandled event bit 0x%x", bit);
    }
  }
}

static void
fd_events_func_cb(int fd, uint32_t epoll_events, void* data)
{
  return fd_events_func(fd, epoll_events, data);
}

int
add_fd_events_to_epoll_loop(int fd, uint32_t epoll_events,
                            const struct fd_events* evfuncs)
{
  if (!evfuncs) {
    ALOGE("no callback structure specified");
    return -1;
  }
  return add_fd_to_epoll_loop(fd, epoll_events, fd_events_func_cb,
                              (void*)evfuncs);
}

static void 
clear_fd_state(struct fd_state *fd_state)
{
  assert(fd_state);

  fd_state->event.events = 0;
  /* use the largest union member to clean allocated memory*/
  fd_state->event.data.u64 = 0;
  fd_state->func = NULL;
  fd_state->data = NULL;
}


void
remove_fd_from_epoll_loop(int fd)
{
  int res;

  if (!fd_is_valid(fd))
    return;

  assert(fd_state[fd].events);

  res = epoll_ctl(epfd, EPOLL_CTL_DEL, fd, NULL);
  if (res < 0)
    ALOGW_ERRNO("epoll_ctl");

  clear_fd_state(&fd_state[fd]);
}

static int
epoll_loop_iteration(void)
{
  struct epoll_event events[MAXNFDS];
  int nevents, i;
{'fraction_non_alphanumeric': 0.12126627521062038, 'fraction_numerical': 0.013403114628542252, 'mean_word_length': 3.0035769034236077, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '3888867', 'n_tokens_mistral': 2970, 'n_tokens_neox': 2868, 'n_words': 760}