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"
31 #include "manes/cds/component.h"
32 #include "resources/memm.h"
34 using namespace manes
;
35 using namespace resources
;
37 static const int _static_mem_amount
= 0x50000;
38 char _static_mem
[_static_mem_amount
];
39 int _static_mem_ptr
= 0;
41 unsigned int allocated_memory
= 0;
43 p
<memm
> allocator
= p
<memm
>::invalid
;
45 void *operator new(unsigned int size
) {
46 allocated_memory
+= size
;
47 // if (!allocator.valid()) {
48 int begin
= _static_mem_ptr
;
49 _static_mem_ptr
+= size
;
50 if (_static_mem_ptr
>= _static_mem_amount
)
51 critical("mem_alloc: not enough space on static heap to allocate more memory");
52 char *buf
= (char*)&(_static_mem
[begin
]);
53 for (unsigned int i
= 0; i
< size
; i
++)
56 if ((int)buf
< 0x100000 || (int)buf
>= 0x200000)
57 asm("cli\nhlt"::"a"(buf
), "b"(0xbaddcafe));
60 //char *buf = (char*)allocator->allocate_space(size);
61 for (unsigned int i
= 0; i
< size
; i
++)
63 if ((int)buf
< 0x300000 || (int)buf
>= 0x400000)
64 critical((string
)"mem_alloc: returned value is incorrect: 0x" + string((unsigned int)buf
,true) + " (" + string(size
) + ")");
69 void *operator new(unsigned int size
, int align
) {
70 void *ptr
= new char[size
+ align
];
71 int add
= align
- (int)ptr
% align
;
72 ptr
= (void*)((int)ptr
+ add
);
74 #if DEBUG_MODE == CONF_YES
75 if ((int)ptr
& (align
- 1))
76 __asm__("cli\nhlt"::"a"(ptr
));
82 void *operator new[](unsigned int size
) {
83 return operator new(size
);
86 void *operator new[](unsigned int size
, int align
) {
87 return operator new(size
, align
);
90 void operator delete(void *ptr
) {
91 if ((unsigned int)ptr
< (unsigned int)_static_mem
+ _static_mem_ptr
)
93 if (!allocator
.valid())
95 allocator
->deallocate_space(ptr
);
98 void operator delete[](void *ptr
) {