aboutsummaryrefslogtreecommitdiff
path: root/jit_util.h
blob: f7848d11203efcf1bce122b85a7b512efd233453 (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
119
120
121
122
123
124
125
126
127
128
/*
 * Copyright (c) 2019 Hanspeter Portner (dev@open-music-kontrollers.ch)
 *
 * This is free software: you can redistribute it and/or modify
 * it under the terms of the Artistic License 2.0 as published by
 * The Perl Foundation.
 *
 * This source 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
 * Artistic License 2.0 for more details.
 *
 * You should have received a copy of the Artistic License 2.0
 * along the source as a COPYING file. If not, obtain it from
 * http://www.perlfoundation.org/artistic_license_2_0.
 */

#ifndef _JIT_UTIL_H
#define _JIT_UTIL_H

#include <stdio.h>
#include <stdarg.h>

#include <jit_api.h>

/****f* jit.lv2/jit_assert
 * SYNOPSIS
 */
static inline void
jit_assert(jit_t *const jit, bool condition)
/*
 * FUNCTION
 *  If condition is false, makes a longjump back to wrapping function start.
 * INPUTS
 *  - jit       Pointer to opaque jit structure
 *  - condition	Condition, whether to longjump or not
 ****
 */
{
	if(!condition && (jit->mask & JIT_MASK_ASSERT) && jit->_assert.func)
	{
		jit->_assert.func(jit->_assert.data);
	}
}

/****f* jit.lv2/jit_vprintf
 * SYNOPSIS
 */
static inline int
jit_vprintf(jit_t *const jit, const char *fmt, va_list ap)
/*
 * FUNCTION
 *  Formatted print to LV2 host log infrastructure.
 * INPUTS
 *  - jit	Pointer to opaque jit structure
 *  - fmt	Format string
 *  - ap	List of argument
 * OUTPUT
 *  Upon successful return, this function returns the number of characters
 *  printed. If an output error is encountered, a negative value is returned.
 ****
 */
{
	char buf [512];

	vsnprintf(buf, sizeof(buf), fmt, ap);

	if( (jit->mask & JIT_MASK_VPRINTF) && jit->_print.func)
	{
		return jit->_print.func(jit->_print.data, buf);
	}

	return -1;
}

static inline int __attribute__((format(printf, 2, 3)))
jit_printf(jit_t *const jit, const char *fmt, ...)
{
	va_list ap;

	va_start(ap, fmt);
	const int ret = jit_vprintf(jit, fmt, ap);
	va_end(ap);

	return ret;
}

static inline void *
jit_work_write_request(jit_t *const jit, size_t len)
{
	if( (jit->mask & JIT_MASK_WORK_WRITE_REQ) && jit->_work_write_request.func)
	{
		return jit->_work_write_request.func(jit->_work_write_request.data, len);
	}

	return NULL;
}

static inline void
jit_work_write_advance(jit_t *const jit, size_t len)
{
	if( (jit->mask & JIT_MASK_WORK_WRITE_ADV) && jit->_work_write_advance.func)
	{
		jit->_work_write_advance.func(jit->_work_write_advance.data, len);
	}
}

static inline void const*
jit_work_read_request(jit_t *const jit, size_t *len)
{
	if( (jit->mask & JIT_MASK_WORK_READ_REQ) && jit->_work_read_request.func)
	{
		return jit->_work_read_request.func(jit->_work_read_request.data, len);
	}

	return NULL;
}

static inline void
jit_work_read_advance(jit_t *const jit)
{
	if( (jit->mask & JIT_MASK_WORK_READ_ADV) && jit->_work_read_advance.func)
	{
		jit->_work_read_advance.func(jit->_work_read_advance.data);
	}
}

#endif // _JIT_UTIL_H