<commit_before>/*
   Copyright 2016 Vanderbilt University

   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.
*/

#define _log_module_index 104

#include <apr_errno.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>

#include "skiplist.h"
#include "tbx/assert_result.h"
#include "tbx/log.h"
#include "tbx/tbx_decl.h"
#include "tbx/type_malloc.h"

// Forward declarations
void destroy_skiplist_node(tbx_sl_t *sl, tbx_sl_node_t *sn);
tbx_sl_node_t *create_skiplist_node(unsigned int level);

int skiplist_compare_fn_int(void *arg, tbx_sl_key_t *k1, tbx_sl_key_t *k2);
int skiplist_compare_fn_strcmp(void *arg, tbx_sl_key_t *k1, tbx_sl_key_t *k2);
int skiplist_compare_fn_strncmp(void *arg, tbx_sl_key_t *k1, tbx_sl_key_t *k2);
int skiplist_compare_fn_ptr(void *arg, tbx_sl_key_t *a, tbx_sl_key_t *b);

tbx_sl_compare_t tbx_sl_compare_int= {skiplist_compare_fn_int, (void *)(int)(long)(1)};
tbx_sl_compare_t tbx_sl_compare_strcmp= {skiplist_compare_fn_strcmp, (void *)(int)(long)1};
tbx_sl_compare_t tbx_sl_compare_strcmp_descending= {skiplist_compare_fn_strcmp, (void *)(int)(long)(-1)};
tbx_sl_compare_t skiplist_compare_ptr= {skiplist_compare_fn_ptr, NULL};

//******************************************************************************
// Boilerplate for tbx_sl_t
//******************************************************************************
TBX_MALLOC tbx_sl_t *tbx_sl_malloc()
{
    tbx_sl_t *sl = (tbx_sl_t *)malloc(sizeof(tbx_sl_t));
    if (!sl) // Do we log here?
        goto error_1;

    apr_status_t ret = apr_pool_create(&(sl->pool), NULL);
    if (ret != APR_SUCCESS)
        goto error_2;

    ret = apr_thread_mutex_create(&(sl->lock), APR_THREAD_MUTEX_DEFAULT, sl->pool);
    if (ret != APR_SUCCESS)
        goto error_3;

    if (tbx_sl_init(sl)) {
        goto error_4;
    }

    return sl;

error_4:
    apr_thread_mutex_destroy(sl->lock);
error_3:
    apr_pool_destroy(sl->pool);
error_2:
    free(sl);
error_1:
    return NULL;
}

tbx_sl_t *tbx_sl_new()<commit_msg>Fix refactor bug resulting in a double allocation of the head node<commit_after>/*
   Copyright 2016 Vanderbilt University

   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.
*/

#define _log_module_index 104

#include <apr_errno.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>

#include "skiplist.h"
#include "tbx/assert_result.h"
#include "tbx/log.h"
#include "tbx/tbx_decl.h"
#include "tbx/type_malloc.h"

// Forward declarations
void destroy_skiplist_node(tbx_sl_t *sl, tbx_sl_node_t *sn);
tbx_sl_node_t *create_skiplist_node(unsigned int level);

int skiplist_compare_fn_int(void *arg, tbx_sl_key_t *k1, tbx_sl_key_t *k2);
int skiplist_compare_fn_strcmp(void *arg, tbx_sl_key_t *k1, tbx_sl_key_t *k2);
int skiplist_compare_fn_strncmp(void *arg, tbx_sl_key_t *k1, tbx_sl_key_t *k2);
int skiplist_compare_fn_ptr(void *arg, tbx_sl_key_t *a, tbx_sl_key_t *b);

tbx_sl_compare_t tbx_sl_compare_int= {skiplist_compare_fn_int, (void *)(int)(long)(1)};
tbx_sl_compare_t tbx_sl_compare_strcmp= {skiplist_compare_fn_strcmp, (void *)(int)(long)1};
tbx_sl_compare_t tbx_sl_compare_strcmp_descending= {skiplist_compare_fn_strcmp, (void *)(int)(long)(-1)};
tbx_sl_compare_t skiplist_compare_ptr= {skiplist_compare_fn_ptr, NULL};

//******************************************************************************
// Boilerplate for tbx_sl_t
//******************************************************************************
TBX_MALLOC tbx_sl_t *tbx_sl_malloc()
{
    tbx_sl_t *sl = (tbx_sl_t *)malloc(sizeof(tbx_sl_t));
    if (!sl) // Do we log here?
        goto error_1;

    apr_status_t ret = apr_pool_create(&(sl->pool), NULL);
    if (ret != APR_SUCCESS)
        goto error_2;

    ret = apr_thread_mutex_create(&(sl->lock), APR_THREAD_MUTEX_DEFAULT, sl->pool);
    if (ret != APR_SUCCESS)
        goto error_3;

    return sl;

error_3:
    apr_pool_destroy(sl->pool);
error_2:
    free(sl);
error_1:
    return NULL;
}

tbx_sl_t *tbx_sl_new()
{'fraction_non_alphanumeric': 0.16189516129032258, 'fraction_numerical': 0.010887096774193548, 'mean_word_length': 5.248110831234257, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 20, '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': '4702466', 'n_tokens_mistral': 1849, 'n_tokens_neox': 1812, 'n_words': 434}