mirror of
https://github.com/louisrubet/rpn
synced 2024-12-30 10:23:32 +01:00
#63: debugging large stack reallocs
This commit is contained in:
parent
20c6b40afd
commit
c4b9bbf3d6
2 changed files with 43 additions and 14 deletions
10
src/rpn.cpp
10
src/rpn.cpp
|
@ -367,7 +367,7 @@ struct branch : public object
|
|||
|
||||
void object::show(ostream& stream)
|
||||
{
|
||||
char buffer[512];
|
||||
//char buffer[512];
|
||||
|
||||
switch(_type)
|
||||
{
|
||||
|
@ -375,12 +375,12 @@ void object::show(ostream& stream)
|
|||
switch(((number*)this)->_representation)
|
||||
{
|
||||
case number::dec:
|
||||
(void)mpfr_snprintf(buffer, sizeof(buffer)-1, s_mpfr_printf_format.c_str(), ((number*)this)->_value.mpfr);
|
||||
stream<<buffer;
|
||||
(void)mpfr_printf(s_mpfr_printf_format.c_str(), ((number*)this)->_value.mpfr);
|
||||
//stream<<buffer;
|
||||
break;
|
||||
case number::hex:
|
||||
(void)mpfr_snprintf(buffer, sizeof(buffer)-1, s_mpfr_printf_format_hex.c_str(), ((number*)this)->_value.mpfr);
|
||||
stream<<buffer;
|
||||
(void)mpfr_printf(s_mpfr_printf_format_hex.c_str(), ((number*)this)->_value.mpfr);
|
||||
//stream<<buffer;
|
||||
break;
|
||||
case number::bin:
|
||||
cout<<"<binary representation TODO>";
|
||||
|
|
47
src/stack.h
47
src/stack.h
|
@ -60,25 +60,54 @@ public:
|
|||
object* allocate_back(unsigned int size, cmd_type_t type)
|
||||
{
|
||||
object* allocated;
|
||||
bool data_is_reallocated = false;
|
||||
ptrdiff_t realloc_offset = 0;
|
||||
|
||||
// manage memory allocation
|
||||
if (_current + size > _base + _total_size)
|
||||
// manage data memory allocation (add as much as memory it is needed)
|
||||
cout<<"** BASE"<<endl;
|
||||
cout<<" size="<<size<<endl;
|
||||
cout<<" _total_size="<<_total_size<<endl;
|
||||
if (((_current - _base) + size) > _total_size)
|
||||
{
|
||||
unsigned long offset = _current - _base;
|
||||
_total_size += ALLOC_STACK_CHUNK;
|
||||
// realloc data
|
||||
unsigned long page_number = 1 + ((_current - _base) + size - _total_size) / ALLOC_STACK_CHUNK;
|
||||
realloc_offset = _current - _base;
|
||||
_total_size += page_number * ALLOC_STACK_CHUNK;
|
||||
_base = (char*)realloc(_base, _total_size);
|
||||
_current = _base + offset;
|
||||
|
||||
//TODO si realloc alors les pointeurs doivent etre recalculés !
|
||||
cout<<" ->realloc size="<<_total_size<<endl;
|
||||
cout<<" ->realloc offset="<<realloc_offset<<endl;
|
||||
cout<<" ->_base="<<(void*)_base<<endl;
|
||||
_current = _base + realloc_offset;
|
||||
data_is_reallocated = true;
|
||||
}
|
||||
if ((_count+1) > _total_count_pointer)
|
||||
|
||||
// manage pointers memory allocation (page by page)
|
||||
cout<<"COUNT"<<endl;
|
||||
cout<<" _count="<<_count<<endl;
|
||||
cout<<" _total_count_pointer="<<_total_count_pointer<<endl;
|
||||
if ((_count + 1) > _total_count_pointer)
|
||||
{
|
||||
cout<<"POINTERS"<<endl;
|
||||
_base_pointer = (object**)realloc(_base_pointer, (_total_count_pointer * sizeof(object*)) + ALLOC_STACK_CHUNK);
|
||||
_total_count_pointer += (ALLOC_STACK_CHUNK / sizeof(object));
|
||||
cout<<" ->_total_count_pointer="<<_total_count_pointer<<endl;
|
||||
}
|
||||
|
||||
if (data_is_reallocated)
|
||||
{
|
||||
// recalc object pointers
|
||||
cout<<" DATA_IS_REALLOCATED!"<<endl;
|
||||
cout<<" realloc_offset="<<realloc_offset<<endl;
|
||||
for(int i = 0; i < _count; i++)
|
||||
{
|
||||
cout<<" _base_pointer["<<i<<"]="<<(void*)_base_pointer[i]<<endl;
|
||||
_base_pointer[i] += realloc_offset;
|
||||
cout<<" ->_base_pointer["<<i<<"]="<<(void*)_base_pointer[i]<<endl;
|
||||
}
|
||||
}
|
||||
|
||||
// manage stack itself
|
||||
_base_pointer[_count++]=(object*)_current;
|
||||
_base_pointer[_count++] = (object*)_current;
|
||||
allocated = (object*)_current;
|
||||
_current += size;
|
||||
|
||||
|
|
Loading…
Reference in a new issue