Use generic file (#97)

This commit is contained in:
Alex Kotov 2022-06-25 18:43:18 +03:00 committed by GitHub
parent 6e2766b938
commit 74d14aff8f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
22 changed files with 95 additions and 141 deletions

View File

@ -1,6 +1,9 @@
2022-06-25 Alex Kotov <kotovalexarian@gmail.com>
* configure.ac: Removed package "io"
* include/kernaux/generic/file.h: Added
* include/kernaux/io.h: Removed
* include/kernaux/printf.h: Use new file API
2022-06-24 Alex Kotov <kotovalexarian@gmail.com>

View File

@ -80,9 +80,6 @@ endif
if WITH_FREE_LIST
libkernaux_la_SOURCES += src/free_list.c
endif
if WITH_IO
libkernaux_la_SOURCES += src/io.c
endif
if WITH_MBR
libkernaux_la_SOURCES += src/mbr.c
endif

View File

@ -44,7 +44,6 @@ zero). Work-in-progress APIs can change at any time.
* [Example: Assert](/examples/assert.c)
* [Example: Panic](/examples/panic.c)
* Stack trace *(planned)*
* [Input/output](/include/kernaux/io.h) (*work in progress*)
* Architecture-specific code (*work in progress*)
* [Declarations](/include/kernaux/arch/)
* [Functions](/include/kernaux/asm/)
@ -76,7 +75,7 @@ zero). Work-in-progress APIs can change at any time.
* Usual functions
* [itoa/ftoa replacement](/include/kernaux/ntoa.h) (*non-breaking since* **0.4.0**)
* [Example](/examples/ntoa.c)
* [printf replacement](/include/kernaux/printf.h.in) (*non-breaking since* **0.4.0**)
* [printf replacement](/include/kernaux/printf.h.in) (*non-breaking since* **?.?.?**)
* Code from [https://github.com/mpaland/printf](https://github.com/mpaland/printf). Thank you!
* [Example: fprintf](/examples/fprintf.c)
* [Example: vfprintf](/examples/fprintf_va.c)
@ -166,7 +165,6 @@ explicitly included, use `--without-all`.
* `--with[out]-asm` - kernel assembler helpers
* `--with[out]-cmdline` - command line parser
* `--with[out]-free-list` - free list memory allocator
* `--with[out]-io` - input/output
* `--with[out]-memmap` - memory map
* `--with[out]-ntoa` - itoa/ftoa
* `--with[out]-printf` - printf

View File

@ -65,7 +65,6 @@ AC_ARG_WITH( [asm], AS_HELP_STRING([--without-asm], [without
AC_ARG_WITH( [cmdline], AS_HELP_STRING([--without-cmdline], [without command line parser]))
AC_ARG_WITH( [elf], AS_HELP_STRING([--without-elf], [without ELF utils]))
AC_ARG_WITH( [free-list], AS_HELP_STRING([--without-free-list], [without free list memory allocator]))
AC_ARG_WITH( [io], AS_HELP_STRING([--without-io], [without input/output]))
AC_ARG_WITH( [mbr], AS_HELP_STRING([--without-mbr], [without Master Boot Record]))
AC_ARG_WITH( [memmap], AS_HELP_STRING([--without-memmap], [without memory map]))
AC_ARG_WITH( [multiboot2], AS_HELP_STRING([--without-multiboot2], [without Multiboot 2 information parser]))
@ -109,7 +108,6 @@ if test -z "$with_asm"; then with_asm=no; fi
if test -z "$with_cmdline"; then with_cmdline=no; fi
if test -z "$with_elf"; then with_elf=no; fi
if test -z "$with_free_list"; then with_free_list=no; fi
if test -z "$with_io"; then with_io=no; fi
if test -z "$with_mbr"; then with_mbr=no; fi
if test -z "$with_memmap"; then with_memmap=no; fi
if test -z "$with_multiboot2"; then with_multiboot2=no; fi
@ -149,7 +147,6 @@ AS_IF([test "$with_asm" = no ], [with_asm=no], [with_asm=
AS_IF([test "$with_cmdline" = no ], [with_cmdline=no], [with_cmdline=yes])
AS_IF([test "$with_elf" = no ], [with_elf=no], [with_elf=yes])
AS_IF([test "$with_free_list" = no ], [with_free_list=no], [with_free_list=yes])
AS_IF([test "$with_io" = no ], [with_io=no], [with_io=yes])
AS_IF([test "$with_mbr" = no ], [with_mbr=no], [with_mbr=yes])
AS_IF([test "$with_memmap" = no ], [with_memmap=no], [with_memmap=yes])
AS_IF([test "$with_multiboot2" = no ], [with_multiboot2=no], [with_multiboot2=yes])
@ -208,7 +205,6 @@ AM_CONDITIONAL([WITH_ASM], [test "$with_asm" = yes])
AM_CONDITIONAL([WITH_CMDLINE], [test "$with_cmdline" = yes])
AM_CONDITIONAL([WITH_ELF], [test "$with_elf" = yes])
AM_CONDITIONAL([WITH_FREE_LIST], [test "$with_free_list" = yes])
AM_CONDITIONAL([WITH_IO], [test "$with_io" = yes])
AM_CONDITIONAL([WITH_MBR], [test "$with_mbr" = yes])
AM_CONDITIONAL([WITH_MEMMAP], [test "$with_memmap" = yes])
AM_CONDITIONAL([WITH_MULTIBOOT2], [test "$with_multiboot2" = yes])
@ -252,7 +248,6 @@ AS_IF([test "$with_asm" = yes], [AC_DEFINE([WITH_ASM], [1]
AS_IF([test "$with_cmdline" = yes], [AC_DEFINE([WITH_CMDLINE], [1], [with command line parser])])
AS_IF([test "$with_elf" = yes], [AC_DEFINE([WITH_ELF], [1], [with ELF utils])])
AS_IF([test "$with_free_list" = yes], [AC_DEFINE([WITH_FREE_LIST], [1], [with free list memory allocator])])
AS_IF([test "$with_io" = yes], [AC_DEFINE([WITH_IO], [1], [with input/output])])
AS_IF([test "$with_mbr" = yes], [AC_DEFINE([WITH_MBR], [1], [with Master Boot Record])])
AS_IF([test "$with_memmap" = yes], [AC_DEFINE([WITH_MEMMAP], [1], [with memory map])])
AS_IF([test "$with_multiboot2" = yes], [AC_DEFINE([WITH_MULTIBOOT2], [1], [with Multiboot 2 information parser])])
@ -282,7 +277,6 @@ AS_IF([test "$with_arch_x86_64" = no], [AC_SUBST([comment_line_arch_x86_64], [
AS_IF([test "$with_cmdline" = no], [AC_SUBST([comment_line_cmdline], [//])])
AS_IF([test "$with_elf" = no], [AC_SUBST([comment_line_elf], [//])])
AS_IF([test "$with_free_list" = no], [AC_SUBST([comment_line_free_list], [//])])
AS_IF([test "$with_io" = no], [AC_SUBST([comment_line_io], [//])])
AS_IF([test "$with_mbr" = no], [AC_SUBST([comment_line_mbr], [//])])
AS_IF([test "$with_memmap" = no], [AC_SUBST([comment_line_memmap], [//])])
AS_IF([test "$with_multiboot2" = no], [AC_SUBST([comment_line_multiboot2], [//])])

View File

@ -26,24 +26,20 @@ endif
###########
if WITH_PRINTF
if WITH_IO
TESTS += fprintf
fprintf_LDADD = $(top_builddir)/libkernaux.la
fprintf_SOURCES = main.c fprintf.c
endif
endif
##############
# fprintf_va #
##############
if WITH_PRINTF
if WITH_IO
TESTS += fprintf_va
fprintf_va_LDADD = $(top_builddir)/libkernaux.la
fprintf_va_SOURCES = main.c fprintf_va.c
endif
endif
################
# generic_file #

View File

@ -1,4 +1,6 @@
#include <kernaux/io.h>
#define KERNAUX_ACCESS_PROTECTED
#include <kernaux/generic/file.h>
#include <kernaux/printf.h>
#include <assert.h>
@ -8,24 +10,26 @@
#define BUFFER_SIZE 1024
static const char *const data = "foobar";
static char buffer[BUFFER_SIZE];
static size_t buffer_index = 0;
static void my_putchar(const char chr, void *arg)
static int my_putc(__attribute__((unused)) void *const file, unsigned char c)
{
assert(arg == data);
if (buffer_index >= BUFFER_SIZE) abort();
buffer[buffer_index++] = chr;
buffer[buffer_index++] = c;
return 1;
}
static const struct KernAux_File file = {
.putc = my_putc,
.puts = NULL,
.write = NULL,
};
void example_main()
{
struct KernAux_OldFile file = KernAux_OldFile_create(my_putchar);
const int result = kernaux_fprintf(
&file,
(char*)data,
"Hello, %s! Session ID: %u.",
"Alex",
123

View File

@ -1,4 +1,6 @@
#include <kernaux/io.h>
#define KERNAUX_ACCESS_PROTECTED
#include <kernaux/generic/file.h>
#include <kernaux/printf.h>
#include <assert.h>
@ -8,24 +10,27 @@
#define BUFFER_SIZE 1024
static const char *const data = "foobar";
static char buffer[BUFFER_SIZE];
static size_t buffer_index = 0;
static void my_putchar(const char chr, void *arg)
static int my_putc(__attribute__((unused)) void *const file, unsigned char c)
{
assert(arg == data);
if (buffer_index >= BUFFER_SIZE) abort();
buffer[buffer_index++] = chr;
buffer[buffer_index++] = c;
return 1;
}
static const struct KernAux_File file = {
.putc = my_putc,
.puts = NULL,
.write = NULL,
};
static int my_printf(const char *const format, ...)
{
va_list va;
va_start(va, format);
struct KernAux_OldFile file = KernAux_OldFile_create(my_putchar);
const int result = kernaux_vfprintf(&file, (char*)data, format, va);
const int result = kernaux_vfprintf(&file, format, va);
va_end(va);
return result;
}

View File

@ -56,9 +56,6 @@ endif
if WITH_FREE_LIST
nobase_include_HEADERS += kernaux/free_list.h
endif
if WITH_IO
nobase_include_HEADERS += kernaux/io.h
endif
if WITH_MBR
nobase_include_HEADERS += kernaux/mbr.h
endif

View File

@ -18,7 +18,6 @@
@comment_line_cmdline@#include <kernaux/cmdline.h>
@comment_line_elf@#include <kernaux/elf.h>
@comment_line_free_list@#include <kernaux/free_list.h>
@comment_line_io@#include <kernaux/io.h>
@comment_line_mbr@#include <kernaux/mbr.h>
@comment_line_memmap@#include <kernaux/memmap.h>
@comment_line_multiboot2@#include <kernaux/multiboot2.h>

View File

@ -15,7 +15,7 @@ typedef int (*KernAux_File_Putc) (void *file, unsigned char c);
typedef int (*KernAux_File_Puts) (void *file, const char *s);
typedef int (*KernAux_File_Write)(void *file, const void *buffer, size_t count);
typedef struct KernAux_File {
typedef const struct KernAux_File {
KernAux_File_Putc KERNAUX_PROTECTED_FIELD(putc);
KernAux_File_Puts KERNAUX_PROTECTED_FIELD(puts);
KernAux_File_Write KERNAUX_PROTECTED_FIELD(write);

View File

@ -14,7 +14,7 @@ typedef void (*KernAux_Malloc_Free) (void *malloc, void *ptr);
typedef void *(*KernAux_Malloc_Malloc) (void *malloc, size_t size);
typedef void *(*KernAux_Malloc_Realloc)(void *malloc, void *ptr, size_t size);
typedef struct KernAux_Malloc {
typedef const struct KernAux_Malloc {
KernAux_Malloc_Calloc KERNAUX_PROTECTED_FIELD(calloc);
KernAux_Malloc_Free KERNAUX_PROTECTED_FIELD(free);
KernAux_Malloc_Malloc KERNAUX_PROTECTED_FIELD(malloc);

View File

@ -10,7 +10,7 @@ extern "C" {
typedef void (*KernAux_Mutex_Lock )(void *mutex);
typedef void (*KernAux_Mutex_Unlock)(void *mutex);
typedef struct KernAux_Mutex {
typedef const struct KernAux_Mutex {
KernAux_Mutex_Lock KERNAUX_PROTECTED_FIELD(lock);
KernAux_Mutex_Unlock KERNAUX_PROTECTED_FIELD(unlock);
} *KernAux_Mutex;

View File

@ -1,21 +0,0 @@
#ifndef KERNAUX_INCLUDED_IO
#define KERNAUX_INCLUDED_IO
#ifdef __cplusplus
extern "C" {
#endif
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);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -5,21 +5,20 @@
extern "C" {
#endif
#include <kernaux/generic/file.h>
#include <stdarg.h>
#include <stddef.h>
@comment_line_io@#include <kernaux/io.h>
/**
* Tiny [v]fprintf implementation
* \param file An output file
* \param arg An argument pointer for user data passed to output function
* \param format A string that specifies the format of the output
* \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);
int kernaux_fprintf(KernAux_File file, const char* format, ...);
int kernaux_vfprintf(KernAux_File file, const char* format, va_list va);
/**
* Tiny [v]snprintf implementation

View File

@ -3,7 +3,6 @@
@comment_line_cmdline@#define KERNAUX_VERSION_WITH_CMDLINE
@comment_line_elf@#define KERNAUX_VERSION_WITH_ELF
@comment_line_io@#define KERNAUX_VERSION_WITH_IO
@comment_line_mbr@#define KERNAUX_VERSION_WITH_MBR
@comment_line_multiboot2@#define KERNAUX_VERSION_WITH_MULTIBOOT2
@comment_line_ntoa@#define KERNAUX_VERSION_WITH_NTOA

View File

@ -4,6 +4,7 @@
#include <kernaux/assert.h>
#include <kernaux/drivers/console.h>
#include <kernaux/generic/file.h>
#ifdef ASM_I386
#include <kernaux/asm/i386.h>
@ -12,22 +13,20 @@
#include <kernaux/asm/x86_64.h>
#endif
#ifdef WITH_IO
#include <kernaux/io.h>
#endif
#ifdef WITH_PRINTF
#include <kernaux/printf.h>
#endif
#include <stddef.h>
#if defined(WITH_IO) && defined(WITH_PRINTF)
static void kernaux_drivers_console_printf_putc(
const char c,
void *const arg __attribute__((unused))
) {
kernaux_drivers_console_putc(c);
}
#ifdef WITH_PRINTF
static int file_putc(void *file, unsigned char c);
static const struct KernAux_File file = {
.putc = file_putc,
.puts = NULL,
.write = NULL,
};
#endif
void kernaux_drivers_console_putc(const char c __attribute__((unused)))
@ -49,16 +48,14 @@ void kernaux_drivers_console_print(const char *const s)
}
}
#if defined(WITH_IO) && defined(WITH_PRINTF)
#ifdef WITH_PRINTF
void kernaux_drivers_console_printf(const char *format, ...)
{
KERNAUX_ASSERT(format);
va_list va;
va_start(va, format);
struct KernAux_OldFile file =
KernAux_OldFile_create(kernaux_drivers_console_printf_putc);
kernaux_vfprintf(&file, NULL, format, va);
kernaux_vfprintf(&file, format, va);
va_end(va);
}
#endif
@ -79,3 +76,11 @@ void kernaux_drivers_console_write(const char *const data, const size_t size)
kernaux_drivers_console_putc(data[i]);
}
}
#ifdef WITH_PRINTF
int file_putc(__attribute__((unused)) void *const file, const unsigned char c)
{
kernaux_drivers_console_putc(c);
return 1;
}
#endif

View File

@ -12,7 +12,7 @@ int KernAux_File_putc(const KernAux_File file, const int c)
KERNAUX_ASSERT(file);
KERNAUX_ASSERT(file->putc);
return file->putc(file, c);
return file->putc((void*)file, c);
}
int KernAux_File_puts(const KernAux_File file, const char *const s)
@ -23,7 +23,7 @@ int KernAux_File_puts(const KernAux_File file, const char *const s)
if (!s) return 0;
// Inherited implementation
if (file->puts) return file->puts(file, s);
if (file->puts) return file->puts((void*)file, s);
// Default implementation
size_t ccount = 0;
@ -45,7 +45,7 @@ int KernAux_File_write(
if (count == 0 || !buffer) return 0;
// Inherited implementation
if (file->write) return file->write(file, buffer, count);
if (file->write) return file->write((void*)file, buffer, count);
// Default implementation
size_t ccount = 0;

View File

@ -12,7 +12,7 @@ void *KernAux_Malloc_calloc(KernAux_Malloc malloc, size_t nmemb, size_t size)
KERNAUX_ASSERT(malloc);
KERNAUX_ASSERT(malloc->calloc);
return malloc->calloc(malloc, nmemb, size);
return malloc->calloc((void*)malloc, nmemb, size);
}
void KernAux_Malloc_free(KernAux_Malloc malloc, void *ptr)
@ -20,7 +20,7 @@ void KernAux_Malloc_free(KernAux_Malloc malloc, void *ptr)
KERNAUX_ASSERT(malloc);
KERNAUX_ASSERT(malloc->free);
malloc->free(malloc, ptr);
malloc->free((void*)malloc, ptr);
}
void *KernAux_Malloc_malloc(KernAux_Malloc malloc, size_t size)
@ -28,7 +28,7 @@ void *KernAux_Malloc_malloc(KernAux_Malloc malloc, size_t size)
KERNAUX_ASSERT(malloc);
KERNAUX_ASSERT(malloc->malloc);
return malloc->malloc(malloc, size);
return malloc->malloc((void*)malloc, size);
}
void *KernAux_Malloc_realloc(KernAux_Malloc malloc, void *ptr, size_t size)
@ -36,5 +36,5 @@ void *KernAux_Malloc_realloc(KernAux_Malloc malloc, void *ptr, size_t size)
KERNAUX_ASSERT(malloc);
KERNAUX_ASSERT(malloc->realloc);
return malloc->realloc(malloc, ptr, size);
return malloc->realloc((void*)malloc, ptr, size);
}

View File

@ -10,7 +10,7 @@ void KernAux_Mutex_lock(const KernAux_Mutex mutex)
KERNAUX_ASSERT(mutex);
KERNAUX_ASSERT(mutex->lock);
mutex->lock(mutex);
mutex->lock((void*)mutex);
}
void KernAux_Mutex_unlock(const KernAux_Mutex mutex)
@ -18,5 +18,5 @@ void KernAux_Mutex_unlock(const KernAux_Mutex mutex)
KERNAUX_ASSERT(mutex);
KERNAUX_ASSERT(mutex->unlock);
mutex->unlock(mutex);
mutex->unlock((void*)mutex);
}

View File

@ -1,23 +0,0 @@
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <kernaux/assert.h>
#include <kernaux/io.h>
#include <stddef.h>
struct KernAux_OldFile KernAux_OldFile_create(const KernAux_OldFile_Out out)
{
struct KernAux_OldFile file;
KernAux_OldFile_init(&file, out);
return file;
}
void KernAux_OldFile_init(const KernAux_OldFile file, const KernAux_OldFile_Out out)
{
KERNAUX_ASSERT(file);
KERNAUX_ASSERT(out);
file->out = out;
}

View File

@ -14,6 +14,7 @@
#endif
#include <kernaux/assert.h>
#include <kernaux/generic/file.h>
#include <kernaux/printf.h>
#include <kernaux/printf_fmt.h>
@ -52,6 +53,8 @@ typedef struct {
static int _vsnprintf(out_fct_type out, char* buffer, const size_t maxlen, const char* format, va_list va);
static void file_putc(char c, void *arg);
static inline void _out_buffer(char character, void* buffer, size_t idx, size_t maxlen);
static inline void _out_null(char character, void* buffer, size_t idx, size_t maxlen);
static inline void _out_fct(char character, void* buffer, size_t idx, size_t maxlen);
@ -69,32 +72,28 @@ static size_t _etoa(out_fct_type out, char* buffer, size_t idx, size_t maxlen, d
* Implementations: main API *
*****************************/
#ifdef WITH_IO
int kernaux_fprintf(const KernAux_OldFile file, void* arg, const char* format, ...)
int kernaux_fprintf(const KernAux_File file, const char* format, ...)
{
KERNAUX_ASSERT(file);
KERNAUX_ASSERT(format);
va_list va;
va_start(va, format);
const out_fct_wrap_type out_fct_wrap = { file->out, arg };
const out_fct_wrap_type out_fct_wrap = { file_putc, (void*)file };
const int ret = _vsnprintf(_out_fct, (char*)(uintptr_t)&out_fct_wrap, (size_t)-1, format, va);
va_end(va);
return ret;
}
int kernaux_vfprintf(const KernAux_OldFile file, void* arg, const char* format, va_list va)
int kernaux_vfprintf(const KernAux_File file, const char* format, va_list va)
{
KERNAUX_ASSERT(file);
KERNAUX_ASSERT(format);
const out_fct_wrap_type out_fct_wrap = { file->out, arg };
const out_fct_wrap_type out_fct_wrap = { file_putc, (void*)file };
return _vsnprintf(_out_fct, (char*)(uintptr_t)&out_fct_wrap, (size_t)-1, format, va);
}
#endif // WITH_IO
int kernaux_snprintf(char* buffer, size_t count, const char* format, ...)
{
KERNAUX_ASSERT(buffer);
@ -127,6 +126,16 @@ int kernaux_sprintf(char* buffer, const char* format, ...)
return ret;
}
/***************************
* Implementation: file IO *
***************************/
void file_putc(const char c, void *const arg)
{
KernAux_File file = arg;
KernAux_File_putc(file, c);
}
/******************************************
* Implementation: main internal function *
******************************************/

View File

@ -2,13 +2,13 @@
#include "config.h"
#endif
#define KERNAUX_ACCESS_PROTECTED
#include <kernaux/generic/file.h>
#include <kernaux/printf.h>
#ifdef WITH_IO
#include <kernaux/io.h>
#endif
#include <assert.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@ -18,39 +18,35 @@
static char buffer[BUFFER_SIZE];
static size_t buffer_index;
#ifdef WITH_IO
static const char *const data = "foobar";
static void test_putchar(const char chr, void *const arg)
static int test_putc(__attribute__((unused)) void *const file, unsigned char c)
{
assert(arg == data);
if (buffer_index >= BUFFER_SIZE) {
printf("Buffer overflow!\n");
abort();
}
buffer[buffer_index++] = chr;
buffer[buffer_index++] = c;
return 1;
}
#endif // WITH_IO
static const struct KernAux_File file = {
.putc = test_putc,
.puts = NULL,
.write = NULL,
};
static void test(const char *const expected, const char *const format, ...)
{
va_list va;
int result;
#ifdef WITH_IO
memset(buffer, '\0', sizeof(buffer));
buffer_index = 0;
va_start(va, format);
struct KernAux_OldFile file = KernAux_OldFile_create(test_putchar);
result = kernaux_vfprintf(&file, (char*)data, format, va);
result = kernaux_vfprintf(&file, format, va);
va_end(va);
assert((size_t)result == strlen(expected));
assert(strcmp(expected, buffer) == 0);
#endif
memset(buffer, '\0', sizeof(buffer));
buffer_index = 0;
@ -63,13 +59,10 @@ static void test(const char *const expected, const char *const format, ...)
void test_main()
{
#ifdef WITH_IO
memset(buffer, '\0', sizeof(buffer));
buffer_index = 0;
struct KernAux_OldFile file = KernAux_OldFile_create(test_putchar);
kernaux_fprintf(&file, (char*)data, "Hello, World!");
kernaux_fprintf(&file, "Hello, World!");
assert(strcmp("Hello, World!", buffer) == 0);
#endif
{% for case in cases %}
{% if case.float %}