aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/Makefile17
-rw-r--r--lib/blake2/BUILD.bazel28
-rw-r--r--lib/blake2/blake2s.c37
-rwxr-xr-xlib/blake2/blake2s_genkat.py (renamed from lib/tst/blake2s_genkat.py)0
-rw-r--r--lib/blake2/blake2s_kat.h (renamed from lib/tst/blake2s_kat.h)2
-rw-r--r--lib/blake2/blake2s_selftest.cc (renamed from lib/tst/blake2s_selftest.cc)12
-rw-r--r--lib/blake2/include/blake2s.h (renamed from lib/blake2/blake2s.h)24
-rw-r--r--lib/libk/BUILD.bazel80
-rw-r--r--lib/libk/endian/little.c4
-rw-r--r--lib/libk/endian/test_endian_little.cc (renamed from lib/tst/endian_little.cc)2
-rw-r--r--lib/libk/include/endian.h (renamed from lib/libk/endian.h)8
-rw-r--r--lib/libk/include/stdio.h (renamed from lib/libk/stdio.h)41
-rw-r--r--lib/libk/include/stdlib.h (renamed from lib/libk/stdlib.h)10
-rw-r--r--lib/libk/include/string.h14
-rw-r--r--lib/libk/stdio/fprintf.c5
-rw-r--r--lib/libk/stdio/printf.c5
-rw-r--r--lib/libk/stdio/vfprintf.c (renamed from lib/libk/stdio/vfprintf.cpp)25
-rw-r--r--lib/libk/stdlib/linked_list_allocator.c15
-rw-r--r--lib/libk/stdlib/memcpy.c2
-rw-r--r--lib/libk/stdlib/memset.c3
-rw-r--r--lib/libk/stdlib/test_allocator.hh (renamed from lib/tst/allocator.hh)0
-rw-r--r--lib/libk/stdlib/test_linked_list_allocator.cc (renamed from lib/tst/linked_list_allocator.cc)4
-rw-r--r--lib/libk/stdlib/test_mem.cc (renamed from lib/tst/mem.cc)4
-rw-r--r--lib/libk/string.h28
-rw-r--r--lib/libk/string/itoa.c6
-rw-r--r--lib/libk/string/test_string.cc (renamed from lib/tst/string.cc)2
26 files changed, 218 insertions, 160 deletions
diff --git a/lib/Makefile b/lib/Makefile
deleted file mode 100644
index f5eeded..0000000
--- a/lib/Makefile
+++ /dev/null
@@ -1,17 +0,0 @@
-include ../Makefile.config
-
-TARGETLIB += libk
-libk.SRCS = \
- libk/endian/little.c \
- libk/stdio/printf.c libk/stdio/fprintf.c libk/stdio/vfprintf.cpp \
- libk/stdlib/memcpy.c libk/stdlib/memset.c libk/stdlib/linked_list_allocator.c \
- libk/string/itoa.c
-
-TESTS += tst/endian_little tst/mem tst/string tst/linked_list_allocator
-
-TARGETLIB += blake2
-blake2.SRCS = blake2/blake2s.c
-TESTS += tst/blake2s_selftest
-
-include ../rules.mk
-
diff --git a/lib/blake2/BUILD.bazel b/lib/blake2/BUILD.bazel
new file mode 100644
index 0000000..4723ae6
--- /dev/null
+++ b/lib/blake2/BUILD.bazel
@@ -0,0 +1,28 @@
+package(default_visibility = ["//visibility:public"])
+
+cc_library(
+ name = "blake2s",
+ srcs = ["blake2s.c"],
+ hdrs = ["include/blake2s.h"],
+ includes = ["include"],
+ deps = select({
+ "@platforms//os:none": ["//lib/libk:k"],
+ "//conditions:default": [],
+ }),
+)
+
+cc_test(
+ name = "test_blake2s",
+ srcs = [
+ "blake2s_kat.h",
+ "blake2s_selftest.cc",
+ ],
+ target_compatible_with = select({
+ "@platforms//os:none": ["@platforms//:incompatible"],
+ "//conditions:default": [],
+ }),
+ deps = [
+ "//lib/blake2:blake2s",
+ "@googletest//:gtest_main",
+ ],
+)
diff --git a/lib/blake2/blake2s.c b/lib/blake2/blake2s.c
index b924a1e..9206c89 100644
--- a/lib/blake2/blake2s.c
+++ b/lib/blake2/blake2s.c
@@ -34,14 +34,16 @@ G(uint32_t v[16], unsigned a, unsigned b, unsigned c, unsigned d, uint32_t x, ui
void
F(struct BLAKE2s_ctx *ctx, uint32_t m[16], uint32_t f)
{
- // Initialize local work vector v
+ unsigned i;
+
+ /* Initialize local work vector v */
uint32_t v[16] = {ctx->h[0], ctx->h[1], ctx->h[2], ctx->h[3], ctx->h[4], ctx->h[5],
ctx->h[6], ctx->h[7], IV[0], IV[1], IV[2], IV[3],
IV[4] ^ ctx->t[0], IV[5] ^ ctx->t[1], IV[6], IV[7]};
- if (f) v[14] = ~v[14]; // if last block flag, invert all bits
+ if (f) v[14] = ~v[14]; /* if last block flag, invert all bits */
- // cryptographic mixing
- for (unsigned i = 0; i < 10; ++i) {
+ /* cryptographic mixing */
+ for (i = 0; i < 10; ++i) {
G(v, 0, 4, 8, 12, m[SIGMA[i][0]], m[SIGMA[i][1]]);
G(v, 1, 5, 9, 13, m[SIGMA[i][2]], m[SIGMA[i][3]]);
G(v, 2, 6, 10, 14, m[SIGMA[i][4]], m[SIGMA[i][5]]);
@@ -53,8 +55,8 @@ F(struct BLAKE2s_ctx *ctx, uint32_t m[16], uint32_t f)
G(v, 3, 4, 9, 14, m[SIGMA[i][14]], m[SIGMA[i][15]]);
}
- // xor the two halves
- for (unsigned i = 0; i < 8; ++i) ctx->h[i] ^= (v[i] ^ v[i + 8]);
+ /* xor the two halves */
+ for (i = 0; i < 8; ++i) ctx->h[i] ^= (v[i] ^ v[i + 8]);
}
int
@@ -70,14 +72,14 @@ BLAKE2s_init(struct BLAKE2s_ctx *ctx, uint8_t outlen, const void *key, uint8_t k
ctx->param.fanout = 1;
ctx->param.depth = 1;
- // copy IV into state vector h
+ /* copy IV into state vector h */
memcpy(ctx->h, IV, 32);
- // copy param block 0 onto h[0]
+ /* copy param block 0 onto h[0] */
ctx->h[0] ^= (ctx->param.depth << 24) ^ (ctx->param.fanout << 16) ^ (keylen << 8) ^ outlen;
if (keylen > 0) {
BLAKE2s_update(ctx, key, keylen);
- ctx->c = 64; // at the end
+ ctx->c = 64; /* at the end */
}
return 0;
@@ -88,15 +90,16 @@ BLAKE2s_init(struct BLAKE2s_ctx *ctx, uint8_t outlen, const void *key, uint8_t k
void
BLAKE2s_update(struct BLAKE2s_ctx *ctx, const void *d, size_t dd)
{
- for (unsigned i = 0; i < dd;) {
+ unsigned i, j;
+ for (i = 0; i < dd;) {
- if (ctx->c == 64) { // if block is full, consume block
+ if (ctx->c == 64) { /* if block is full, consume block */
ctx->t[0] += ctx->c;
if (ctx->t[0] < ctx->c) ctx->t[1] += 1;
- ctx->c = 0; // reset counter
+ ctx->c = 0; /* reset counter */
uint32_t *m = (uint32_t *)ctx->b;
- for (unsigned j = 0; j < 16; ++j) m[j] = htole32(m[j]);
+ for (j = 0; j < 16; ++j) m[j] = htole32(m[j]);
F(ctx, m, 0);
}
@@ -110,14 +113,16 @@ BLAKE2s_update(struct BLAKE2s_ctx *ctx, const void *d, size_t dd)
void
BLAKE2s_final(struct BLAKE2s_ctx *ctx, void *out)
{
+ unsigned i;
+
ctx->t[0] += ctx->c;
if (ctx->t[0] < ctx->c) ctx->t[1] += 1;
- for (; ctx->c < 64; ++(ctx->c)) ctx->b[ctx->c] = 0; // fill up block with zeroes
+ for (; ctx->c < 64; ++(ctx->c)) ctx->b[ctx->c] = 0; /* fill up block with zeroes */
uint32_t *m = (uint32_t *)ctx->b;
- for (unsigned i = 0; i < 16; ++i) m[i] = htole32(m[i]);
+ for (i = 0; i < 16; ++i) m[i] = htole32(m[i]);
F(ctx, m, 1);
- for (unsigned i = 0; i < ctx->param.outlen; ++i) ((uint8_t *)out)[i] = (ctx->h[i >> 2] >> (8 * (i & 3))) & 0xff;
+ for (i = 0; i < ctx->param.outlen; ++i) ((uint8_t *)out)[i] = (ctx->h[i >> 2] >> (8 * (i & 3))) & 0xff;
}
diff --git a/lib/tst/blake2s_genkat.py b/lib/blake2/blake2s_genkat.py
index 2dd5370..2dd5370 100755
--- a/lib/tst/blake2s_genkat.py
+++ b/lib/blake2/blake2s_genkat.py
diff --git a/lib/tst/blake2s_kat.h b/lib/blake2/blake2s_kat.h
index aa42ef5..dec250a 100644
--- a/lib/tst/blake2s_kat.h
+++ b/lib/blake2/blake2s_kat.h
@@ -1,5 +1,7 @@
#pragma once
+#include <cstdint>
+
static const unsigned KATs_len = 256;
static const uint8_t KAT_secret[32] = { 0xba, 0x80, 0xfb, 0x8f, 0x1b, 0x7b, 0xa1, 0x49, 0x3c, 0x6a, 0xe8, 0x8f, 0xd, 0x66, 0xa1, 0xae, 0xff, 0xa2, 0x5c, 0x8a, 0x7d, 0x4c, 0x1f, 0xb6, 0x81, 0x1, 0xb5, 0xe4, 0xc2, 0x8e, 0x37, 0x3 };
static const uint8_t KATs[256][32] = {
diff --git a/lib/tst/blake2s_selftest.cc b/lib/blake2/blake2s_selftest.cc
index 58199f6..420544d 100644
--- a/lib/tst/blake2s_selftest.cc
+++ b/lib/blake2/blake2s_selftest.cc
@@ -1,8 +1,10 @@
// Self test Modules for BLAKE2s
+#include "blake2s_kat.h"
#include <gtest/gtest.h>
-#include "../blake2/blake2s.c"
-#include "blake2s_kat.h"
+extern "C" {
+#include "blake2s.h"
+}
static_assert(sizeof(BLAKE2s_param) == (8 * sizeof(uint32_t)), "sizeof struct BLAKE2s_param");
@@ -67,12 +69,6 @@ blake2s_selftest()
return 0;
}
-TEST(blake2s, rotr_u32)
-{
- EXPECT_EQ(rotr_u32(0xdecafade, 16), 0xfadedeca);
- EXPECT_EQ(rotr_u32(0xdecafade, 8), 0xdedecafa);
-}
-
TEST(blake2s, selftest) { EXPECT_EQ(blake2s_selftest(), 0); }
TEST(blake2s, selftestAllInOne)
diff --git a/lib/blake2/blake2s.h b/lib/blake2/include/blake2s.h
index 64b4156..ede170c 100644
--- a/lib/blake2/blake2s.h
+++ b/lib/blake2/include/blake2s.h
@@ -4,8 +4,8 @@
#include <stdint.h>
struct BLAKE2s_param {
- uint8_t outlen; // digest length
- uint8_t keylen; // key length
+ uint8_t outlen; /* digest length */
+ uint8_t keylen; /* key length */
uint8_t fanout;
uint8_t depth;
uint32_t leaf_length;
@@ -18,11 +18,11 @@ struct BLAKE2s_param {
};
struct BLAKE2s_ctx {
- uint8_t b[64]; // input buffer
- size_t c; // pointer for b[]
- uint32_t h[8]; // chained state vector h
- uint32_t t[2]; // total number of bytes
- struct BLAKE2s_param param; // parameter block
+ uint8_t b[64]; /* input buffer */
+ size_t c; /* pointer for b[] */
+ uint32_t h[8]; /* chained state vector h */
+ uint32_t t[2]; /* total number of bytes */
+ struct BLAKE2s_param param; /* parameter block */
};
/**
@@ -41,11 +41,11 @@ int BLAKE2s_init(struct BLAKE2s_ctx *ctx, uint8_t outlen, const void *key, uint8
void BLAKE2s_update(struct BLAKE2s_ctx *ctx, const void *d, size_t dd);
void BLAKE2s_final(struct BLAKE2s_ctx *ctx, void *out);
-// All-in-one convenience function.
-static inline int
-BLAKE2s(void *out, uint8_t outlen, // return buffer for digest
- const void *key, uint8_t keylen, // optional secret key
- const void *in, size_t inlen) // data to be hashed
+/* All-in-one convenience function. */
+static __inline__ int
+BLAKE2s(void *out, uint8_t outlen, /* return buffer for digest */
+ const void *key, uint8_t keylen, /* optional secret key */
+ const void *in, size_t inlen) /* data to be hashed */
{
struct BLAKE2s_ctx ctx;
if (BLAKE2s_init(&ctx, outlen, key, keylen)) return -1;
diff --git a/lib/libk/BUILD.bazel b/lib/libk/BUILD.bazel
new file mode 100644
index 0000000..51b4c1b
--- /dev/null
+++ b/lib/libk/BUILD.bazel
@@ -0,0 +1,80 @@
+filegroup(
+ name = "k_srcs",
+ srcs = [
+ "endian/little.c",
+ "stdio/fprintf.c",
+ "stdio/printf.c",
+ "stdio/vfprintf.c",
+ "stdlib/linked_list_allocator.c",
+ "stdlib/memcpy.c",
+ "stdlib/memset.c",
+ "string/itoa.c",
+ ],
+)
+
+cc_library(
+ name = "k",
+ srcs = [":k_srcs"],
+ hdrs = glob(["include/*.h"]),
+ includes = ["include"],
+ target_compatible_with = [
+ "@platforms//os:none",
+ ],
+ visibility = ["//visibility:public"],
+)
+
+# tests
+cc_library(
+ name = "k_sut",
+ includes = ["."],
+ target_compatible_with = select({
+ "@platforms//os:none": ["@platforms//:incompatible"],
+ "//conditions:default": [],
+ }),
+ textual_hdrs = [":k_srcs"],
+)
+
+cc_test(
+ name = "test_endian_little",
+ srcs = [
+ "endian/test_endian_little.cc",
+ ],
+ deps = [
+ ":k_sut",
+ "@googletest//:gtest_main",
+ ],
+)
+
+cc_test(
+ name = "test_linked_list_allocator",
+ srcs = [
+ "stdlib/test_allocator.hh",
+ "stdlib/test_linked_list_allocator.cc",
+ ],
+ deps = [
+ ":k_sut",
+ "@googletest//:gtest_main",
+ ],
+)
+
+cc_test(
+ name = "test_mem",
+ srcs = [
+ "stdlib/test_mem.cc",
+ ],
+ deps = [
+ ":k_sut",
+ "@googletest//:gtest_main",
+ ],
+)
+
+cc_test(
+ name = "test_string",
+ srcs = [
+ "string/test_string.cc",
+ ],
+ deps = [
+ ":k_sut",
+ "@googletest//:gtest_main",
+ ],
+)
diff --git a/lib/libk/endian/little.c b/lib/libk/endian/little.c
index 042bb55..56a20ad 100644
--- a/lib/libk/endian/little.c
+++ b/lib/libk/endian/little.c
@@ -1,6 +1,4 @@
-//=====================================================================
-// spdx-license-identifier: ISC
-//=====================================================================
+/* spdx-license-identifier: ISC */
#include "endian.h"
diff --git a/lib/tst/endian_little.cc b/lib/libk/endian/test_endian_little.cc
index 9c8c73b..97ee286 100644
--- a/lib/tst/endian_little.cc
+++ b/lib/libk/endian/test_endian_little.cc
@@ -2,7 +2,7 @@
#include <gtest/gtest.h>
namespace libk {
-#include "../libk/endian/little.c"
+#include "little.c"
} // namespace libk
TEST(endian_little, htole16)
diff --git a/lib/libk/endian.h b/lib/libk/include/endian.h
index 70bc5f7..6aa2669 100644
--- a/lib/libk/endian.h
+++ b/lib/libk/include/endian.h
@@ -1,13 +1,11 @@
-//=====================================================================
-// spdx-license-identifier: ISC
-//=====================================================================
+/* spdx-license-identifier: ISC */
#pragma once
#include <stdint.h>
-// These functions convert the byte encoding of integer values from host byte order to and from little-endian and
-// big-endian byte order
+/* These functions convert the byte encoding of integer values from host byte order to and from little-endian and
+ * big-endian byte order */
uint16_t htole16(uint16_t host_16b);
uint32_t htole32(uint32_t host_32b);
uint64_t htole64(uint64_t host_64b);
diff --git a/lib/libk/stdio.h b/lib/libk/include/stdio.h
index 5ef68f1..7a6e663 100644
--- a/lib/libk/stdio.h
+++ b/lib/libk/include/stdio.h
@@ -2,27 +2,19 @@
#include <stdarg.h>
-///@defgroup libk libk
-///@{
-///@defgroup stdio stdio
-///@{
+/** An object type used for streams */
+typedef struct FILE {
+ int id;
-#ifdef __cplusplus
-/**
- * An object type used for streams
- */
-struct kIoDevice {
/** Function that prints a character to the stream */
- virtual void putc(char) = 0;
+ void (*putc)(const struct FILE *, char);
+
/** Function that prints a string to the stream */
- virtual int puts(const char *, int) = 0;
+ int (*puts)(const struct FILE *, const char *, int);
+
/** Flush write buffers */
- virtual void flush() = 0;
-};
-typedef kIoDevice FILE;
-#else
-typedef void FILE;
-#endif
+ void (*flush)(const struct FILE *);
+} FILE;
/** A FILE value corresponding to stdin, the keyboard buffer */
extern FILE *stdin;
@@ -31,28 +23,19 @@ extern FILE *stdout;
/** A FILE value corresponding to stderr, the uart */
extern FILE *stderr;
-#ifdef __cplusplus
-extern "C" {
-#endif
/**
* Write the formatted string to stdout
* Supports ``%s`` (string), ``%d`` (decimal), ``%u`` (unsigned), ``%x`` (hexadecimal)
* @return number of bytes written
*/
-int printf(const char *restrict format, ...);
+int printf(const char *__restrict__ format, ...);
/**
* Write the formatted string to stream; see printf
*/
-int fprintf(FILE *restrict stream, const char *restrict format, ...);
+int fprintf(FILE *__restrict__ stream, const char *__restrict__ format, ...);
/**
* Write the formatted string to stream; see printf
*/
-int vfprintf(FILE *restrict stream, const char *restrict format, va_list ap);
-#ifdef __cplusplus
-}
-#endif
-
-///@}
-///@}
+int vfprintf(FILE *__restrict__ stream, const char *__restrict__ format, va_list ap);
diff --git a/lib/libk/stdlib.h b/lib/libk/include/stdlib.h
index 84d9b2d..143c931 100644
--- a/lib/libk/stdlib.h
+++ b/lib/libk/include/stdlib.h
@@ -2,11 +2,6 @@
#include <stddef.h>
-///@defgroup libk libk
-///@{
-///@defgroup stdlib stdlib
-///@{
-
/**
* Allocate size bytes and return a pointer to the allocated memory
*/
@@ -25,7 +20,4 @@ void *memset(void *s, int c, long unsigned n);
/**
* Copy n bytes from memory area src to memory area dest. The memory areas must not overlap.
*/
-void *memcpy(void *restrict dest, const void *restrict src, long unsigned n);
-
-///@}
-///@}
+void *memcpy(void *__restrict__ dest, const void *__restrict__ src, long unsigned n);
diff --git a/lib/libk/include/string.h b/lib/libk/include/string.h
new file mode 100644
index 0000000..45b05a5
--- /dev/null
+++ b/lib/libk/include/string.h
@@ -0,0 +1,14 @@
+#pragma once
+
+#define OCTAL 8
+#define DECIMAL 10
+#define HEX 16
+
+/**
+ * Convert int into a string
+ */
+char *itoa(char *p, int x, unsigned base);
+/**
+ * Convert unsigned int into a string
+ */
+char *utoa(char *p, unsigned x, unsigned base);
diff --git a/lib/libk/stdio/fprintf.c b/lib/libk/stdio/fprintf.c
index 9a96dc6..c088f54 100644
--- a/lib/libk/stdio/fprintf.c
+++ b/lib/libk/stdio/fprintf.c
@@ -1,11 +1,12 @@
#include <stdio.h>
int
-fprintf(FILE *restrict stream, const char *restrict format, ...)
+fprintf(FILE *__restrict__ stream, const char *__restrict__ format, ...)
{
+ int c = 0;
va_list ap;
va_start(ap, format);
- int c = vfprintf(stream, format, ap);
+ c += vfprintf(stream, format, ap);
va_end(ap);
return c;
}
diff --git a/lib/libk/stdio/printf.c b/lib/libk/stdio/printf.c
index 4efc1ac..4c45593 100644
--- a/lib/libk/stdio/printf.c
+++ b/lib/libk/stdio/printf.c
@@ -1,11 +1,12 @@
#include <stdio.h>
int
-printf(const char *restrict format, ...)
+printf(const char *__restrict__ format, ...)
{
+ int c = 0;
va_list ap;
va_start(ap, format);
- int c = vfprintf(stdout, format, ap);
+ c += vfprintf(stdout, format, ap);
va_end(ap);
return c;
}
diff --git a/lib/libk/stdio/vfprintf.cpp b/lib/libk/stdio/vfprintf.c
index aa9256d..807c26a 100644
--- a/lib/libk/stdio/vfprintf.cpp
+++ b/lib/libk/stdio/vfprintf.c
@@ -3,40 +3,41 @@
static char buffer[3 * sizeof(int) + 2];
-extern "C" int
-vfprintf(FILE *restrict stream, const char *restrict format, va_list params)
+int
+vfprintf(FILE *__restrict__ stream, const char *__restrict__ format, va_list params)
{
int written = 0;
+ int i;
int s = 0;
int l = 0;
- for (int i = 0; format[i] != '\0'; ++i) {
+ for (i = 0; format[i] != '\0'; ++i) {
if (format[i] == '%') {
- written += stream->puts(&format[s], l);
+ written += stream->puts(stream, &format[s], l);
s = i + 2;
++i;
switch (format[i]) {
case 's': {
const char *arg = va_arg(params, const char *);
- written += stream->puts(arg, -1);
+ written += stream->puts(stream, arg, -1);
} break;
case 'c': {
- const int arg = va_arg(params, int);
- stream->putc(arg);
+ const char arg = (char)va_arg(params, int);
+ stream->putc(stream, arg);
++written;
} break;
case 'd': {
const char *arg = itoa(buffer, va_arg(params, int), 10);
- written += stream->puts(arg, -1);
+ written += stream->puts(stream, arg, -1);
} break;
case 'u': {
const char *arg = utoa(buffer, va_arg(params, unsigned int), 10);
- written += stream->puts(arg, -1);
+ written += stream->puts(stream, arg, -1);
} break;
case 'x': {
const char *arg = utoa(buffer, va_arg(params, unsigned int), 16);
- written += stream->puts(arg, -1);
+ written += stream->puts(stream, arg, -1);
} break;
}
@@ -47,8 +48,8 @@ vfprintf(FILE *restrict stream, const char *restrict format, va_list params)
++l;
}
- if (l > 0) { written += stream->puts(&format[s], l); }
+ if (l > 0) { written += stream->puts(stream, &format[s], l); }
- stream->flush();
+ stream->flush(stream);
return written;
}
diff --git a/lib/libk/stdlib/linked_list_allocator.c b/lib/libk/stdlib/linked_list_allocator.c
index 66c63d1..bcec580 100644
--- a/lib/libk/stdlib/linked_list_allocator.c
+++ b/lib/libk/stdlib/linked_list_allocator.c
@@ -31,13 +31,14 @@ alloc_init(void *mem, size_t size)
void *
malloc(size_t size)
{
+ struct Chunk *iter;
if (begin == NULL) return NULL;
- // find free chunk that is at least (size + sizeof(struct Chunk))
- for (struct Chunk *iter = begin; iter != NULL; iter = iter->next) {
+ /* find free chunk that is at least (size + sizeof(struct Chunk)) */
+ for (iter = begin; iter != NULL; iter = iter->next) {
if (iter->used != 0 || iter->size < size) continue;
- // if there's at least sizeof(struct Chunk) bytes left over, create a new Chunk
+ /* if there's at least sizeof(struct Chunk) bytes left over, create a new Chunk */
if (iter->size >= (size + 2 * sizeof(struct Chunk))) {
struct Chunk *next = (struct Chunk *)((uintptr_t)iter + sizeof(struct Chunk) + size);
Chunk_ctor(next, iter->size - size - sizeof(struct Chunk));
@@ -57,18 +58,20 @@ malloc(size_t size)
void
free(void *ptr)
{
+ struct Chunk *chunk;
if (ptr == NULL) return;
- struct Chunk *chunk = (struct Chunk *)((uintptr_t)ptr - sizeof(struct Chunk));
+
+ chunk = (struct Chunk *)((uintptr_t)ptr - sizeof(struct Chunk));
chunk->used = 0;
- // merge next chunk
+ /* merge next chunk */
if (chunk->next != NULL && chunk->next->used == 0) {
chunk->size += chunk->next->size + sizeof(struct Chunk);
chunk->next = chunk->next->next;
if (chunk->next != NULL) chunk->next->prev = chunk;
}
- // merge into prev chunk
+ /* merge into prev chunk */
if (chunk->prev != NULL && chunk->prev->used == 0) {
chunk->prev->size += chunk->size + sizeof(struct Chunk);
chunk->prev->next = chunk->next;
diff --git a/lib/libk/stdlib/memcpy.c b/lib/libk/stdlib/memcpy.c
index 90470d5..db7d21e 100644
--- a/lib/libk/stdlib/memcpy.c
+++ b/lib/libk/stdlib/memcpy.c
@@ -1,5 +1,5 @@
void *
-memcpy(void *restrict dest, const void *restrict src, long unsigned n)
+memcpy(void *__restrict__ dest, const void *__restrict__ src, long unsigned n)
{
char *pDest = (char *)dest;
const char *pSrc = (const char *)src;
diff --git a/lib/libk/stdlib/memset.c b/lib/libk/stdlib/memset.c
index a16bd05..2a86f8e 100644
--- a/lib/libk/stdlib/memset.c
+++ b/lib/libk/stdlib/memset.c
@@ -1,7 +1,8 @@
void *
memset(void *s, int c, long unsigned n)
{
+ unsigned i;
char *pDest = (char *)s;
- for (unsigned i = 0; i < n; ++i) pDest[i] = (char)c;
+ for (i = 0; i < n; ++i) pDest[i] = (char)c;
return s;
}
diff --git a/lib/tst/allocator.hh b/lib/libk/stdlib/test_allocator.hh
index 3bc1715..3bc1715 100644
--- a/lib/tst/allocator.hh
+++ b/lib/libk/stdlib/test_allocator.hh
diff --git a/lib/tst/linked_list_allocator.cc b/lib/libk/stdlib/test_linked_list_allocator.cc
index a2575d5..5963ce1 100644
--- a/lib/tst/linked_list_allocator.cc
+++ b/lib/libk/stdlib/test_linked_list_allocator.cc
@@ -3,7 +3,7 @@
#include <iostream>
namespace libk {
-#include "../libk/stdlib/linked_list_allocator.c"
+#include "linked_list_allocator.c"
std::ostream &
operator<<(std::ostream &os, const Chunk &b)
@@ -16,7 +16,7 @@ operator<<(std::ostream &os, const Chunk &b)
}
}; // namespace libk
-#include "allocator.hh"
+#include "test_allocator.hh"
TEST(UninitializedAllocator, malloc) { EXPECT_EQ(libk::malloc(1024), nullptr); }
diff --git a/lib/tst/mem.cc b/lib/libk/stdlib/test_mem.cc
index 1ad266c..f8a5e18 100644
--- a/lib/tst/mem.cc
+++ b/lib/libk/stdlib/test_mem.cc
@@ -3,8 +3,8 @@
#define restrict __restrict__
namespace libk {
-#include "../libk/stdlib/memcpy.c"
-#include "../libk/stdlib/memset.c"
+#include "memcpy.c"
+#include "memset.c"
} // namespace libk
TEST(mem, memset)
diff --git a/lib/libk/string.h b/lib/libk/string.h
deleted file mode 100644
index c8196c8..0000000
--- a/lib/libk/string.h
+++ /dev/null
@@ -1,28 +0,0 @@
-#pragma once
-
-///@defgroup libk libk
-///@{
-///@defgroup string string
-///@{
-
-#define OCTAL 8
-#define DECIMAL 10
-#define HEX 16
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-/**
- * Convert int into a string
- */
-char *itoa(char *p, int x, int base);
-/**
- * Convert unsigned int into a string
- */
-char *utoa(char *p, unsigned x, int base);
-#ifdef __cplusplus
-}
-#endif
-
-///@}
-///@}
diff --git a/lib/libk/string/itoa.c b/lib/libk/string/itoa.c
index 2db9768..0997345 100644
--- a/lib/libk/string/itoa.c
+++ b/lib/libk/string/itoa.c
@@ -4,7 +4,7 @@
static const char *numbers = "0123456789abcdef";
char *
-utoa(char *p, unsigned x, int base)
+utoa(char *p, unsigned x, unsigned base)
{
p += 3 * sizeof(unsigned);
*--p = '\0';
@@ -18,12 +18,12 @@ utoa(char *p, unsigned x, int base)
}
char *
-itoa(char *p, int x, int base)
+itoa(char *p, int x, unsigned base)
{
const bool is_negative = (x < 0);
if (is_negative) x = -x;
- p = utoa(p, x, base);
+ p = utoa(p, (unsigned)x, base);
if (is_negative) *--p = '-';
return p;
diff --git a/lib/tst/string.cc b/lib/libk/string/test_string.cc
index f22c123..d12b318 100644
--- a/lib/tst/string.cc
+++ b/lib/libk/string/test_string.cc
@@ -1,7 +1,7 @@
#include <gtest/gtest.h>
namespace libk {
-#include "../libk/string/itoa.c"
+#include "itoa.c"
}
char buffer[64];