xref: /haiku/src/servers/app/ClientMemoryAllocator.h (revision c237c4ce593ee823d9867fd997e51e4c447f5623)
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 #include <Referenceable.h>
14 
15 #include <util/DoublyLinkedList.h>
16 
17 
18 class ServerApp;
19 struct chunk;
20 struct block;
21 
22 struct chunk : DoublyLinkedListLinkImpl<struct chunk> {
23 	area_id	area;
24 	uint8*	base;
25 	size_t	size;
26 };
27 
28 struct block : DoublyLinkedListLinkImpl<struct block> {
29 	struct chunk* chunk;
30 	uint8*	base;
31 	size_t	size;
32 };
33 
34 typedef DoublyLinkedList<block> block_list;
35 typedef DoublyLinkedList<chunk> chunk_list;
36 
37 
38 class ClientMemoryAllocator : public BReferenceable {
39 public:
40 								ClientMemoryAllocator(ServerApp* application);
41 								~ClientMemoryAllocator();
42 
43 			void*				Allocate(size_t size, block** _address,
44 									bool& newArea);
45 			void				Free(block* cookie);
46 
47 			void				Detach();
48 
49 			void				Dump();
50 
51 private:
52 			struct block*		_AllocateChunk(size_t size, bool& newArea);
53 
54 private:
55 			ServerApp*			fApplication;
56 			BLocker				fLock;
57 			chunk_list			fChunks;
58 			block_list			fFreeBlocks;
59 };
60 
61 
62 class AreaMemory {
63 public:
64 	virtual						~AreaMemory() {}
65 
66 	virtual area_id				Area() = 0;
67 	virtual uint8*				Address() = 0;
68 	virtual uint32				AreaOffset() = 0;
69 };
70 
71 
72 class ClientMemory : public AreaMemory {
73 public:
74 								ClientMemory();
75 
76 	virtual						~ClientMemory();
77 
78 			void*				Allocate(ClientMemoryAllocator* allocator,
79 									size_t size, bool& newArea);
80 
81 	virtual area_id				Area();
82 	virtual uint8*				Address();
83 	virtual uint32				AreaOffset();
84 
85 private:
86 			BReference<ClientMemoryAllocator>
87 								fAllocator;
88 			block*				fBlock;
89 };
90 
91 
92 /*! Just clones an existing area. */
93 class ClonedAreaMemory  : public AreaMemory{
94 public:
95 								ClonedAreaMemory();
96 	virtual						~ClonedAreaMemory();
97 
98 			void*				Clone(area_id area, uint32 offset);
99 
100 	virtual area_id				Area();
101 	virtual uint8*				Address();
102 	virtual uint32				AreaOffset();
103 
104 private:
105 			area_id		fClonedArea;
106 			uint32		fOffset;
107 			uint8*		fBase;
108 };
109 
110 
111 #endif	/* CLIENT_MEMORY_ALLOCATOR_H */
112