lokinet/llarp/util/mem.hpp

94 lines
1.6 KiB
C++
Raw Normal View History

#pragma once
#include "buffer.hpp"
#include "mem.h"
2018-12-12 01:50:03 +00:00
#include <cctype>
#include <cstdio>
#include <memory>
namespace llarp
{
void
Zero(void* ptr, size_t sz);
2018-12-12 01:50:03 +00:00
template <typename T>
2018-12-12 01:50:03 +00:00
void
dumphex(const uint8_t* t)
2018-12-12 01:50:03 +00:00
{
size_t idx = 0;
while (idx < sizeof(T))
2018-12-12 01:50:03 +00:00
{
printf("%.2x ", t[idx++]);
if (idx % 8 == 0)
2018-12-12 01:50:03 +00:00
printf("\n");
}
}
template <typename T, size_t align = 128>
2018-12-12 01:50:03 +00:00
void
DumpBufferHex(const T& buff)
2018-12-12 01:50:03 +00:00
{
size_t idx = 0;
printf("buffer of size %zu\n", buff.sz);
while (idx < buff.sz)
2018-12-12 01:50:03 +00:00
{
if (buff.base + idx == buff.cur)
2018-12-12 01:50:03 +00:00
{
#ifndef _WIN32
printf("%c[1;31m", 27);
#endif
}
printf("%.2x", buff.base[idx]);
if (buff.base + idx == buff.cur)
2018-12-12 01:50:03 +00:00
{
#ifndef _WIN32
printf("%c[0;0m", 27);
#endif
}
++idx;
if (idx % align == 0)
2018-12-12 01:50:03 +00:00
printf("\n");
}
printf("\n");
fflush(stdout);
}
template <typename T, size_t align = 128>
2018-12-12 01:50:03 +00:00
void
DumpBuffer(const T& buff)
2018-12-12 01:50:03 +00:00
{
size_t idx = 0;
printf("buffer of size %zu\n", buff.sz);
while (idx < buff.sz)
2018-12-12 01:50:03 +00:00
{
if (buff.base + idx == buff.cur)
2018-12-12 01:50:03 +00:00
{
#ifndef _WIN32
printf("%c[1;31m", 27);
#endif
}
if (std::isprint(buff.base[idx]))
2018-12-12 01:50:03 +00:00
{
printf("%c", buff.base[idx]);
}
else
{
printf(".");
}
if (buff.base + idx == buff.cur)
2018-12-12 01:50:03 +00:00
{
#ifndef _WIN32
printf("%c[0;0m", 27);
#endif
}
++idx;
if (idx % align == 0)
2018-12-12 01:50:03 +00:00
printf("\n");
}
printf("\n");
fflush(stdout);
}
} // namespace llarp