root/oshmem/mca/memheap/ptmalloc/memheap_ptmalloc.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. mca_memheap_ptmalloc_module_init
  2. mca_memheap_ptmalloc_alloc
  3. mca_memheap_ptmalloc_align
  4. mca_memheap_ptmalloc_realloc
  5. mca_memheap_ptmalloc_free
  6. mca_memheap_ptmalloc_finalize
  7. mca_memheap_ptmalloc_getpagesize
  8. mca_memheap_ptmalloc_sbrk

   1 /* Copyright (c) 2013      Mellanox Technologies, Inc.
   2  *                         All rights reserved.
   3  * Copyright (c) 2019      Research Organization for Information Science
   4  *                         and Technology (RIST).  All rights reserved.
   5  * $COPYRIGHT$
   6  *
   7  * Additional copyrights may follow
   8  *
   9  * $HEADER$
  10  */
  11 
  12 #include "oshmem_config.h"
  13 #include "oshmem/proc/proc.h"
  14 #include "oshmem/mca/spml/spml.h"
  15 #include "oshmem/mca/memheap/memheap.h"
  16 #include "oshmem/mca/memheap/ptmalloc/memheap_ptmalloc.h"
  17 #include "oshmem/mca/memheap/ptmalloc/memheap_ptmalloc_component.h"
  18 #include "oshmem/mca/memheap/base/base.h"
  19 #include "opal/class/opal_hash_table.h"
  20 #include "opal/class/opal_object.h"
  21 
  22 mca_memheap_ptmalloc_module_t memheap_ptmalloc = {
  23     {
  24         &mca_memheap_ptmalloc_component,
  25         mca_memheap_ptmalloc_finalize,
  26         mca_memheap_ptmalloc_alloc,
  27         mca_memheap_ptmalloc_align,
  28         mca_memheap_ptmalloc_realloc,
  29         mca_memheap_ptmalloc_free,
  30 
  31         mca_memheap_ptmalloc_alloc,
  32         mca_memheap_ptmalloc_free,
  33 
  34         mca_memheap_base_get_mkey,
  35         mca_memheap_base_is_symmetric_addr,
  36         mca_memheap_modex_recv_all,
  37 
  38         0
  39     },
  40     100   /* priority */
  41 };
  42 
  43 /* Memory Heap Buddy Implementation */
  44 /**
  45  * Initialize the Memory Heap
  46  */
  47 int mca_memheap_ptmalloc_module_init(memheap_context_t *context)
  48 {
  49     if (!context || !context->user_size || !context->private_size) {
  50         return OSHMEM_ERR_BAD_PARAM;
  51     }
  52 
  53     /* Construct a mutex object */
  54     OBJ_CONSTRUCT(&memheap_ptmalloc.lock, opal_mutex_t);
  55     memheap_ptmalloc.base = context->user_base_addr;
  56     memheap_ptmalloc.cur_size = 0;
  57     memheap_ptmalloc.max_size = context->user_size + context->private_size;
  58     memheap_ptmalloc.max_alloc_size = context->user_size;
  59 
  60     MEMHEAP_VERBOSE(1,
  61                     "symmetric heap memory (user+private): %llu bytes",
  62                     (unsigned long long)(context->user_size + context->private_size));
  63 
  64     /* disable till we figure out double modex&grpcomm.bad problem */
  65     //        memheap_modex_mkey_exchange();
  66     return OSHMEM_SUCCESS;
  67 
  68 }
  69 
  70 /**
  71  * Allocate size bytes on the symmetric heap.
  72  * The allocated variable is aligned to its size.
  73  */
  74 int mca_memheap_ptmalloc_alloc(size_t size, void** p_buff)
  75 {
  76     if (size > memheap_ptmalloc.max_alloc_size) {
  77         *p_buff = 0;
  78         return OSHMEM_ERR_OUT_OF_RESOURCE;
  79     }
  80 
  81     OPAL_THREAD_LOCK(&memheap_ptmalloc.lock);
  82     *p_buff = dlmalloc(size);
  83     OPAL_THREAD_UNLOCK(&memheap_ptmalloc.lock);
  84 
  85     if (NULL == *p_buff)
  86         return OSHMEM_ERROR;
  87 
  88     MCA_SPML_CALL(memuse_hook(*p_buff, size));
  89     return OSHMEM_SUCCESS;
  90 }
  91 
  92 int mca_memheap_ptmalloc_align(size_t align, size_t size, void **p_buff)
  93 {
  94     if (size > memheap_ptmalloc.max_alloc_size) {
  95         *p_buff = 0;
  96         return OSHMEM_ERR_OUT_OF_RESOURCE;
  97     }
  98 
  99     if (align == 0) {
 100         *p_buff = 0;
 101         return OSHMEM_ERROR;
 102     }
 103 
 104     /* check that align is power of 2 */
 105     if (align & (align - 1)) {
 106         *p_buff = 0;
 107         return OSHMEM_ERROR;
 108     }
 109 
 110     OPAL_THREAD_LOCK(&memheap_ptmalloc.lock);
 111     *p_buff = dlmemalign(align, size);
 112     OPAL_THREAD_UNLOCK(&memheap_ptmalloc.lock);
 113 
 114     if (NULL == *p_buff)
 115         return OSHMEM_ERROR;
 116 
 117     MCA_SPML_CALL(memuse_hook(*p_buff, size));
 118     return OSHMEM_SUCCESS;
 119 }
 120 
 121 int mca_memheap_ptmalloc_realloc(size_t new_size,
 122                                  void *p_buff,
 123                                  void **p_new_buff)
 124 {
 125     if (new_size > memheap_ptmalloc.max_alloc_size) {
 126         *p_new_buff = 0;
 127         return OSHMEM_ERR_OUT_OF_RESOURCE;
 128     }
 129 
 130     OPAL_THREAD_LOCK(&memheap_ptmalloc.lock);
 131     *p_new_buff = dlrealloc(p_buff, new_size);
 132     OPAL_THREAD_UNLOCK(&memheap_ptmalloc.lock);
 133 
 134     if (!*p_new_buff)
 135         return OSHMEM_ERR_OUT_OF_RESOURCE;
 136 
 137     MCA_SPML_CALL(memuse_hook(*p_new_buff, new_size));
 138     return OSHMEM_SUCCESS;
 139 }
 140 
 141 /*
 142  * Free a variable allocated on the
 143  * symmetric heap.
 144  */
 145 int mca_memheap_ptmalloc_free(void* ptr)
 146 {
 147     OPAL_THREAD_LOCK(&memheap_ptmalloc.lock);
 148     dlfree(ptr);
 149     OPAL_THREAD_UNLOCK(&memheap_ptmalloc.lock);
 150     return OSHMEM_SUCCESS;
 151 }
 152 
 153 int mca_memheap_ptmalloc_finalize()
 154 {
 155     MEMHEAP_VERBOSE(5, "deregistering symmetric heap");
 156     return OSHMEM_SUCCESS;
 157 }
 158 
 159 int mca_memheap_ptmalloc_getpagesize(void)
 160 {
 161     return 2 * 1024 * 1024;
 162 }
 163 
 164 /* must be same as in malloc.c */
 165 #define PTMALLOC_MAX_SIZE_T           (~(size_t)0)
 166 #define PTMALLOC_MFAIL                ((void*)(PTMALLOC_MAX_SIZE_T))
 167 void *mca_memheap_ptmalloc_sbrk(size_t size)
 168 {
 169     char *ret;
 170 
 171     if (memheap_ptmalloc.cur_size + size > memheap_ptmalloc.max_size) {
 172         return PTMALLOC_MFAIL ;
 173     }
 174 
 175     ret = (char *) memheap_ptmalloc.base + memheap_ptmalloc.cur_size;
 176     memheap_ptmalloc.cur_size += size;
 177 
 178     return ret;
 179 }
 180 

/* [<][>][^][v][top][bottom][index][help] */