From 4d394e845efc6267be2b1a36a310fea8af4fa7de Mon Sep 17 00:00:00 2001 From: Louis Rubet Date: Sun, 23 Apr 2017 15:49:28 +0200 Subject: [PATCH] #34: stack refactoring --- src/rpn-stack.h | 42 +++++++++++++++++++++++------------------- src/stack.h | 49 +++++++++++++++---------------------------------- 2 files changed, 38 insertions(+), 53 deletions(-) diff --git a/src/rpn-stack.h b/src/rpn-stack.h index 4fcba65..42c40bf 100644 --- a/src/rpn-stack.h +++ b/src/rpn-stack.h @@ -1,13 +1,17 @@ +stack _stack_manip; + // void swap(void) { MIN_ARGUMENTS(2); - _stack->copy_obj_to_local(0, 0); - _stack->copy_obj_to_local(1, 1); + + stack::copy_and_push_back(*_stack, _stack->size()-1, _stack_manip); + stack::copy_and_push_back(*_stack, _stack->size()-2, _stack_manip); (void)_stack->pop_back(); (void)_stack->pop_back(); - _stack->push_obj_from_local(0); - _stack->push_obj_from_local(1); + stack::copy_and_push_back(_stack_manip, 0, *_stack); + stack::copy_and_push_back(_stack_manip, 1, *_stack); + _stack_manip.erase(); } void drop(void) @@ -32,17 +36,14 @@ void erase(void) void dup(void) { MIN_ARGUMENTS(1); - _stack->copy_obj_to_local(0, 0); - _stack->push_obj_from_local(0); + stack::copy_and_push_back(*_stack, _stack->size()-1, *_stack); } void dup2(void) { MIN_ARGUMENTS(2); - _stack->copy_obj_to_local(0, 0); - _stack->copy_obj_to_local(1, 1); - _stack->push_obj_from_local(1); - _stack->push_obj_from_local(0); + stack::copy_and_push_back(*_stack, _stack->size()-2, *_stack); + stack::copy_and_push_back(*_stack, _stack->size()-2, *_stack); } void pick(void) @@ -55,25 +56,28 @@ void pick(void) // treat stack depth errors if ((to_pick == 0) || (to_pick > _stack->size())) { - ERR_CONTEXT(ret_missing_operand); + ERR_CONTEXT(ret_out_of_range); return; } - _stack->copy_obj_to_local(to_pick - 1, 0); - _stack->push_obj_from_local(0); + + stack::copy_and_push_back(*_stack, _stack->size()-to_pick, *_stack); } void rot(void) { MIN_ARGUMENTS(3); - _stack->copy_obj_to_local(0, 0); - _stack->copy_obj_to_local(1, 1); - _stack->copy_obj_to_local(2, 2); + + //TODO could be optimized + stack::copy_and_push_back(*_stack, _stack->size()-3, _stack_manip); + stack::copy_and_push_back(*_stack, _stack->size()-2, _stack_manip); + stack::copy_and_push_back(*_stack, _stack->size()-1, _stack_manip); (void)_stack->pop_back(); (void)_stack->pop_back(); (void)_stack->pop_back(); - _stack->push_obj_from_local(1); - _stack->push_obj_from_local(0); - _stack->push_obj_from_local(2); + stack::copy_and_push_back(_stack_manip, 2, *_stack); + stack::copy_and_push_back(_stack_manip, 1, *_stack); + stack::copy_and_push_back(_stack_manip, 0, *_stack); + _stack_manip.erase(); } void depth(void) diff --git a/src/stack.h b/src/stack.h index b19031c..f7c10ac 100644 --- a/src/stack.h +++ b/src/stack.h @@ -13,20 +13,10 @@ using namespace std; // allocation base size #define ALLOC_STACK_CHUNK (64*1024) #define ALLOC_BLOB_CHUNK (64*1024) -#define LOCAL_COPY_PLACES 3 -#define LOCAL_COPY_SIZE 128 // class stack { -private: - struct local_copy - { - unsigned int length; - int type; - int blob; - }; - public: stack() { @@ -34,17 +24,27 @@ public: _blob = new char[ALLOC_BLOB_CHUNK]; _total_size = ALLOC_STACK_CHUNK; _total_blob_size = ALLOC_BLOB_CHUNK; - _current = _base; - _current_blob = _blob; - _count = 0; - } + erase(); + } virtual ~stack() { delete[] _base; delete[] _blob; } - + + void erase() + { + _current = _base; + _current_blob = _blob; + _vpointer.clear(); + _vpointer_blob.clear(); + _vlen.clear(); + _vlen_blob.clear(); + _vtype.clear(); + _count = 0; + } + void push_back(void* obj, unsigned int size, int type = 0, bool dont_copy = false, void* blob = NULL, unsigned int blob_size = 0) { void* allocated_blob; @@ -222,23 +222,6 @@ public: return 0; } - // local objects copy - void copy_obj_to_local(unsigned int index, unsigned int to_place) - { - assert(to_place < LOCAL_COPY_PLACES); - struct local_copy* local = (struct local_copy*)_places[to_place]; - local->length = get_len(index); - local->type= get_type(index); - memcpy(&local->blob, get_obj(index), local->length); - } - - void push_obj_from_local(unsigned int from_place) - { - assert(from_place < LOCAL_COPY_PLACES); - struct local_copy* local = (struct local_copy*)_places[from_place]; - push_back(&local->blob, local->length, local->type); - } - private: char* _base; char* _blob; @@ -253,8 +236,6 @@ private: unsigned int _count;// =_vlen.size()=_vpointer.size()=_vtype.size() unsigned int _total_size;//total allocated size in bytes unsigned int _total_blob_size;//total allocated blob size in bytes - - char _places[LOCAL_COPY_PLACES][LOCAL_COPY_SIZE]; }; //