ARGOBOTS
abtu.h
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 #ifndef ABTU_H_INCLUDED
7 #define ABTU_H_INCLUDED
8 
9 #include <stdlib.h>
10 #include <string.h>
11 #include <assert.h>
12 #include "abt_config.h"
13 
14 /* Utility feature */
15 #ifdef ABT_CONFIG_HAVE___BUILTIN_EXPECT
16 #define ABTU_likely(cond) __builtin_expect(!!(cond), 1)
17 #define ABTU_unlikely(cond) __builtin_expect(!!(cond), 0)
18 #else
19 #define ABTU_likely(cond) (cond)
20 #define ABTU_unlikely(cond) (cond)
21 #endif
22 
23 /* Utility Functions */
24 
25 static inline void *ABTU_memalign(size_t alignment, size_t size)
26 {
27  void *p_ptr;
28  int ret = posix_memalign(&p_ptr, alignment, size);
29  assert(ret == 0);
30  return p_ptr;
31 }
32 static inline void ABTU_free(void *ptr)
33 {
34  free(ptr);
35 }
36 
37 #ifdef ABT_CONFIG_USE_ALIGNED_ALLOC
38 
39 static inline void *ABTU_malloc(size_t size)
40 {
41  /* Round up to the smallest multiple of ABT_CONFIG_STATIC_CACHELINE_SIZE
42  * which is greater than or equal to size in order to avoid any
43  * false-sharing. */
44  size = (size + ABT_CONFIG_STATIC_CACHELINE_SIZE - 1) &
47 }
48 
49 static inline void *ABTU_calloc(size_t num, size_t size)
50 {
51  void *ptr = ABTU_malloc(num * size);
52  memset(ptr, 0, num * size);
53  return ptr;
54 }
55 
56 static inline void *ABTU_realloc(void *ptr, size_t old_size, size_t new_size)
57 {
58  void *new_ptr = ABTU_malloc(new_size);
59  memcpy(new_ptr, ptr, (old_size < new_size) ? old_size : new_size);
60  ABTU_free(ptr);
61  return new_ptr;
62 }
63 
64 #else /* ABT_CONFIG_USE_ALIGNED_ALLOC */
65 
66 static inline void *ABTU_malloc(size_t size)
67 {
68  return malloc(size);
69 }
70 
71 static inline void *ABTU_calloc(size_t num, size_t size)
72 {
73  return calloc(num, size);
74 }
75 
76 static inline void *ABTU_realloc(void *ptr, size_t old_size, size_t new_size)
77 {
78  (void)old_size;
79  return realloc(ptr, new_size);
80 }
81 
82 #endif /* !ABT_CONFIG_USE_ALIGNED_ALLOC */
83 
84 #define ABTU_strcpy(d, s) strcpy(d, s)
85 #define ABTU_strncpy(d, s, n) strncpy(d, s, n)
86 
87 /* The caller should free the memory returned. */
88 char *ABTU_get_indent_str(int indent);
89 
90 int ABTU_get_int_len(size_t num);
91 
92 #endif /* ABTU_H_INCLUDED */
int ABTU_get_int_len(size_t num)
Definition: util.c:23
char * ABTU_get_indent_str(int indent)
Definition: util.c:12
static void * ABTU_malloc(size_t size)
Definition: abtu.h:39
#define ABT_CONFIG_STATIC_CACHELINE_SIZE
Definition: abt_config.h:51
static void * ABTU_realloc(void *ptr, size_t old_size, size_t new_size)
Definition: abtu.h:56
static void * ABTU_memalign(size_t alignment, size_t size)
Definition: abtu.h:25
static void ABTU_free(void *ptr)
Definition: abtu.h:32
static void * ABTU_calloc(size_t num, size_t size)
Definition: abtu.h:49