ARGOBOTS  dce6e727ffc4ca5b3ffc04cb9517c6689be51ec5
pool_config.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 #include <stdlib.h>
9 #include <stdarg.h>
10 #include <string.h>
11 
12 #define POOL_CONFIG_HTABLE_SIZE 8
13 typedef struct {
14  ABT_pool_config_type type; /* Element type. */
15  union {
16  int v_int;
17  double v_double;
18  void *v_ptr;
19  } val;
21 
23  int val);
25  double val);
27  void *ptr);
28 ABTU_ret_err static int
30  ABT_pool_config_type type, const void *p_val);
31 static void pool_config_read_element(const pool_config_element *p_elem,
32  void *ptr);
33 
38 /* Global configurable parameters */
40  .key = -2, .type = ABT_POOL_CONFIG_INT
41 };
42 
80 {
81  ABTI_UB_ASSERT(ABTI_initialized());
82 
83  int abt_errno;
84  ABTI_pool_config *p_config;
85 
86  abt_errno = ABTU_calloc(1, sizeof(ABTI_pool_config), (void **)&p_config);
87  ABTI_CHECK_ERROR(abt_errno);
88  abt_errno =
90  sizeof(pool_config_element), &p_config->p_table);
91  if (abt_errno != ABT_SUCCESS) {
92  ABTU_free(p_config);
93  ABTI_HANDLE_ERROR(abt_errno);
94  }
95 
96  *config = ABTI_pool_config_get_handle(p_config);
97  return ABT_SUCCESS;
98 }
99 
124 {
125  ABTI_UB_ASSERT(ABTI_initialized());
126  ABTI_UB_ASSERT(config);
127 
128  ABTI_pool_config *p_config = ABTI_pool_config_get_ptr(*config);
129  ABTI_CHECK_NULL_POOL_CONFIG_PTR(p_config);
130 
131  ABTU_hashtable_free(p_config->p_table);
132  ABTU_free(p_config);
133 
134  *config = ABT_POOL_CONFIG_NULL;
135 
136  return ABT_SUCCESS;
137 }
138 
184  ABT_pool_config_type type, const void *val)
185 {
186  ABTI_UB_ASSERT(ABTI_initialized());
187 
188  ABTI_pool_config *p_config = ABTI_pool_config_get_ptr(config);
189  ABTI_CHECK_NULL_POOL_CONFIG_PTR(p_config);
190  if (val) {
191  /* Add a value. */
192  pool_config_element data;
193  int abt_errno;
194  abt_errno = pool_config_create_element_typed(&data, type, val);
195  ABTI_CHECK_ERROR(abt_errno);
196  abt_errno = ABTU_hashtable_set(p_config->p_table, key, &data, NULL);
197  ABTI_CHECK_ERROR(abt_errno);
198  } else {
199  /* Delete a value. */
200  ABTU_hashtable_delete(p_config->p_table, key, NULL);
201  }
202  return ABT_SUCCESS;
203 }
204 
244  ABT_pool_config_type *type, void *val)
245 {
246  ABTI_UB_ASSERT(ABTI_initialized());
247 
248  ABTI_pool_config *p_config = ABTI_pool_config_get_ptr(config);
249  ABTI_CHECK_NULL_POOL_CONFIG_PTR(p_config);
250  pool_config_element data;
251  int found;
252  ABTU_hashtable_get(p_config->p_table, key, &data, &found);
253  if (found) {
254  if (val) {
255  pool_config_read_element(&data, val);
256  }
257  if (type) {
258  *type = data.type;
259  }
260  } else {
261  ABTI_HANDLE_ERROR(ABT_ERR_INV_ARG);
262  }
263  return ABT_SUCCESS;
264 }
265 
266 /*****************************************************************************/
267 /* Private APIs */
268 /*****************************************************************************/
269 
270 ABTU_ret_err int ABTI_pool_config_read(const ABTI_pool_config *p_config,
271  int key, void *p_val)
272 {
273  int found;
274  pool_config_element data;
275  ABTU_hashtable_get(p_config->p_table, key, &data, &found);
276  if (found) {
277  if (p_val) {
278  pool_config_read_element(&data, p_val);
279  }
280  return ABT_SUCCESS;
281  } else {
282  return ABT_ERR_INV_ARG;
283  }
284 }
285 
286 /*****************************************************************************/
287 /* Internal static functions */
288 /*****************************************************************************/
289 
291 {
292  memset(p_elem, 0, sizeof(pool_config_element));
293  p_elem->type = ABT_POOL_CONFIG_INT;
294  p_elem->val.v_int = val;
295 }
296 
298  double val)
299 {
300  memset(p_elem, 0, sizeof(pool_config_element));
301  p_elem->type = ABT_POOL_CONFIG_DOUBLE;
302  p_elem->val.v_double = val;
303 }
304 
306  void *ptr)
307 {
308  memset(p_elem, 0, sizeof(pool_config_element));
309  p_elem->type = ABT_POOL_CONFIG_PTR;
310  p_elem->val.v_ptr = ptr;
311 }
312 
313 ABTU_ret_err static int
315  ABT_pool_config_type type, const void *p_val)
316 {
317  switch (type) {
318  case ABT_POOL_CONFIG_INT: {
319  pool_config_create_element_int(p_elem, *(const int *)p_val);
320  break;
321  }
322  case ABT_POOL_CONFIG_DOUBLE: {
323  pool_config_create_element_double(p_elem, *(const double *)p_val);
324  break;
325  }
326  case ABT_POOL_CONFIG_PTR: {
327  pool_config_create_element_ptr(p_elem, *(void *const *)p_val);
328  break;
329  }
330  default:
331  return ABT_ERR_INV_ARG;
332  }
333  return ABT_SUCCESS;
334 }
335 
337  void *ptr)
338 {
339  switch (p_elem->type) {
340  case ABT_POOL_CONFIG_INT: {
341  *((int *)ptr) = p_elem->val.v_int;
342  break;
343  }
344  case ABT_POOL_CONFIG_DOUBLE: {
345  *((double *)ptr) = p_elem->val.v_double;
346  break;
347  }
348  case ABT_POOL_CONFIG_PTR: {
349  *((void **)ptr) = p_elem->val.v_ptr;
350  break;
351  }
352  default:
353  ABTI_ASSERT(0);
354  }
355 }
pool_config_element::val
union pool_config_element::@1 val
ABTU_hashtable_create
ABTU_ret_err int ABTU_hashtable_create(size_t num_entries, size_t data_size, ABTU_hashtable **pp_hashtable)
Definition: hashtable.c:21
ABT_pool_config_type
ABT_pool_config_type
A struct that sets and gets a pool configuration.
Definition: abt.h:1516
pool_config_create_element_int
static void pool_config_create_element_int(pool_config_element *p_elem, int val)
Definition: pool_config.c:290
pool_config_element
Definition: pool_config.c:13
ABTU_hashtable_get
void ABTU_hashtable_get(const ABTU_hashtable *p_hashtable, int key, void *data, int *found)
Definition: hashtable.c:56
ABT_pool_config_get
int ABT_pool_config_get(ABT_pool_config config, int key, ABT_pool_config_type *type, void *val)
Retrieve a value from a pool configuration.
Definition: pool_config.c:243
POOL_CONFIG_HTABLE_SIZE
#define POOL_CONFIG_HTABLE_SIZE
Definition: pool_config.c:12
ABT_pool_config_create
int ABT_pool_config_create(ABT_pool_config *config)
Create a new pool configuration.
Definition: pool_config.c:79
ABT_POOL_CONFIG_INT
@ ABT_POOL_CONFIG_INT
Definition: abt.h:1518
abti.h
ABTU_hashtable_delete
void ABTU_hashtable_delete(ABTU_hashtable *p_hashtable, int key, int *deleted)
Definition: hashtable.c:144
ABT_POOL_CONFIG_DOUBLE
@ ABT_POOL_CONFIG_DOUBLE
Definition: abt.h:1520
ABT_POOL_CONFIG_NULL
#define ABT_POOL_CONFIG_NULL
Definition: abt.h:1103
ABT_pool_config
struct ABT_pool_config_opaque * ABT_pool_config
Pool configuration handle type.
Definition: abt.h:885
pool_config_element::v_int
int v_int
Definition: pool_config.c:16
pool_config_create_element_double
static void pool_config_create_element_double(pool_config_element *p_elem, double val)
Definition: pool_config.c:297
ABT_pool_config_automatic
const ABT_pool_config_var ABT_pool_config_automatic
Predefined ABT_pool_config_var to configure whether the pool is freed automatically or not.
Definition: pool_config.c:39
ABTU_calloc
static ABTU_ret_err int ABTU_calloc(size_t num, size_t size, void **p_ptr)
Definition: abtu.h:244
ABT_SUCCESS
#define ABT_SUCCESS
Error code: the routine returns successfully.
Definition: abt.h:92
ABT_pool_config_set
int ABT_pool_config_set(ABT_pool_config config, int key, ABT_pool_config_type type, const void *val)
Register a value to a pool configuration.
Definition: pool_config.c:183
ABTU_ret_err
#define ABTU_ret_err
Definition: abtu.h:155
ABT_pool_config_free
int ABT_pool_config_free(ABT_pool_config *config)
Free a pool configuration.
Definition: pool_config.c:123
pool_config_create_element_ptr
static void pool_config_create_element_ptr(pool_config_element *p_elem, void *ptr)
Definition: pool_config.c:305
ABT_POOL_CONFIG_PTR
@ ABT_POOL_CONFIG_PTR
Definition: abt.h:1522
pool_config_create_element_typed
static ABTU_ret_err int pool_config_create_element_typed(pool_config_element *p_elem, ABT_pool_config_type type, const void *p_val)
Definition: pool_config.c:314
pool_config_element::type
ABT_pool_config_type type
Definition: pool_config.c:14
ABT_ERR_INV_ARG
#define ABT_ERR_INV_ARG
Error code: invalid user argument.
Definition: abt.h:260
ABTU_free
static void ABTU_free(void *ptr)
Definition: abtu.h:228
ABT_pool_config_var::key
int key
Definition: abt.h:1534
pool_config_element::v_ptr
void * v_ptr
Definition: pool_config.c:18
ABTU_hashtable_free
void ABTU_hashtable_free(ABTU_hashtable *p_hashtable)
Definition: hashtable.c:42
ABTU_hashtable_set
ABTU_ret_err int ABTU_hashtable_set(ABTU_hashtable *p_hashtable, int key, const void *data, int *overwritten)
Definition: hashtable.c:91
ABT_pool_config_var
A struct that sets and gets a pool configuration.
Definition: abt.h:1529
pool_config_read_element
static void pool_config_read_element(const pool_config_element *p_elem, void *ptr)
Definition: pool_config.c:336
pool_config_element::v_double
double v_double
Definition: pool_config.c:17