xref: /haiku/src/apps/cortex/addons/common/RawBuffer.h (revision d3d8b26997fac34a84981e6d2b649521de2cc45a)
1 // RawBuffer.h
2 // eamoon@meadgroup.com
3 //
4 // * PURPOSE
5 // A basic representation of a media buffer.  RawBuffer
6 // instances may either allocate and maintain their own buffer or
7 // represent external data.
8 //
9 // * HISTORY
10 // e.moon 	16jun99
11 //		realtime allocation support
12 // e.moon		31mar99
13 //		begun
14 //
15 
16 #ifndef __RawBuffer_H__
17 #define __RawBuffer_H__
18 
19 #include <SupportDefs.h>
20 
21 class rtm_pool;
22 
23 class RawBuffer {
24 public:						// ctor/dtor/accessors
25 	virtual ~RawBuffer();
26 
27 	// allocate buffer (if frames > 0)
28 	// [16jun99] if pFromPool is nonzero, uses realtime allocator
29 	//           w/ the provided pool; otherwise uses standard
30 	//           new[] allocator.
31 	RawBuffer(
32 		uint32 frameSize=1,
33 		uint32 frames=0,
34 		bool circular=true,
35 		rtm_pool* pFromPool=0);
36 
37 	// point to given data (does NOT take responsibility for
38 	// deleting it; use adopt() for that.)
39 	RawBuffer(
40 		void* pData,
41 		uint32 frameSize,
42 		uint32 frames,
43 		bool bCircular=true,
44 		rtm_pool* pFromPool=0);
45 
46 	// generate reference to the given target buffer
47 	RawBuffer(const RawBuffer& clone);
48 	RawBuffer& operator=(const RawBuffer& clone);
49 
50 	// returns pointer to start of buffer
51 	char* data() const;
52 	// returns pointer to given frame
53 	char* frame(uint32 frame) const;
54 
55 	uint32 frameSize() const;
56 	uint32 frames() const;
57 	uint32 size() const;
58 
59 	bool isCircular() const;
60 	bool ownsBuffer() const;
61 
62 	rtm_pool* pool() const;
63 
64 	// resize buffer, re-allocating if necessary to contain
65 	// designated number of frames
66 	// Does not preserve buffer contents.
67 
68 	void resize(uint32 frames);
69 
70 	// take ownership of buffer from target
71 	// (deletes current buffer data, if any owned)
72 
73 	void adopt(
74 		void* pData,
75 		uint32 frameSize,
76 		uint32 frames,
77 		bool bCircular=true,
78 		rtm_pool* pPool=0);
79 
80 	// returns false if the target doesn't own the data, but references it
81 	// one way or the other
82 	bool adopt(RawBuffer& target);
83 
84 	// adopt currently ref'd data (if any; returns false if no buffer data or
85 	// already owned)
86 	bool adopt();
87 
88 public:						// operations
89 
90 	// fill the buffer with zeroes
91 
92 	void zero();
93 
94 	// raw copy to destination buffer, returning the number of
95 	// frames written, and adjusting both offsets accordingly.
96 	//
97 	// no frames will be written if the buffers' frame sizes
98 	// differ.
99 	//
100 	// rawCopyTo() will repeat the source data as many times as
101 	// necessary to fill the desired number of frames, but
102 	// will write no more than the target buffer's size.
103 
104 	uint32 rawCopyTo(
105 		RawBuffer& target,
106 		uint32* pioFromFrame,
107 		uint32* pioTargetFrame,
108 		uint32 frames) const;
109 
110 	// more convenient version of above if you don't care
111 	// how the offsets change.
112 
113 	uint32 rawCopyTo(
114 		RawBuffer& target,
115 		uint32 fromFrame,
116 		uint32 targetFrame,
117 		uint32 frames) const;
118 
119 protected:					// internal operations
120 	// free owned data, if any
121 	// [16jun99] uses proper rtm_free() call if needed
122 	void free();
123 
124 protected:					// members
125 	void*				m_pData;
126 	rtm_pool*		m_pPool;
127 	uint32			m_frameSize;
128 	uint32			m_frames;
129 	uint32			m_allocatedSize;
130 
131 	bool					m_bCircular;
132 	bool					m_bOwnData;
133 };
134 
135 #endif /* __RawBuffer_H__ */
136