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