Skip to content

Commit f0c207a

Browse files
committed
use umf_result in ctl
fixes: #1365
1 parent b1ca59d commit f0c207a

File tree

10 files changed

+175
-195
lines changed

10 files changed

+175
-195
lines changed

src/ctl/ctl.c

Lines changed: 58 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -247,50 +247,49 @@ static void ctl_query_cleanup_real_args(const umf_ctl_node_t *n, void *real_arg,
247247
/*
248248
* ctl_exec_query_read -- (internal) calls the read callback of a node
249249
*/
250-
static int ctl_exec_query_read(void *ctx, const umf_ctl_node_t *n,
251-
umf_ctl_query_source_t source, void *arg,
252-
size_t size, umf_ctl_index_utlist_t *indexes,
253-
const char *extra_name,
254-
umf_ctl_query_type_t query_type) {
250+
static umf_result_t ctl_exec_query_read(void *ctx, const umf_ctl_node_t *n,
251+
umf_ctl_query_source_t source,
252+
void *arg, size_t size,
253+
umf_ctl_index_utlist_t *indexes,
254+
const char *extra_name,
255+
umf_ctl_query_type_t query_type) {
255256
(void)query_type;
256257
assert(n != NULL);
257258
assert(n->cb[CTL_QUERY_READ] != NULL);
258259
assert(MAX_CTL_QUERY_TYPE != query_type);
259260

260261
if (arg == NULL) {
261-
errno = EINVAL;
262-
return -1;
262+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
263263
}
264-
265264
return n->cb[CTL_QUERY_READ](ctx, source, arg, size, indexes, extra_name,
266265
MAX_CTL_QUERY_TYPE);
267266
}
268267

269268
/*
270269
* ctl_exec_query_write -- (internal) calls the write callback of a node
271270
*/
272-
static int ctl_exec_query_write(void *ctx, const umf_ctl_node_t *n,
273-
umf_ctl_query_source_t source, void *arg,
274-
size_t size, umf_ctl_index_utlist_t *indexes,
275-
const char *extra_name,
276-
umf_ctl_query_type_t query_type) {
271+
static umf_result_t ctl_exec_query_write(void *ctx, const umf_ctl_node_t *n,
272+
umf_ctl_query_source_t source,
273+
void *arg, size_t size,
274+
umf_ctl_index_utlist_t *indexes,
275+
const char *extra_name,
276+
umf_ctl_query_type_t query_type) {
277277
(void)query_type;
278278
assert(n != NULL);
279279
assert(n->cb[CTL_QUERY_WRITE] != NULL);
280280
assert(MAX_CTL_QUERY_TYPE != query_type);
281281

282282
if (arg == NULL) {
283-
errno = EINVAL;
284-
return -1;
283+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
285284
}
286285

287286
void *real_arg = ctl_query_get_real_args(n, arg, source);
288287
if (real_arg == NULL) {
289-
return -1;
288+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
290289
}
291290

292-
int ret = n->cb[CTL_QUERY_WRITE](ctx, source, real_arg, size, indexes,
293-
extra_name, MAX_CTL_QUERY_TYPE);
291+
umf_result_t ret = n->cb[CTL_QUERY_WRITE](
292+
ctx, source, real_arg, size, indexes, extra_name, MAX_CTL_QUERY_TYPE);
294293
ctl_query_cleanup_real_args(n, real_arg, source);
295294

296295
return ret;
@@ -299,11 +298,12 @@ static int ctl_exec_query_write(void *ctx, const umf_ctl_node_t *n,
299298
/*
300299
* ctl_exec_query_runnable -- (internal) calls the run callback of a node
301300
*/
302-
static int ctl_exec_query_runnable(void *ctx, const umf_ctl_node_t *n,
303-
umf_ctl_query_source_t source, void *arg,
304-
size_t size, umf_ctl_index_utlist_t *indexes,
305-
const char *extra_name,
306-
umf_ctl_query_type_t query_type) {
301+
static umf_result_t ctl_exec_query_runnable(void *ctx, const umf_ctl_node_t *n,
302+
umf_ctl_query_source_t source,
303+
void *arg, size_t size,
304+
umf_ctl_index_utlist_t *indexes,
305+
const char *extra_name,
306+
umf_ctl_query_type_t query_type) {
307307
(void)query_type;
308308
assert(n != NULL);
309309
assert(n->cb[CTL_QUERY_RUNNABLE] != NULL);
@@ -312,24 +312,25 @@ static int ctl_exec_query_runnable(void *ctx, const umf_ctl_node_t *n,
312312
extra_name, MAX_CTL_QUERY_TYPE);
313313
}
314314

315-
static int ctl_exec_query_subtree(void *ctx, const umf_ctl_node_t *n,
316-
umf_ctl_query_source_t source, void *arg,
317-
size_t size, umf_ctl_index_utlist_t *indexes,
318-
const char *extra_name,
319-
umf_ctl_query_type_t query_type) {
315+
static umf_result_t ctl_exec_query_subtree(void *ctx, const umf_ctl_node_t *n,
316+
umf_ctl_query_source_t source,
317+
void *arg, size_t size,
318+
umf_ctl_index_utlist_t *indexes,
319+
const char *extra_name,
320+
umf_ctl_query_type_t query_type) {
320321
assert(n != NULL);
321322
assert(n->cb[CTL_QUERY_SUBTREE] != NULL);
322323
assert(MAX_CTL_QUERY_TYPE != query_type);
323324
return n->cb[CTL_QUERY_SUBTREE](ctx, source, arg, size, indexes, extra_name,
324325
query_type);
325326
}
326327

327-
typedef int (*umf_ctl_exec_query_t)(void *ctx, const umf_ctl_node_t *n,
328-
umf_ctl_query_source_t source, void *arg,
329-
size_t size,
330-
umf_ctl_index_utlist_t *indexes,
331-
const char *extra_name,
332-
umf_ctl_query_type_t query_type);
328+
typedef umf_result_t (*umf_ctl_exec_query_t)(void *ctx, const umf_ctl_node_t *n,
329+
umf_ctl_query_source_t source,
330+
void *arg, size_t size,
331+
umf_ctl_index_utlist_t *indexes,
332+
const char *extra_name,
333+
umf_ctl_query_type_t query_type);
333334

334335
static umf_ctl_exec_query_t ctl_exec_query[MAX_CTL_QUERY_TYPE] = {
335336
ctl_exec_query_read,
@@ -342,12 +343,11 @@ static umf_ctl_exec_query_t ctl_exec_query[MAX_CTL_QUERY_TYPE] = {
342343
* ctl_query -- (internal) parses the name and calls the appropriate methods
343344
* from the ctl tree
344345
*/
345-
int ctl_query(struct ctl *ctl, void *ctx, umf_ctl_query_source_t source,
346-
const char *name, umf_ctl_query_type_t type, void *arg,
347-
size_t size) {
346+
umf_result_t ctl_query(struct ctl *ctl, void *ctx,
347+
umf_ctl_query_source_t source, const char *name,
348+
umf_ctl_query_type_t type, void *arg, size_t size) {
348349
if (name == NULL) {
349-
errno = EINVAL;
350-
return -1;
350+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
351351
}
352352

353353
/*
@@ -358,10 +358,10 @@ int ctl_query(struct ctl *ctl, void *ctx, umf_ctl_query_source_t source,
358358
umf_ctl_index_utlist_t *indexes = NULL;
359359
indexes = Zalloc(sizeof(*indexes));
360360
if (!indexes) {
361-
return -1;
361+
return UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY;
362362
}
363363

364-
int ret = -1;
364+
umf_result_t ret = UMF_RESULT_ERROR_UNKNOWN;
365365
size_t name_offset = 0;
366366

367367
const umf_ctl_node_t *n =
@@ -377,7 +377,7 @@ int ctl_query(struct ctl *ctl, void *ctx, umf_ctl_query_source_t source,
377377
if (n == NULL ||
378378
(n->type != CTL_NODE_LEAF && n->type != CTL_NODE_SUBTREE) ||
379379
n->cb[n->type == CTL_NODE_SUBTREE ? CTL_QUERY_SUBTREE : type] == NULL) {
380-
errno = EINVAL;
380+
ret = UMF_RESULT_ERROR_INVALID_ARGUMENT;
381381
goto out;
382382
}
383383

@@ -436,24 +436,24 @@ static int ctl_parse_query(char *qbuf, char **name, char **value) {
436436
/*
437437
* ctl_load_config -- executes the entire query collection from a provider
438438
*/
439-
static int ctl_load_config(struct ctl *ctl, void *ctx, char *buf) {
440-
int r = 0;
439+
static umf_result_t ctl_load_config(struct ctl *ctl, void *ctx, char *buf) {
440+
umf_result_t ret = UMF_RESULT_SUCCESS;
441441
char *sptr = NULL; /* for internal use of strtok */
442442
char *name;
443443
char *value;
444444
char *qbuf = strtok_r(buf, CTL_STRING_QUERY_SEPARATOR, &sptr);
445445

446446
while (qbuf != NULL) {
447-
r = ctl_parse_query(qbuf, &name, &value);
448-
if (r != 0) {
449-
return -1;
447+
int parse_res = ctl_parse_query(qbuf, &name, &value);
448+
if (parse_res != 0) {
449+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
450450
}
451451

452-
r = ctl_query(ctl, ctx, CTL_QUERY_CONFIG_INPUT, name, CTL_QUERY_WRITE,
453-
value, 0);
452+
ret = ctl_query(ctl, ctx, CTL_QUERY_CONFIG_INPUT, name, CTL_QUERY_WRITE,
453+
value, 0);
454454

455-
if (r < 0 && ctx != NULL) {
456-
return -1;
455+
if (ret != UMF_RESULT_SUCCESS && ctx != NULL) {
456+
return ret;
457457
}
458458

459459
qbuf = strtok_r(NULL, CTL_STRING_QUERY_SEPARATOR, &sptr);
@@ -465,14 +465,14 @@ static int ctl_load_config(struct ctl *ctl, void *ctx, char *buf) {
465465
/*
466466
* ctl_load_config_from_string -- loads obj configuration from string
467467
*/
468-
int ctl_load_config_from_string(struct ctl *ctl, void *ctx,
469-
const char *cfg_string) {
468+
umf_result_t ctl_load_config_from_string(struct ctl *ctl, void *ctx,
469+
const char *cfg_string) {
470470
char *buf = Strdup(cfg_string);
471471
if (buf == NULL) {
472-
return -1;
472+
return UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY;
473473
}
474474

475-
int ret = ctl_load_config(ctl, ctx, buf);
475+
umf_result_t ret = ctl_load_config(ctl, ctx, buf);
476476

477477
umf_ba_global_free(buf);
478478
return ret;
@@ -485,9 +485,9 @@ int ctl_load_config_from_string(struct ctl *ctl, void *ctx,
485485
* the size of the file, reads its content and sanitizes it for ctl_load_config.
486486
*/
487487
#ifndef _WIN32 // TODO: implement for Windows
488-
int ctl_load_config_from_file(struct ctl *ctl, void *ctx,
489-
const char *cfg_file) {
490-
int ret = -1;
488+
umf_result_t ctl_load_config_from_file(struct ctl *ctl, void *ctx,
489+
const char *cfg_file) {
490+
umf_result_t ret = UMF_RESULT_ERROR_UNKNOWN;
491491
long fsize = 0;
492492
char *buf = NULL;
493493

@@ -608,7 +608,6 @@ int ctl_arg_integer(const void *arg, void *dest, size_t dest_size) {
608608
*(uint8_t *)dest = (uint8_t)val;
609609
break;
610610
default:
611-
errno = EINVAL;
612611
return -1;
613612
}
614613

src/ctl/ctl.h

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,11 @@ typedef enum ctl_query_source {
4545
MAX_CTL_QUERY_SOURCE
4646
} umf_ctl_query_source_t;
4747

48-
typedef int (*node_callback)(void *ctx, umf_ctl_query_source_t type, void *arg,
49-
size_t size, umf_ctl_index_utlist_t *indexes,
50-
const char *extra_name,
51-
umf_ctl_query_type_t query_type);
48+
typedef umf_result_t (*node_callback)(void *ctx, umf_ctl_query_source_t type,
49+
void *arg, size_t size,
50+
umf_ctl_index_utlist_t *indexes,
51+
const char *extra_name,
52+
umf_ctl_query_type_t query_type);
5253

5354
enum ctl_node_type {
5455
CTL_NODE_UNKNOWN,
@@ -115,9 +116,10 @@ struct ctl {
115116

116117
void initialize_global_ctl(void);
117118

118-
int ctl_load_config_from_string(struct ctl *ctl, void *ctx,
119-
const char *cfg_string);
120-
int ctl_load_config_from_file(struct ctl *ctl, void *ctx, const char *cfg_file);
119+
umf_result_t ctl_load_config_from_string(struct ctl *ctl, void *ctx,
120+
const char *cfg_string);
121+
umf_result_t ctl_load_config_from_file(struct ctl *ctl, void *ctx,
122+
const char *cfg_file);
121123

122124
/* Use through CTL_REGISTER_MODULE, never directly */
123125
void ctl_register_module_node(struct ctl *c, const char *name,
@@ -149,9 +151,9 @@ int ctl_arg_string(const void *arg, void *dest, size_t dest_size);
149151

150152
#define CTL_NODE(name, ...) ctl_node_##__VA_ARGS__##_##name
151153

152-
int ctl_query(struct ctl *ctl, void *ctx, umf_ctl_query_source_t source,
153-
const char *name, umf_ctl_query_type_t type, void *arg,
154-
size_t size);
154+
umf_result_t ctl_query(struct ctl *ctl, void *ctx,
155+
umf_ctl_query_source_t source, const char *name,
156+
umf_ctl_query_type_t type, void *arg, size_t size);
155157

156158
/* Declaration of a new child node */
157159
#define CTL_CHILD(name, ...) \

src/libumf.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,7 @@ umf_result_t umfCtlSet(const char *name, void *ctx, void *arg, size_t size) {
115115
}
116116

117117
return ctl_query(NULL, ctx, CTL_QUERY_PROGRAMMATIC, name, CTL_QUERY_WRITE,
118-
arg, size)
119-
? UMF_RESULT_ERROR_UNKNOWN
120-
: UMF_RESULT_SUCCESS;
118+
arg, size);
121119
}
122120

123121
umf_result_t umfCtlExec(const char *name, void *ctx, void *arg, size_t size) {
@@ -133,7 +131,5 @@ umf_result_t umfCtlExec(const char *name, void *ctx, void *arg, size_t size) {
133131
}
134132

135133
return ctl_query(NULL, ctx, CTL_QUERY_PROGRAMMATIC, name,
136-
CTL_QUERY_RUNNABLE, arg, size)
137-
? UMF_RESULT_ERROR_UNKNOWN
138-
: UMF_RESULT_SUCCESS;
134+
CTL_QUERY_RUNNABLE, arg, size);
139135
}

src/memory_pool.c

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -37,32 +37,27 @@ static struct ctl umf_pool_ctl_root;
3737

3838
static void ctl_init(void);
3939

40-
static int CTL_SUBTREE_HANDLER(by_handle_pool)(void *ctx,
41-
umf_ctl_query_source_t source,
42-
void *arg, size_t size,
43-
umf_ctl_index_utlist_t *indexes,
44-
const char *extra_name,
45-
umf_ctl_query_type_t queryType) {
40+
static umf_result_t CTL_SUBTREE_HANDLER(by_handle_pool)(
41+
void *ctx, umf_ctl_query_source_t source, void *arg, size_t size,
42+
umf_ctl_index_utlist_t *indexes, const char *extra_name,
43+
umf_ctl_query_type_t queryType) {
4644
(void)indexes, (void)source;
4745
umf_memory_pool_handle_t hPool = (umf_memory_pool_handle_t)ctx;
48-
int ret = ctl_query(&umf_pool_ctl_root, hPool, source, extra_name,
49-
queryType, arg, size);
50-
if (ret == -1 &&
51-
errno == EINVAL) { // node was not found in pool_ctl_root, try to
52-
// query the specific pool directly
53-
hPool->ops.ext_ctl(hPool->pool_priv, source, extra_name, arg, size,
54-
queryType);
46+
umf_result_t ret = ctl_query(&umf_pool_ctl_root, hPool, source, extra_name,
47+
queryType, arg, size);
48+
if (ret == UMF_RESULT_ERROR_INVALID_ARGUMENT) {
49+
// Node was not found in pool_ctl_root, try to query the specific pool
50+
ret = hPool->ops.ext_ctl(hPool->pool_priv, source, extra_name, arg,
51+
size, queryType);
5552
}
5653

57-
return 0;
54+
return ret;
5855
}
5956

60-
static int CTL_SUBTREE_HANDLER(default)(void *ctx,
61-
umf_ctl_query_source_t source,
62-
void *arg, size_t size,
63-
umf_ctl_index_utlist_t *indexes,
64-
const char *extra_name,
65-
umf_ctl_query_type_t queryType) {
57+
static umf_result_t CTL_SUBTREE_HANDLER(default)(
58+
void *ctx, umf_ctl_query_source_t source, void *arg, size_t size,
59+
umf_ctl_index_utlist_t *indexes, const char *extra_name,
60+
umf_ctl_query_type_t queryType) {
6661
(void)indexes, (void)source, (void)ctx;
6762
utils_init_once(&mem_pool_ctl_initialized, ctl_init);
6863
utils_mutex_lock(&ctl_mtx);
@@ -101,15 +96,13 @@ static int CTL_SUBTREE_HANDLER(default)(void *ctx,
10196

10297
utils_mutex_unlock(&ctl_mtx);
10398

104-
return 0;
99+
return UMF_RESULT_SUCCESS;
105100
}
106101

107-
static int CTL_READ_HANDLER(alloc_count)(void *ctx,
108-
umf_ctl_query_source_t source,
109-
void *arg, size_t size,
110-
umf_ctl_index_utlist_t *indexes,
111-
const char *extra_name,
112-
umf_ctl_query_type_t query_type) {
102+
static umf_result_t CTL_READ_HANDLER(alloc_count)(
103+
void *ctx, umf_ctl_query_source_t source, void *arg, size_t size,
104+
umf_ctl_index_utlist_t *indexes, const char *extra_name,
105+
umf_ctl_query_type_t query_type) {
113106
/* suppress unused-parameter errors */
114107
(void)source, (void)size, (void)indexes, (void)extra_name, (void)query_type;
115108

src/memory_provider.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@
2121
#include "umf/base.h"
2222
#include "utils_assert.h"
2323

24-
static int CTL_SUBTREE_HANDLER(by_handle_provider)(
24+
static umf_result_t CTL_SUBTREE_HANDLER(by_handle_provider)(
2525
void *ctx, umf_ctl_query_source_t source, void *arg, size_t size,
2626
umf_ctl_index_utlist_t *indexes, const char *extra_name,
2727
umf_ctl_query_type_t queryType) {
2828
(void)indexes, (void)source;
2929
umf_memory_provider_handle_t hProvider = (umf_memory_provider_handle_t)ctx;
3030
hProvider->ops.ext_ctl(hProvider->provider_priv, /*unused*/ 0, extra_name,
3131
arg, size, queryType);
32-
return 0;
32+
return UMF_RESULT_SUCCESS;
3333
}
3434

3535
umf_ctl_node_t CTL_NODE(provider)[] = {

0 commit comments

Comments
 (0)