summaryrefslogtreecommitdiffstats
path: root/src/rtpmemorymanager.h
blob: bfc50faf839a4496c53d587c4e3d70c41c2c826a (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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*

  This file is a part of JRTPLIB
  Copyright (c) 1999-2007 Jori Liesenborgs

  Contact: jori.liesenborgs@gmail.com

  This library was developed at the "Expertisecentrum Digitale Media"
  (http://www.edm.uhasselt.be), a research center of the Hasselt University
  (http://www.uhasselt.be). The library is based upon work done for 
  my thesis at the School for Knowledge Technology (Belgium/The Netherlands).

  Permission is hereby granted, free of charge, to any person obtaining a
  copy of this software and associated documentation files (the "Software"),
  to deal in the Software without restriction, including without limitation
  the rights to use, copy, modify, merge, publish, distribute, sublicense,
  and/or sell copies of the Software, and to permit persons to whom the
  Software is furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included
  in all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  IN THE SOFTWARE.

*/

/**
 * \file rtpmemorymanager.h
 */

#ifndef RTPMEMORYMANAGER_H

#define RTPMEMORYMANAGER_H

#include "rtpconfig.h"
#include "rtptypes.h"

/** Used to indicate a general kind of memory block. */
#define RTPMEM_TYPE_OTHER							0

/** Buffer to store an incoming RTP packet. */
#define RTPMEM_TYPE_BUFFER_RECEIVEDRTPPACKET					1

/** Buffer to store an incoming RTCP packet. */
#define RTPMEM_TYPE_BUFFER_RECEIVEDRTCPPACKET					2

/** Buffer to store an RTCP APP packet. */
#define RTPMEM_TYPE_BUFFER_RTCPAPPPACKET					3

/** Buffer to store an RTCP BYE packet. */
#define RTPMEM_TYPE_BUFFER_RTCPBYEPACKET					4

/** Buffer to store a BYE reason. */
#define RTPMEM_TYPE_BUFFER_RTCPBYEREASON					5

/** Buffer to store an RTCP compound packet. */
#define RTPMEM_TYPE_BUFFER_RTCPCOMPOUNDPACKET					6

/** Buffer to store an SDES block. */
#define RTPMEM_TYPE_BUFFER_RTCPSDESBLOCK					7

/** Buffer to store an RTP packet. */
#define RTPMEM_TYPE_BUFFER_RTPPACKET						8

/** Buffer used by an RTPPacketBuilder instance. */
#define RTPMEM_TYPE_BUFFER_RTPPACKETBUILDERBUFFER				9

/** Buffer to store an SDES item. */
#define RTPMEM_TYPE_BUFFER_SDESITEM						10

/** Hash element used in the accept/ignore table. */
#define RTPMEM_TYPE_CLASS_ACCEPTIGNOREHASHELEMENT				11

/** Buffer to store a PortInfo instance, used by the UDP over IPv4 and IPv6 transmitters. */
#define RTPMEM_TYPE_CLASS_ACCEPTIGNOREPORTINFO					12

/** Buffer to store a HashElement instance for the destination hash table. */
#define RTPMEM_TYPE_CLASS_DESTINATIONLISTHASHELEMENT				13

/** Buffer to store a HashElement instance for the multicast hash table. */
#define RTPMEM_TYPE_CLASS_MULTICASTHASHELEMENT					14

/** Buffer to store an instance of RTCPAPPPacket. */
#define RTPMEM_TYPE_CLASS_RTCPAPPPACKET						15

/** Buffer to store an instance of RTCPBYEPacket. */
#define RTPMEM_TYPE_CLASS_RTCPBYEPACKET						16

/** Buffer to store an instance of RTCPCompoundPacketBuilder. */
#define RTPMEM_TYPE_CLASS_RTCPCOMPOUNDPACKETBUILDER				17

/** Buffer to store an RTCPReceiverReport instance. */
#define RTPMEM_TYPE_CLASS_RTCPRECEIVERREPORT					18

/** Buffer to store an instance of RTCPRRPacket. */
#define RTPMEM_TYPE_CLASS_RTCPRRPACKET						19

/** Buffer to store an instance of RTCPSDESPacket. */
#define RTPMEM_TYPE_CLASS_RTCPSDESPACKET					20

/** Buffer to store an instance of RTCPSRPacket. */
#define RTPMEM_TYPE_CLASS_RTCPSRPACKET						21

/** Buffer to store an instance of RTCPUnknownPacket. */
#define RTPMEM_TYPE_CLASS_RTCPUNKNOWNPACKET					22

/** Buffer to store an instance of an RTPAddress derived class. */
#define RTPMEM_TYPE_CLASS_RTPADDRESS						23

/** Buffer to store an instance of RTPInternalSourceData. */
#define RTPMEM_TYPE_CLASS_RTPINTERNALSOURCEDATA					24

/** Buffer to store an RTPPacket instance. */
#define RTPMEM_TYPE_CLASS_RTPPACKET						25

/** Buffer to store an RTPPollThread instance. */
#define RTPMEM_TYPE_CLASS_RTPPOLLTHREAD						26

/** Buffer to store an RTPRawPacket instance. */
#define RTPMEM_TYPE_CLASS_RTPRAWPACKET						27

/** Buffer to store an RTPTransmissionInfo derived class. */
#define RTPMEM_TYPE_CLASS_RTPTRANSMISSIONINFO					28

/** Buffer to store an RTPTransmitter derived class. */
#define RTPMEM_TYPE_CLASS_RTPTRANSMITTER					29

/** Buffer to store an SDESPrivateItem instance. */
#define RTPMEM_TYPE_CLASS_SDESPRIVATEITEM					30

/** Buffer to store an SDESSource instance. */
#define RTPMEM_TYPE_CLASS_SDESSOURCE						31

/** Buffer to store a HashElement instance for the source table. */
#define RTPMEM_TYPE_CLASS_SOURCETABLEHASHELEMENT				32

/** A memory manager. */
class RTPMemoryManager
{
public:	
	RTPMemoryManager()									{ }
	virtual ~RTPMemoryManager()								{ }
	
	/** Called to allocate \c numbytes of memory.
	 *  Called to allocate \c numbytes of memory. The \c memtype parameter
	 *  indicates what the purpose of the memory block is. Relevant values
	 *  can be found in rtpmemorymanager.h . Note that the types starting with
	 *  \c RTPMEM_TYPE_CLASS indicate fixed size buffers and that types starting
	 *  with \c RTPMEM_TYPE_BUFFER indicate variable size buffers.
	 */
	virtual void *AllocateBuffer(size_t numbytes, int memtype) = 0;

	/** Frees the previously allocated memory block \c buffer */
	virtual void FreeBuffer(void *buffer) = 0;
};

#ifdef RTP_SUPPORT_MEMORYMANAGEMENT	

#include <new>

inline void *operator new(size_t numbytes, RTPMemoryManager *mgr, int memtype)
{
	if (mgr == 0)
		return operator new(numbytes);
	return mgr->AllocateBuffer(numbytes,memtype);
}

inline void operator delete(void *buffer, RTPMemoryManager *mgr, int memtype)
{
	if (mgr == 0)
		operator delete(buffer);
	else
		mgr->FreeBuffer(buffer);
}

#if defined(WIN32) || defined(_WIN32_WCE)
#if _MSC_VER >= 1300
inline void *operator new[](size_t numbytes, RTPMemoryManager *mgr, int memtype)
{
	if (mgr == 0)
		return operator new[](numbytes);
	return mgr->AllocateBuffer(numbytes,memtype);
}

inline void operator delete[](void *buffer, RTPMemoryManager *mgr, int memtype)
{
	if (mgr == 0)
		operator delete[](buffer);
	else
		mgr->FreeBuffer(buffer);
}
#endif // _MSC_VER >= 1300
#else
inline void *operator new[](size_t numbytes, RTPMemoryManager *mgr, int memtype)
{
	if (mgr == 0)
		return operator new[](numbytes);
	return mgr->AllocateBuffer(numbytes,memtype);
}

inline void operator delete[](void *buffer, RTPMemoryManager *mgr, int memtype)
{
	if (mgr == 0)
		operator delete[](buffer);
	else
		mgr->FreeBuffer(buffer);
}
#endif // WIN32 || _WIN32_WCE

inline void RTPDeleteByteArray(uint8_t *buf, RTPMemoryManager *mgr)
{
	if (mgr == 0)
		delete [] buf;
	else
		mgr->FreeBuffer(buf);
}

template<class ClassName> 
inline void RTPDelete(ClassName *obj, RTPMemoryManager *mgr)
{
	if (mgr == 0)
		delete obj;
	else
	{
		obj->~ClassName();
		mgr->FreeBuffer(obj);
	}
}

#define RTPNew(a,b) 			new(a,b)

#else

#define RTPNew(a,b) 			new
#define RTPDelete(a,b) 			delete a
#define RTPDeleteByteArray(a,b) 	delete [] a;

#endif // RTP_SUPPORT_MEMORYMANAGEMENT

#endif // RTPMEMORYMANAGER_H