summaryrefslogtreecommitdiff
path: root/guile/src/utils.h
blob: 8a30ff59877ea955eba901f0a5f8998a7fa31bb0 (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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/* GNUTLS --- Guile bindings for GnuTLS.
   Copyright (C) 2007  Free Software Foundation

   GNUTLS is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   GNUTLS is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with GNUTLS; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA  */

#ifndef GUILE_GNUTLS_UTILS_H
#define GUILE_GNUTLS_UTILS_H

/* Common utilities.  */

#include "config.h"
#include <libguile.h>


/* Compiler twiddling.  */

#ifdef __GNUC__
# define EXPECT    __builtin_expect
# define NO_RETURN __attribute__ ((__noreturn__))
#else
# define EXPECT(_expr, _value) (_expr)
# define NO_RETURN
#endif

#define EXPECT_TRUE(_expr)  EXPECT ((_expr), 1)
#define EXPECT_FALSE(_expr) EXPECT ((_expr), 0)


/* Arrays as byte vectors.  */

extern const char scm_gnutls_array_error_message[];

/* Initialize C_HANDLE and C_LEN and return the contiguous C array
   corresponding to ARRAY.  */
static inline const char *
scm_gnutls_get_array (SCM array, scm_t_array_handle *c_handle, size_t *c_len,
		      const char *func_name)
{
  const char *c_array = NULL;
  const scm_t_array_dim *c_dims;

  scm_array_get_handle (array, c_handle);
  c_dims = scm_array_handle_dims (c_handle);
  if ((scm_array_handle_rank (c_handle) != 1) || (c_dims->inc != 1))
    {
      scm_array_handle_release (c_handle);
      scm_misc_error (func_name, scm_gnutls_array_error_message,
		      scm_list_1 (array));
    }
  else
    {
      size_t c_elem_size;

      c_elem_size = scm_array_handle_uniform_element_size (c_handle);
      *c_len = c_elem_size * (c_dims->ubnd - c_dims->lbnd + 1);

      c_array = (char *) scm_array_handle_uniform_elements (c_handle);
    }

  return (c_array);
}

/* Initialize C_HANDLE and C_LEN and return the contiguous C array
   corresponding to ARRAY.  The returned array can be written to.  */
static inline char *
scm_gnutls_get_writable_array (SCM array, scm_t_array_handle *c_handle,
			       size_t *c_len,
			       const char *func_name)
{
  char *c_array = NULL;
  const scm_t_array_dim *c_dims;

  scm_array_get_handle (array, c_handle);
  c_dims = scm_array_handle_dims (c_handle);
  if ((scm_array_handle_rank (c_handle) != 1) || (c_dims->inc != 1))
    {
      scm_array_handle_release (c_handle);
      scm_misc_error (func_name, scm_gnutls_array_error_message,
		      scm_list_1 (array));
    }
  else
    {
      size_t c_elem_size;

      c_elem_size = scm_array_handle_uniform_element_size (c_handle);
      *c_len = c_elem_size * (c_dims->ubnd - c_dims->lbnd + 1);

      c_array = (char *) scm_array_handle_uniform_writable_elements (c_handle);
    }

  return (c_array);
}

#define scm_gnutls_release_array  scm_array_handle_release



/* Type conversion.  */

/* Return a list corresponding to the key usage values ORed in C_USAGE.  */
SCM_API SCM scm_from_gnutls_key_usage_flags (unsigned int c_usage);

#endif

/* arch-tag: a33400bc-b5e3-429e-80e0-6ff14cab79e7
 */