root/oshmem/shmem/c/shmem_alloc.c

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

DEFINITIONS

This source file includes following definitions.
  1. shmem_malloc
  2. shmem_calloc
  3. shmalloc
  4. _shmalloc
  5. shmemx_malloc_with_hint

   1 /*
   2  * Copyright (c) 2013-2015 Mellanox Technologies, Inc.
   3  *                         All rights reserved.
   4  * $COPYRIGHT$
   5  *
   6  * Additional copyrights may follow
   7  *
   8  * $HEADER$
   9  */
  10 #include "oshmem_config.h"
  11 
  12 #include "oshmem/constants.h"
  13 #include "oshmem/include/shmem.h"
  14 #include "oshmem/include/shmemx.h"
  15 
  16 #include "oshmem/shmem/shmem_api_logger.h"
  17 
  18 #include "oshmem/runtime/runtime.h"
  19 #include "oshmem/mca/memheap/memheap.h"
  20 
  21 #if OSHMEM_PROFILING
  22 #include "oshmem/include/pshmem.h"
  23 #include "oshmem/include/pshmemx.h"
  24 #pragma weak shmem_malloc            = pshmem_malloc
  25 #pragma weak shmem_calloc            = pshmem_calloc
  26 #pragma weak shmalloc                = pshmalloc
  27 #pragma weak shmemx_malloc_with_hint = pshmemx_malloc_with_hint
  28 #include "oshmem/shmem/c/profile/defines.h"
  29 #endif
  30 
  31 static inline void* _shmalloc(size_t size);
  32 
  33 void* shmem_malloc(size_t size)
  34 {
  35     return _shmalloc(size);
  36 }
  37 
  38 void* shmem_calloc(size_t count, size_t size)
  39 {
  40     size_t req_sz = count * size;
  41     void *ptr = _shmalloc(req_sz);
  42     if (ptr) {
  43         memset(ptr, 0, req_sz);
  44     }
  45     return ptr;
  46 }
  47 
  48 void* shmalloc(size_t size)
  49 {
  50     return _shmalloc(size);
  51 }
  52 
  53 static inline void* _shmalloc(size_t size)
  54 {
  55     int rc;
  56     void* pBuff = NULL;
  57 
  58     RUNTIME_CHECK_INIT();
  59     RUNTIME_CHECK_WITH_MEMHEAP_SIZE(size);
  60 
  61     SHMEM_MUTEX_LOCK(shmem_internal_mutex_alloc);
  62 
  63     rc = MCA_MEMHEAP_CALL(alloc(size, &pBuff));
  64 
  65     SHMEM_MUTEX_UNLOCK(shmem_internal_mutex_alloc);
  66 
  67     if (OSHMEM_SUCCESS != rc) {
  68         SHMEM_API_VERBOSE(10,
  69                           "Allocation with shmalloc(size=%lu) failed.",
  70                           (unsigned long)size);
  71         return NULL ;
  72     }
  73 #if OSHMEM_SPEC_COMPAT == 1
  74     shmem_barrier_all();
  75 #endif
  76     return pBuff;
  77 }
  78 
  79 void* shmemx_malloc_with_hint(size_t size, long hint)
  80 {
  81     int rc;
  82     void* pBuff = NULL;
  83 
  84     if (!hint) {
  85         return _shmalloc(size);
  86     }
  87 
  88     RUNTIME_CHECK_INIT();
  89     RUNTIME_CHECK_WITH_MEMHEAP_SIZE(size);
  90 
  91     SHMEM_MUTEX_LOCK(shmem_internal_mutex_alloc);
  92 
  93     rc = mca_memheap_alloc_with_hint(size, hint, &pBuff);
  94 
  95     SHMEM_MUTEX_UNLOCK(shmem_internal_mutex_alloc);
  96 
  97     if (OSHMEM_SUCCESS != rc) {
  98         SHMEM_API_VERBOSE(10,
  99                           "Allocation with shmalloc(size=%lu) failed.",
 100                           (unsigned long)size);
 101         return NULL ;
 102     }
 103 #if OSHMEM_SPEC_COMPAT == 1
 104     shmem_barrier_all();
 105 #endif
 106     return pBuff;
 107 }

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