File: mpool_base_alloc.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 (142 lines) | stat: -rw-r--r-- 4,646 bytes parent folder | download | duplicates (4)
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil -*- */
/*
 * Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
 *                         University Research and Technology
 *                         Corporation.  All rights reserved.
 * Copyright (c) 2004-2006 The University of Tennessee and The University
 *                         of Tennessee Research Foundation.  All rights
 *                         reserved.
 * Copyright (c) 2004-2020 High Performance Computing Center Stuttgart,
 *                         University of Stuttgart.  All rights reserved.
 * Copyright (c) 2004-2005 The Regents of the University of California.
 *                         All rights reserved.
 * Copyright (c) 2009      Sun Microsystems, Inc.  All rights reserved.
 * Copyright (c) 2010-2017 IBM Corporation. All rights reserved.
 * Copyright (c) 2015      Los Alamos National Security, LLC.  All rights
 *                         reserved.
 * Copyright (c) 2017      Research Organization for Information Science
 *                         and Technology (RIST). All rights reserved.
 * $COPYRIGHT$
 *
 * Additional copyrights may follow
 *
 * $HEADER$
 */

#include "opal_config.h"
#include "base.h"
#include "mpool_base_tree.h"
#include "opal/align.h"
#include "opal/mca/mpool/mpool.h"
#include "opal/mca/threads/mutex.h"
#include "opal/util/info.h"
#include <stdint.h>
#include <string.h>

static void unregister_tree_item(mca_mpool_base_tree_item_t *mpool_tree_item)
{
    mca_mpool_base_module_t *mpool;

    mpool = mpool_tree_item->mpool;
    mpool->mpool_free(mpool, mpool_tree_item->key);
}

/**
 * Function to allocate special memory according to what the user requests in
 * the info object.
 *
 * If the info parameter is MPI_INFO_NULL, then this function will try to allocate
 * the memory with the optionally named mpool or malloc and try to register the
 * pointer with as many registration caches as possible. Registration caches that
 * fail to register the region will be ignored. The mpool name can optionally be
 * specified in the info object.
 *
 * @param size the size of the memory area to allocate
 * @param info an info object which tells us what kind of memory to allocate
 *
 * @retval pointer to the allocated memory
 * @retval NULL on failure
 */
void *mca_mpool_base_alloc(size_t size, opal_info_t *info, const char *hints)
{
    mca_mpool_base_tree_item_t *mpool_tree_item = NULL;
    mca_mpool_base_module_t *mpool;
    void *mem = NULL;
    opal_cstring_t *align_info_str;
    long long memory_alignment = OPAL_ALIGN_MIN;

    mpool_tree_item = mca_mpool_base_tree_item_get();
    if (!mpool_tree_item) {
        return NULL;
    }

    if (NULL != info) {
        int flag;
        opal_info_get(info, "mpi_minimum_memory_alignment",
                      &align_info_str, &flag);

        if (flag) {
            long long tmp_align = atoll(align_info_str->string);
            OBJ_RELEASE(align_info_str);
            if (tmp_align > memory_alignment) {
                memory_alignment = tmp_align;
            }
        }
    }

    mpool_tree_item->num_bytes = size;
    mpool_tree_item->count = 0;

    mpool = mca_mpool_base_module_lookup(hints);
    if (NULL != mpool) {
        mem = mpool->mpool_alloc (mpool, size, memory_alignment, 0);
    }

    if (NULL == mem) {
        /* fall back to default mpool */
        mem = mca_mpool_base_default_module->mpool_alloc(mca_mpool_base_default_module,
                                                         size, memory_alignment, 0);

        mca_mpool_base_tree_item_put(mpool_tree_item);
    } else {
        mpool_tree_item->mpool = mpool;
        mpool_tree_item->key = mem;
        mca_mpool_base_tree_insert(mpool_tree_item);
    }

    return mem;
}

/**
 * Function to free memory previously allocated by mca_mpool_base_alloc
 *
 * @param base pointer to the memory to free
 *
 * @retval OPAL_SUCCESS
 * @retval OPAL_ERR_BAD_PARAM if the passed base pointer was invalid
 */
int mca_mpool_base_free(void *base)
{
    mca_mpool_base_tree_item_t *mpool_tree_item = NULL;
    int rc;

    if (!base) {
        return OPAL_ERROR;
    }

    mpool_tree_item = mca_mpool_base_tree_find(base);

    if (!mpool_tree_item) {
        /* nothing in the tree this was just plain old malloc'd memory */
        mca_mpool_base_default_module->mpool_free(mca_mpool_base_default_module, base);
        return OPAL_SUCCESS;
    }

    rc = mca_mpool_base_tree_delete(mpool_tree_item);
    if (OPAL_SUCCESS == rc) {
        unregister_tree_item(mpool_tree_item);
        mca_mpool_base_tree_item_put(mpool_tree_item);
    }

    return rc;
}