about summary refs log tree commit diff
path: root/src/libnm-std-aux/nm-std-utils.h
blob: 04c61c30f50e7191508b2d43b773ba5d3f84e94b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/* SPDX-License-Identifier: LGPL-2.1-or-later */

#ifndef __NM_STD_UTILS_H__
#define __NM_STD_UTILS_H__

#include <stdbool.h>

#include "nm-std-aux.h"

/*****************************************************************************/

/* nm_utils_get_next_realloc_size() is used to grow buffers exponentially, when
 * the final size is unknown. As such, it has borders for which it allocates
 * certain buffer sizes.
 *
 * The use of these defines is to get favorable allocation sequences.
 * For example, NM_STR_BUF_INIT() asks for an initial allocation size. Note that
 * it reserves the exactly requested amount, under the assumption that the
 * user may know how many bytes will be required. However, often the caller
 * doesn't know in advance, and NMStrBuf grows exponentially by calling
 * nm_utils_get_next_realloc_size().
 * Imagine you call NM_STR_BUF_INIT() with an initial buffer size 100, and you
 * add one character at a time. Then the first reallocation will increase the
 * buffer size only from 100 to 104.
 * If you however start with an initial buffer size of 104, then the next reallocation
 * via nm_utils_get_next_realloc_size() gives you 232, and so on. By using
 * these sizes, it results in one less allocation, if you anyway don't know the
 * exact size in advance. */
#define NM_UTILS_GET_NEXT_REALLOC_SIZE_32   ((size_t) 32)
#define NM_UTILS_GET_NEXT_REALLOC_SIZE_40   ((size_t) 40)
#define NM_UTILS_GET_NEXT_REALLOC_SIZE_104  ((size_t) 104)
#define NM_UTILS_GET_NEXT_REALLOC_SIZE_232  ((size_t) 232)
#define NM_UTILS_GET_NEXT_REALLOC_SIZE_488  ((size_t) 488)
#define NM_UTILS_GET_NEXT_REALLOC_SIZE_1000 ((size_t) 1000)

size_t nm_utils_get_next_realloc_size(bool true_realloc, size_t requested);

const char *_nm_strerror_r(int errsv, char *buf, size_t buf_size);

bool nm_utils_set_effective_user(const char *user, char *errbuf, size_t errbuf_size);

bool nm_utils_read_file_to_stdout(const char *filename, char *errbuf, size_t errbuf_len);

#endif /* __NM_STD_UTILS_H__ */