ARGOBOTS  1227c643f7a7f974f1f1778a9ffebd29d7dafecf
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups
util.c
Go to the documentation of this file.
1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
2 /*
3  * See COPYRIGHT in top-level directory.
4  */
5 
6 #include "abti.h"
7 
8 #if defined(__ibmxl__) || defined(__xlc__)
9 
10 ABTU_ret_err int ABTU_memalign(size_t alignment, size_t size, void **p_ptr)
11 {
12  void *ptr;
13  int ret = posix_memalign(&ptr, alignment, size);
14  if (ABTI_IS_ERROR_CHECK_ENABLED && ret != 0) {
15  return ABT_ERR_MEM;
16  }
17  *p_ptr = ptr;
18  return ABT_SUCCESS;
19 }
20 
21 void ABTU_free(void *ptr)
22 {
23  free(ptr);
24 }
25 
26 #ifdef ABT_CONFIG_USE_ALIGNED_ALLOC
27 
28 ABTU_ret_err int ABTU_malloc(size_t size, void **p_ptr)
29 {
30  /* Round up to the smallest multiple of ABT_CONFIG_STATIC_CACHELINE_SIZE
31  * which is greater than or equal to size in order to avoid any
32  * false-sharing. */
33  size = (size + ABT_CONFIG_STATIC_CACHELINE_SIZE - 1) &
36 }
37 
38 ABTU_ret_err int ABTU_calloc(size_t num, size_t size, void **p_ptr)
39 {
40  void *ptr;
41  int ret = ABTU_malloc(num * size, &ptr);
43  return ABT_ERR_MEM;
44  }
45  memset(ptr, 0, num * size);
46  *p_ptr = ptr;
47  return ABT_SUCCESS;
48 }
49 
50 ABTU_ret_err int ABTU_realloc(size_t old_size, size_t new_size, void **p_ptr)
51 {
52  void *new_ptr, *old_ptr = *p_ptr;
53  int ret = ABTU_malloc(new_size, &new_ptr);
55  return ABT_ERR_MEM;
56  }
57  memcpy(new_ptr, old_ptr, (old_size < new_size) ? old_size : new_size);
58  ABTU_free(old_ptr);
59  *p_ptr = new_ptr;
60  return ABT_SUCCESS;
61 }
62 
63 #else /* ABT_CONFIG_USE_ALIGNED_ALLOC */
64 
65 ABTU_ret_err int ABTU_malloc(size_t size, void **p_ptr)
66 {
67  void *ptr = malloc(size);
68  if (ABTI_IS_ERROR_CHECK_ENABLED && ptr == NULL) {
69  return ABT_ERR_MEM;
70  }
71  *p_ptr = ptr;
72  return ABT_SUCCESS;
73 }
74 
75 ABTU_ret_err int ABTU_calloc(size_t num, size_t size, void **p_ptr)
76 {
77  void *ptr = calloc(num, size);
78  if (ABTI_IS_ERROR_CHECK_ENABLED && ptr == NULL) {
79  return ABT_ERR_MEM;
80  }
81  *p_ptr = ptr;
82  return ABT_SUCCESS;
83 }
84 
85 ABTU_ret_err int ABTU_realloc(size_t old_size, size_t new_size, void **p_ptr)
86 {
87  (void)old_size;
88  void *ptr = realloc(*p_ptr, new_size);
89  if (ABTI_IS_ERROR_CHECK_ENABLED && ptr == NULL) {
90  return ABT_ERR_MEM;
91  }
92  *p_ptr = ptr;
93  return ABT_SUCCESS;
94 }
95 #endif
96 
97 #endif /* !(defined(__ibmxl__) || defined(__xlc__)) */
#define ABT_CONFIG_STATIC_CACHELINE_SIZE
Definition: abt_config.h:57
static ABTU_ret_err int ABTU_malloc(size_t size, void **p_ptr)
Definition: abtu.h:142
static ABTU_ret_err int ABTU_realloc(size_t old_size, size_t new_size, void **p_ptr)
Definition: abtu.h:165
static ABTU_ret_err int ABTU_memalign(size_t alignment, size_t size, void **p_ptr)
Definition: abtu.h:123
#define ABT_ERR_MEM
Definition: abt.h:66
#define ABT_SUCCESS
Definition: abt.h:64
#define ABTI_IS_ERROR_CHECK_ENABLED
Definition: abti.h:20
static ABTU_ret_err int ABTU_calloc(size_t num, size_t size, void **p_ptr)
Definition: abtu.h:152
static void ABTU_free(void *ptr)
Definition: abtu.h:135
#define ABTU_ret_err
Definition: abtu.h:49