#include <memory.h>
#include <exception>
#include <stdexcept>
#include <string>
#include "ht_memory.h"
#define nullptr 0
union endian_check
{
unsigned short us;
unsigned char sz[2];
};
union endian_check const g_ec = {0x0001};
inline unsigned int get_trimmed_size(const unsigned int& u_expect_size, const unsigned int& u_trim_size)
{
return (u_expect_size / u_trim_size + u_expect_size % u_trim_size == 0 ? 0 : 1)*u_trim_size;
}
ht_memory::endian system_endian()
{
return g_ec.sz[0] == 0x01 ? ht_memory::little_endian : ht_memory::big_endian;
}
ht_memory::ht_memory(const unsigned int & u_expand_size)
:m_sz_buf(nullptr)
, m_u_buf_len(0u)
, m_u_read_idx(0u)
, m_u_write_idx(0u)
, m_e_endian(system_endian())
, m_e_strategy(buf_flexable)
, m_u_expand_size((u_expand_size == 0 || u_expand_size > 1024 * 1024 * 128) ? 512 : u_expand_size)
{
}
ht_memory::ht_memory(const endian& e_endian, const unsigned int& u_expand_size)
:m_sz_buf(nullptr)
, m_u_buf_len(0u)
, m_u_read_idx(0u)
, m_u_write_idx(0u)
, m_e_endian(e_endian)
, m_e_strategy(buf_flexable)
, m_u_expand_size((u_expand_size==0||u_expand_size>1024*1024*128)?512:u_expand_size)
{
}
ht_memory::ht_memory(const ht_memory & other)
:m_sz_buf(nullptr)
, m_u_buf_len(other.m_u_buf_len)
, m_u_read_idx(other.m_u_read_idx)
, m_u_write_idx(other.m_u_write_idx)
, m_e_endian(other.m_e_endian)
, m_e_strategy(other.m_e_strategy)
, m_u_expand_size(other.m_u_expand_size)
{
/* 如果是固定内存策略的则赋值指针即可,如果是可扩展存储区则复制存储区 */
if (other.m_e_strategy == buf_stable)
{
m_sz_buf = other.m_sz_buf;
}
else if (other.m_e_strategy == buf_flexable)
{
m_sz_buf = reinterpret_cast<unsigned char*>(ht_realloc(m_sz_buf, other.m_u_buf_len));
memset(m_sz_buf, 0, other.m_u_buf_len);
memcpy(m_sz_buf, other.m_sz_buf, other.m_u_buf_len);
}
}
ht_memory & ht_memory::operator=(const ht_memory & other)
{
if (other.m_e_strategy == buf_stable)
{
if (m_sz_buf)
{
ht_free(m_sz_buf);
}
m_sz_buf = other.m_sz_buf;
/* 进一步进行处理 */
m_u_buf_len = other.m_u_buf_len;
m_u_read_idx = other.m_u_read_idx;
m_u_write_idx = other.m_u_write_idx;
m_e_endian = other.m_e_endian;
m_e_strategy = other.m_e_strategy;
m_u_expand_size = other.m_u_expand_size;
}
else if (other.m_e_strategy == buf_flexable)
{
if (m_u_buf_len < other.size())
{
m_u_buf_len = get_trimmed_size(other.size(), other.m_u_expand_size);
m_sz_buf = reinterpret_cast<unsigned char*>(ht_realloc(m_sz_buf, m_u_buf_len));
}
memset(m_sz_buf, 0, m_u_buf_len);
memcpy(m_sz_buf, other.buf(), other.size());
/* 进一步进行处理 */
m_u_read_idx = 0;
m_u_write_idx = other.size();
m_e_endian = other.m_e_endian;
m_e_strategy = other.m_e_strategy;
m_u_expand_size = other.m_u_expand_size;
}
return *this;
}
#if __cplusplus >= 201103L
ht_memory::ht_memory(ht_memory&& other)
:m_sz_buf(nullptr)
, m_u_buf_len(other.m_u_buf_len)
, m_u_read_idx(other.m_u_read_idx)
, m_u_write_idx(other.m_u_write_idx)
, m_e_endian(other.m_e_endian)
, m_e_strategy(other.m_e_strategy)
, m_u_expand_size(other.m_u_expand_size)
{
/* 如果是固定内存策略的则赋值指针即可,如果是可扩展存储区则复制存储区 */
if (other.m_e_strategy == buf_stable)
{
m_sz_buf = other.m_sz_buf;
}
else if (other.m_e_strategy == buf_flexable)
{
m_sz_buf = reinterpret_cast<unsigned char*>(ht_realloc(m_sz_buf, other.m_u_buf_len));
memset(m_sz_buf, 0, other.m_u_buf_len);
memcpy(m_sz_buf, other.m_sz_buf, other.m_u_buf_len);
}
other.m_sz_buf = nullptr;
other.m_u_buf_len = 0;
other.m_u_read_idx = 0;
other.m_u_write_idx = 0;
other.m_u_expand_size = 0;
}
ht_memory & ht_memory::operator=(ht_memory && other)
{
if (other.m_e_strategy == buf_stable)
{
if (m_sz_buf)
{
ht_free(m_sz_buf);
}
m_sz_buf = other.m_sz_buf;
/* 进一步进行处理 */
m_u_buf_len = other.m_u_buf_len;
m_u_read_idx = other.m_u_read_idx;
m_u_write_idx = other.m_u_write_idx;
m_e_endian = other.m_e_endian;
m_e_strategy = other.m_e_strategy;
m_u_expand_size = other.m_u_expand_size;
}
else if (other.m_e_strategy == buf_flexable)
{
if (m_u_buf_len < other.size())
{
m_u_buf_len = get_trimmed_size(other.size(), other.m_u_expand_size);
m_sz_buf = reinterpret_cast<unsigned char*>(ht_realloc(m_sz_buf, m_u_buf_len));
}
memset(m_sz_buf, 0, m_u_buf_len);
memcpy(m_sz_buf, other.buf(), other.size());
/* 进一步进行处理 */
m_u_read_idx = 0;
m_u_write_idx = other.size();
m_e_endian = other.m_e_endian;
m_e_strategy = other.m_e_strategy;
m_u_expand_size = other.m_u_expand_size;
}
other.m_sz_buf = nullptr;
other.m_u_buf_len = 0;
other.m_u_read_idx = 0;
other.m_u_write_idx = 0;
other.m_u_expand_size = 0;
return *this;
}
#endif
void ht_memory::clone(const ht_memory & other)
{
if (m_u_buf_len < other.size())
{
m_u_buf_len = get_trimmed_size(other.size(), other.m_u_expand_size);
m_sz_buf = reinterpret_cast<unsigned char*>(ht_realloc(m_sz_buf, m_u_buf_len));
}
memset(m_sz_buf, 0, m_u_buf_len);
memcpy(m_sz_buf, other.buf(), other.size());
/* 进一步进行处理 */
m_u_read_idx = 0;
m_u_write_idx = other.size();
m_e_endian = other.m_e_endian;
m_e_strategy = other.m_e_strategy;
m_u_expand_size = other.m_u_expand_size;
}
void ht_memory::get_buf_from(ht_memory & other)
{
ht_free(m_sz_buf);
m_u_buf_len = m_u_read_idx = m_u_write_idx = 0u;
m_u_buf_len = other.m_u_buf_len;
m_e_endian = other.m_e_endian;
m_e_strategy = other.m_e_strategy;
m_u_expand_size = other.m_u_expand_size;
m_sz_buf = reinterpret_cast<unsigned char*>(other.abort_memory(m_u_read_idx, m_u_write_idx));
}
ht_memory::~ht_memory()
{
if (m_sz_buf)
{
ht_free(m_sz_buf);
}
}
void * ht_memory::ht_realloc(void* p, const unsigned int& u_expected_size)
{
if (m_e_strategy == buf_flexable)
{
void* p1 = realloc(p, u_expected_size);
return p1;
}
return nullptr;
}
void ht_memory::ht_free(void * p)
{
if (p && m_e_strategy == buf_flexable)
free(p);
}
unsigned char & ht_memory::operator[](const unsigned int & idx) const
{
if (idx >= m_u_buf_len)
{
throw std::runtime_error(std::string("ht_memory::operator[](const unsigned int&) const越界访问"));
}
return m_sz_buf[idx];
}
unsigned int ht_memory::read_size() const
{
return m_u_read_idx;
}
unsigned int ht_memory::write_size() const
{
return m_u_write_idx;
}
unsigned int ht_memory::size() const
{
if (m_u_read_idx > m_u_write_idx)
{
throw std::runtime_error("ht_memory::size已读长度超过已写长度");
}
return m_u_write_idx - m_u_read_idx;
}
void ht_memory::load(void * p, const unsigned int & u_len, const strategy& e_strategy)
{
m_e_strategy = e_strategy;
m_u_write_idx = u_len;
m_u_read_idx = 0;
if (e_strategy == buf_flexable)
{
/* 判断长度是否超标 */
if (u_len > m_u_buf_len)
{
unsigned char* p = reinterpret_cast<unsigned char*>(ht_realloc(m_sz_buf, u_len));
if (u_len == 0)
{
/* 原指针失效,属于未定义行为,可能是程序有问题 */
return;
}
if (!p)
{
/* 内存分配失败,原空间依然有效,但是内存已经不足 */
return;
}
memset(p, 0, u_len);
m_sz_buf = p;
m_u_buf_len = u_len;
}
/* 在write位置写入数据 */
memcpy(m_sz_buf, p, u_len);
m_u_write_idx = u_len;
}
else
{
m_sz_buf = (unsigned char*)p;
m_u_write_idx = u_len;
m_u_buf_len = u_len;
}
}
void ht_memory::cload(const void * p, const unsigned int & u_len)
{
m_e_strategy = buf_flexable;
m_u_write_idx = u_len;
m_u_read_idx = 0;
/* 判断长度是否超标 */
if (u_len > m_u_buf_len)
{
unsigned char* p = reinterpret_cast<unsigned char*>(ht_realloc(m_sz_buf, u_len));
if (u_len == 0)
{
/* 原指针失效,属于未定义行为,可能是程序有问题 */
return;
}
if (!p)
{
/* 内存分配
没有合适的资源?快使用搜索试试~ 我知道了~
net-tool网络库
共71个文件
sample:13个
cpp:6个
h:5个
需积分: 5 0 下载量 74 浏览量
2022-12-23
14:34:38
上传
评论
收藏 167KB ZIP 举报
温馨提示
net_tool网络库
资源推荐
资源详情
资源评论
收起资源包目录
net_tool.zip (71个子文件)
net_tool
net_tool.sln 2KB
net_tool_counter
net_tool_counter.vcxproj.filters 2KB
net_tool_counter.vcxproj 7KB
counter_main.cpp 989B
.git
index 2KB
HEAD 23B
refs
heads
master 41B
tags
COMMIT_EDITMSG 13B
objects
01
c60ef81af0488e9a40482d025a56a936e13aa3 616B
1e
ede3dc97e5b9327b0749a84f49a447f72361ed 562B
c1
8b5ab3068051e5049b70bb205bd0c0ea4a3d8b 666B
7d
adb385bd4170f68d6cda0c39db162d445e1ee7 3KB
e6
118df494e8da550cbe1764f372aae1fb64c882 136B
bd
89ac8eaeeccde0a9a3289ef48640888c86fcfc 3KB
b0
aa8995532f95e9fd7d4fbbc509dce10a52fcce 1KB
3e
34b261316a6007738efb7c61ba09bf3f8fd3e3 1KB
86
234f9b16b3b9517574db42d973fb853deeaa4a 46KB
45
ef7495ed2bff693997aa048428759d5cb2e5d6 1KB
37
1f24d551d699fc633a1bf6eacdd255b941c607 168B
8cf98550526ad0a201b9a4db0ba7f0df512751 510B
55
c82e70e9384ea0ec69d2a492add1c89f9a782c 431B
5c
5d268e3feb74d2075c1ca0ccc89c74228ce87a 459B
d0
fd47e5eb694b34f26d8fd91efb7cf6caad5737 39B
d9
66e7e13f6d1700ad34e2927159b927a49391d8 2KB
53
3f2f061810bed442d4de072f5b1b0f013b357b 431B
8a
54a1fd483e974b1d39ffe18d431e0ebb70020a 121B
a5
bcad0a2cfbbbea3bc0adbdc3a0d9ce74fc0c66 671B
4b
019e2755b02fcf6f9d17ef3834552799266ad4 1KB
e2
0a6790a12032b94ffd0ae4d9449eeb85c81a87 552B
pack
f6
6dce2ac6c187091a03c4abbd43e6564453e66c 790B
89
ba4e33470dedc2f6c789d67675b10f977840b1 1KB
info
e4
47f323f49c232c89122715603568b7756a4451 159B
25
6311199eda4a83fe4b74381925b0ca9fe0bb8b 716B
785f1cb452bb8ecbf8d33970afeed5cac31a32 736B
62
2a059e8b5c520f67fb3ee62455eba102112ca6 619B
a0
b7509a2798a93d0866f579bea58e170395f3bb 85B
d6
1d1affcbe9fa01f9fba77c66575074e02a984b 2KB
47
33ebefb68e3f760a944d0f9ce429d42f90e9ee 752B
98
b61c7b6792730d09e7b9b927a1b37b3556e983 500B
description 73B
info
exclude 240B
logs
HEAD 300B
refs
heads
master 300B
hooks
post-update.sample 189B
prepare-commit-msg.sample 1KB
commit-msg.sample 896B
pre-receive.sample 544B
update.sample 4KB
pre-commit.sample 2KB
pre-rebase.sample 5KB
applypatch-msg.sample 478B
fsmonitor-watchman.sample 5KB
push-to-checkout.sample 3KB
pre-applypatch.sample 424B
pre-push.sample 1KB
pre-merge-commit.sample 416B
config 130B
net_tool
id_generator.h 1KB
net_tool.vcxproj 7KB
tcp_mgr.h 1KB
ts_ptr.hpp 2KB
tcp_connect.cpp 4KB
main_net.cpp 1KB
tcp_mgr.cpp 26B
tcp_listen.h 3KB
tcp_connect.h 1KB
tcp_listen.cpp 9KB
ht_memory.cpp 11KB
ht_memory.h 6KB
net_tool.vcxproj.filters 2KB
程序说明.vsdx 49KB
共 71 条
- 1
资源评论
腾昵猫
- 粉丝: 429
- 资源: 5
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功