diff options
Diffstat (limited to 'src/include/memory_chunk.h')
-rw-r--r-- | src/include/memory_chunk.h | 38 |
1 files changed, 34 insertions, 4 deletions
diff --git a/src/include/memory_chunk.h b/src/include/memory_chunk.h index 044fa0d..713fb5c 100644 --- a/src/include/memory_chunk.h +++ b/src/include/memory_chunk.h @@ -33,6 +33,7 @@ #define LIBPINYIN_USE_MMAP #endif #include "stl_lite.h" +#include "pinyin_utils.h" namespace pinyin{ @@ -72,7 +73,7 @@ private: munmap(m_data_begin - header, header + capacity()); #endif else - assert(FALSE); + abort(); } @@ -134,7 +135,7 @@ private: /* checksum for aligned parts. */ guint32 index = 0; for (; index < aligns; index += sizeof(guint32)) { - const char * p = data + index; + const unsigned char * p = (const unsigned char *)data + index; /* use little endian here. */ guint32 item = *p | *(p + 1) << 8 | @@ -146,7 +147,7 @@ private: /* checksum for remained parts. */ guint32 shift = 0; for (; index < length; index++) { - const char * p = data + index; + const unsigned char * p = (const unsigned char *)data + index; guint32 item = *p << shift; shift += 8; @@ -290,6 +291,20 @@ 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){ + return set_content(offset, &data, sizeof(T)); + } + + /** * MemoryChunk::append_content: * @data: the begin of the data to be copied. * @len: the length of the data to be copied. @@ -349,7 +364,7 @@ public: * Get the content in this MemoryChunk. * */ - bool get_content(size_t offset, void * buffer, size_t length){ + bool get_content(size_t offset, void * buffer, size_t length) const { if ( size() < offset + length ) return false; memcpy( buffer, m_data_begin + offset, length); @@ -357,6 +372,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; + check_result(get_content(offset, &value, sizeof(T))); + return value; + } + + /** * MemoryChunk::compact_memory: * * Compact memory, reduce the size. |