#63: debug vars realloc problem

This commit is contained in:
Louis Rubet 2017-05-24 23:32:25 +02:00
parent bd2ec62c87
commit 7bf122d18f
2 changed files with 26 additions and 18 deletions

View file

@ -368,7 +368,7 @@ void object::show(FILE* stream)
fprintf(stream, "<<%s>>", ((oprogram*)this)->_value); fprintf(stream, "<<%s>>", ((oprogram*)this)->_value);
break; break;
case cmd_symbol: case cmd_symbol:
fprintf(stream, "'%s'", ((oprogram*)this)->_value); fprintf(stream, "'%s'", ((symbol*)this)->_value);
break; break;
case cmd_keyword: case cmd_keyword:
case cmd_branch: case cmd_branch:

View file

@ -193,14 +193,18 @@ public:
virtual ~heap() { } virtual ~heap() { }
object* add(const string name, object* obj, unsigned int size) object* add(const string name, object* obj, unsigned int size)
{ {
object* local = _map[name]; map<string, unsigned int>::iterator i = _map.find(name);
object* local = NULL;
// variable does not exist in heap or already exists but its size is too short -> allocate // variable does not exist in heap or already exists but its size is too short -> allocate
if (i!=_map.end())
local = seq_obj(i->second);
if (local==NULL || (local!=NULL && size>local->_size)) if (local==NULL || (local!=NULL && size>local->_size))
{ {
copy_and_push_back(obj, *this, size); copy_and_push_back(obj, *this, size);
_map[name] = back(); _map[name] = this->size()-1;
} }
else else
{ {
@ -217,12 +221,12 @@ public:
bool get(const string name, object*& obj, unsigned int& size) bool get(const string name, object*& obj, unsigned int& size)
{ {
bool ret = false; bool ret = false;
map<string, object*>::iterator i = _map.find(name); map<string, unsigned int>::iterator i = _map.find(name);
if (i!=_map.end() && i->second!=NULL) if (i!=_map.end())
{ {
obj = i->second; obj = seq_obj(i->second);
size = i->second->_size; size = obj->_size;
ret = true; ret = true;
} }
return ret; return ret;
@ -231,14 +235,18 @@ public:
bool replace_value(const string name, object* obj, unsigned int size) bool replace_value(const string name, object* obj, unsigned int size)
{ {
bool ret=false; bool ret=false;
map<string, object*>::iterator i = _map.find(name); map<string, unsigned int>::iterator i = _map.find(name);
if (i!=_map.end() && i->second!=NULL && size==i->second->_size) if (i!=_map.end())
{ {
(void)memcpy(i->second, obj, size); object* obj_dst = seq_obj(i->second);
if (i->second->_type == cmd_number) if (size<=obj_dst->_size)
((number*)i->second)->_value.set_significand(((number*)i->second)+1); {
ret = true; (void)memcpy(obj_dst, obj, size);
if (obj_dst->_type == cmd_number)
((number*)obj_dst)->_value.set_significand(((number*)obj_dst)+1);
ret = true;
}
} }
} }
@ -252,12 +260,12 @@ public:
if (num>=0 && num<(int)_map.size()) if (num>=0 && num<(int)_map.size())
{ {
object* local; object* local;
map<string, object*>::iterator i= _map.begin(); map<string, unsigned int>::iterator i= _map.begin();
for(int j = 0; j < num; j++) for(int j = 0; j < num; j++)
i++; i++;
local = (object*)i->second; local = (object*)seq_obj(i->second);
name = i->first; name = i->first;
obj = local; obj = local;
size = local->_size; size = local->_size;
@ -269,7 +277,7 @@ public:
bool erase(const string& name) bool erase(const string& name)
{ {
map<string, object*>::iterator i = _map.find(name); map<string, unsigned int>::iterator i = _map.find(name);
bool ret = false; bool ret = false;
if (i != _map.end()) if (i != _map.end())
@ -283,7 +291,7 @@ public:
unsigned int size() { return _map.size(); } unsigned int size() { return _map.size(); }
private: private:
map<string, object*> _map; map<string, unsigned int> _map;
}; };
#endif // __stack_h__ #endif // __stack_h__