Skip to content

Commit 6ffc7cc

Browse files
committed
mpool: add new base module type "basic"
This commit adds a new mpool base module type: basic. This module can be used with an opal_free_list_t to allocate space from a pre-allocated block (such as a shared memory region). The new module only supports allocation and is not meant for more dynamic use cases at this time. Signed-off-by: Nathan Hjelm <hjelmn@lanl.gov>
1 parent 1c5f97b commit 6ffc7cc

File tree

3 files changed

+113
-1
lines changed

3 files changed

+113
-1
lines changed

opal/mca/mpool/base/Makefile.am

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ libmca_mpool_la_SOURCES += \
2828
base/mpool_base_lookup.c \
2929
base/mpool_base_alloc.c \
3030
base/mpool_base_tree.c \
31-
base/mpool_base_default.c
31+
base/mpool_base_default.c \
32+
base/mpool_base_basic.c
3233

3334
dist_opaldata_DATA += \
3435
base/help-mpool-base.txt

opal/mca/mpool/base/base.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ OPAL_DECLSPEC OBJ_CLASS_DECLARATION(mca_mpool_base_selected_module_t);
5353
OPAL_DECLSPEC mca_mpool_base_component_t* mca_mpool_base_component_lookup(const char* name);
5454
OPAL_DECLSPEC mca_mpool_base_module_t* mca_mpool_base_module_lookup(const char* name);
5555

56+
OPAL_DECLSPEC mca_mpool_base_module_t *mca_mpool_basic_create (void *base, size_t size, unsigned min_align);
57+
5658
/*
5759
* Globals
5860
*/
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil -*- */
2+
/*
3+
* Copyright (c) 2015 Los Alamos National Security, LLC. All rights
4+
* reserved.
5+
* Copyright (c) 2016 Intel, Inc. All rights reserved.
6+
* Copyrigth (c) 2018 Triad National Security, LLC. All rights
7+
* reserved.
8+
* $COPYRIGHT$
9+
*
10+
* Additional copyrights may follow
11+
*
12+
* $HEADER$
13+
*/
14+
15+
#include "opal_config.h"
16+
#include "opal/align.h"
17+
18+
#include <stdio.h>
19+
#include <stdlib.h>
20+
#ifdef HAVE_UNISTD_H
21+
#include <unistd.h>
22+
#endif /* HAVE_UNISTD_H */
23+
24+
#include "opal/mca/mca.h"
25+
#include "opal/mca/base/base.h"
26+
#include "opal/mca/mpool/base/base.h"
27+
#include "opal/constants.h"
28+
#include "opal/util/sys_limits.h"
29+
30+
struct mca_mpool_base_basic_module_t {
31+
mca_mpool_base_module_t super;
32+
opal_mutex_t lock;
33+
uintptr_t ptr;
34+
size_t size;
35+
size_t avail;
36+
unsigned min_align;
37+
};
38+
typedef struct mca_mpool_base_basic_module_t mca_mpool_base_basic_module_t;
39+
40+
static void *mca_mpool_base_basic_alloc (mca_mpool_base_module_t *mpool, size_t size,
41+
size_t align, uint32_t flags)
42+
{
43+
mca_mpool_base_basic_module_t *basic_module = (mca_mpool_base_basic_module_t *) mpool;
44+
uintptr_t next_ptr;
45+
void *ptr;
46+
47+
opal_mutex_lock (&basic_module->lock);
48+
49+
align = align > basic_module->min_align ? align : basic_module->min_align;
50+
51+
next_ptr = OPAL_ALIGN(basic_module->ptr, align, uintptr_t);
52+
53+
size = OPAL_ALIGN(size, 8, size_t) + next_ptr - basic_module->ptr;
54+
55+
if (size > basic_module->avail) {
56+
opal_mutex_unlock (&basic_module->lock);
57+
return NULL;
58+
}
59+
60+
ptr = (void *) next_ptr;
61+
basic_module->avail -= size;
62+
basic_module->ptr += size;
63+
64+
opal_mutex_unlock (&basic_module->lock);
65+
return ptr;
66+
}
67+
68+
/**
69+
* free function
70+
*/
71+
static void mca_mpool_base_basic_free (mca_mpool_base_module_t *mpool, void *addr)
72+
{
73+
/* nothing to do for now */
74+
}
75+
76+
static void mca_mpool_base_basic_finalize (struct mca_mpool_base_module_t *mpool)
77+
{
78+
mca_mpool_base_basic_module_t *basic_module = (mca_mpool_base_basic_module_t *) mpool;
79+
80+
OBJ_DESTRUCT(&basic_module->lock);
81+
free (mpool);
82+
}
83+
84+
static mca_mpool_base_module_t mca_mpool_basic_template = {
85+
.mpool_alloc = mca_mpool_base_basic_alloc,
86+
.mpool_free = mca_mpool_base_basic_free,
87+
.mpool_finalize = mca_mpool_base_basic_finalize,
88+
.flags = MCA_MPOOL_FLAGS_MPI_ALLOC_MEM,
89+
};
90+
91+
mca_mpool_base_module_t *mca_mpool_basic_create (void *base, size_t size, unsigned min_align)
92+
{
93+
mca_mpool_base_basic_module_t *basic_module = calloc (1, sizeof (*basic_module));
94+
95+
if (OPAL_UNLIKELY(NULL == basic_module)) {
96+
return NULL;
97+
}
98+
99+
memcpy (&basic_module->super, &mca_mpool_basic_template, sizeof (mca_mpool_basic_template));
100+
101+
OBJ_CONSTRUCT(&basic_module->lock, opal_mutex_t);
102+
103+
basic_module->super.mpool_base = base;
104+
basic_module->ptr = (uintptr_t) base;
105+
basic_module->size = basic_module->avail = size;
106+
basic_module->min_align = min_align;
107+
108+
return &basic_module->super;
109+
}

0 commit comments

Comments
 (0)