From 396b71f4ddcbcdd7772bb9c02be8df642b7a1be2 Mon Sep 17 00:00:00 2001 From: Modestas Vainius Date: Thu, 2 Sep 2010 10:51:13 -0300 Subject: Fixes various memory alignment issues which cause generator to crash on alignment-sensitive architectures. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Reviewer: Luciano Wolf Renato Araújo --- parser/ast.h | 2 +- parser/list.h | 2 +- parser/rpp/pp-symbol.h | 9 +++++++-- parser/rxx_allocator.h | 18 ++++++++++++++++++ parser/smallobject.h | 6 ++++++ 5 files changed, 33 insertions(+), 4 deletions(-) (limited to 'parser') diff --git a/parser/ast.h b/parser/ast.h index aa98ee4db..d98497649 100644 --- a/parser/ast.h +++ b/parser/ast.h @@ -862,7 +862,7 @@ struct QEnumsAST : public DeclarationAST template _Tp *CreateNode(pool *memory_pool) { - _Tp *node = reinterpret_cast<_Tp*>(memory_pool->allocate(sizeof(_Tp))); + _Tp *node = reinterpret_cast<_Tp*>(memory_pool->allocate(sizeof(_Tp), strideof(_Tp))); node->kind = _Tp::__node_kind; return node; } diff --git a/parser/list.h b/parser/list.h index 64aef452f..fe0e6276a 100644 --- a/parser/list.h +++ b/parser/list.h @@ -35,7 +35,7 @@ struct ListNode { mutable const ListNode *next; static ListNode *create(const Tp &element, pool *p) { - ListNode *node = new(p->allocate(sizeof(ListNode))) ListNode(); + ListNode *node = new(p->allocate(sizeof(ListNode), strideof(ListNode))) ListNode(); node->element = element; node->index = 0; node->next = node; diff --git a/parser/rpp/pp-symbol.h b/parser/rpp/pp-symbol.h index eef668379..8078aba8c 100644 --- a/parser/rpp/pp-symbol.h +++ b/parser/rpp/pp-symbol.h @@ -39,6 +39,11 @@ class pp_symbol static rxx_allocator__allocator; return __allocator; } + static rxx_allocator &ppfs_allocator_instance () + { + static rxx_allocator__ppfs_allocator; + return __ppfs_allocator; + } public: static int &N() { @@ -52,7 +57,7 @@ public: memcpy(data, __data, __size); data[__size] = '\0'; - char *where = allocator_instance().allocate(sizeof(pp_fast_string)); + pp_fast_string *where = ppfs_allocator_instance ().allocate (sizeof (pp_fast_string)); return new(where) pp_fast_string(data, __size); } @@ -71,7 +76,7 @@ public: std::copy(__first, __last, data); data[__size] = '\0'; - char *where = allocator_instance().allocate(sizeof(pp_fast_string)); + pp_fast_string *where = ppfs_allocator_instance ().allocate (sizeof (pp_fast_string)); return new(where) pp_fast_string(data, __size); } diff --git a/parser/rxx_allocator.h b/parser/rxx_allocator.h index f0159e936..8325edbdf 100644 --- a/parser/rxx_allocator.h +++ b/parser/rxx_allocator.h @@ -24,6 +24,18 @@ #include #include +// Stride calculation +template +struct Tchar { + T t; + char c; +}; + +#define strideof(T) \ + ((sizeof(Tchar) > sizeof(T)) ? \ + sizeof(Tchar)-sizeof(T) : sizeof(T)) + + /**The allocator which uses fixed size blocks for allocation of its elements. Block size is currently 64k, allocated space is not reclaimed, if the size of the element being allocated extends the amount of free @@ -93,6 +105,12 @@ public: return p; } + pointer allocate(size_type __n, size_type stride, const void* = 0) { + if (reinterpret_cast(_M_current_block + _M_current_index) % stride > 0) + _M_current_index += stride - reinterpret_cast(_M_current_block + _M_current_index) % stride; + return allocate(__n); + } + /**Deallocate does nothing in this implementation.*/ void deallocate(pointer /*__p*/, size_type /*__n*/) {} diff --git a/parser/smallobject.h b/parser/smallobject.h index 52cdc232f..a9eb4990b 100644 --- a/parser/smallobject.h +++ b/parser/smallobject.h @@ -35,6 +35,7 @@ class pool public: inline void *allocate(std::size_t __size); + inline void *allocate(std::size_t __size, std::size_t __stride); }; inline void *pool::allocate(std::size_t __size) @@ -42,6 +43,11 @@ inline void *pool::allocate(std::size_t __size) return __alloc.allocate(__size); } +inline void *pool::allocate(std::size_t __size, std::size_t __stride) +{ + return __alloc.allocate(__size, __stride); +} + #endif // kate: space-indent on; indent-width 2; replace-tabs on; -- cgit v1.2.3