new facade system: manec and manei, removed manager
[quarnos.git] / manes / mem_alloc.cpp
blob17dc690f59bc70baeabc4970ba114dcacff52253
1 /* Quarn OS
3 * C++ memory allocation functions
5 * Copyright (C) 2008-2009 Pawel Dziepak
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 /* Functions that makes it possible to allocate more memory using operators
24 * new and delete. After booting they uses static part of memory, but then
25 * they change to the memory allocator.
28 #include "services/kernel_state.h"
29 #include "manec.h"
30 #include "error.h"
31 #include "manes/component.h"
32 #include "resources/buffer.h"
34 using namespace manes;
35 using namespace resources;
37 static const int _static_mem_amount = 0x5000;
38 char _static_mem[_static_mem_amount];
39 int _static_mem_ptr = 0;
40 bool _static_mem_off = false;
42 unsigned int allocated_memory = 0;
44 void *operator new(unsigned int size) {
45 allocated_memory += size;
46 if (!_static_mem_off) {
47 int begin = _static_mem_ptr;
48 _static_mem_ptr += size;
49 if (_static_mem_ptr >= _static_mem_amount)
50 critical("mem_alloc: not enough space on static heap to allocate more memory");
51 char *buf = (char*)&(_static_mem[begin]);
52 for (unsigned int i = 0; i < size; i++)
53 buf[i] = 0;
55 if ((int)buf < 0x100000 || (int)buf >= 0x200000)
56 asm("cli\nhlt"::"a"(buf), "b"(0xbaddcafe));
57 return (void*)buf;
59 char *buf = (char*)manec::get()->state()->get_memalloc()->allocate_space(size);
60 for (unsigned int i = 0; i < size; i++)
61 buf[i] = 0;
62 if ((int)buf < 0x300000 || (int)buf >= 0x400000)
63 critical((string)"mem_alloc: returned value is incorrect: 0x" + string((unsigned int)buf,true));
65 return (void*)buf;
68 void *operator new(unsigned int size, int align) {
69 void *ptr = new char[size + align];
70 int add = align - (int)ptr % align;
71 ptr = (void*)((int)ptr + add);
73 #if DEBUG_MODE == CONF_YES
74 if ((int)ptr & (align - 1))
75 __asm__("cli\nhlt"::"a"(ptr));
76 #endif
78 return ptr;
81 void *operator new[](unsigned int size) {
82 return operator new(size);
85 void *operator new[](unsigned int size, int align) {
86 return operator new(size, align);
89 void operator delete(void *ptr) {
90 if ((unsigned int)ptr < (unsigned int)_static_mem + _static_mem_ptr)
91 return;
92 if (!_static_mem_off)
93 return;
94 manec::get()->state()->get_memalloc()->deallocate_space(ptr);
97 void operator delete[](void *ptr) {
98 operator delete(ptr);