1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil -*- */
2 /*
3 * Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
4 * University Research and Technology
5 * Corporation. All rights reserved.
6 * Copyright (c) 2004-2006 The University of Tennessee and The University
7 * of Tennessee Research Foundation. All rights
8 * reserved.
9 * Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
10 * University of Stuttgart. All rights reserved.
11 * Copyright (c) 2004-2005 The Regents of the University of California.
12 * All rights reserved.
13 * Copyright (c) 2009 Sun Microsystems, Inc. All rights reserved.
14 * Copyright (c) 2010-2017 IBM Corporation. All rights reserved.
15 * Copyright (c) 2015 Los Alamos National Security, LLC. All rights
16 * reserved.
17 * Copyright (c) 2017 Research Organization for Information Science
18 * and Technology (RIST). All rights reserved.
19 * $COPYRIGHT$
20 *
21 * Additional copyrights may follow
22 *
23 * $HEADER$
24 */
25
26 #include "opal_config.h"
27 #include <stdint.h>
28 #include <string.h>
29 #include "opal/mca/mpool/mpool.h"
30 #include "base.h"
31 #include "mpool_base_tree.h"
32 #include "opal/threads/mutex.h"
33 #include "opal/util/info.h"
34
35
36 static void unregister_tree_item(mca_mpool_base_tree_item_t *mpool_tree_item)
37 {
38 mca_mpool_base_module_t *mpool;
39
40 mpool = mpool_tree_item->mpool;
41 mpool->mpool_free(mpool, mpool_tree_item->key);
42 }
43
44 /**
45 * Function to allocate special memory according to what the user requests in
46 * the info object.
47 *
48 * If the info parameter is MPI_INFO_NULL, then this function will try to allocate
49 * the memory with the optionally named mpool or malloc and try to register the
50 * pointer with as many registration caches as possible. Registration caches that
51 * fail to register the region will be ignored. The mpool name can optionally be
52 * specified in the info object.
53 *
54 * @param size the size of the memory area to allocate
55 * @param info an info object which tells us what kind of memory to allocate
56 *
57 * @retval pointer to the allocated memory
58 * @retval NULL on failure
59 */
60 void *mca_mpool_base_alloc(size_t size, opal_info_t *info, const char *hints)
61 {
62 mca_mpool_base_tree_item_t *mpool_tree_item = NULL;
63 mca_mpool_base_module_t *mpool;
64 void *mem = NULL;
65 #if defined(TODO_BTL_GB)
66 int flag = 0;
67 #endif /* defined(TODO_BTL_GB) */
68
69 mpool_tree_item = mca_mpool_base_tree_item_get ();
70 if (!mpool_tree_item) {
71 return NULL;
72 }
73
74 mpool_tree_item->num_bytes = size;
75 mpool_tree_item->count = 0;
76
77 mpool = mca_mpool_base_module_lookup (hints);
78 if (NULL != mpool) {
79 mem = mpool->mpool_alloc (mpool, size, sizeof(void *), 0);
80 }
81
82 if (NULL == mem) {
83 /* fall back on malloc */
84 mem = malloc(size);
85
86 mca_mpool_base_tree_item_put (mpool_tree_item);
87 } else {
88 mpool_tree_item->mpool = mpool;
89 mpool_tree_item->key = mem;
90 mca_mpool_base_tree_insert (mpool_tree_item);
91 }
92
93 return mem;
94 }
95
96 /**
97 * Function to free memory previously allocated by mca_mpool_base_alloc
98 *
99 * @param base pointer to the memory to free
100 *
101 * @retval OPAL_SUCCESS
102 * @retval OPAL_ERR_BAD_PARAM if the passed base pointer was invalid
103 */
104 int mca_mpool_base_free(void *base)
105 {
106 mca_mpool_base_tree_item_t *mpool_tree_item = NULL;
107 int rc;
108
109 if(!base) {
110 return OPAL_ERROR;
111 }
112
113 mpool_tree_item = mca_mpool_base_tree_find(base);
114
115 if(!mpool_tree_item) {
116 /* nothing in the tree this was just plain old malloc'd memory */
117 free(base);
118 return OPAL_SUCCESS;
119 }
120
121 rc = mca_mpool_base_tree_delete(mpool_tree_item);
122 if(OPAL_SUCCESS == rc) {
123 unregister_tree_item(mpool_tree_item);
124 mca_mpool_base_tree_item_put(mpool_tree_item);
125 }
126
127 return rc;
128 }