1# Varchunk
2
3## Ringbuffer optimized for realtime event handling
4
5### Properties
6
7* Is realtime-safe
8* Is lock-free
9* Supports variably sized chunks
10* Supports contiguous memory chunks
11* Supports zero copy operation
12* Uses a simplistic API
13
14### Build Status
15
16[![build status](https://gitlab.com/OpenMusicKontrollers/varchunk/badges/master/build.svg)](https://gitlab.com/OpenMusicKontrollers/varchunk/commits/master)
17
18### Build / test
19
20	git clone https://git.open-music-kontrollers.ch/lad/varchunk
21	cd varchunk
22	meson build
23	cd build
24	ninja -j4
25	ninja test
26
27### Usage
28
29	#include <pthread.h>
30	#include <varchunk.h>
31
32	static void *
33	producer_main(void *arg)
34	{
35		varchunk_t *varchunk = arg;
36		void *ptr;
37		const size_t towrite = sizeof(uint32_t);
38		uint32_t counter = 0;
39
40		while(counter <= 1000000)
41		{
42			if( (ptr = varchunk_write_request(varchunk, towrite)) )
43			{
44				// write 'towrite' bytes to 'ptr'
45				*(uint32_t *)ptr = counter++;
46				varchunk_write_advance(varchunk, towrite);
47			}
48		}
49
50		return NULL;
51	}
52
53	static void *
54	consumer_main(void *arg)
55	{
56		varchunk_t *varchunk = arg;
57		const void *ptr;
58		size_t toread;
59
60		while(1)
61		{
62			if( (ptr = varchunk_read_request(varchunk, &toread)) )
63			{
64				// read 'toread' bytes from 'ptr'
65				if(*(uint32_t *)ptr >= 1000000)
66					break;
67				varchunk_read_advance(varchunk);
68			}
69		}
70
71		return NULL;
72	}
73
74	int
75	main(int argc, char **argv)
76	{
77		if(!varchunk_is_lock_free())
78			return -1;
79
80		pthread_t producer;
81		pthread_t consumer;
82		varchunk_t *varchunk = varchunk_new(8192, true);
83		if(!varchunk)
84			return -1;
85
86		pthread_create(&consumer, NULL, consumer_main, varchunk);
87		pthread_create(&producer, NULL, producer_main, varchunk);
88
89		pthread_join(producer, NULL);
90		pthread_join(consumer, NULL);
91
92		varchunk_free(varchunk);
93
94		return 0;
95	}
96
97### License
98
99Copyright (c) 2015-2017 Hanspeter Portner (dev@open-music-kontrollers.ch)
100
101This is free software: you can redistribute it and/or modify
102it under the terms of the Artistic License 2.0 as published by
103The Perl Foundation.
104
105This source is distributed in the hope that it will be useful,
106but WITHOUT ANY WARRANTY; without even the implied warranty of
107MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
108Artistic License 2.0 for more details.
109
110You should have received a copy of the Artistic License 2.0
111along the source as a COPYING file. If not, obtain it from
112<http://www.perlfoundation.org/artistic_license_2_0>.
113