From: Ulya Trofimovich Date: Mon, 31 Jul 2017 12:59:51 +0000 (+0100) Subject: Fixed unaligned memory access. X-Git-Tag: 1.0~33 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=c7b8af0f6134c7675e5559165be5514a17286ae8;p=re2c Fixed unaligned memory access. With CXXFLAGS='-fsanitize=undefined' GCC complains about unaligned access: if custom allocator is used to allocate structs or other alignment-sensitive things, then it must take care of the alignment (for example, add padding to all unaligned blocks of memory which it allocates). --- diff --git a/re2c/src/dfa/tcmd.h b/re2c/src/dfa/tcmd.h index d48890bb..0badf037 100644 --- a/re2c/src/dfa/tcmd.h +++ b/re2c/src/dfa/tcmd.h @@ -33,7 +33,7 @@ static const tcid_t TCID0 = 0; class tcpool_t { - typedef slab_allocator_t<~0u, 4096> alc_t; + typedef slab_allocator_t<~0u, 4096, sizeof(void*)> alc_t; typedef lookup_t index_t; alc_t alc; diff --git a/re2c/src/re/re.h b/re2c/src/re/re.h index e7472c99..305c28b4 100644 --- a/re2c/src/re/re.h +++ b/re2c/src/re/re.h @@ -15,7 +15,7 @@ namespace re2c struct RE { - typedef slab_allocator_t<~0u, 4096> alc_t; + typedef slab_allocator_t<~0u, 4096, sizeof(void*)> alc_t; enum type_t {NIL, SYM, ALT, CAT, ITER, TAG} type; union { const Range *sym; diff --git a/re2c/src/util/slab_allocator.h b/re2c/src/util/slab_allocator.h index 4baa8c13..f34600d9 100644 --- a/re2c/src/util/slab_allocator.h +++ b/re2c/src/util/slab_allocator.h @@ -15,7 +15,8 @@ * Works ~20 times faster, than linux's glibc allocator :] */ template + uint32_t SLAB_SIZE = 1024 * 1024, + size_t ALIGN = 1> class slab_allocator_t { typedef std::vector slabs_t; @@ -33,6 +34,9 @@ public: { char *result; + /* alignment */ + size += ALIGN - size % ALIGN; + /* very large objects */ if (size > MAXIMUM_INLINE) { result = static_cast(malloc(size));