Blame src/heap_manager.c

Packit 345191
/*
Packit 345191
 * Copyright (C) 2017 - 2019 Intel Corporation.
Packit 345191
 * All rights reserved.
Packit 345191
 *
Packit 345191
 * Redistribution and use in source and binary forms, with or without
Packit 345191
 * modification, are permitted provided that the following conditions are met:
Packit 345191
 * 1. Redistributions of source code must retain the above copyright notice(s),
Packit 345191
 *    this list of conditions and the following disclaimer.
Packit 345191
 * 2. Redistributions in binary form must reproduce the above copyright notice(s),
Packit 345191
 *    this list of conditions and the following disclaimer in the documentation
Packit 345191
 *    and/or other materials provided with the distribution.
Packit 345191
 *
Packit 345191
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS
Packit 345191
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
Packit 345191
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
Packit 345191
 * EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
Packit 345191
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
Packit 345191
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
Packit 345191
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
Packit 345191
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
Packit 345191
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
Packit 345191
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit 345191
 */
Packit 345191
Packit 345191
#include <memkind/internal/heap_manager.h>
Packit 345191
#include <memkind/internal/tbb_wrapper.h>
Packit 345191
#include <memkind/internal/memkind_arena.h>
Packit 345191
Packit 345191
#include <stdio.h>
Packit 345191
#include <pthread.h>
Packit 345191
#include <string.h>
Packit 345191
Packit 345191
static struct heap_manager_ops *heap_manager_g;
Packit 345191
Packit 345191
static pthread_once_t heap_manager_init_once_g = PTHREAD_ONCE_INIT;
Packit 345191
Packit 345191
struct heap_manager_ops {
Packit 345191
    void (*init)(struct memkind *kind);
Packit 345191
    size_t (*heap_manager_malloc_usable_size)(void *ptr);
Packit 345191
    void (*heap_manager_free)(void *ptr);
Packit 345191
    void *(*heap_manager_realloc)(void *ptr, size_t size);
Packit 345191
    struct memkind *(*heap_manager_detect_kind)(void *ptr);
Packit 345191
    int (*heap_manager_update_cached_stats)(void);
Packit 345191
    int (*heap_manager_get_stat)(memkind_stat_type stat, size_t *value);
Packit 345191
    void *(*heap_manager_defrag_reallocate)(void *ptr);
Packit 345191
};
Packit 345191
Packit 345191
static struct heap_manager_ops arena_heap_manager_g = {
Packit 345191
    .init = memkind_arena_init,
Packit 345191
    .heap_manager_malloc_usable_size = memkind_arena_malloc_usable_size,
Packit 345191
    .heap_manager_free = memkind_arena_free_with_kind_detect,
Packit 345191
    .heap_manager_realloc = memkind_arena_realloc_with_kind_detect,
Packit 345191
    .heap_manager_detect_kind = memkind_arena_detect_kind,
Packit 345191
    .heap_manager_update_cached_stats = memkind_arena_update_cached_stats,
Packit 345191
    .heap_manager_get_stat = memkind_arena_get_global_stat,
Packit 345191
    .heap_manager_defrag_reallocate = memkind_arena_defrag_reallocate_with_kind_detect
Packit 345191
};
Packit 345191
Packit 345191
static struct heap_manager_ops tbb_heap_manager_g = {
Packit 345191
    .init = tbb_initialize,
Packit 345191
    .heap_manager_malloc_usable_size = tbb_pool_malloc_usable_size_with_kind_detect,
Packit 345191
    .heap_manager_free = tbb_pool_free_with_kind_detect,
Packit 345191
    .heap_manager_realloc = tbb_pool_realloc_with_kind_detect,
Packit 345191
    .heap_manager_detect_kind = tbb_detect_kind,
Packit 345191
    .heap_manager_update_cached_stats = tbb_update_cached_stats,
Packit 345191
    .heap_manager_get_stat = tbb_get_global_stat,
Packit 345191
    .heap_manager_defrag_reallocate = tbb_pool_defrag_reallocate_with_kind_detect
Packit 345191
};
Packit 345191
Packit 345191
static void set_heap_manager()
Packit 345191
{
Packit 345191
    heap_manager_g = &arena_heap_manager_g;
Packit 345191
    const char *env = secure_getenv("MEMKIND_HEAP_MANAGER");
Packit 345191
    if(env && strcmp(env, "TBB") == 0) {
Packit 345191
        heap_manager_g = &tbb_heap_manager_g;
Packit 345191
    }
Packit 345191
}
Packit 345191
Packit 345191
static inline struct heap_manager_ops *get_heap_manager()
Packit 345191
{
Packit 345191
    pthread_once(&heap_manager_init_once_g, set_heap_manager);
Packit 345191
    return heap_manager_g;
Packit 345191
}
Packit 345191
Packit 345191
void heap_manager_init(struct memkind *kind)
Packit 345191
{
Packit 345191
    get_heap_manager()->init(kind);
Packit 345191
}
Packit 345191
Packit 345191
size_t heap_manager_malloc_usable_size(void *ptr)
Packit 345191
{
Packit 345191
    return get_heap_manager()->heap_manager_malloc_usable_size(ptr);
Packit 345191
}
Packit 345191
Packit 345191
void heap_manager_free(void *ptr)
Packit 345191
{
Packit 345191
    get_heap_manager()->heap_manager_free(ptr);
Packit 345191
}
Packit 345191
Packit 345191
void *heap_manager_realloc(void *ptr, size_t size)
Packit 345191
{
Packit 345191
    return get_heap_manager()->heap_manager_realloc(ptr, size);
Packit 345191
}
Packit 345191
Packit 345191
struct memkind *heap_manager_detect_kind(void *ptr)
Packit 345191
{
Packit 345191
    return get_heap_manager()->heap_manager_detect_kind(ptr);
Packit 345191
}
Packit 345191
Packit 345191
int heap_manager_update_cached_stats(void)
Packit 345191
{
Packit 345191
    return get_heap_manager()->heap_manager_update_cached_stats();
Packit 345191
}
Packit 345191
Packit 345191
int heap_manager_get_stat(memkind_stat_type stat, size_t *value)
Packit 345191
{
Packit 345191
    return get_heap_manager()->heap_manager_get_stat(stat, value);
Packit 345191
}
Packit 345191
Packit 345191
void *heap_manager_defrag_reallocate(void *ptr)
Packit 345191
{
Packit 345191
    return get_heap_manager()->heap_manager_defrag_reallocate(ptr);
Packit 345191
}