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
18class ServerApp;
19struct chunk;
20struct block;
21
22struct chunk : DoublyLinkedListLinkImpl<struct chunk> {
23	area_id	area;
24	uint8*	base;
25	size_t	size;
26};
27
28struct block : DoublyLinkedListLinkImpl<struct block> {
29	struct chunk* chunk;
30	uint8*	base;
31	size_t	size;
32};
33
34typedef DoublyLinkedList<block> block_list;
35typedef DoublyLinkedList<chunk> chunk_list;
36
37
38class ClientMemoryAllocator : public BReferenceable {
39public:
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
51private:
52			struct block*		_AllocateChunk(size_t size, bool& newArea);
53
54private:
55			ServerApp*			fApplication;
56			BLocker				fLock;
57			chunk_list			fChunks;
58			block_list			fFreeBlocks;
59};
60
61
62class AreaMemory {
63public:
64	virtual						~AreaMemory() {}
65
66	virtual area_id				Area() = 0;
67	virtual uint8*				Address() = 0;
68	virtual uint32				AreaOffset() = 0;
69};
70
71
72class ClientMemory : public AreaMemory {
73public:
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
85private:
86			BReference<ClientMemoryAllocator>
87								fAllocator;
88			block*				fBlock;
89};
90
91
92/*! Just clones an existing area. */
93class ClonedAreaMemory  : public AreaMemory{
94public:
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
104private:
105			area_id		fClonedArea;
106			uint32		fOffset;
107			uint8*		fBase;
108};
109
110
111#endif	/* CLIENT_MEMORY_ALLOCATOR_H */
112