File: common.h

package info (click to toggle)
ruby-libvirt 0.8.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 832 kB
  • sloc: ansic: 8,052; ruby: 2,541; makefile: 6
file content (287 lines) | stat: -rw-r--r-- 14,331 bytes parent folder | download
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#ifndef COMMON_H
#define COMMON_H

/* Macros to ease some of the boilerplate */
VALUE ruby_libvirt_new_class(VALUE klass, void *ptr, VALUE conn,
                             RUBY_DATA_FUNC free_func);

#define RUBY_LIBVIRT_UNUSED(x) UNUSED_ ## x __attribute__((__unused__))

#define ruby_libvirt_get_struct(kind, v)                                \
    do {                                                                \
        vir##kind##Ptr ptr;                                             \
        Data_Get_Struct(v, vir##kind, ptr);                             \
        if (!ptr) {                                                     \
            rb_raise(rb_eArgError, #kind " has been freed");            \
        }                                                               \
        return ptr;                                                     \
    } while (0);

#define ruby_libvirt_free_struct(kind, p)                               \
    do {                                                                \
        int r;                                                          \
        r = vir##kind##Free((vir##kind##Ptr) p);                        \
        if (r < 0) {                                                    \
            rb_raise(rb_eSystemCallError, # kind " free failed");       \
        }                                                               \
    } while(0);

void ruby_libvirt_raise_error_if(const int condition, VALUE error,
                                 const char *method, virConnectPtr conn);

/*
 * Code generating macros.
 *
 * We only generate function bodies, not the whole function
 * declaration.
 */

/* Generate a call to a function FUNC which returns a string. The Ruby
 * function will return the string on success and throw an exception on
 * error. The string returned by FUNC is freed if dealloc is true.
 */
#define ruby_libvirt_generate_call_string(func, conn, dealloc, args...)  \
    do {                                                                 \
        const char *str;                                                 \
        VALUE result;                                                    \
        int exception;                                                   \
                                                                         \
        str = func(args);                                                \
        ruby_libvirt_raise_error_if(str == NULL, e_Error, # func, conn); \
        if (dealloc) {                                                   \
            result = rb_protect(ruby_libvirt_str_new2_wrap, (VALUE)&str, &exception); \
            xfree((void *) str);                                         \
            if (exception) {                                             \
                rb_jump_tag(exception);                                  \
            }                                                            \
        }                                                                \
        else {                                                           \
            result = ruby_libvirt_str_new2_wrap((VALUE)&str);            \
        }                                                                \
        return result;                                                   \
    } while(0)

/* Generate a call to vir##KIND##Free and return Qnil. Set the the embedded
 * vir##KIND##Ptr to NULL. If that pointer is already NULL, do nothing.
 */
#define ruby_libvirt_generate_call_free(kind, s)                        \
    do {                                                                \
        vir##kind##Ptr ptr;                                             \
        Data_Get_Struct(s, vir##kind, ptr);                             \
        if (ptr != NULL) {                                              \
            int r = vir##kind##Free(ptr);                               \
            ruby_libvirt_raise_error_if(r < 0, e_Error, "vir" #kind "Free", ruby_libvirt_connect_get(s)); \
            DATA_PTR(s) = NULL;                                         \
        }                                                               \
        return Qnil;                                                    \
    } while (0)

/* Generate a call to a function FUNC which returns an int error, where -1
 * indicates error and 0 success. The Ruby function will return Qnil on
 * success and throw an exception on error.
 */
#define ruby_libvirt_generate_call_nil(func, conn, args...)               \
    do {                                                                  \
        int _r_##func;                                                    \
        _r_##func = func(args);                                           \
        ruby_libvirt_raise_error_if(_r_##func < 0, e_Error, #func, conn); \
        return Qnil;                                                      \
    } while(0)

/* Generate a call to a function FUNC which returns an int; -1 indicates
 * error, 0 indicates Qfalse, and 1 indicates Qtrue.
 */
#define ruby_libvirt_generate_call_truefalse(func, conn, args...)         \
    do {                                                                  \
        int _r_##func;                                                    \
        _r_##func = func(args);                                           \
        ruby_libvirt_raise_error_if(_r_##func < 0, e_Error, #func, conn); \
        return _r_##func ? Qtrue : Qfalse;                                \
    } while(0)

/* Generate a call to a function FUNC which returns an int error, where -1
 * indicates error and >= 0 success. The Ruby function will return the integer
 * success and throw an exception on error.
 */
#define ruby_libvirt_generate_call_int(func, conn, args...)             \
    do {                                                                \
        int _r_##func;                                                  \
        _r_##func = func(args);                                         \
        ruby_libvirt_raise_error_if(_r_##func < 0, e_RetrieveError, #func, conn); \
        return INT2NUM(_r_##func);                                      \
    } while(0)

#define ruby_libvirt_generate_uuid(func, conn, obj)                     \
    do {                                                                \
        char uuid[VIR_UUID_STRING_BUFLEN];                              \
        int _r_##func;                                                  \
        _r_##func = func(obj, uuid);                                    \
        ruby_libvirt_raise_error_if(_r_##func < 0, e_RetrieveError, #func, conn); \
        return rb_str_new2((char *) uuid);                              \
    } while (0)


#define ruby_libvirt_generate_call_list_all(type, argc, argv, listfunc, object, val, newfunc, freefunc) \
    do {                                                                \
        VALUE flags = RUBY_Qnil;                                        \
        type *list;                                                     \
        int i;                                                          \
        int ret;                                                        \
        VALUE result;                                                   \
        int exception = 0;                                              \
        struct ruby_libvirt_ary_push_arg arg;                           \
                                                                        \
        rb_scan_args(argc, argv, "01", &flags);                         \
        ret = listfunc(object, &list, ruby_libvirt_value_to_uint(flags)); \
        ruby_libvirt_raise_error_if(ret < 0, e_RetrieveError, #listfunc, ruby_libvirt_connect_get(val)); \
        result = rb_protect(ruby_libvirt_ary_new2_wrap, (VALUE)&ret, &exception); \
        if (exception) {                                                \
            goto exception;                                             \
        }                                                               \
        for (i = 0; i < ret; i++) {                                     \
            arg.arr = result;                                           \
            arg.value = newfunc(list[i], val);                          \
            rb_protect(ruby_libvirt_ary_push_wrap, (VALUE)&arg, &exception); \
            if (exception) {                                            \
                goto exception;                                         \
            }                                                           \
        }                                                               \
                                                                        \
        free(list);                                                     \
                                                                        \
        return result;                                                  \
                                                                        \
    exception:                                                          \
        for (i = 0; i < ret; i++) {                                     \
            freefunc(list[i]);                                          \
        }                                                               \
        free(list);                                                     \
        rb_jump_tag(exception);                                         \
                                                                        \
        /* not needed, but here to shut the compiler up */              \
        return Qnil;                                                    \
    } while(0)

int ruby_libvirt_is_symbol_or_proc(VALUE handle);

extern VALUE e_RetrieveError;
extern VALUE e_Error;
extern VALUE e_DefinitionError;
extern VALUE e_NoSupportError;

extern VALUE m_libvirt;

char *ruby_libvirt_get_cstring_or_null(VALUE arg);

VALUE ruby_libvirt_generate_list(int num, char **list);

VALUE ruby_libvirt_get_parameters(VALUE d, unsigned int flags, void *opaque,
                                  unsigned int typesize,
                                  const char *(*nparams_cb)(VALUE d,
                                                            unsigned int flags,
                                                            void *opaque,
                                                            int *nparams),
                                  const char *(*get_cb)(VALUE d,
                                                        unsigned int flags,
                                                        void *voidparams,
                                                        int *nparams,
                                                        void *opaque),
                                  void (*hash_set)(void *voidparams, int i,
                                                   VALUE result));
VALUE ruby_libvirt_get_typed_parameters(VALUE d, unsigned int flags,
                                        void *opaque,
                                        const char *(*nparams_cb)(VALUE d,
                                                                  unsigned int flags,
                                                                  void *opaque,
                                                                  int *nparams),
                                        const char *(*get_cb)(VALUE d,
                                                              unsigned int flags,
                                                              void *params,
                                                              int *nparams,
                                                              void *opaque));
struct ruby_libvirt_typed_param {
    const char *name;
    int type;
};
struct ruby_libvirt_parameter_assign_args {
    struct ruby_libvirt_typed_param *allowed;
    unsigned int num_allowed;

    virTypedParameter *params;
    int i;
};
int ruby_libvirt_typed_parameter_assign(VALUE key, VALUE val, VALUE in);
VALUE ruby_libvirt_set_typed_parameters(VALUE d, VALUE input,
                                        unsigned int flags, void *opaque,
                                        struct ruby_libvirt_typed_param *allowed,
                                        unsigned int num_allowed,
                                        const char *(*set_cb)(VALUE d,
                                                              unsigned int flags,
                                                              virTypedParameterPtr params,
                                                              int nparams,
                                                              void *opaque));

int ruby_libvirt_get_maxcpus(virConnectPtr conn);

void ruby_libvirt_typed_params_to_hash(void *voidparams, int i, VALUE hash);
void ruby_libvirt_assign_hash_and_flags(VALUE in, VALUE *hash, VALUE *flags);

unsigned int ruby_libvirt_value_to_uint(VALUE in);
int ruby_libvirt_value_to_int(VALUE in);
unsigned long ruby_libvirt_value_to_ulong(VALUE in);
unsigned long long ruby_libvirt_value_to_ulonglong(VALUE in);

VALUE ruby_libvirt_ary_new2_wrap(VALUE arg);

struct ruby_libvirt_ary_push_arg {
    VALUE arr;
    VALUE value;
};
VALUE ruby_libvirt_ary_push_wrap(VALUE arg);

struct ruby_libvirt_ary_store_arg {
    VALUE arr;
    long index;
    VALUE elem;
};
VALUE ruby_libvirt_ary_store_wrap(VALUE arg);

VALUE ruby_libvirt_str_new2_wrap(VALUE arg);

struct ruby_libvirt_str_new_arg {
    char *val;
    size_t size;
};
VALUE ruby_libvirt_str_new_wrap(VALUE arg);

struct ruby_libvirt_hash_aset_arg {
    VALUE hash;
    const char *name;
    VALUE val;
};
VALUE ruby_libvirt_hash_aset_wrap(VALUE arg);

struct ruby_libvirt_str_new2_and_ary_store_arg {
    VALUE arr;
    long index;
    char *value;
};
VALUE ruby_libvirt_str_new2_and_ary_store_wrap(VALUE arg);

VALUE ruby_libvirt_new_not_allowed(int argc, VALUE *argv, VALUE obj);

#ifndef RARRAY_LEN
#define RARRAY_LEN(ar) (RARRAY(ar)->len)
#endif

#ifndef RSTRING_PTR
#define RSTRING_PTR(str) (RSTRING(str)->ptr)
#endif

#ifndef RSTRING_LEN
#define RSTRING_LEN(str) (RSTRING(str)->len)
#endif

#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))

#endif