[util] Replace kutil with util

Now that kutil has no kernel-specific code in it anymore, it can
actually be linked to by anything, so I'm renaming it 'util'.

Also, I've tried to unify the way that the system libraries from
src/libraries are #included using <> instead of "".

Other small change: util::bip_buffer got a spinlock to guard against
state corruption.
This commit is contained in:
Justin C. Miller
2022-01-03 00:03:29 -08:00
parent 5f88f5ed02
commit cd9b85b555
68 changed files with 231 additions and 203 deletions

View File

@@ -4,9 +4,9 @@
#define assert(x) ((void)0)
#endif
#include "kutil/bip_buffer.h"
#include <util/bip_buffer.h>
namespace kutil {
namespace util {
bip_buffer::bip_buffer() :
m_start_a(0),
@@ -30,6 +30,8 @@ bip_buffer::bip_buffer(uint8_t *buffer, size_t size) :
size_t bip_buffer::reserve(size_t size, void **area)
{
scoped_lock lock {m_lock};
if (m_size_r) {
*area = nullptr;
return 0;
@@ -66,6 +68,8 @@ size_t bip_buffer::reserve(size_t size, void **area)
void bip_buffer::commit(size_t size)
{
scoped_lock lock {m_lock};
assert(size <= m_size_r && "Tried to commit more than reserved");
if (m_start_r == m_start_a + m_size_a) {
@@ -82,12 +86,16 @@ void bip_buffer::commit(size_t size)
size_t bip_buffer::get_block(void **area) const
{
scoped_lock lock {m_lock};
*area = m_size_a ? &m_buffer[m_start_a] : nullptr;
return m_size_a;
}
void bip_buffer::consume(size_t size)
{
scoped_lock lock {m_lock};
assert(size <= m_size_a && "Consumed more bytes than exist in A");
if (size >= m_size_a) {
m_size_a = m_size_b;
@@ -99,4 +107,4 @@ void bip_buffer::consume(size_t size)
}
}
} // namespace kutil
} // namespace util

View File

@@ -6,7 +6,9 @@
#include <stddef.h>
#include <stdint.h>
namespace kutil {
#include <util/spinlock.h>
namespace util {
class bip_buffer
{
@@ -52,8 +54,10 @@ private:
size_t m_size_b;
size_t m_size_r;
mutable spinlock m_lock;
const size_t m_buffer_size;
uint8_t * const m_buffer;
};
} // namespace kutil
} // namespace util

View File

@@ -5,7 +5,7 @@
#include <stdint.h>
#include <stddef.h>
namespace kutil {
namespace util {
constexpr static const uint8_t pearson_hash_table[256] = {
0x76,0x07,0xbe,0x47,0xcf,0x41,0x0a,0xe8,0x01,0x5c,0x9f,0xc5,0x24,0x63,0x9a,0x85,
@@ -34,9 +34,9 @@ constexpr inline uint32_t djb_hash_32(const char *s, int off = 0) {
return !s[off] ? 5381 : (djb_hash_32(s, off+1)*33) ^ s[off];
}
} // namespace kutil
} // namespace util
constexpr inline uint8_t operator "" _h (const char *s, size_t len) {
return kutil::pearson_hash_8(s, static_cast<uint8_t>(len & 0xff));
return util::pearson_hash_8(s, static_cast<uint8_t>(len & 0xff));
}

View File

@@ -5,7 +5,7 @@
#include <stddef.h>
#include <stdint.h>
namespace kutil {
namespace util {
constexpr uint64_t fnv_64_prime = 0x100000001b3ull;
constexpr uint64_t fnv1a_64_init = 0xcbf29ce484222325ull;
@@ -40,4 +40,4 @@ inline uint64_t hash(const T &v) {
template <> inline uint64_t hash<uint64_t>(const uint64_t &i) { return i; }
template <> inline uint64_t hash<const char *>(const char * const &s) { return hash_string(s); }
} // namespace kutil
} // namespace util

View File

@@ -3,7 +3,7 @@
/// A generic templatized linked list.
#include <stddef.h>
namespace kutil {
namespace util {
template <typename T> class linked_list;
@@ -346,4 +346,4 @@ private:
};
} // namespace kutil
} // namespace util

View File

@@ -14,11 +14,12 @@
#include <assert.h>
#include <stdint.h>
#include <string.h>
#include "kutil/hash.h"
#include "kutil/vector.h"
#include "kutil/util.h"
namespace kutil {
#include <util/hash.h>
#include <util/vector.h>
#include <util/util.h>
namespace util {
/// Templated equality check to allow overriding
template <typename T>
@@ -290,4 +291,4 @@ public:
}
};
} // namespace kutil
} // namespace util

View File

@@ -1,6 +1,6 @@
#pragma once
namespace kutil {
namespace util {
constexpr uint32_t
byteswap(uint32_t x)

View File

@@ -2,7 +2,7 @@
/// \file no_construct.h
/// Tools for creating objects witout running constructors
namespace kutil {
namespace util {
/// Helper template for creating objects witout running constructors
template <typename T>
@@ -13,4 +13,4 @@ union no_construct
~no_construct() {}
};
} // namespace kutil
} // namespace util

View File

@@ -3,7 +3,7 @@
#pragma once
namespace kutil {
namespace util {
/// An MCS based spinlock
class spinlock
@@ -43,4 +43,4 @@ private:
spinlock::waiter m_waiter;
};
} // namespace kutil
} // namespace util

View File

@@ -1,10 +1,10 @@
#pragma once
/// \file util.h
/// Utility functions used in other kutil code
/// Utility functions used in other util code
#include <stdint.h>
namespace kutil {
namespace util {
// Get the base-2 logarithm of i
inline unsigned log2(uint64_t i) {

View File

@@ -5,9 +5,10 @@
#include <assert.h>
#include <string.h>
#include <utility>
#include "kutil/util.h"
namespace kutil {
#include <util/util.h>
namespace util {
/// A dynamic array.
template <typename T, typename S = uint32_t>
@@ -292,4 +293,4 @@ private:
T *m_elements;
};
} // namespace kutil
} // namespace util

View File

@@ -1,6 +1,6 @@
#include "kutil/spinlock.h"
#include <util/spinlock.h>
namespace kutil {
namespace util {
static constexpr int memorder = __ATOMIC_SEQ_CST;
@@ -43,4 +43,4 @@ spinlock::release(waiter *w)
}
} // namespace kutil
} // namespace util

View File

@@ -1,6 +1,6 @@
# vim: ft=python
module("kutil",
module("util",
kind = "lib",
includes = [ "include" ],
sources = [