summaryrefslogtreecommitdiff
path: root/src/include/memory_chunk.h
diff options
context:
space:
mode:
authorMatias Larsson <matias.larsson@matthews.se>2022-11-24 18:22:55 +0100
committerPeng Wu <alexepico@gmail.com>2022-11-29 16:16:21 +0800
commit8bcea4710bd328ef74dc852d9fdffb3c47ed8abe (patch)
tree88a92418b2408d31aeb5ae21d04a5ae1c4ce4a8d /src/include/memory_chunk.h
parent521340f02907dae96f6f13dbf7997a34ebe864f2 (diff)
downloadlibpinyin-8bcea4710bd328ef74dc852d9fdffb3c47ed8abe.tar.gz
Fix libpinyin crash on ARMv7
Fix the alignment trap in get_unigram_frequency(). Fix also other places where this same trap could happen (depending on compiler and surrounding code). The trap happened when the ARM GCC generated a SIMD instruction (specifically VLDR) to load 32 bits in a single instruction, and when the memory address was not aligned to 32 bits. VLDR traps if the address is not aligned. GCC generated the instruction because of the cast to uint32 from the address. The fix is to allocate a uint32 variable in stack and use memcpy to copy the data to that variable. This way we ensure that appropriate instructions are generated. **Links** About the issue with GCC: https://trust-in-soft.com/blog/2020/04/06/gcc-always-assumes-aligned-pointer-accesses/ How Linux does it: https://elixir.bootlin.com/linux/v5.10.155/source/include/linux/unaligned/memmove.h#L13 ARM documentation: https://documentation-service.arm.com/static/5f8dc043f86e16515cdbbc92?token= See 'A3.2.1 Unaligned data access'
Diffstat (limited to 'src/include/memory_chunk.h')
-rw-r--r--src/include/memory_chunk.h34
1 files changed, 34 insertions, 0 deletions
diff --git a/src/include/memory_chunk.h b/src/include/memory_chunk.h
index c106dba..baa5679 100644
--- a/src/include/memory_chunk.h
+++ b/src/include/memory_chunk.h
@@ -290,6 +290,25 @@ public:
}
/**
+ * MemoryChunk::set_content:
+ * @offset: the offset in this MemoryChunk.
+ * @data: the data to be copied.
+ * @returns: whether the data is copied successfully.
+ *
+ * Data are written directly to the memory area in this MemoryChunk.
+ *
+ */
+ template <typename T>
+ bool set_content(size_t offset, T data){
+ const size_t len = sizeof(data);
+ size_t cursize = std_lite::max(size(), offset + len);
+ ensure_has_space(offset + len);
+ memmove(m_data_begin + offset, &data, len);
+ m_data_end = m_data_begin + cursize;
+ return true;
+ }
+
+ /**
* MemoryChunk::append_content:
* @data: the begin of the data to be copied.
* @len: the length of the data to be copied.
@@ -357,6 +376,21 @@ public:
}
/**
+ * MemoryChunk::get_content:
+ * @offset: the offset in this MemoryChunk.
+ * @returns: the content
+ *
+ * Get the content in this MemoryChunk.
+ *
+ */
+ template <typename T>
+ T get_content(size_t offset) const {
+ T value;
+ memcpy(&value, m_data_begin + offset, sizeof(value));
+ return value;
+ }
+
+ /**
* MemoryChunk::compact_memory:
*
* Compact memory, reduce the size.