Blame src/memkind_regular.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.h>
Packit 345191
#include <memkind/internal/memkind_arena.h>
Packit 345191
#include <memkind/internal/memkind_default.h>
Packit 345191
#include <memkind/internal/heap_manager.h>
Packit 345191
Packit 345191
#include <numa.h>
Packit 345191
Packit 345191
static struct bitmask *regular_nodes_mask = NULL;
Packit 345191
Packit 345191
static void regular_nodes_init(void)
Packit 345191
{
Packit 345191
    unsigned i;
Packit 345191
    unsigned nodes_num = (unsigned)numa_num_configured_nodes();
Packit 345191
    struct bitmask *node_cpus = numa_allocate_cpumask();
Packit 345191
Packit 345191
    regular_nodes_mask = numa_allocate_nodemask();
Packit 345191
Packit 345191
    for (i = 0; i < nodes_num; i++) {
Packit 345191
        numa_node_to_cpus(i, node_cpus);
Packit 345191
        if (numa_bitmask_weight(node_cpus))
Packit 345191
            numa_bitmask_setbit(regular_nodes_mask, i);
Packit 345191
    }
Packit 345191
    numa_bitmask_free(node_cpus);
Packit 345191
}
Packit 345191
Packit 345191
static void memkind_regular_init_once(void)
Packit 345191
{
Packit 345191
    regular_nodes_init();
Packit 345191
    memkind_init(MEMKIND_REGULAR, true);
Packit 345191
}
Packit 345191
Packit 345191
static int memkind_regular_check_available(struct memkind *kind)
Packit 345191
{
Packit 345191
    /* init_once method is called in memkind_malloc function
Packit 345191
     * when memkind malloc is not called this function will fail.
Packit 345191
     * Call pthread_once to be sure that situation mentioned
Packit 345191
     * above will never happen */
Packit 345191
    pthread_once(&kind->init_once, kind->ops->init_once);
Packit 345191
    return regular_nodes_mask != NULL ? MEMKIND_SUCCESS : MEMKIND_ERROR_UNAVAILABLE;
Packit 345191
}
Packit 345191
Packit 345191
MEMKIND_EXPORT int memkind_regular_all_get_mbind_nodemask(struct memkind *kind,
Packit 345191
                                                          unsigned long *nodemask,
Packit 345191
                                                          unsigned long maxnode)
Packit 345191
{
Packit 345191
    struct bitmask nodemask_bm = {maxnode, nodemask};
Packit 345191
Packit 345191
    if(!regular_nodes_mask) {
Packit 345191
        return MEMKIND_ERROR_UNAVAILABLE;
Packit 345191
    }
Packit 345191
Packit 345191
    copy_bitmask_to_bitmask(regular_nodes_mask, &nodemask_bm);
Packit 345191
    return MEMKIND_SUCCESS;
Packit 345191
}
Packit 345191
Packit 345191
static int memkind_regular_finalize(memkind_t kind)
Packit 345191
{
Packit 345191
    if(regular_nodes_mask)
Packit 345191
        numa_bitmask_free(regular_nodes_mask);
Packit 345191
Packit 345191
    return memkind_arena_finalize(kind);
Packit 345191
}
Packit 345191
Packit 345191
MEMKIND_EXPORT struct memkind_ops MEMKIND_REGULAR_OPS = {
Packit 345191
    .create = memkind_arena_create,
Packit 345191
    .destroy = memkind_default_destroy,
Packit 345191
    .malloc = memkind_arena_malloc,
Packit 345191
    .calloc = memkind_arena_calloc,
Packit 345191
    .posix_memalign = memkind_arena_posix_memalign,
Packit 345191
    .realloc = memkind_arena_realloc,
Packit 345191
    .free = memkind_arena_free,
Packit 345191
    .check_available = memkind_regular_check_available,
Packit 345191
    .mbind = memkind_default_mbind,
Packit 345191
    .get_mmap_flags = memkind_default_get_mmap_flags,
Packit 345191
    .get_mbind_mode = memkind_default_get_mbind_mode,
Packit 345191
    .get_mbind_nodemask = memkind_regular_all_get_mbind_nodemask,
Packit 345191
    .get_arena = memkind_thread_get_arena,
Packit 345191
    .init_once = memkind_regular_init_once,
Packit 345191
    .malloc_usable_size = memkind_default_malloc_usable_size,
Packit 345191
    .finalize = memkind_regular_finalize,
Packit 345191
    .get_stat = memkind_arena_get_kind_stat,
Packit 345191
    .defrag_reallocate = memkind_arena_defrag_reallocate
Packit 345191
};
Packit 345191
Packit 345191