Add/move code into kutil library

This commit is contained in:
Justin C. Miller
2018-04-17 23:41:28 -07:00
parent 2050b89334
commit a27b8d6a3a
15 changed files with 80 additions and 45 deletions

14
src/modules/kutil/coord.h Normal file
View File

@@ -0,0 +1,14 @@
#pragma once
namespace kutil {
template <typename T>
struct coord
{
T x, y;
coord() : x(T{}), y(T{}) {}
coord(T x, T y) : x(x), y(y) {}
T size() const { return x * y; }
};
} // namespace kutil

View File

@@ -0,0 +1,13 @@
#include "memory.h"
namespace kutil {
void *
memset(void *s, uint8_t v, size_t n)
{
uint8_t *p = reinterpret_cast<uint8_t *>(s);
for (int i = 0; i < n; ++i) p[i] = 0;
return s;
}
} // namespace kutil

View File

@@ -0,0 +1,13 @@
#pragma once
#include <stddef.h>
#include <stdint.h>
namespace kutil {
void * memset(void *p, uint8_t v, size_t n);
template <typename T>
T read_from(const void *p) { return *reinterpret_cast<const T *>(p); }
} // namespace kutil

12
src/modules/kutil/misc.h Normal file
View File

@@ -0,0 +1,12 @@
#pragma once
namespace kutil {
constexpr uint32_t
byteswap(uint32_t x)
{
return ((x >> 24) & 0x000000ff) | ((x >> 8) & 0x0000ff00)
| ((x << 8) & 0x00ff0000) | ((x << 24) & 0xff000000);
}
}

View File

@@ -0,0 +1,2 @@
MOD_NAME := kutil
include modules.mk