|
| 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