<commit_before>// -------------------------------------------------------------------
//
// enm_drv.c: driver entry points for nanomsg Erlang language binding
//
// Copyright (c) 2014 Basho Technologies, Inc. All Rights Reserved.
//
// This file is provided to you 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 <sys/resource.h>
#include <errno.h>
#include <string.h>
#include <assert.h>
#include "enm.h"

#define ENM_REFS_EQUAL(R1,R2) \
    (strcmp((R1).node, (R2).node) == 0 && \
     (R1).len == (R2).len && memcmp((R1).n, (R2).n, (R1).len) == 0)

static EnmData** enm_sockets;

static int
enm_cmd_to_protocol(int cmd)
{
    switch (cmd) {
    case ENM_REQ:
        return NN_REQ;
    case ENM_REP:
        return NN_REP;
    case ENM_BUS:
        return NN_BUS;
    case ENM_PUB:
        return NN_PUB;
    case ENM_SUB:
        return NN_SUB;
    case ENM_PUSH:
        return NN_PUSH;
    case ENM_PULL:
        return NN_PULL;
    case ENM_SVYR:
        return NN_SURVEYOR;
    case ENM_RESP:
        return NN_RESPONDENT;
    case ENM_PAIR:
        return NN_PAIR;
    default:
        errno = ENOPROTOOPT;
        return -1;
    }
}

static ErlDrvTermData
enm_protocol_atom(int protocol)
{
    return driver_mk_atom((char*)enm_protocol_name(protocol));
}

int
enm_write_select(EnmData* d, int start)
{
    ErlDrvEvent event;
    int rc;
    size_t optlen = sizeof d->sfd;

    if (start) {
        if (d->b.write_poll)
            return 0;
        if (d->sfd == -1) {
            if (d->protocol == NN_PULL || d->protocol == NN_SUB) {
                errno = EINVAL;
                return -1;
            }
            rc = nn_getsockopt(d->fd, NN_SOL_SOCKET, NN_SNDFD, &d->sfd, &optlen);
            if (rc < 0)
                return -1;
            enm_sockets[d->sfd] = d;
        }
        event = (ErlDrvEvent)(long)d->sfd;
        driver_select(d->port, event, ERL_DRV_WRITE|ERL_DRV_USE, 1);
        d->b.write_poll = 1;
    } else {
        if (!d->b.write_poll)
            return 0;
        assert(d->sfd != -1);
        event = (ErlDrvEvent)(long)d->sfd;
        driver_select(d->port, event, ERL_DRV_WRITE|ERL_DRV_USE, 0);
        d->b.write_poll = 0;
    }
    return 0;
}

int
enm_read_select(EnmData* d, int start)
{
    ErlDrvEvent event;
    int rc;
    size_t optlen = sizeof d->rfd;

    if (start) {
        if (d->b.read_poll)
            return 0;
        if (d->rfd == -1) {
            if (d->protocol == NN_PUSH) {
                errno = EINVAL;
                return -1;
            }
            rc = nn_getsockopt(d->fd, NN_SOL_SOCKET, NN_RCVFD, &d->rfd, &optlen);
            if (rc < 0)
                return -1;
            enm_sockets[d->rfd] = d;
        }
        event = (ErlDrvEvent)(long)d->rfd;
        driver_select(d->port, event, ERL_DRV_READ|ERL_DRV_USE, 1);
        d->b.read_poll = 1;
    } else {
        if (!d->b.read_poll)
            return 0;
        assert(d->rfd != -1);
        event = (ErlDrvEvent)(long)d->rfd;
        driver_select(d->port, event, ERL_DRV_READ|ERL_DRV_USE, 0);
        d->b.read_poll = 0;
    }
    return 0;
}

static void
enm_add_waiter(EnmData* d, erlang_ref* ref)
{
    EnmRecv *rcv, *cur;

    rcv = driver_alloc(sizeof(EnmRecv));
    if (rcv == 0)
        driver_failure(d->port, ENOMEM);
    memcpy(&rcv->ref, ref, sizeof *ref);
    rcv->rcvr = driver_caller(d->port);
    rcv->next = 0;
    driver_monitor_process(d->port, rcv->rcvr, &rcv->monitor);
    cur = d->waiting_recvs;
    if (cur == 0)
        d->waiting_recvs = rcv;
    else {
        while (cur) {
            if (cur->next != 0)
                cur = cur->next;
            else {
                cur->next = rcv;
                break;
            }
        }
    }
}

static int
enm_init()
{
    struct rlimit rl;

    if (getrlimit(RLIMIT_NOFILE, &rl) < 0)
        return -1;
    enm_sockets = (EnmData**)driver_alloc(rl.rlim_max * sizeof(EnmData*));
    if (enm_sockets == 0)
        return -1;
    return 0;
}

static void
enm_finish()
{
    driver_free(enm_sockets);
}

static ErlDrvData
enm_start(ErlDrvPort port, char* command)
{
    EnmData* d = (EnmData*)driver_alloc(sizeof(EnmData));
    memset(d, 0, sizeof *d);
    d->port = port;
    d->waiting_recvs = 0;
    set_port_control_flags(port, PORT_CONTROL_FLAG_BINARY);
    return (ErlDrvData)d;
}

static void
enm_stop(ErlDrvData drv_data)
{<commit_msg>Use rlimit.rlim_cur for socket tracking table size<commit_after>// -------------------------------------------------------------------
//
// enm_drv.c: driver entry points for nanomsg Erlang language binding
//
// Copyright (c) 2014 Basho Technologies, Inc. All Rights Reserved.
//
// This file is provided to you 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 <sys/resource.h>
#include <errno.h>
#include <string.h>
#include <assert.h>
#include "enm.h"

#define ENM_REFS_EQUAL(R1,R2) \
    (strcmp((R1).node, (R2).node) == 0 && \
     (R1).len == (R2).len && memcmp((R1).n, (R2).n, (R1).len) == 0)

static EnmData** enm_sockets;

static int
enm_cmd_to_protocol(int cmd)
{
    switch (cmd) {
    case ENM_REQ:
        return NN_REQ;
    case ENM_REP:
        return NN_REP;
    case ENM_BUS:
        return NN_BUS;
    case ENM_PUB:
        return NN_PUB;
    case ENM_SUB:
        return NN_SUB;
    case ENM_PUSH:
        return NN_PUSH;
    case ENM_PULL:
        return NN_PULL;
    case ENM_SVYR:
        return NN_SURVEYOR;
    case ENM_RESP:
        return NN_RESPONDENT;
    case ENM_PAIR:
        return NN_PAIR;
    default:
        errno = ENOPROTOOPT;
        return -1;
    }
}

static ErlDrvTermData
enm_protocol_atom(int protocol)
{
    return driver_mk_atom((char*)enm_protocol_name(protocol));
}

int
enm_write_select(EnmData* d, int start)
{
    ErlDrvEvent event;
    int rc;
    size_t optlen = sizeof d->sfd;

    if (start) {
        if (d->b.write_poll)
            return 0;
        if (d->sfd == -1) {
            if (d->protocol == NN_PULL || d->protocol == NN_SUB) {
                errno = EINVAL;
                return -1;
            }
            rc = nn_getsockopt(d->fd, NN_SOL_SOCKET, NN_SNDFD, &d->sfd, &optlen);
            if (rc < 0)
                return -1;
            enm_sockets[d->sfd] = d;
        }
        event = (ErlDrvEvent)(long)d->sfd;
        driver_select(d->port, event, ERL_DRV_WRITE|ERL_DRV_USE, 1);
        d->b.write_poll = 1;
    } else {
        if (!d->b.write_poll)
            return 0;
        assert(d->sfd != -1);
        event = (ErlDrvEvent)(long)d->sfd;
        driver_select(d->port, event, ERL_DRV_WRITE|ERL_DRV_USE, 0);
        d->b.write_poll = 0;
    }
    return 0;
}

int
enm_read_select(EnmData* d, int start)
{
    ErlDrvEvent event;
    int rc;
    size_t optlen = sizeof d->rfd;

    if (start) {
        if (d->b.read_poll)
            return 0;
        if (d->rfd == -1) {
            if (d->protocol == NN_PUSH) {
                errno = EINVAL;
                return -1;
            }
            rc = nn_getsockopt(d->fd, NN_SOL_SOCKET, NN_RCVFD, &d->rfd, &optlen);
            if (rc < 0)
                return -1;
            enm_sockets[d->rfd] = d;
        }
        event = (ErlDrvEvent)(long)d->rfd;
        driver_select(d->port, event, ERL_DRV_READ|ERL_DRV_USE, 1);
        d->b.read_poll = 1;
    } else {
        if (!d->b.read_poll)
            return 0;
        assert(d->rfd != -1);
        event = (ErlDrvEvent)(long)d->rfd;
        driver_select(d->port, event, ERL_DRV_READ|ERL_DRV_USE, 0);
        d->b.read_poll = 0;
    }
    return 0;
}

static void
enm_add_waiter(EnmData* d, erlang_ref* ref)
{
    EnmRecv *rcv, *cur;

    rcv = driver_alloc(sizeof(EnmRecv));
    if (rcv == 0)
        driver_failure(d->port, ENOMEM);
    memcpy(&rcv->ref, ref, sizeof *ref);
    rcv->rcvr = driver_caller(d->port);
    rcv->next = 0;
    driver_monitor_process(d->port, rcv->rcvr, &rcv->monitor);
    cur = d->waiting_recvs;
    if (cur == 0)
        d->waiting_recvs = rcv;
    else {
        while (cur) {
            if (cur->next != 0)
                cur = cur->next;
            else {
                cur->next = rcv;
                break;
            }
        }
    }
}

static int
enm_init()
{
    struct rlimit rl;

    if (getrlimit(RLIMIT_NOFILE, &rl) < 0)
        return -1;
    enm_sockets = (EnmData**)driver_alloc(rl.rlim_cur * sizeof(EnmData*));
    if (enm_sockets == 0)
        return -1;
    return 0;
}

static void
enm_finish()
{
    driver_free(enm_sockets);
}

static ErlDrvData
enm_start(ErlDrvPort port, char* command)
{
    EnmData* d = (EnmData*)driver_alloc(sizeof(EnmData));
    memset(d, 0, sizeof *d);
    d->port = port;
    d->waiting_recvs = 0;
    set_port_control_flags(port, PORT_CONTROL_FLAG_BINARY);
    return (ErlDrvData)d;
}

static void
enm_stop(ErlDrvData drv_data)
{
{'fraction_non_alphanumeric': 0.15416290047202974, 'fraction_numerical': 0.011047504268353922, 'mean_word_length': 1.9185228604923799, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 103, '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': '2829567', 'n_tokens_mistral': 3755, 'n_tokens_neox': 3614, 'n_words': 838}