<commit_before>
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

/*  Monkey HTTP Daemon
 *  ------------------
 *  Copyright (C) 2001-2011, <NAME>. <<EMAIL>>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Library General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <pthread.h>
#include <sys/epoll.h>
#include <unistd.h>
#include <sys/syscall.h>
#include <string.h>

#include "monkey.h"
#include "connection.h"
#include "scheduler.h"
#include "memory.h"
#include "epoll.h"
#include "request.h"
#include "cache.h"
#include "config.h"
#include "clock.h"
#include "signals.h"
#include "plugin.h"
#include "utils.h"

/* Register thread information */
int mk_sched_register_thread(pthread_t tid, int efd)
{
    int i;
    struct sched_list_node *sl, *last;

    sl = mk_mem_malloc_z(sizeof(struct sched_list_node));
    sl->tid = tid;
    sl->pid = -1;
    sl->epoll_fd = efd;
    sl->active_connections = 0;
    sl->queue = mk_mem_malloc_z(sizeof(struct sched_connection) *
                                config->worker_capacity);
    sl->request_handler = NULL;

    for (i = 0; i < config->worker_capacity; i++) {
        /* Pre alloc IPv4 memory buffer */
	sl->queue[i].ipv4.data = mk_mem_malloc_z(16);
        sl->queue[i].status = MK_SCHEDULER_CONN_AVAILABLE;
    }

    if (!sched_list) {
        sl->idx = 1;

        /* Alloc and init list */
        sched_list = mk_mem_malloc(sizeof(struct mk_list));
        if (!sched_list) {
            mk_error(MK_ERROR_FATAL, "Could not initilize Scheduler list. Aborting");
        }

        mk_list_init(sched_list);
<commit_msg>Scheduler: validate memory allocation for IP cache queue<commit_after>
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

/*  Monkey HTTP Daemon
 *  ------------------
 *  Copyright (C) 2001-2011, <NAME>. <<EMAIL>>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Library General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <pthread.h>
#include <sys/epoll.h>
#include <unistd.h>
#include <sys/syscall.h>
#include <string.h>

#include "monkey.h"
#include "connection.h"
#include "scheduler.h"
#include "memory.h"
#include "epoll.h"
#include "request.h"
#include "cache.h"
#include "config.h"
#include "clock.h"
#include "signals.h"
#include "plugin.h"
#include "utils.h"

/* Register thread information */
int mk_sched_register_thread(pthread_t tid, int efd)
{
    int i;
    struct sched_list_node *sl, *last;

    sl = mk_mem_malloc_z(sizeof(struct sched_list_node));
    sl->tid = tid;
    sl->pid = -1;
    sl->epoll_fd = efd;
    sl->active_connections = 0;
    sl->queue = mk_mem_malloc_z(sizeof(struct sched_connection) *
                                config->worker_capacity);
    sl->request_handler = NULL;

    for (i = 0; i < config->worker_capacity; i++) {
        /* Pre alloc IPv4 memory buffer */
	sl->queue[i].ipv4.data = mk_mem_malloc_z(16);
        sl->queue[i].status = MK_SCHEDULER_CONN_AVAILABLE;

        if (!sl->queue[i].ipv4.data) {
          mk_error(MK_ERROR_FATAL, "Could not initialize memory for IP cache queue. Aborting");
        }
    }

    if (!sched_list) {
        sl->idx = 1;

        /* Alloc and init list */
        sched_list = mk_mem_malloc(sizeof(struct mk_list));
        if (!sched_list) {
            mk_error(MK_ERROR_FATAL, "Could not initialize memory for Scheduler list. Aborting");
        }

        mk_list_init(sched_list);

{'fraction_non_alphanumeric': 0.1262176165803109, 'fraction_numerical': 0.012642487046632124, 'mean_word_length': 3.0452640402347027, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 48, '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': '6563367', 'n_tokens_mistral': 1631, 'n_tokens_neox': 1556, 'n_words': 497}