xref: /haiku/src/apps/cortex/addons/common/RawBuffer.h (revision 815212800210a100d07c2c3d8dfea18360426319)
1c284bb0fSMatt Madia /*
2c284bb0fSMatt Madia  * Copyright (c) 1999-2000, Eric Moon.
3c284bb0fSMatt Madia  * All rights reserved.
4c284bb0fSMatt Madia  *
5c284bb0fSMatt Madia  * Redistribution and use in source and binary forms, with or without
6c284bb0fSMatt Madia  * modification, are permitted provided that the following conditions
7c284bb0fSMatt Madia  * are met:
8c284bb0fSMatt Madia  *
9c284bb0fSMatt Madia  * 1. Redistributions of source code must retain the above copyright
10c284bb0fSMatt Madia  *    notice, this list of conditions, and the following disclaimer.
11c284bb0fSMatt Madia  *
12c284bb0fSMatt Madia  * 2. Redistributions in binary form must reproduce the above copyright
13c284bb0fSMatt Madia  *    notice, this list of conditions, and the following disclaimer in the
14c284bb0fSMatt Madia  *    documentation and/or other materials provided with the distribution.
15c284bb0fSMatt Madia  *
16c284bb0fSMatt Madia  * 3. The name of the author may not be used to endorse or promote products
17c284bb0fSMatt Madia  *    derived from this software without specific prior written permission.
18c284bb0fSMatt Madia  *
19c284bb0fSMatt Madia  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
20c284bb0fSMatt Madia  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21c284bb0fSMatt Madia  * OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22c284bb0fSMatt Madia  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
23c284bb0fSMatt Madia  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24c284bb0fSMatt Madia  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25c284bb0fSMatt Madia  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26c284bb0fSMatt Madia  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
27c284bb0fSMatt Madia  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28c284bb0fSMatt Madia  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29c284bb0fSMatt Madia  */
30c284bb0fSMatt Madia 
31c284bb0fSMatt Madia 
32a0795c6fSMarcus Overhagen // RawBuffer.h
33a0795c6fSMarcus Overhagen // eamoon@meadgroup.com
34a0795c6fSMarcus Overhagen //
35a0795c6fSMarcus Overhagen // * PURPOSE
36a0795c6fSMarcus Overhagen // A basic representation of a media buffer.  RawBuffer
37a0795c6fSMarcus Overhagen // instances may either allocate and maintain their own buffer or
38a0795c6fSMarcus Overhagen // represent external data.
39a0795c6fSMarcus Overhagen //
40a0795c6fSMarcus Overhagen // * HISTORY
41a0795c6fSMarcus Overhagen // e.moon 	16jun99
42a0795c6fSMarcus Overhagen //		realtime allocation support
43a0795c6fSMarcus Overhagen // e.moon		31mar99
44a0795c6fSMarcus Overhagen //		begun
45a0795c6fSMarcus Overhagen //
46a0795c6fSMarcus Overhagen 
47a0795c6fSMarcus Overhagen #ifndef __RawBuffer_H__
48a0795c6fSMarcus Overhagen #define __RawBuffer_H__
49a0795c6fSMarcus Overhagen 
50a0795c6fSMarcus Overhagen #include <SupportDefs.h>
51a0795c6fSMarcus Overhagen 
52*81521280SX512 struct rtm_pool;
53a0795c6fSMarcus Overhagen 
54a0795c6fSMarcus Overhagen class RawBuffer {
55a0795c6fSMarcus Overhagen public:						// ctor/dtor/accessors
56a0795c6fSMarcus Overhagen 	virtual ~RawBuffer();
57a0795c6fSMarcus Overhagen 
58a0795c6fSMarcus Overhagen 	// allocate buffer (if frames > 0)
59a0795c6fSMarcus Overhagen 	// [16jun99] if pFromPool is nonzero, uses realtime allocator
60a0795c6fSMarcus Overhagen 	//           w/ the provided pool; otherwise uses standard
61a0795c6fSMarcus Overhagen 	//           new[] allocator.
62a0795c6fSMarcus Overhagen 	RawBuffer(
63a0795c6fSMarcus Overhagen 		uint32 frameSize=1,
64a0795c6fSMarcus Overhagen 		uint32 frames=0,
65a0795c6fSMarcus Overhagen 		bool circular=true,
66a0795c6fSMarcus Overhagen 		rtm_pool* pFromPool=0);
67a0795c6fSMarcus Overhagen 
68a0795c6fSMarcus Overhagen 	// point to given data (does NOT take responsibility for
69a0795c6fSMarcus Overhagen 	// deleting it; use adopt() for that.)
70a0795c6fSMarcus Overhagen 	RawBuffer(
71a0795c6fSMarcus Overhagen 		void* pData,
72a0795c6fSMarcus Overhagen 		uint32 frameSize,
73a0795c6fSMarcus Overhagen 		uint32 frames,
74a0795c6fSMarcus Overhagen 		bool bCircular=true,
75a0795c6fSMarcus Overhagen 		rtm_pool* pFromPool=0);
76a0795c6fSMarcus Overhagen 
77a0795c6fSMarcus Overhagen 	// generate reference to the given target buffer
78a0795c6fSMarcus Overhagen 	RawBuffer(const RawBuffer& clone);
79a0795c6fSMarcus Overhagen 	RawBuffer& operator=(const RawBuffer& clone);
80a0795c6fSMarcus Overhagen 
81a0795c6fSMarcus Overhagen 	// returns pointer to start of buffer
82a0795c6fSMarcus Overhagen 	char* data() const;
83a0795c6fSMarcus Overhagen 	// returns pointer to given frame
84a0795c6fSMarcus Overhagen 	char* frame(uint32 frame) const;
85a0795c6fSMarcus Overhagen 
86a0795c6fSMarcus Overhagen 	uint32 frameSize() const;
87a0795c6fSMarcus Overhagen 	uint32 frames() const;
88a0795c6fSMarcus Overhagen 	uint32 size() const;
89a0795c6fSMarcus Overhagen 
90a0795c6fSMarcus Overhagen 	bool isCircular() const;
91a0795c6fSMarcus Overhagen 	bool ownsBuffer() const;
92a0795c6fSMarcus Overhagen 
93a0795c6fSMarcus Overhagen 	rtm_pool* pool() const;
94a0795c6fSMarcus Overhagen 
95a0795c6fSMarcus Overhagen 	// resize buffer, re-allocating if necessary to contain
96a0795c6fSMarcus Overhagen 	// designated number of frames
97a0795c6fSMarcus Overhagen 	// Does not preserve buffer contents.
98a0795c6fSMarcus Overhagen 
99a0795c6fSMarcus Overhagen 	void resize(uint32 frames);
100a0795c6fSMarcus Overhagen 
101a0795c6fSMarcus Overhagen 	// take ownership of buffer from target
102a0795c6fSMarcus Overhagen 	// (deletes current buffer data, if any owned)
103a0795c6fSMarcus Overhagen 
104a0795c6fSMarcus Overhagen 	void adopt(
105a0795c6fSMarcus Overhagen 		void* pData,
106a0795c6fSMarcus Overhagen 		uint32 frameSize,
107a0795c6fSMarcus Overhagen 		uint32 frames,
108a0795c6fSMarcus Overhagen 		bool bCircular=true,
109a0795c6fSMarcus Overhagen 		rtm_pool* pPool=0);
110a0795c6fSMarcus Overhagen 
111a0795c6fSMarcus Overhagen 	// returns false if the target doesn't own the data, but references it
112a0795c6fSMarcus Overhagen 	// one way or the other
113a0795c6fSMarcus Overhagen 	bool adopt(RawBuffer& target);
114a0795c6fSMarcus Overhagen 
115a0795c6fSMarcus Overhagen 	// adopt currently ref'd data (if any; returns false if no buffer data or
116a0795c6fSMarcus Overhagen 	// already owned)
117a0795c6fSMarcus Overhagen 	bool adopt();
118a0795c6fSMarcus Overhagen 
119a0795c6fSMarcus Overhagen public:						// operations
120a0795c6fSMarcus Overhagen 
121a0795c6fSMarcus Overhagen 	// fill the buffer with zeroes
122a0795c6fSMarcus Overhagen 
123a0795c6fSMarcus Overhagen 	void zero();
124a0795c6fSMarcus Overhagen 
125a0795c6fSMarcus Overhagen 	// raw copy to destination buffer, returning the number of
126a0795c6fSMarcus Overhagen 	// frames written, and adjusting both offsets accordingly.
127a0795c6fSMarcus Overhagen 	//
128a0795c6fSMarcus Overhagen 	// no frames will be written if the buffers' frame sizes
129a0795c6fSMarcus Overhagen 	// differ.
130a0795c6fSMarcus Overhagen 	//
131a0795c6fSMarcus Overhagen 	// rawCopyTo() will repeat the source data as many times as
132a0795c6fSMarcus Overhagen 	// necessary to fill the desired number of frames, but
133a0795c6fSMarcus Overhagen 	// will write no more than the target buffer's size.
134a0795c6fSMarcus Overhagen 
135a0795c6fSMarcus Overhagen 	uint32 rawCopyTo(
136a0795c6fSMarcus Overhagen 		RawBuffer& target,
137a0795c6fSMarcus Overhagen 		uint32* pioFromFrame,
138a0795c6fSMarcus Overhagen 		uint32* pioTargetFrame,
139a0795c6fSMarcus Overhagen 		uint32 frames) const;
140a0795c6fSMarcus Overhagen 
141a0795c6fSMarcus Overhagen 	// more convenient version of above if you don't care
142a0795c6fSMarcus Overhagen 	// how the offsets change.
143a0795c6fSMarcus Overhagen 
144a0795c6fSMarcus Overhagen 	uint32 rawCopyTo(
145a0795c6fSMarcus Overhagen 		RawBuffer& target,
146a0795c6fSMarcus Overhagen 		uint32 fromFrame,
147a0795c6fSMarcus Overhagen 		uint32 targetFrame,
148a0795c6fSMarcus Overhagen 		uint32 frames) const;
149a0795c6fSMarcus Overhagen 
150a0795c6fSMarcus Overhagen protected:					// internal operations
151a0795c6fSMarcus Overhagen 	// free owned data, if any
152a0795c6fSMarcus Overhagen 	// [16jun99] uses proper rtm_free() call if needed
153a0795c6fSMarcus Overhagen 	void free();
154a0795c6fSMarcus Overhagen 
155a0795c6fSMarcus Overhagen protected:					// members
156a0795c6fSMarcus Overhagen 	void*				m_pData;
157a0795c6fSMarcus Overhagen 	rtm_pool*		m_pPool;
158a0795c6fSMarcus Overhagen 	uint32			m_frameSize;
159a0795c6fSMarcus Overhagen 	uint32			m_frames;
160a0795c6fSMarcus Overhagen 	uint32			m_allocatedSize;
161a0795c6fSMarcus Overhagen 
162a0795c6fSMarcus Overhagen 	bool					m_bCircular;
163a0795c6fSMarcus Overhagen 	bool					m_bOwnData;
164a0795c6fSMarcus Overhagen };
165a0795c6fSMarcus Overhagen 
166a0795c6fSMarcus Overhagen #endif /* __RawBuffer_H__ */
167