From dc0061c724205b41fe3ec9b0363b20e811343db9 Mon Sep 17 00:00:00 2001 From: Louis Rubet Date: Mon, 22 May 2017 18:22:15 +0200 Subject: [PATCH] #63: removing vectors from stack --- src/rpn-branch.h | 2 +- src/rpn-program.h | 2 +- src/stack.h | 158 ++++++++++++++++++++-------------------------- 3 files changed, 72 insertions(+), 90 deletions(-) diff --git a/src/rpn-branch.h b/src/rpn-branch.h index e951d30..4078f79 100644 --- a/src/rpn-branch.h +++ b/src/rpn-branch.h @@ -105,7 +105,7 @@ int rpn_for(branch& myobj) else { // store symbol with first value - _local_heap.add(sym->_value, myobj.farg1, myobj.farg1->size()); + _local_heap.add(sym->_value, (object*)myobj.farg1, myobj.farg1->size()); (void)_stack->pop_back(); ret = myobj.arg1 + 1; } diff --git a/src/rpn-program.h b/src/rpn-program.h index e86ac8f..479f22a 100644 --- a/src/rpn-program.h +++ b/src/rpn-program.h @@ -123,7 +123,7 @@ int inprog(branch& myobj) heap local_heap; for (unsigned int i = myobj.arg1 + count_symbols; i > myobj.arg1; i--) { - local_heap.add(string(((symbol*)seq_obj(i))->_value), _stack->get_obj(0), _stack->get_len(0), _stack->get_type(0)); + local_heap.add(string(((symbol*)seq_obj(i))->_value), _stack->get_obj(0), _stack->get_len(0)); (void)_stack->pop_back(); } diff --git a/src/stack.h b/src/stack.h index bb57ca1..412f714 100644 --- a/src/stack.h +++ b/src/stack.h @@ -2,9 +2,6 @@ #define __stack_h__ #include - -#include -#include #include using namespace std; @@ -18,7 +15,9 @@ public: stack() { _base = NULL; + _base_pointer = NULL; _total_size = 0; + _total_count_pointer = 0; erase(); } @@ -26,12 +25,13 @@ public: { if (_base != NULL) free(_base); + if (_base_pointer != NULL) + free(_base_pointer); } void erase() { _current = _base; - _vpointer.clear(); _count = 0; } @@ -43,7 +43,7 @@ public: memcpy(allocated, from.seq_obj(index_from), from.seq_len(index_from)); if (allocated->_type == cmd_number) - ((number*)allocated)->_value.set_significand(((number*)allocated)+1); + ((number*)allocated)->_value.set_significand(((number*)allocated) + 1); } // @@ -54,7 +54,7 @@ public: memcpy(allocated, from, size); if (allocated->_type == cmd_number) - ((number*)allocated)->_value.set_significand(((number*)allocated)+1); + ((number*)allocated)->_value.set_significand(((number*)allocated) + 1); } object* allocate_back(unsigned int size, cmd_type_t type) @@ -68,19 +68,25 @@ public: _total_size += ALLOC_STACK_CHUNK; _base = (char*)realloc(_base, _total_size); _current = _base + offset; + + //TODO si realloc alors les pointeurs doivent etre recalculés ! + } + if ((_count+1) > _total_count_pointer) + { + _base_pointer = (object**)realloc(_base_pointer, (_total_count_pointer * sizeof(object*)) + ALLOC_STACK_CHUNK); + _total_count_pointer += (ALLOC_STACK_CHUNK / sizeof(object)); } // manage stack itself - _vpointer.push_back((object*)_current); + _base_pointer[_count++]=(object*)_current; allocated = (object*)_current; _current += size; - _count++; // init object allocated->_type = type; allocated->_size = size; if (type == cmd_number) - ((number*)allocated)->_value.init(((number*)allocated)+1); + ((number*)allocated)->_value.init(((number*)allocated) + 1); return allocated; } @@ -91,9 +97,7 @@ public: if (_count > 0) { - _current = (char*)_vpointer[_count - 1]; - _vpointer.pop_back(); - + _current = (char*)_base_pointer[_count - 1]; _count--; back = (object*)_current; @@ -111,7 +115,7 @@ public: object* get_obj(unsigned int index) { if (index<_count) - return _vpointer[_count-index-1]; + return _base_pointer[_count - index - 1]; else return NULL; } @@ -124,7 +128,7 @@ public: object* back() { if (_count>0) - return _vpointer[_count-1]; + return _base_pointer[_count - 1]; else return NULL; } @@ -132,7 +136,7 @@ public: unsigned int get_len(unsigned int index) { if (index<_count) - return _vpointer[_count-index-1]->_size; + return _base_pointer[_count - index - 1]->_size; else return 0; } @@ -140,7 +144,7 @@ public: cmd_type_t get_type(unsigned int index) { if (index<_count) - return _vpointer[_count-index-1]->_type; + return _base_pointer[_count - index - 1]->_type; else return cmd_undef; } @@ -149,7 +153,7 @@ public: object* seq_obj(unsigned int index) { if (index<_count) - return _vpointer[index]; + return _base_pointer[index]; else return NULL; } @@ -157,7 +161,7 @@ public: unsigned int seq_len(unsigned int index) { if (index<_count) - return _vpointer[index]->_size; + return _base_pointer[index]->_size; else return 0; } @@ -165,7 +169,7 @@ public: cmd_type_t seq_type(unsigned int index) { if (index<_count) - return _vpointer[index]->_type; + return _base_pointer[index]->_type; else return cmd_undef; } @@ -173,89 +177,68 @@ public: private: char* _base; char* _current; + object** _base_pointer; - vector _vpointer;//pointer on each entry unsigned int _count;// =_vlen.size()=_vpointer.size() + unsigned int _total_count_pointer;//total number of possible pointers unsigned int _total_size;//total allocated size in bytes }; // -class heap +class heap : public stack { -private: - struct local_var - { - unsigned int length; - object obj[0]; - }; - public: heap() { } - virtual ~heap() - { - for(map::iterator i=_map.begin(); i!=_map.end(); i++) - free(i->second); - } + virtual ~heap() { } - object* add(const string name, object* obj, unsigned int size, int type = 0) - { - struct local_var* local = _map[name]; - if (local == NULL) - { - //TODO gerer les pbs de memoire - local = (struct local_var*)malloc(size + sizeof(local_var)); - _map[name] = local; - } - else if (size != local->length) - { - //TODO gerer les pbs de memoire - local = (struct local_var*)realloc(local, size + sizeof(local_var)); - _map[name] = local; - } - local->length = size; + object* add(const string name, object* obj, unsigned int size) + { + object* local = _map[name]; - if (obj != NULL) + // variable does not exist in heap or already exists but its size is too short -> allocate + if (local==NULL || (local!=NULL && size>local->_size)) { - memcpy(local->obj, obj, size); - if (local->obj->_type == cmd_number) - ((number*)local->obj)->_value.set_significand(((number*)local->obj)+1); + copy_and_push_back(obj, *this, size); + _map[name] = back(); } - - return local->obj; + else + { + // variable already exists in heap but previous was larger -> don't reallocate + // copy a whole stack entry and push it back to another stack + memcpy(local, obj, size); + if (local->_type == cmd_number) + ((number*)local)->_value.set_significand(((number*)local)+1); + } + + return local; } bool get(const string name, object*& obj, unsigned int& size) { - map::iterator i = _map.find(name); - if (i != _map.end()) + bool ret = false; + map::iterator i = _map.find(name); + + if (i!=_map.end() && i->second!=NULL) { - if (i->second != NULL) - { - obj = i->second->obj; - size = i->second->length; - } - return true; + obj = i->second; + size = i->second->_size; + ret = true; } - else - return false; + return ret; } bool replace_value(const string name, object* obj, unsigned int size) { bool ret=false; - map::iterator i = _map.find(name); + map::iterator i = _map.find(name); - if (i != _map.end()) + if (i!=_map.end() && i->second!=NULL && size==i->second->_size) { - if (i->second != NULL && size==i->second->length) - { - (void)memcpy(i->second->obj, obj, size); - if (i->second->obj->_type == cmd_number) - ((number*)i->second->obj)->_value.set_significand(((number*)i->second->obj)+1); - } - return true; + (void)memcpy(i->second, obj, size); + if (i->second->_type == cmd_number) + ((number*)i->second)->_value.set_significand(((number*)i->second)+1); + ret = true; } - return ret; } bool exist(const string name) @@ -267,19 +250,17 @@ public: { if (num>=0 && num<(int)_map.size()) { - struct local_var* local; - map::iterator i=_map.begin(); + object* local; + map::iterator i= _map.begin(); - //TODO moche moche moche + //TODO another formulation, please for(int j=0;jsecond; - assert(local != NULL); - + local = (object*)i->second; name = i->first; - obj = local->obj; - size = local->length; + obj = local; + size = local->_size; return true; } else @@ -288,20 +269,21 @@ public: bool erase(const string& name) { - map::iterator i = _map.find(name); + map::iterator i = _map.find(name); + bool ret = false; + if (i != _map.end()) { - free(i->second); _map.erase(i->first); - return true; + ret = true; } - return false; + return ret; } unsigned int size() { return _map.size(); } private: - map _map; + map _map; }; #endif // __stack_h__