uhci: get device descriptor, mem_alloc: aligned addresses
[quarnos.git] / manes / mem_alloc.cpp
blob403f99faa3c791ae4d70da8fe92873eb7677c337
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;
54 return (void*)buf;
56 char *buf = (char*)manec::state()->get_memalloc()->allocate_space(size);
57 for (unsigned int i = 0; i < size; i++)
58 buf[i] = 0;
59 return (void*)buf;
62 void *operator new(unsigned int size, int align) {
63 void *ptr = new char[size + align];
64 int add = align - (int)ptr % align;
65 ptr = (void*)((int)ptr + add);
67 #if DEBUG_MODE == CONF_YES
68 if ((int)ptr & (align - 1))
69 __asm__("cli\nhlt"::"a"(ptr));
70 #endif
72 return ptr;
75 void *operator new[](unsigned int size) {
76 return operator new(size);
79 void *operator new[](unsigned int size, int align) {
80 return operator new(size, align);
83 void operator delete(void *ptr) {
84 if ((unsigned int)ptr < (unsigned int)_static_mem + _static_mem_ptr)
85 return;
86 if (!_static_mem_off)
87 return;
88 manec::state()->get_memalloc()->deallocate_space(ptr);
91 void operator delete[](void *ptr) {
92 operator delete(ptr);