Subversion Repositories Kolibri OS

Rev

Rev 8129 | Rev 8184 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 8129 Rev 8170
Line 1... Line 1...
1
#ifndef __MENUET_HEAP_H_INCLUDED_
1
#ifndef __KOLIBRI_HEAP_H_INCLUDED_
2
#define __MENUET_HEAP_H_INCLUDED_
2
#define __KOLIBRI_HEAP_H_INCLUDED_
Line 3... Line 3...
3
 
3
 
4
#include 
4
#include 
Line 5... Line 5...
5
#include 
5
#include 
Line 11... Line 11...
11
	void *Alloc(unsigned int size);
11
	void *Alloc(unsigned int size);
12
	void *ReAlloc(void *mem, unsigned int size);
12
	void *ReAlloc(void *mem, unsigned int size);
13
	void Free(void *mem);
13
	void Free(void *mem);
14
}
14
}
Line 15... Line 15...
15
 
15
 
Line 16... Line 16...
16
#ifdef __MENUET__
16
#ifdef __KOLIBRI__
17
 
17
 
Line 18... Line 18...
18
namespace Kolibri
18
namespace Kolibri
Line 19... Line 19...
19
{
19
{
20
 
20
 
21
// Global variables
21
// Global variables
Line 22... Line 22...
22
 
22
 
Line 23... Line 23...
23
	MemoryHeap::TFreeSpace _MenuetFreeSpace;
23
	MemoryHeap::TFreeSpace _KolibriFreeSpace;
24
	MemoryHeap::TMemBlock  _MenuetMemBlock;
24
	MemoryHeap::TMemBlock  _KolibriMemBlock;
25
	TMutex _MemHeapMutex = MENUET_MUTEX_INIT;
25
	TMutex _MemHeapMutex = KOLIBRI_MUTEX_INIT;
26
 
26
 
27
// Functions
27
// Functions
28
 
28
 
29
	void *_HeapInit(void *begin, void *use_end, void *end)
29
	void *_HeapInit(void *begin, void *use_end, void *end)
30
	{
30
	{
31
		MemoryHeap::InitFreeSpace(_MenuetFreeSpace);
31
		MemoryHeap::InitFreeSpace(_KolibriFreeSpace);
32
		_MenuetMemBlock = MemoryHeap::CreateBlock(begin, end, _MenuetFreeSpace);
32
		_KolibriMemBlock = MemoryHeap::CreateBlock(begin, end, _KolibriFreeSpace);
33
		unsigned int use_beg = (unsigned int)MemoryHeap::BlockBegin(_MenuetMemBlock) +
33
		unsigned int use_beg = (unsigned int)MemoryHeap::BlockBegin(_KolibriMemBlock) +
Line 34... Line 34...
34
					MemoryHeap::BlockAddSize - MemoryHeap::BlockEndSize;
34
					MemoryHeap::BlockAddSize - MemoryHeap::BlockEndSize;
Line 35... Line 35...
35
		unsigned int use_size = (unsigned int)use_end;
35
		unsigned int use_size = (unsigned int)use_end;
Line 36... Line 36...
36
		if (use_size <= use_beg) return 0;
36
		if (use_size <= use_beg) return 0;
37
		else use_size -= use_beg;
37
		else use_size -= use_beg;
38
		return MemoryHeap::Alloc(_MenuetFreeSpace, use_size);
38
		return MemoryHeap::Alloc(_KolibriFreeSpace, use_size);
39
	}
39
	}
40
 
40
 
41
	bool _SetUseMemory(unsigned int use_mem);
41
	bool _SetUseMemory(unsigned int use_mem);
42
 
42
 
43
	int _RecalculateUseMemory(unsigned int use_mem);
43
	int _RecalculateUseMemory(unsigned int use_mem);
44
 
44
 
45
	void *Alloc(unsigned int size)
45
	void *Alloc(unsigned int size)
46
	{
46
	{
47
		if (!size) return 0;
47
		if (!size) return 0;
48
		Lock(&_MemHeapMutex);
48
		Lock(&_MemHeapMutex);
49
		void *res = MemoryHeap::Alloc(_MenuetFreeSpace, size);
49
		void *res = MemoryHeap::Alloc(_KolibriFreeSpace, size);
50
		if (!res)
50
		if (!res)
51
		{
51
		{
Line 52... Line 52...
52
			unsigned use_mem = (unsigned int)MemoryHeap::BlockEndFor(_MenuetMemBlock, size);
52
			unsigned use_mem = (unsigned int)MemoryHeap::BlockEndFor(_KolibriMemBlock, size);
53
			if (_SetUseMemory(_RecalculateUseMemory(use_mem)))
53
			if (_SetUseMemory(_RecalculateUseMemory(use_mem)))
54
			{
54
			{
55
				res = MemoryHeap::Alloc(_MenuetFreeSpace, size);
55
				res = MemoryHeap::Alloc(_KolibriFreeSpace, size);
56
			}
56
			}
57
		}
57
		}
58
		UnLock(&_MemHeapMutex);
58
		UnLock(&_MemHeapMutex);
59
		return res;
59
		return res;
60
	}
60
	}
61
 
61
 
62
	void *ReAlloc(void *mem, unsigned int size)
62
	void *ReAlloc(void *mem, unsigned int size)
63
	{
63
	{
64
		Lock(&_MemHeapMutex);
64
		Lock(&_MemHeapMutex);
65
		void *res = MemoryHeap::ReAlloc(_MenuetFreeSpace, mem, size);
65
		void *res = MemoryHeap::ReAlloc(_KolibriFreeSpace, mem, size);
66
		if (!res && size)
66
		if (!res && size)
Line 67... Line 67...
67
		{
67
		{
68
			unsigned use_mem = (unsigned int)MemoryHeap::BlockEndFor(_MenuetMemBlock, size);
68
			unsigned use_mem = (unsigned int)MemoryHeap::BlockEndFor(_KolibriMemBlock, size);
69
			if (_SetUseMemory(_RecalculateUseMemory(use_mem)))
69
			if (_SetUseMemory(_RecalculateUseMemory(use_mem)))
70
			{
70
			{
71
				res = MemoryHeap::ReAlloc(_MenuetFreeSpace, mem, size);
71
				res = MemoryHeap::ReAlloc(_KolibriFreeSpace, mem, size);
72
			}
72
			}
Line 73... Line 73...
73
		}
73
		}
74
		UnLock(&_MemHeapMutex);
74
		UnLock(&_MemHeapMutex);
Line 75... Line 75...
75
		return res;
75
		return res;
Line 76... Line 76...
76
	}
76
	}