Add bip buffer and constexpr hash
This commit is contained in:
@@ -88,6 +88,7 @@ kutil:
|
|||||||
- src/libraries/kutil/include
|
- src/libraries/kutil/include
|
||||||
source:
|
source:
|
||||||
- src/libraries/kutil/assert.cpp
|
- src/libraries/kutil/assert.cpp
|
||||||
|
- src/libraries/kutil/bip_buffer.cpp
|
||||||
- src/libraries/kutil/frame_allocator.cpp
|
- src/libraries/kutil/frame_allocator.cpp
|
||||||
- src/libraries/kutil/heap_manager.cpp
|
- src/libraries/kutil/heap_manager.cpp
|
||||||
- src/libraries/kutil/memory.cpp
|
- src/libraries/kutil/memory.cpp
|
||||||
@@ -110,6 +111,7 @@ tests:
|
|||||||
- kutil
|
- kutil
|
||||||
source:
|
source:
|
||||||
- src/tests/address_manager.cpp
|
- src/tests/address_manager.cpp
|
||||||
|
- src/tests/constexpr_hash.cpp
|
||||||
- src/tests/frame_allocator.cpp
|
- src/tests/frame_allocator.cpp
|
||||||
- src/tests/linked_list.cpp
|
- src/tests/linked_list.cpp
|
||||||
- src/tests/heap_manager.cpp
|
- src/tests/heap_manager.cpp
|
||||||
|
|||||||
95
src/libraries/kutil/bip_buffer.cpp
Normal file
95
src/libraries/kutil/bip_buffer.cpp
Normal file
@@ -0,0 +1,95 @@
|
|||||||
|
#include "kutil/assert.h"
|
||||||
|
#include "kutil/bip_buffer.h"
|
||||||
|
|
||||||
|
namespace kutil {
|
||||||
|
|
||||||
|
bip_buffer::bip_buffer() :
|
||||||
|
m_start_a(0),
|
||||||
|
m_start_b(0),
|
||||||
|
m_size_a(0),
|
||||||
|
m_size_b(0),
|
||||||
|
m_buffer_size(0),
|
||||||
|
m_buffer(nullptr)
|
||||||
|
{}
|
||||||
|
|
||||||
|
bip_buffer::bip_buffer(uint8_t *buffer, size_t size) :
|
||||||
|
m_start_a(0),
|
||||||
|
m_start_b(0),
|
||||||
|
m_size_a(0),
|
||||||
|
m_size_b(0),
|
||||||
|
m_buffer_size(size),
|
||||||
|
m_buffer(buffer)
|
||||||
|
{}
|
||||||
|
|
||||||
|
size_t bip_buffer::reserve(size_t size, void **area)
|
||||||
|
{
|
||||||
|
if (m_size_r) {
|
||||||
|
*area = nullptr;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t remaining = 0;
|
||||||
|
if (m_size_b) {
|
||||||
|
// If B exists, we're appending there. Get space between
|
||||||
|
// the end of B and start of A.
|
||||||
|
remaining = m_start_a - m_start_b - m_size_b;
|
||||||
|
m_start_r = m_start_b + m_size_b;
|
||||||
|
} else {
|
||||||
|
// B doesn't exist, check the space both before and after A.
|
||||||
|
// If the end of A has enough room for this write, put it there.
|
||||||
|
remaining = m_buffer_size - m_start_a - m_size_a;
|
||||||
|
m_start_r = m_start_a + m_size_a;
|
||||||
|
|
||||||
|
// Otherwise use the bigger of the areas in front of and after A
|
||||||
|
if (remaining < size && m_start_a > remaining) {
|
||||||
|
remaining = m_start_a;
|
||||||
|
m_start_r = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!remaining) {
|
||||||
|
*area = nullptr;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
m_size_r = (remaining < size) ? remaining : size;
|
||||||
|
*area = &m_buffer[m_start_r];
|
||||||
|
return m_size_r;
|
||||||
|
}
|
||||||
|
|
||||||
|
void bip_buffer::commit(size_t size)
|
||||||
|
{
|
||||||
|
kassert(size <= m_size_r, "Tried to commit more than reserved");
|
||||||
|
|
||||||
|
if (m_start_r == m_start_a + m_size_a) {
|
||||||
|
// We were adding to A
|
||||||
|
m_size_a += size;
|
||||||
|
} else {
|
||||||
|
// We were adding to B
|
||||||
|
kassert(m_start_r == m_start_b + m_size_b, "Bad m_start_r!");
|
||||||
|
m_size_b += size;
|
||||||
|
}
|
||||||
|
|
||||||
|
m_start_r = m_size_r = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t bip_buffer::get_block(void **area) const
|
||||||
|
{
|
||||||
|
*area = m_size_a ? &m_buffer[m_start_a] : nullptr;
|
||||||
|
return m_size_a;
|
||||||
|
}
|
||||||
|
|
||||||
|
void bip_buffer::consume(size_t size)
|
||||||
|
{
|
||||||
|
kassert(size <= m_size_a, "Consumed more bytes than exist in A");
|
||||||
|
if (size >= m_size_a) {
|
||||||
|
m_size_a = m_size_b;
|
||||||
|
m_start_a = m_start_b;
|
||||||
|
m_size_b = m_start_b = 0;
|
||||||
|
} else {
|
||||||
|
m_size_a -= size;
|
||||||
|
m_start_a += size;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace kutil
|
||||||
59
src/libraries/kutil/include/kutil/bip_buffer.h
Normal file
59
src/libraries/kutil/include/kutil/bip_buffer.h
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
#pragma once
|
||||||
|
/// \file bip_buffer.h
|
||||||
|
/// A Bip Buffer (bipartite circular buffer). For more on the Bip Buffer structure, see
|
||||||
|
/// https://www.codeproject.com/Articles/3479/The-Bip-Buffer-The-Circular-Buffer-with-a-Twist
|
||||||
|
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
namespace kutil {
|
||||||
|
|
||||||
|
class bip_buffer
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
/// Default constructor. Creates a zero-size buffer.
|
||||||
|
bip_buffer();
|
||||||
|
|
||||||
|
/// Constructor.
|
||||||
|
bip_buffer(uint8_t *buffer, size_t size);
|
||||||
|
|
||||||
|
/// Reserve an area of buffer for a write.
|
||||||
|
/// \arg size Requested size, in bytes
|
||||||
|
/// \arg area [out] Pointer to returned area
|
||||||
|
/// \returns Size of returned area, in bytes, or 0 on failure
|
||||||
|
size_t reserve(size_t size, void **area);
|
||||||
|
|
||||||
|
/// Commit a pending write started by reserve()
|
||||||
|
/// \arg size Amount of data used, in bytes
|
||||||
|
void commit(size_t size);
|
||||||
|
|
||||||
|
/// Get a pointer to a block of data in the buffer.
|
||||||
|
/// \arg area [out] Pointer to the retuned area
|
||||||
|
/// \returns Size of the returned area, in bytes
|
||||||
|
size_t get_block(void **area) const;
|
||||||
|
|
||||||
|
/// Mark a number of bytes as consumed, freeing buffer space
|
||||||
|
/// \arg size Number of bytes to consume
|
||||||
|
void consume(size_t size);
|
||||||
|
|
||||||
|
/// Get total amount of data in the buffer.
|
||||||
|
/// \returns Number of bytes committed to the buffer
|
||||||
|
inline size_t size() const { return m_size_a + m_size_b; }
|
||||||
|
|
||||||
|
/// Get total amount of free buffer remaining
|
||||||
|
/// \returns Number of bytes of buffer that are free
|
||||||
|
inline size_t free_space() const { return m_buffer_size - size(); }
|
||||||
|
|
||||||
|
private:
|
||||||
|
size_t m_start_a;
|
||||||
|
size_t m_start_b;
|
||||||
|
size_t m_start_r;
|
||||||
|
size_t m_size_a;
|
||||||
|
size_t m_size_b;
|
||||||
|
size_t m_size_r;
|
||||||
|
|
||||||
|
const size_t m_buffer_size;
|
||||||
|
uint8_t * const m_buffer;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace kutil
|
||||||
38
src/libraries/kutil/include/kutil/constexpr_hash.h
Normal file
38
src/libraries/kutil/include/kutil/constexpr_hash.h
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
#pragma once
|
||||||
|
/// \file constexpr_hash.h
|
||||||
|
/// A complile-time hashing function
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
|
namespace kutil {
|
||||||
|
|
||||||
|
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,
|
||||||
|
0x39,0x2c,0xe2,0x34,0xb9,0xf2,0xae,0x40,0x10,0x90,0x94,0xd1,0x98,0x2d,0x16,0xfd,
|
||||||
|
0xc6,0x48,0x0d,0xce,0x74,0x43,0x28,0xf9,0x61,0x12,0xd0,0xcd,0xd8,0xd7,0xa8,0x78,
|
||||||
|
0x73,0x70,0xcc,0x1e,0x17,0xa7,0x87,0x38,0x68,0x91,0xc1,0x04,0x3f,0xf5,0xde,0xa3,
|
||||||
|
0x8a,0xe5,0x9b,0xec,0x97,0xd5,0x71,0x4a,0x20,0xca,0xc8,0xc4,0x83,0x53,0xe7,0x7b,
|
||||||
|
0x64,0x31,0x06,0xe0,0x7a,0xb6,0x52,0x8c,0xba,0x58,0xcb,0xb5,0x37,0x51,0x59,0xa1,
|
||||||
|
0x11,0xe3,0x5a,0xdb,0xe1,0x6d,0x46,0x62,0xaf,0xbd,0x57,0xb8,0x0e,0xf4,0xdd,0xa6,
|
||||||
|
0x45,0xf8,0x35,0x42,0x56,0xdf,0xad,0x80,0xb2,0x0b,0x5b,0xd4,0x86,0xb3,0xf0,0xc9,
|
||||||
|
0x3c,0xa5,0xc0,0x8e,0x55,0x77,0xeb,0x36,0x79,0xab,0x4c,0x25,0xed,0xa9,0x75,0x8f,
|
||||||
|
0xee,0xc2,0x72,0x8b,0x60,0x2a,0xfa,0x32,0xe9,0xda,0x03,0x1b,0x27,0x69,0x18,0x9e,
|
||||||
|
0x88,0x96,0x54,0x81,0x30,0x22,0x7c,0x4f,0xc7,0xef,0x5d,0xa4,0x67,0x44,0xc3,0x99,
|
||||||
|
0xbb,0xd3,0x8d,0x65,0xb1,0x82,0x09,0x1a,0x13,0xd9,0x9c,0x4d,0xb0,0xfc,0xac,0xbc,
|
||||||
|
0x6a,0x29,0x95,0x19,0x92,0xaa,0x49,0x7d,0x3b,0xfb,0x50,0xb7,0xf3,0x5e,0x3e,0x6b,
|
||||||
|
0x3a,0x14,0x2b,0xb4,0xfe,0xe6,0x93,0x23,0xd6,0x1f,0xd2,0x0c,0x1d,0x9d,0x6c,0x66,
|
||||||
|
0x1c,0x89,0xbf,0xf6,0xff,0x6f,0x84,0x6e,0x2e,0xea,0x21,0xf7,0x7f,0x33,0xf1,0xe4,
|
||||||
|
0x3d,0x0f,0x05,0x08,0x4e,0xa2,0xa0,0x2f,0xdc,0x00,0x5f,0x15,0x7e,0x02,0x4b,0x26
|
||||||
|
};
|
||||||
|
|
||||||
|
constexpr inline uint8_t pearson_hash_8(const char *s, uint8_t inv) {
|
||||||
|
return (*s) ? pearson_hash_8(s + 1, pearson_hash_table[inv ^ *s]) : inv;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace kutil
|
||||||
|
|
||||||
|
constexpr inline uint8_t operator "" _h (const char *s, size_t len) {
|
||||||
|
return kutil::pearson_hash_8(s, static_cast<uint8_t>(len & 0xff));
|
||||||
|
}
|
||||||
|
|
||||||
21
src/tests/constexpr_hash.cpp
Normal file
21
src/tests/constexpr_hash.cpp
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
#include "kutil/constexpr_hash.h"
|
||||||
|
#include "catch.hpp"
|
||||||
|
|
||||||
|
using namespace kutil;
|
||||||
|
|
||||||
|
TEST_CASE( "constexpr hash", "[hash]" )
|
||||||
|
{
|
||||||
|
const unsigned hash1 = static_cast<unsigned>("hash1!"_h);
|
||||||
|
CHECK(hash1 == 210);
|
||||||
|
|
||||||
|
const unsigned hash2 = static_cast<unsigned>("hash1!"_h);
|
||||||
|
CHECK(hash1 == hash2);
|
||||||
|
|
||||||
|
const unsigned hash3 = static_cast<unsigned>("not hash1!"_h);
|
||||||
|
CHECK(hash1 != hash3);
|
||||||
|
CHECK(hash3 == 37);
|
||||||
|
|
||||||
|
const unsigned hash4 = static_cast<unsigned>("another thing that's longer"_h);
|
||||||
|
CHECK(hash1 != hash4);
|
||||||
|
CHECK(hash4 == 212);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user