File: mpool_base_basic.c

package info (click to toggle)
openmpi 5.0.8-4
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 201,684 kB
  • sloc: ansic: 613,078; makefile: 42,353; sh: 11,194; javascript: 9,244; f90: 7,052; java: 6,404; perl: 5,179; python: 1,859; lex: 740; fortran: 61; cpp: 20; tcl: 12
file content (116 lines) | stat: -rw-r--r-- 3,271 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil -*- */
/*
 * Copyright (c) 2015      Los Alamos National Security, LLC. All rights
 *                         reserved.
 * Copyright (c) 2016      Intel, Inc. All rights reserved.
 * Copyright (c) 2018      Triad National Security, LLC. All rights
 *                         reserved.
 * $COPYRIGHT$
 *
 * Additional copyrights may follow
 *
 * $HEADER$
 */

#include "opal_config.h"
#include "opal/align.h"

#include <stdio.h>
#include <stdlib.h>
#ifdef HAVE_UNISTD_H
#    include <unistd.h>
#endif /* HAVE_UNISTD_H */

#include "opal/constants.h"
#include "opal/mca/base/base.h"
#include "opal/mca/mca.h"
#include "opal/mca/mpool/base/base.h"
#include "opal/util/sys_limits.h"

struct mca_mpool_base_basic_module_t {
    mca_mpool_base_module_t super;
    opal_mutex_t lock;
    uintptr_t ptr;
    size_t size;
    size_t avail;
    unsigned min_align;
};
typedef struct mca_mpool_base_basic_module_t mca_mpool_base_basic_module_t;

static void *mca_mpool_base_basic_alloc(mca_mpool_base_module_t *mpool, size_t size, size_t align,
                                        uint32_t flags)
{
    mca_mpool_base_basic_module_t *basic_module = (mca_mpool_base_basic_module_t *) mpool;
    uintptr_t next_ptr;
    void *ptr;

    opal_mutex_lock(&basic_module->lock);

    align = align > basic_module->min_align ? align : basic_module->min_align;

    next_ptr = OPAL_ALIGN(basic_module->ptr, align, uintptr_t);

    size = OPAL_ALIGN(size, 8, size_t) + next_ptr - basic_module->ptr;

    if (size > basic_module->avail) {
        opal_mutex_unlock(&basic_module->lock);
        return NULL;
    }

    ptr = (void *) next_ptr;
    basic_module->avail -= size;
    basic_module->ptr += size;

    opal_mutex_unlock(&basic_module->lock);
    return ptr;
}

/**
 * free function
 */
static void mca_mpool_base_basic_free(mca_mpool_base_module_t *mpool, void *addr)
{
    /* nothing to do for now */
}

static void mca_mpool_base_basic_finalize(struct mca_mpool_base_module_t *mpool)
{
    mca_mpool_base_basic_module_t *basic_module = (mca_mpool_base_basic_module_t *) mpool;

    OBJ_DESTRUCT(&basic_module->lock);
    free(mpool);
}

static void *mca_mpool_base_basic_base(mca_mpool_base_module_t *mpool)
{
    mca_mpool_base_basic_module_t *basic_module = (mca_mpool_base_basic_module_t *) mpool;

    return (void *) basic_module->ptr;
}

static mca_mpool_base_module_t mca_mpool_basic_template = {
    .mpool_base = mca_mpool_base_basic_base,
    .mpool_alloc = mca_mpool_base_basic_alloc,
    .mpool_free = mca_mpool_base_basic_free,
    .mpool_finalize = mca_mpool_base_basic_finalize,
    .flags = MCA_MPOOL_FLAGS_MPI_ALLOC_MEM,
};

mca_mpool_base_module_t *mca_mpool_basic_create(void *base, size_t size, unsigned min_align)
{
    mca_mpool_base_basic_module_t *basic_module = calloc(1, sizeof(*basic_module));

    if (OPAL_UNLIKELY(NULL == basic_module)) {
        return NULL;
    }

    memcpy(&basic_module->super, &mca_mpool_basic_template, sizeof(mca_mpool_basic_template));

    OBJ_CONSTRUCT(&basic_module->lock, opal_mutex_t);

    basic_module->ptr = (uintptr_t) base;
    basic_module->size = basic_module->avail = size;
    basic_module->min_align = min_align;

    return &basic_module->super;
}