Main: remove MemStore (memory store) and SizedVoid (sized void)

This commit is contained in:
Alex Kotov 2022-06-17 18:50:46 +03:00
parent 44ffbdea54
commit 6d171841b4
Signed by: kotovalexarian
GPG Key ID: 553C0EBBEB5D5F08
18 changed files with 25 additions and 390 deletions

1
.gitignore vendored
View File

@ -110,7 +110,6 @@
/examples/cmdline
/examples/fprintf
/examples/fprintf_va
/examples/io_memstore
/examples/memmap
/examples/ntoa
/examples/panic

View File

@ -21,7 +21,7 @@ libc/libc.la:
lib_LTLIBRARIES = libkernaux.la
libkernaux_la_SOURCES = src/libc.h src/assert.c src/sized_void.c
libkernaux_la_SOURCES = src/libc.h src/assert.c
libkernaux_la_LIBADD =
if ASM_I386

View File

@ -45,7 +45,6 @@ zero). Work-in-progress APIs can change at any time.
* [Example: Panic](/examples/panic.c)
* Stack trace *(planned)*
* [Input/output](/include/kernaux/io.h) (*work in progress*)
* [Example: Memory store](/examples/io_memstore.c)
* Architecture-specific code (*work in progress*)
* [Declarations](/include/kernaux/arch/)
* [Functions](/include/kernaux/asm/)
@ -64,7 +63,6 @@ zero). Work-in-progress APIs can change at any time.
* [Multiboot 2 (GRUB 2)](/include/kernaux/multiboot2.h) (*work in progress*)
* Stivale 2 (Limine) (*planned*)
* Utilities
* [Sized void](/include/kernaux/sized_void.h) (*work in progress*)
* [Measurement units utils](/include/kernaux/units.h) (*work in progress*)
* [Example: To human](/examples/units_human.c)
* [Memory map](/include/kernaux/memmap.h.in) (*non-breaking since* **0.4.0**)

View File

@ -2,23 +2,23 @@
#ifdef KERNAUX_VERSION_WITH_IO
static VALUE rb_KernAux_OldFile_initialize(VALUE self, VALUE out);
static VALUE rb_KernAux_File_initialize(VALUE self, VALUE out);
static ID rb_intern_ATout = Qnil;
static VALUE rb_KernAux_OldFile = Qnil;
static VALUE rb_KernAux_File = Qnil;
void init_io() {
rb_gc_register_mark_object(ID2SYM(rb_intern_ATout = rb_intern("@out")));
rb_gc_register_mark_object(rb_KernAux_OldFile =
rb_gc_register_mark_object(rb_KernAux_File =
rb_define_class_under(rb_KernAux, "File", rb_cObject));
rb_define_method(rb_KernAux_OldFile, "initialize",
rb_KernAux_OldFile_initialize, 1);
rb_define_method(rb_KernAux_File, "initialize",
rb_KernAux_File_initialize, 1);
}
VALUE rb_KernAux_OldFile_initialize(VALUE self, VALUE out)
VALUE rb_KernAux_File_initialize(VALUE self, VALUE out)
{
rb_ivar_set(self, rb_intern_ATout, out);
return Qnil;

View File

@ -45,16 +45,6 @@ fprintf_va_SOURCES = main.c fprintf_va.c
endif
endif
###############
# io_memstore #
###############
if WITH_IO
TESTS += io_memstore
io_memstore_LDADD = $(top_builddir)/libkernaux.la
io_memstore_SOURCES = main.c io_memstore.c
endif
##########
# memmap #
##########

View File

@ -22,7 +22,7 @@ static void my_putchar(const char chr, void *arg)
void example_main()
{
struct KernAux_OldFile file = KernAux_OldFile_create(my_putchar);
struct KernAux_File file = KernAux_File_create(my_putchar);
const int result = kernaux_fprintf(
&file,
(char*)data,

View File

@ -24,7 +24,7 @@ static int my_printf(const char *const format, ...)
{
va_list va;
va_start(va, format);
struct KernAux_OldFile file = KernAux_OldFile_create(my_putchar);
struct KernAux_File file = KernAux_File_create(my_putchar);
const int result = kernaux_vfprintf(&file, (char*)data, format, va);
va_end(va);
return result;

View File

@ -1,56 +0,0 @@
#include <kernaux/io.h>
#include <assert.h>
#include <string.h>
#define BUFFER_SIZE 4096
#define SMALL_BUFFER_SIZE 5
static const char *const hello = "Hello, World!";
static char buffer[BUFFER_SIZE];
static struct KernAux_MemStore buffer_mem_store;
static struct KernAux_SizedVoid buffer_mem_store_files_sized_void;
static unsigned char buffer_mem_store_files_void[1000];
void example_main()
{
KernAux_SizedVoid_init(
&buffer_mem_store_files_sized_void,
buffer_mem_store_files_void,
sizeof(buffer_mem_store_files_void)
);
{
KernAux_MemStore_init(
&buffer_mem_store,
&buffer_mem_store_files_sized_void,
buffer,
BUFFER_SIZE
);
KernAux_File file = KernAux_Store_open(&buffer_mem_store.store);
assert(file);
assert(KernAux_File_puts(file, hello));
assert(strncmp(buffer, hello, strlen(hello)) == 0);
}
memset(buffer, 0, sizeof(buffer));
{
KernAux_MemStore_init(
&buffer_mem_store,
&buffer_mem_store_files_sized_void,
buffer,
SMALL_BUFFER_SIZE
);
KernAux_File file = KernAux_Store_open(&buffer_mem_store.store);
assert(file);
assert(!KernAux_File_puts(file, hello));
assert(strncmp(buffer, hello, SMALL_BUFFER_SIZE) == 0);
assert(strncmp(buffer, hello, strlen(hello)) != 0);
}
}

View File

@ -4,7 +4,6 @@ nobase_include_HEADERS = \
kernaux/arch/riscv64.h \
kernaux/arch/x86_64.h \
kernaux/assert.h \
kernaux/sized_void.h \
kernaux/version.h
if ASM_I386

View File

@ -4,7 +4,6 @@
*/
#include <kernaux/assert.h>
#include <kernaux/sized_void.h>
#include <kernaux/version.h>
@comment_line_cmdline@#include <kernaux/cmdline.h>

View File

@ -5,73 +5,14 @@
extern "C" {
#endif
#include <kernaux/sized_void.h>
typedef void (*KernAux_File_Out)(char c, void *arg);
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
typedef struct KernAux_File {
KernAux_File_Out out;
} *KernAux_File;
#define KERNAUX_EOF (-1)
/****************
* KernAux_File *
****************/
typedef const struct KernAux_File *KernAux_File;
size_t KernAux_File_write(KernAux_File file, const void *buffer, size_t size);
int KernAux_File_putc(KernAux_File file, int c);
bool KernAux_File_puts(KernAux_File file, const char *s);
/*****************
* KernAux_Store *
*****************/
typedef KernAux_File (*KernAux_Store_Open)(void *store);
typedef size_t (*KernAux_Store_Read)(void *store, void *buffer, size_t size);
typedef size_t (*KernAux_Store_Write)(void *store, const void *buffer, size_t size);
typedef struct KernAux_Store {
KernAux_SizedVoid files;
KernAux_Store_Open open;
KernAux_Store_Read read;
KernAux_Store_Write write;
} *KernAux_Store;
KernAux_File KernAux_Store_open(KernAux_Store store);
/********************
* KernAux_MemStore *
********************/
typedef struct KernAux_MemStore {
struct KernAux_Store store;
uint8_t *start, *end, *limit, *ptr;
size_t size;
} *KernAux_MemStore;
struct KernAux_MemStore
KernAux_MemStore_create(KernAux_SizedVoid files, void *start, size_t size);
void KernAux_MemStore_init(
KernAux_MemStore mem_store,
KernAux_SizedVoid files,
void *start,
size_t size
);
/***********
* Old API *
***********/
typedef void (*KernAux_OldFile_Out)(char c, void *arg);
typedef struct KernAux_OldFile {
KernAux_OldFile_Out out;
} *KernAux_OldFile;
struct KernAux_OldFile KernAux_OldFile_create(KernAux_OldFile_Out out);
void KernAux_OldFile_init(KernAux_OldFile file, KernAux_OldFile_Out out);
struct KernAux_File KernAux_File_create(KernAux_File_Out out);
void KernAux_File_init(KernAux_File file, KernAux_File_Out out);
#ifdef __cplusplus
}

View File

@ -18,8 +18,8 @@ extern "C" {
* \param va A value identifying a variable arguments list
* \return The number of characters that are sent to the output function, not counting the terminating null character
*/
@comment_line_io@int kernaux_fprintf(KernAux_OldFile file, void* arg, const char* format, ...);
@comment_line_io@int kernaux_vfprintf(KernAux_OldFile file, void* arg, const char* format, va_list va);
@comment_line_io@int kernaux_fprintf(KernAux_File file, void* arg, const char* format, ...);
@comment_line_io@int kernaux_vfprintf(KernAux_File file, void* arg, const char* format, va_list va);
/**
* Tiny [v]snprintf implementation

View File

@ -1,30 +0,0 @@
#ifndef KERNAUX_INCLUDED_SIZED_VOID
#define KERNAUX_INCLUDED_SIZED_VOID
#ifdef __cplusplus
extern "C" {
#endif
#include <stddef.h>
typedef struct KernAux_SizedVoid {
size_t memory_size;
void *ptr;
} *KernAux_SizedVoid;
struct KernAux_SizedVoid
KernAux_SizedVoid_create(void *ptr, size_t memory_size);
void KernAux_SizedVoid_init(
KernAux_SizedVoid sized_void,
void *ptr,
size_t memory_size
);
void KernAux_SizedVoid_memset(KernAux_SizedVoid sized_void, int c);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -58,7 +58,7 @@ void kernaux_console_printf(const char *format, ...)
va_list va;
va_start(va, format);
struct KernAux_OldFile file = KernAux_OldFile_create(kernaux_console_printf_putc);
struct KernAux_File file = KernAux_File_create(kernaux_console_printf_putc);
kernaux_vfprintf(&file, NULL, format, va);
va_end(va);
}

170
src/io.c
View File

@ -5,180 +5,16 @@
#include <kernaux/assert.h>
#include <kernaux/io.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>
/*****************
* KernAux_Store *
*****************/
KernAux_File KernAux_Store_open(const KernAux_Store store)
struct KernAux_File KernAux_File_create(const KernAux_File_Out out)
{
KERNAUX_ASSERT(store);
KERNAUX_ASSERT(store->open);
return store->open(store);
}
/****************
* KernAux_File *
****************/
struct KernAux_File {
bool is_active;
KernAux_Store store;
};
size_t KernAux_File_write(
const KernAux_File file,
const void *const buffer,
const size_t size
) {
KERNAUX_ASSERT(file);
KERNAUX_ASSERT(file->store);
KERNAUX_ASSERT(file->store->write);
return file->store->write(file->store, buffer, size);
}
int KernAux_File_putc(const KernAux_File file, const int c)
{
KERNAUX_ASSERT(file);
char buffer[1] = { c };
const size_t result = KernAux_File_write(file, buffer, 1);
return result == 0 ? KERNAUX_EOF : buffer[0];
}
bool KernAux_File_puts(const KernAux_File file, const char *const s)
{
KERNAUX_ASSERT(file);
if (!s) return 0;
const size_t size = strlen(s);
return KernAux_File_write(file, s, size) >= size;
}
/********************
* KernAux_MemStore *
********************/
#define MEMSTORE_FILES(mem_store) \
((struct KernAux_MemFile*)((mem_store)->store.files->ptr))
typedef struct KernAux_MemFile {
struct KernAux_File file;
uint8_t *ptr;
} *KernAux_MemFile;
static KernAux_File KernAux_MemStore_open(void *store);
static size_t KernAux_MemStore_read(void *store, void *buffer, size_t size);
static size_t KernAux_MemStore_write(void *store, const void *buffer, size_t size);
struct KernAux_MemStore KernAux_MemStore_create(
const KernAux_SizedVoid files,
void *const start,
const size_t size
) {
struct KernAux_MemStore mem_store;
KernAux_MemStore_init(&mem_store, files, start, size);
return mem_store;
}
void KernAux_MemStore_init(
KernAux_MemStore mem_store,
const KernAux_SizedVoid files,
void *const start,
const size_t size
) {
KERNAUX_ASSERT(mem_store);
KERNAUX_ASSERT(files);
KernAux_SizedVoid_memset(files, 0);
mem_store->store.files = files;
mem_store->store.open = KernAux_MemStore_open;
mem_store->store.write = KernAux_MemStore_write;
mem_store->store.read = KernAux_MemStore_read;
mem_store->start = start;
mem_store->size = size;
mem_store->end = mem_store->start + size - 1;
mem_store->limit = mem_store->start + size;
mem_store->ptr = mem_store->start;
KERNAUX_ASSERT(mem_store->start < mem_store->limit);
}
KernAux_File KernAux_MemStore_open(void *const store)
{
struct KernAux_MemStore *mem_store = store;
KERNAUX_ASSERT(mem_store);
// FIXME: open many files
const KernAux_MemFile mem_file = &MEMSTORE_FILES(mem_store)[0];
if (mem_file->file.is_active) return NULL;
mem_file->file.is_active = true;
mem_file->file.store = &mem_store->store;
mem_file->ptr = mem_store->start;
return &mem_file->file;
}
size_t KernAux_MemStore_read(
void *const store,
void *const buffer,
size_t size
) {
struct KernAux_MemStore *mem_store = store;
KERNAUX_ASSERT(mem_store);
KERNAUX_ASSERT(buffer);
uint8_t *buffer_ptr = buffer;
while (size && mem_store->ptr <= mem_store->end) {
--size;
*(buffer_ptr++) = *(mem_store->ptr++);
}
return (uint8_t*)buffer_ptr - (uint8_t*)buffer;
}
size_t KernAux_MemStore_write(
void *const store,
const void *const buffer,
size_t size
) {
struct KernAux_MemStore *mem_store = store;
KERNAUX_ASSERT(mem_store);
KERNAUX_ASSERT(buffer);
const uint8_t *buffer_ptr = buffer;
while (size && mem_store->ptr <= mem_store->end) {
--size;
*(mem_store->ptr++) = *(buffer_ptr++);
}
return (uint8_t*)buffer_ptr - (uint8_t*)buffer;
}
/***********
* Old API *
***********/
struct KernAux_OldFile KernAux_OldFile_create(const KernAux_OldFile_Out out)
{
struct KernAux_OldFile file;
KernAux_OldFile_init(&file, out);
KernAux_File_init(&file, out);
return file;
}
void KernAux_OldFile_init(const KernAux_OldFile file, const KernAux_OldFile_Out out)
void KernAux_File_init(const KernAux_File file, const KernAux_File_Out out)
{
KERNAUX_ASSERT(file);
KERNAUX_ASSERT(out);

View File

@ -70,7 +70,7 @@ static size_t _etoa(out_fct_type out, char* buffer, size_t idx, size_t maxlen, d
#ifdef WITH_IO
int kernaux_fprintf(const KernAux_OldFile file, void* arg, const char* format, ...)
int kernaux_fprintf(const KernAux_File file, void* arg, const char* format, ...)
{
KERNAUX_ASSERT(file);
KERNAUX_ASSERT(format);
@ -83,7 +83,7 @@ int kernaux_fprintf(const KernAux_OldFile file, void* arg, const char* format, .
return ret;
}
int kernaux_vfprintf(const KernAux_OldFile file, void* arg, const char* format, va_list va)
int kernaux_vfprintf(const KernAux_File file, void* arg, const char* format, va_list va)
{
KERNAUX_ASSERT(file);
KERNAUX_ASSERT(format);

View File

@ -1,41 +0,0 @@
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <kernaux/assert.h>
#include <kernaux/sized_void.h>
#include <stddef.h>
#include <string.h>
struct KernAux_SizedVoid
KernAux_SizedVoid_create(void *const ptr, const size_t memory_size)
{
KERNAUX_ASSERT(ptr);
KERNAUX_ASSERT(memory_size);
struct KernAux_SizedVoid sized_void;
KernAux_SizedVoid_init(&sized_void, ptr, memory_size);
return sized_void;
}
void KernAux_SizedVoid_init(
const KernAux_SizedVoid sized_void,
void *const ptr,
const size_t memory_size
) {
KERNAUX_ASSERT(sized_void);
KERNAUX_ASSERT(ptr);
KERNAUX_ASSERT(memory_size);
sized_void->memory_size = memory_size;
sized_void->ptr = ptr;
}
void KernAux_SizedVoid_memset(const KernAux_SizedVoid sized_void, const int c)
{
KERNAUX_ASSERT(sized_void);
KERNAUX_ASSERT(sized_void->ptr);
memset(sized_void->ptr, c, sized_void->memory_size);
}

View File

@ -45,7 +45,7 @@ static void test(const char *const expected, const char *const format, ...)
memset(buffer, '\0', sizeof(buffer));
buffer_index = 0;
va_start(va, format);
struct KernAux_OldFile file = KernAux_OldFile_create(test_putchar);
struct KernAux_File file = KernAux_File_create(test_putchar);
result = kernaux_vfprintf(&file, (char*)data, format, va);
va_end(va);
assert((size_t)result == strlen(expected));
@ -66,7 +66,7 @@ int main()
#ifdef WITH_IO
memset(buffer, '\0', sizeof(buffer));
buffer_index = 0;
struct KernAux_OldFile file = KernAux_OldFile_create(test_putchar);
struct KernAux_File file = KernAux_File_create(test_putchar);
kernaux_fprintf(&file, (char*)data, "Hello, World!");
assert(strcmp("Hello, World!", buffer) == 0);
#endif