1// BlockAllocatorAreaBucket.h
2
3#ifndef BLOCK_ALLOCATOR_AREA_BUCKET_H
4#define BLOCK_ALLOCATOR_AREA_BUCKET_H
5
6#include <util/DoublyLinkedList.h>
7
8#include "BlockAllocator.h"
9#include "BlockAllocatorArea.h"
10#include "Debug.h"
11
12
13class BlockAllocator::AreaBucket {
14public:
15	AreaBucket();
16	~AreaBucket();
17
18	inline void SetIndex(int32 index)	{ fIndex = index; }
19	inline int32 GetIndex() const		{ return fIndex; }
20
21	inline void SetSizeLimits(size_t minSize, size_t maxSize);
22	inline size_t GetMinSize() const	{ return fMinSize; }	// incl.
23	inline size_t GetMaxSize() const	{ return fMaxSize; }	// excl.
24
25	inline void AddArea(Area *area);
26	inline void RemoveArea(Area *area);
27
28	inline Area *GetFirstArea() const	{ return fAreas.First(); }
29	inline Area *GetLastArea() const	{ return fAreas.Last(); }
30	inline Area *GetNextArea(Area* area) const;
31
32	inline bool IsEmpty() const			{ return fAreas.IsEmpty(); }
33
34	// debugging only
35	bool SanityCheck(bool deep = false) const;
36
37private:
38	DoublyLinkedList<Area>	fAreas;
39	int32			fIndex;
40	size_t			fMinSize;
41	size_t			fMaxSize;
42};
43
44typedef BlockAllocator::AreaBucket AreaBucket;
45
46
47// inline methods
48
49// debugging
50#if BA_DEFINE_INLINES
51
52// SetSizeLimits
53/*!	\brief Sets the size limits for areas this bucket may contain.
54	\param minSize Minimal area size. Inclusively.
55	\param maxSize Maximal area size. Exlusively.
56*/
57inline
58void
59BlockAllocator::AreaBucket::SetSizeLimits(size_t minSize, size_t maxSize)
60{
61	fMinSize = minSize;
62	fMaxSize = maxSize;
63}
64
65// AddArea
66inline
67void
68BlockAllocator::AreaBucket::AddArea(Area *area)
69{
70	if (area) {
71		fAreas.Insert(area, area->NeedsDefragmenting());
72		area->SetBucket(this);
73		D(SanityCheck(false));
74	}
75}
76
77// RemoveArea
78inline
79void
80BlockAllocator::AreaBucket::RemoveArea(Area *area)
81{
82	if (area) {
83		fAreas.Remove(area);
84		area->SetBucket(NULL);
85		D(SanityCheck(false));
86	}
87}
88
89// GetNextArea
90inline
91Area *
92BlockAllocator::AreaBucket::GetNextArea(Area* area) const
93{
94	return fAreas.GetNext(area);
95}
96
97#endif	// BA_DEFINE_INLINES
98
99#endif	// BLOCK_ALLOCATOR_AREA_BUCKET_H
100