Debian Codegraph @

guile/src/utils.h

raw
1 /* GnuTLS --- Guile bindings for GnuTLS.
2 Copyright (C) 2007-2025 Free Software Foundation, Inc.
3
4 This file is part of Guile-GnuTLS.
5
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
10
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with this library; if not, see <https://www.gnu.org/licenses/>. */
18
19 #ifndef GUILE_GNUTLS_UTILS_H
20 # define GUILE_GNUTLS_UTILS_H
21
22 /* Common utilities. */
23
24 # include <libguile.h>
25
26
27 /* Compiler twiddling. */
28
29 # ifdef __GNUC__
30 # define EXPECT __builtin_expect
31 # define NO_RETURN __attribute__ ((__noreturn__))
32 # else
33 # define EXPECT(_expr, _value) (_expr)
34 # define NO_RETURN
35 # endif
36
37 # define EXPECT_TRUE(_expr) EXPECT ((_expr), 1)
38 # define EXPECT_FALSE(_expr) EXPECT ((_expr), 0)
39
40
41 /* Arrays as byte vectors. */
42
43 extern const char scm_gnutls_array_error_message[];
44
45 /* Initialize C_HANDLE and C_LEN and return the contiguous C array
46 corresponding to ARRAY. */
47 static inline const char *
48 scm_gnutls_get_array (SCM array, scm_t_array_handle *c_handle,
49 size_t *c_len, const char *func_name)
50 {
51 const char *c_array = NULL;
52 const scm_t_array_dim *c_dims;
53
54 scm_array_get_handle (array, c_handle);
55 c_dims = scm_array_handle_dims (c_handle);
56 if ((scm_array_handle_rank (c_handle) != 1) || (c_dims->inc != 1))
57 {
58 scm_array_handle_release (c_handle);
59 scm_misc_error (func_name, scm_gnutls_array_error_message,
60 scm_list_1 (array));
61 }
62 else
63 {
64 size_t c_elem_size;
65
66 c_elem_size = scm_array_handle_uniform_element_size (c_handle);
67 *c_len = c_elem_size * (c_dims->ubnd - c_dims->lbnd + 1);
68
69 c_array = (char *) scm_array_handle_uniform_elements (c_handle);
70 }
71
72 return (c_array);
73 }
74
75 /* Initialize C_HANDLE and C_LEN and return the contiguous C array
76 corresponding to ARRAY. The returned array can be written to. */
77 static inline char *
78 scm_gnutls_get_writable_array (SCM array, scm_t_array_handle *c_handle,
79 size_t *c_len, const char *func_name)
80 {
81 char *c_array = NULL;
82 const scm_t_array_dim *c_dims;
83
84 scm_array_get_handle (array, c_handle);
85 c_dims = scm_array_handle_dims (c_handle);
86 if ((scm_array_handle_rank (c_handle) != 1) || (c_dims->inc != 1))
87 {
88 scm_array_handle_release (c_handle);
89 scm_misc_error (func_name, scm_gnutls_array_error_message,
90 scm_list_1 (array));
91 }
92 else
93 {
94 size_t c_elem_size;
95
96 c_elem_size = scm_array_handle_uniform_element_size (c_handle);
97 *c_len = c_elem_size * (c_dims->ubnd - c_dims->lbnd + 1);
98
99 c_array =
100 (char *) scm_array_handle_uniform_writable_elements (c_handle);
101 }
102
103 return (c_array);
104 }
105
106 # define scm_gnutls_release_array scm_array_handle_release
107
108 static inline void
109 do_scm_gnutls_release_array (void *arg)
110 {
111 /* Make the function pointer type compatible for unwind_handler */
112 scm_gnutls_release_array (arg);
113 }
114
115 /* Release an array handle at the end of a dynwind section. */
116 static inline void
117 scm_dynwind_release_handle (scm_t_array_handle *handle)
118 {
119 scm_dynwind_unwind_handler (do_scm_gnutls_release_array, handle,
120 SCM_F_WIND_EXPLICITLY);
121 }
122
123
124
125 /* Type conversion. */
126
127 /* Return a list corresponding to the key usage values ORed in C_USAGE. */
128 SCM_API SCM scm_from_gnutls_key_usage_flags (unsigned int c_usage);
129
130 #endif
131
132 /* arch-tag: a33400bc-b5e3-429e-80e0-6ff14cab79e7
133 */