<commit_before>/* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/*
 *     Copyright 2015 Couchbase, Inc
 *
 *   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 "config.h"
#include "alloc_hooks.h"
#include <stdbool.h>

#include "memcached/visibility.h"

/* Irrespective of how jemalloc was configured on this platform,
* don't rename je_FOO to FOO.
*/
#define JEMALLOC_NO_RENAME
#include <jemalloc/jemalloc.h>
#if defined(WIN32)
#    error Memory tracking not supported with jemalloc on Windows.
#elif defined(__APPLE__)
/* memory tracking implemented using custom malloc zone: */
#    include "darwin_zone.h"
#else
/* assume some other *ix-style OS which permits malloc/free symbol interposing. */
#    define INTERPOSE_MALLOC 1
#endif

#if defined(HAVE_MEMALIGN)
#include <malloc.h>
#endif

/******************************************************************************
 * jemalloc memory tracking support.
 * jemalloc (unlike TCmalloc) has no builtin support for this, so instead we
 * use our own malloc wrapper functions which will check for the presence of
 * registered hooks and call if necessary.
 *
 *****************************************************************************/

/* jemalloc checks for this symbol, and it's contents for the config to use. */
const char* je_malloc_conf =
    /* Use just one arena, instead of the default based on number of CPUs.
       Helps to minimize heap fragmentation. */
    "narenas:1";

static malloc_new_hook_t new_hook = NULL;
static malloc_delete_hook_t delete_hook = NULL;

#if defined(INTERPOSE_MALLOC)

static inline void invoke_new_hook(void* ptr, size_t size) {
    if (new_hook != NULL) {
        new_hook(ptr, size);
    }
}

static inline void invoke_delete_hook(void* ptr) {
    if (delete_hook != NULL) {
        delete_hook(ptr);
    }
}

extern "C" {

MEMCACHED_PUBLIC_API void* malloc(size_t size) throw () {
    void* ptr = je_malloc(size);
    invoke_new_hook(ptr, size);
    return ptr;
}

MEMCACHED_PUBLIC_API void* calloc(size_t nmemb, size_t size) throw () {
    void* ptr = je_calloc(nmemb, size);
    invoke_new_hook(ptr, nmemb * size);
    return ptr;
}

MEMCACHED_PUBLIC_API void* realloc(void* ptr, size_t size) throw () {
    invoke_delete_hook(ptr);
    void* result = je_realloc(ptr, size);
    invoke_new_hook(result, size);
    return result;
}

MEMCACHED_PUBLIC_API void free(void* ptr) throw () {
    invoke_delete_hook(ptr);
    je_free(ptr);
}

#if defined(HAVE_MEMALIGN)
MEMCACHED_PUBLIC_API void *memalign(size_t alignment, size_t size) throw () {
    void* result = je_memalign(alignment, size);
    invoke_new_hook(result, size);
    return result;
}
#endif

MEMCACHED_PUBLIC_API int posix_memalign(void **memptr, size_t alignment,
                                        size_t size) throw () {
    int result = je_posix_memalign(memptr, alignment, size);
    invoke_new_hook(*memptr, size);
    return result;
}

} // extern "C"
<commit_msg>Fix compile problems on FreeBSD<commit_after>/* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/*
 *     Copyright 2015 Couchbase, Inc
 *
 *   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 "config.h"
#include "alloc_hooks.h"
#include <stdbool.h>

#include "memcached/visibility.h"

/* Irrespective of how jemalloc was configured on this platform,
* don't rename je_FOO to FOO.
*/
#define JEMALLOC_NO_RENAME
#include <jemalloc/jemalloc.h>
#if defined(WIN32)
#    error Memory tracking not supported with jemalloc on Windows.
#elif defined(__APPLE__)
/* memory tracking implemented using custom malloc zone: */
#    include "darwin_zone.h"
#else
/* assume some other *ix-style OS which permits malloc/free symbol interposing. */
#    define INTERPOSE_MALLOC 1
#endif

#if defined(HAVE_MEMALIGN)
#include <malloc.h>
#endif

/******************************************************************************
 * jemalloc memory tracking support.
 * jemalloc (unlike TCmalloc) has no builtin support for this, so instead we
 * use our own malloc wrapper functions which will check for the presence of
 * registered hooks and call if necessary.
 *
 *****************************************************************************/

/* jemalloc checks for this symbol, and it's contents for the config to use. */
const char* je_malloc_conf =
    /* Use just one arena, instead of the default based on number of CPUs.
       Helps to minimize heap fragmentation. */
    "narenas:1";

static malloc_new_hook_t new_hook = NULL;
static malloc_delete_hook_t delete_hook = NULL;

#if defined(INTERPOSE_MALLOC)

static inline void invoke_new_hook(void* ptr, size_t size) {
    if (new_hook != NULL) {
        new_hook(ptr, size);
    }
}

static inline void invoke_delete_hook(void* ptr) {
    if (delete_hook != NULL) {
        delete_hook(ptr);
    }
}

extern "C" {

#if defined(__sun) || defined(__FreeBSD__)
#define throwspec
#else
#define throwspec throw()
#endif

MEMCACHED_PUBLIC_API void* malloc(size_t size) throwspec {
    void* ptr = je_malloc(size);
    invoke_new_hook(ptr, size);
    return ptr;
}

MEMCACHED_PUBLIC_API void* calloc(size_t nmemb, size_t size) throwspec {
    void* ptr = je_calloc(nmemb, size);
    invoke_new_hook(ptr, nmemb * size);
    return ptr;
}

MEMCACHED_PUBLIC_API void* realloc(void* ptr, size_t size) throwspec {
    invoke_delete_hook(ptr);
    void* result = je_realloc(ptr, size);
    invoke_new_hook(result, size);
    return result;
}

MEMCACHED_PUBLIC_API void free(void* ptr) throwspec {
    invoke_delete_hook(ptr);
    je_free(ptr);
}

#if defined(HAVE_MEMALIGN)
MEMCACHED_PUBLIC_API void *memalign(size_t alignment, size_t size) throwspec {
    void* result = je_memalign(alignment, size);
    invoke_new_hook(result, size);
    return result;
}
#endif

MEMCACHED_PUBLIC_API int posix_memalign(void **memptr, size_t alignment,
                                        size_t size) throwspec {
    int result = je_posix_memalign(memptr, alignment, size);
    invoke_new_hook(*memptr, size);
    return result;
}

#undef throwspec

} // extern "C"

{'fraction_non_alphanumeric': 0.1363572728545429, 'fraction_numerical': 0.003919921601567969, 'mean_word_length': 3.9201101928374658, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7223794', 'n_tokens_mistral': 2266, 'n_tokens_neox': 2201, 'n_words': 718}