Welcome to mirror list, hosted at ThFree Co, Russian Federation.

RingBuffer.h « util « include « audaspace « extern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 67bd1cc86409c54649b4b1a039e822555be11ad0 (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
/*******************************************************************************
 * Copyright 2009-2021 Jörg Müller
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/

#pragma once

/**
 * @file RingBuffer.h
 * @ingroup util
 * The RingBuffer class.
 */

#include "Audaspace.h"
#include "Buffer.h"

#include <cstddef>

AUD_NAMESPACE_BEGIN

/**
 * This class is a simple ring buffer in RAM which is 32 Byte aligned and provides
 * functionality for concurrent reading and writting without locks.
 */
class AUD_API RingBuffer
{
private:
	/// The buffer storing the actual data.
	Buffer m_buffer;

	/// The reading pointer.
	volatile size_t m_read;

	/// The writing pointer.
	volatile size_t m_write;

	// delete copy constructor and operator=
	RingBuffer(const RingBuffer&) = delete;
	RingBuffer& operator=(const RingBuffer&) = delete;

public:
	/**
	 * Creates a new ring buffer.
	 * \param size The size of the buffer in bytes.
	 */
	RingBuffer(int size = 0);

	/**
	 * Returns the pointer to the ring buffer in memory.
	 */
	sample_t* getBuffer() const;

	/**
	 * Returns the size of the ring buffer in bytes.
	 */
	int getSize() const;

	size_t getReadSize() const;

	size_t getWriteSize() const;

	size_t read(data_t* target, size_t size);

	size_t write(data_t* source, size_t size);

	/**
	 * Resets the ring buffer to a state where nothing has been written or read.
	 */
	void reset();

	/**
	 * Resizes the ring buffer.
	 * \param size The new size of the ring buffer, measured in bytes.
	 */
	void resize(int size);

	/**
	 * Makes sure the ring buffer has a minimum size.
	 * If size is >= current size, nothing will happen.
	 * Otherwise the ring buffer is resized with keep as parameter.
	 * \param size The new minimum size of the ring buffer, measured in bytes.
	 */
	void assureSize(int size);
};

AUD_NAMESPACE_END