xref: /haiku/src/servers/app/ClientMemoryAllocator.h (revision 04a0e9c7b68cbe3a43d38e2bca8e860fd80936fb)
1 /*
2  * Copyright 2006-2013, Haiku, Inc. All Rights Reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Axel Dörfler, axeld@pinc-software.de
7  */
8 #ifndef CLIENT_MEMORY_ALLOCATOR_H
9 #define CLIENT_MEMORY_ALLOCATOR_H
10 
11 
12 #include <Locker.h>
13 
14 #include <util/DoublyLinkedList.h>
15 
16 
17 class ServerApp;
18 struct chunk;
19 struct block;
20 
21 struct chunk : DoublyLinkedListLinkImpl<struct chunk> {
22 	area_id	area;
23 	uint8*	base;
24 	size_t	size;
25 };
26 
27 struct block : DoublyLinkedListLinkImpl<struct block> {
28 	struct chunk* chunk;
29 	uint8*	base;
30 	size_t	size;
31 };
32 
33 typedef DoublyLinkedList<block> block_list;
34 typedef DoublyLinkedList<chunk> chunk_list;
35 
36 
37 class ClientMemoryAllocator {
38 public:
39 								ClientMemoryAllocator(ServerApp* application);
40 								~ClientMemoryAllocator();
41 
42 			void*				Allocate(size_t size, block** _address,
43 									bool& newArea);
44 			void				Free(block* cookie);
45 
46 			void				Detach();
47 
48 			void				Dump();
49 
50 private:
51 			struct block*		_AllocateChunk(size_t size, bool& newArea);
52 
53 private:
54 			ServerApp*			fApplication;
55 			BLocker				fLock;
56 			chunk_list			fChunks;
57 			block_list			fFreeBlocks;
58 };
59 
60 
61 class AreaMemory {
62 public:
63 	virtual						~AreaMemory() {}
64 
65 	virtual area_id				Area() = 0;
66 	virtual uint8*				Address() = 0;
67 	virtual uint32				AreaOffset() = 0;
68 };
69 
70 
71 class ClientMemory : public AreaMemory {
72 public:
73 								ClientMemory();
74 
75 	virtual						~ClientMemory();
76 
77 			void*				Allocate(ClientMemoryAllocator* allocator,
78 									size_t size, bool& newArea);
79 
80 	virtual area_id				Area();
81 	virtual uint8*				Address();
82 	virtual uint32				AreaOffset();
83 
84 private:
85 			ClientMemoryAllocator*	fAllocator;
86 			block*				fBlock;
87 };
88 
89 
90 /*! Just clones an existing area. */
91 class ClonedAreaMemory  : public AreaMemory{
92 public:
93 								ClonedAreaMemory();
94 	virtual						~ClonedAreaMemory();
95 
96 			void*				Clone(area_id area, uint32 offset);
97 
98 	virtual area_id				Area();
99 	virtual uint8*				Address();
100 	virtual uint32				AreaOffset();
101 
102 private:
103 			area_id		fClonedArea;
104 			uint32		fOffset;
105 			uint8*		fBase;
106 };
107 
108 
109 #endif	/* CLIENT_MEMORY_ALLOCATOR_H */
110