xref: /haiku/src/servers/media/BufferManager.h (revision 579f1dbca962a2a03df54f69fdc6e9423f91f20e)
1 /*
2  * Copyright 2002, Marcus Overhagen. All rights reserved.
3  * Copyright 2009, Axel Dörfler, axeld@pinc-software.de.
4  * Distributed under the terms of the MIT License.
5  */
6 #ifndef _BUFFER_MANAGER_H
7 #define _BUFFER_MANAGER_H
8 
9 #include <set>
10 
11 #include <Locker.h>
12 #include <MediaDefs.h>
13 
14 #include <HashMap.h>
15 
16 
17 namespace BPrivate {
18 	class SharedBufferList;
19 }
20 
21 
22 class BufferManager {
23 public:
24 							BufferManager();
25 							~BufferManager();
26 
27 			area_id			SharedBufferListArea();
28 
29 			status_t		RegisterBuffer(team_id team,
30 								media_buffer_id bufferID, size_t* _size,
31 								int32* _flags, size_t* _offset, area_id* _area);
32 
33 			status_t		RegisterBuffer(team_id team, size_t size,
34 								int32 flags, size_t offset, area_id area,
35 								media_buffer_id* _bufferID);
36 
37 			status_t		UnregisterBuffer(team_id team,
38 								media_buffer_id bufferID);
39 
40 			void			CleanupTeam(team_id team);
41 
42 			void			Dump();
43 
44 private:
45 			area_id			_CloneArea(area_id area);
46 			void			_ReleaseClonedArea(area_id clone);
47 
48 private:
49 	struct clone_info {
50 		area_id				clone;
51 		vint32				ref_count;
52 	};
53 
54 	struct buffer_info {
55 		media_buffer_id		id;
56 		area_id				area;
57 		size_t				offset;
58 		size_t				size;
59 		int32				flags;
60 		std::set<team_id>	teams;
61 	};
62 
63 	template<typename Type> struct id_hash {
64 		id_hash()
65 			:
66 			fID(0)
67 		{
68 		}
69 
70 		id_hash(Type id)
71 			:
72 			fID(id)
73 		{
74 		}
75 
76 		id_hash(const id_hash& other)
77 		{
78 			fID = other.fID;
79 		}
80 
81 		uint32 GetHashCode() const
82 		{
83 			return fID;
84 		}
85 
86 		operator Type() const
87 		{
88 			return fID;
89 		}
90 
91 		id_hash& operator=(const id_hash& other)
92 		{
93 			fID = other.fID;
94 			return *this;
95 		}
96 
97 	private:
98 		Type	fID;
99 	};
100 
101 	typedef HashMap<id_hash<media_buffer_id>, buffer_info> BufferInfoMap;
102 	typedef HashMap<id_hash<area_id>, clone_info> CloneInfoMap;
103 	typedef HashMap<id_hash<area_id>, area_id> SourceInfoMap;
104 
105 			BPrivate::SharedBufferList* fSharedBufferList;
106 			area_id			fSharedBufferListArea;
107 			media_buffer_id	fNextBufferID;
108 			BLocker			fLocker;
109 			BufferInfoMap	fBufferInfoMap;
110 			CloneInfoMap	fCloneInfoMap;
111 			SourceInfoMap	fSourceInfoMap;
112 };
113 
114 #endif // _BUFFER_MANAGER_H
115