mirror of
https://github.com/tailix/libclayer.git
synced 2024-11-20 11:06:24 -05:00
Remove examples
This commit is contained in:
parent
34a934c4e1
commit
d97262d9b5
27 changed files with 5 additions and 1401 deletions
5
.gitignore
vendored
5
.gitignore
vendored
|
@ -42,7 +42,6 @@
|
|||
!/m4/.keep
|
||||
|
||||
/Makefile.in
|
||||
/examples/Makefile.in
|
||||
/include/Makefile.in
|
||||
/libc/Makefile.in
|
||||
/libc/include/Makefile.in
|
||||
|
@ -66,15 +65,11 @@
|
|||
/conftest.c
|
||||
/conftest.err
|
||||
|
||||
/examples/test-suite.log
|
||||
/examples/*.log
|
||||
/examples/*.trs
|
||||
/tests/test-suite.log
|
||||
/tests/test_*.log
|
||||
/tests/test_*.trs
|
||||
|
||||
/Makefile
|
||||
/examples/Makefile
|
||||
/include/Makefile
|
||||
/libc/Makefile
|
||||
/libc/include/Makefile
|
||||
|
|
|
@ -14,7 +14,7 @@ endif
|
|||
SUBDIRS += .
|
||||
|
||||
if ENABLE_CHECKS
|
||||
SUBDIRS += examples tests
|
||||
SUBDIRS += tests
|
||||
endif
|
||||
|
||||
libc/libc.la:
|
||||
|
|
20
README.md
20
README.md
|
@ -42,44 +42,26 @@ zero). Work-in-progress APIs can change at any time.
|
|||
* [Feature macros](/include/kernaux/version.h.in) (*work in progress*)
|
||||
* [Runtime environment](/include/kernaux/runtime.h) (*non-breaking since* **0.7.0**)
|
||||
* [Macros](/include/kernaux/macro.h) (*non-breaking since* **0.6.0**)
|
||||
* [Example: packing](/examples/macro_packing.c)
|
||||
* [Example: BITS](/examples/macro_bits.c)
|
||||
* [Example: CAST\_\*](/examples/macro_cast.c);
|
||||
* [Example: CONTAINER\_OF](/examples/macro_container_of.c)
|
||||
* [Example: STATIC\_TEST\*](/examples/macro_static_test.c)
|
||||
* Stack trace *(planned)*
|
||||
* Generic types
|
||||
* [Display](/include/kernaux/generic/display.h) (*non-breaking since* **0.7.0**)
|
||||
* [Example](/examples/generic_display.c)
|
||||
* [Memory allocator](/include/kernaux/generic/malloc.h) (*non-breaking since* **0.5.0**)
|
||||
* [Example](/examples/generic_malloc.c)
|
||||
* [Mutex](/include/kernaux/generic/mutex.h) (*non-breaking since* **0.5.0**)
|
||||
* Algorithms
|
||||
* [Free list memory allocator](/include/kernaux/free_list.h) (*non-breaking since* **0.5.0**)
|
||||
* [Simple command line parser](/include/kernaux/cmdline.h) (*non-breaking since* **0.2.0**)
|
||||
* [Example](/examples/cmdline.c)
|
||||
* [Page Frame Allocator](/include/kernaux/pfa.h) (*work in progress*)
|
||||
* [Example](/examples/pfa.c)
|
||||
* Data formats
|
||||
* [ELF](/include/kernaux/elf.h) (*work in progress*)
|
||||
* [MBR](/include/kernaux/mbr.h) (*work in progress*)
|
||||
* [Multiboot 2 (GRUB 2)](/include/kernaux/multiboot2.h.in) (*non-breaking since* **0.7.0**)
|
||||
* [Example: header macros](/examples/multiboot2_header_macro.c)
|
||||
* Utilities
|
||||
* [Measurement units utils](/include/kernaux/units.h) (*work in progress*)
|
||||
* [Example: To human](/examples/units_human.c)
|
||||
* [Memory map](/include/kernaux/memmap.h) (*non-breaking since* **0.7.0**)
|
||||
* [Example](/examples/memmap.c)
|
||||
* [printf format parser](/include/kernaux/printf_fmt.h) (*non-breaking since* **0.6.0**)
|
||||
* [Example](/examples/printf_fmt.c)
|
||||
* 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) (*non-breaking since* **0.5.0**)
|
||||
* [Example: fprintf](/examples/printf_file.c)
|
||||
* [Example: vfprintf](/examples/printf_file_va.c)
|
||||
* [Example: snprintf](/examples/printf_str.c)
|
||||
* [Example: vsnprintf](/examples/printf_str_va.c)
|
||||
* libc replacement (*work in progress*)
|
||||
* [ctype.h](/libc/include/ctype.h)
|
||||
* [errno.h](/libc/include/errno.h)
|
||||
|
@ -132,7 +114,7 @@ stable options.
|
|||
|
||||
#### Features
|
||||
|
||||
* `--enable-checks` - enable usual tests and examples
|
||||
* `--enable-checks` - enable usual tests
|
||||
* `--enable-checks-all` - enable all checks
|
||||
* `--enable-checks-cppcheck` - enable cppcheck
|
||||
Jinja2
|
||||
|
|
|
@ -26,7 +26,6 @@ AC_CONFIG_SRCDIR([src/runtime.c])
|
|||
AC_CONFIG_FILES([
|
||||
Makefile
|
||||
libkernaux.pc
|
||||
examples/Makefile
|
||||
include/Makefile
|
||||
libc/Makefile
|
||||
libc/include/Makefile
|
||||
|
@ -54,7 +53,7 @@ AC_ARG_ENABLE([werror], AS_HELP_STRING([--disable-werror], [dis
|
|||
|
||||
dnl Features (disabled by default)
|
||||
AC_ARG_ENABLE([freestanding], AS_HELP_STRING([--enable-freestanding], [build for freestanding environment]))
|
||||
AC_ARG_ENABLE([checks], AS_HELP_STRING([--enable-checks], [enable usual tests and examples]))
|
||||
AC_ARG_ENABLE([checks], AS_HELP_STRING([--enable-checks], [enable usual tests]))
|
||||
AC_ARG_ENABLE([checks-all], AS_HELP_STRING([--enable-checks-all], [enable all checks]))
|
||||
AC_ARG_ENABLE([checks-cppcheck], AS_HELP_STRING([--enable-checks-cppcheck], [enable cppcheck]))
|
||||
AC_ARG_ENABLE([split-libc], AS_HELP_STRING([--enable-split-libc], [split off libc]))
|
||||
|
@ -255,7 +254,7 @@ AS_IF([test "$enable_werror" = yes], [AC_DEFINE([ENABLE_WERROR],
|
|||
|
||||
dnl Features (disabled by default)
|
||||
AS_IF([test "$enable_freestanding" = yes], [AC_DEFINE([ENABLE_FREESTANDING], [1], [build for freestanding environment])])
|
||||
AS_IF([test "$enable_checks" = yes], [AC_DEFINE([ENABLE_CHECKS], [1], [enabled usual tests and examples])])
|
||||
AS_IF([test "$enable_checks" = yes], [AC_DEFINE([ENABLE_CHECKS], [1], [enabled usual tests])])
|
||||
AS_IF([test "$enable_checks_cppcheck" = yes], [AC_DEFINE([ENABLE_CHECKS_CPPCHECK], [1], [enabled cppcheck])])
|
||||
AS_IF([test "$enable_split_libc" = yes], [AC_DEFINE([ENABLE_SPLIT_LIBC], [1], [split off libc])])
|
||||
|
||||
|
|
18
examples/.gitignore
vendored
18
examples/.gitignore
vendored
|
@ -1,18 +0,0 @@
|
|||
/cmdline
|
||||
/generic_display
|
||||
/generic_malloc
|
||||
/macro_bits
|
||||
/macro_cast
|
||||
/macro_container_of
|
||||
/macro_packing
|
||||
/macro_static_test
|
||||
/memmap
|
||||
/multiboot2_header_macro
|
||||
/ntoa
|
||||
/pfa
|
||||
/printf_file
|
||||
/printf_file_va
|
||||
/printf_fmt
|
||||
/printf_str
|
||||
/printf_str_va
|
||||
/units_human
|
|
@ -1,170 +0,0 @@
|
|||
include $(top_srcdir)/make/shared.am
|
||||
|
||||
TESTS =
|
||||
noinst_PROGRAMS = $(TESTS)
|
||||
|
||||
###########
|
||||
# cmdline #
|
||||
###########
|
||||
|
||||
if WITH_CMDLINE
|
||||
TESTS += cmdline
|
||||
cmdline_LDADD = $(top_builddir)/libkernaux.la
|
||||
cmdline_SOURCES = main.c cmdline.c
|
||||
endif
|
||||
|
||||
###################
|
||||
# generic_display #
|
||||
###################
|
||||
|
||||
TESTS += generic_display
|
||||
generic_display_LDADD = $(top_builddir)/libkernaux.la
|
||||
generic_display_SOURCES = main.c generic_display.c
|
||||
|
||||
##################
|
||||
# generic_malloc #
|
||||
##################
|
||||
|
||||
TESTS += generic_malloc
|
||||
generic_malloc_LDADD = $(top_builddir)/libkernaux.la
|
||||
generic_malloc_SOURCES = main.c generic_malloc.c
|
||||
|
||||
##############
|
||||
# macro_bits #
|
||||
##############
|
||||
|
||||
TESTS += macro_bits
|
||||
macro_bits_LDADD = $(top_builddir)/libkernaux.la
|
||||
macro_bits_SOURCES = main.c macro_bits.c
|
||||
|
||||
##############
|
||||
# macro_cast #
|
||||
##############
|
||||
|
||||
TESTS += macro_cast
|
||||
macro_cast_LDADD = $(top_builddir)/libkernaux.la
|
||||
macro_cast_SOURCES = main.c macro_cast.c
|
||||
|
||||
######################
|
||||
# macro_container_of #
|
||||
######################
|
||||
|
||||
TESTS += macro_container_of
|
||||
macro_container_of_LDADD = $(top_builddir)/libkernaux.la
|
||||
macro_container_of_SOURCES = main.c macro_container_of.c
|
||||
|
||||
#################
|
||||
# macro_packing #
|
||||
#################
|
||||
|
||||
TESTS += macro_packing
|
||||
macro_packing_LDADD = $(top_builddir)/libkernaux.la
|
||||
macro_packing_SOURCES = main.c macro_packing.c
|
||||
|
||||
#####################
|
||||
# macro_static_test #
|
||||
#####################
|
||||
|
||||
TESTS += macro_static_test
|
||||
macro_static_test_LDADD = $(top_builddir)/libkernaux.la
|
||||
macro_static_test_SOURCES = main.c macro_static_test.c
|
||||
|
||||
##########
|
||||
# memmap #
|
||||
##########
|
||||
|
||||
if WITH_MEMMAP
|
||||
TESTS += memmap
|
||||
memmap_LDADD = $(top_builddir)/libkernaux.la
|
||||
memmap_SOURCES = main.c memmap.c
|
||||
endif
|
||||
|
||||
###########################
|
||||
# multiboot2_header_macro #
|
||||
###########################
|
||||
|
||||
if WITH_MULTIBOOT2
|
||||
TESTS += multiboot2_header_macro
|
||||
multiboot2_header_macro_LDADD = $(top_builddir)/libkernaux.la
|
||||
multiboot2_header_macro_SOURCES = main.c multiboot2_header_macro.c
|
||||
endif
|
||||
|
||||
########
|
||||
# ntoa #
|
||||
########
|
||||
|
||||
if WITH_NTOA
|
||||
TESTS += ntoa
|
||||
ntoa_LDADD = $(top_builddir)/libkernaux.la
|
||||
ntoa_SOURCES = main.c ntoa.c
|
||||
endif
|
||||
|
||||
#######
|
||||
# pfa #
|
||||
#######
|
||||
|
||||
if WITH_PFA
|
||||
TESTS += pfa
|
||||
pfa_LDADD = $(top_builddir)/libkernaux.la
|
||||
pfa_SOURCES = main.c pfa.c
|
||||
endif
|
||||
|
||||
###############
|
||||
# printf_file #
|
||||
###############
|
||||
|
||||
if WITH_PRINTF
|
||||
TESTS += printf_file
|
||||
printf_file_LDADD = $(top_builddir)/libkernaux.la
|
||||
printf_file_SOURCES = main.c printf_file.c
|
||||
endif
|
||||
|
||||
##################
|
||||
# printf_file_va #
|
||||
##################
|
||||
|
||||
if WITH_PRINTF
|
||||
TESTS += printf_file_va
|
||||
printf_file_va_LDADD = $(top_builddir)/libkernaux.la
|
||||
printf_file_va_SOURCES = main.c printf_file_va.c
|
||||
endif
|
||||
|
||||
##############
|
||||
# printf_fmt #
|
||||
##############
|
||||
|
||||
if WITH_PRINTF_FMT
|
||||
TESTS += printf_fmt
|
||||
printf_fmt_LDADD = $(top_builddir)/libkernaux.la
|
||||
printf_fmt_SOURCES = main.c printf_fmt.c
|
||||
endif
|
||||
|
||||
##############
|
||||
# printf_str #
|
||||
##############
|
||||
|
||||
if WITH_PRINTF
|
||||
TESTS += printf_str
|
||||
printf_str_LDADD = $(top_builddir)/libkernaux.la
|
||||
printf_str_SOURCES = main.c printf_str.c
|
||||
endif
|
||||
|
||||
#################
|
||||
# printf_str_va #
|
||||
#################
|
||||
|
||||
if WITH_PRINTF
|
||||
TESTS += printf_str_va
|
||||
printf_str_va_LDADD = $(top_builddir)/libkernaux.la
|
||||
printf_str_va_SOURCES = main.c printf_str_va.c
|
||||
endif
|
||||
|
||||
###############
|
||||
# units_human #
|
||||
###############
|
||||
|
||||
if WITH_UNITS
|
||||
TESTS += units_human
|
||||
units_human_LDADD = $(top_builddir)/libkernaux.la
|
||||
units_human_SOURCES = main.c units_human.c
|
||||
endif
|
|
@ -1,34 +0,0 @@
|
|||
#include <kernaux/cmdline.h>
|
||||
|
||||
#include <assert.h>
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
|
||||
#define ARG_COUNT_MAX 100
|
||||
#define BUFFER_SIZE 4096
|
||||
|
||||
static const char *const cmdline = "foo bar\\ baz \"car cdr\"";
|
||||
|
||||
void example_main()
|
||||
{
|
||||
char error_msg[KERNAUX_CMDLINE_ERROR_MSG_SIZE_MAX];
|
||||
size_t argc;
|
||||
char *argv[ARG_COUNT_MAX];
|
||||
char buffer[BUFFER_SIZE];
|
||||
|
||||
assert(kernaux_cmdline(
|
||||
cmdline,
|
||||
error_msg,
|
||||
&argc,
|
||||
argv,
|
||||
buffer,
|
||||
ARG_COUNT_MAX,
|
||||
BUFFER_SIZE
|
||||
));
|
||||
|
||||
assert(strcmp(error_msg, "") == 0);
|
||||
assert(argc == 3);
|
||||
assert(strcmp(argv[0], "foo") == 0);
|
||||
assert(strcmp(argv[1], "bar baz") == 0);
|
||||
assert(strcmp(argv[2], "car cdr") == 0);
|
||||
}
|
|
@ -1,115 +0,0 @@
|
|||
//==============
|
||||
// my_display.c
|
||||
//==============
|
||||
|
||||
// To not always use macro "KERNAUX_PROTECTED_FIELD" around the names of
|
||||
// structure fields you may define "KERNAUX_ACCESS_PROTECTED" before including
|
||||
// any other headers, but ONLY in the file where you implement a generic type.
|
||||
//
|
||||
#define KERNAUX_ACCESS_PROTECTED
|
||||
|
||||
//==============
|
||||
// my_display.h
|
||||
//==============
|
||||
|
||||
#include <kernaux/generic/display.h>
|
||||
#include <stddef.h>
|
||||
|
||||
typedef struct MyDisplay {
|
||||
struct KernAux_Display display;
|
||||
char *buffer, *cursor;
|
||||
size_t capacity;
|
||||
} *MyDisplay;
|
||||
|
||||
struct MyDisplay MyDisplay_create();
|
||||
|
||||
//==============
|
||||
// my_display.c
|
||||
//==============
|
||||
|
||||
#include <assert.h>
|
||||
#include <kernaux/generic/display.h>
|
||||
#include <stdarg.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define CAP_FREE (1024)
|
||||
#define CAP_INCR (2 * (CAP_FREE))
|
||||
|
||||
static void MyDisplay_putc(void *display, char c);
|
||||
void MyDisplay_vprintf(void *display, const char *format, va_list va);
|
||||
|
||||
static void MyDisplay_realloc(MyDisplay my_display);
|
||||
|
||||
struct MyDisplay MyDisplay_create()
|
||||
{
|
||||
char *const buffer = malloc(CAP_INCR);
|
||||
if (!buffer) abort();
|
||||
|
||||
return (struct MyDisplay){
|
||||
.display = {
|
||||
.putc = MyDisplay_putc,
|
||||
.vprintf = MyDisplay_vprintf,
|
||||
},
|
||||
.buffer = buffer,
|
||||
.cursor = buffer,
|
||||
.capacity = CAP_INCR,
|
||||
};
|
||||
}
|
||||
|
||||
void MyDisplay_putc(void *display, char c)
|
||||
{
|
||||
const MyDisplay my_display = display;
|
||||
MyDisplay_realloc(my_display);
|
||||
*(my_display->cursor++) = c;
|
||||
}
|
||||
|
||||
void MyDisplay_vprintf(void *display, const char *format, va_list va)
|
||||
{
|
||||
const MyDisplay my_display = display;
|
||||
MyDisplay_realloc(my_display);
|
||||
size_t left =
|
||||
my_display->buffer + my_display->capacity - my_display->cursor;
|
||||
const int delta = vsnprintf(my_display->cursor, left, format, va);
|
||||
if (delta < 0) abort();
|
||||
my_display->cursor += delta;
|
||||
}
|
||||
|
||||
void MyDisplay_realloc(const MyDisplay my_display)
|
||||
{
|
||||
size_t left =
|
||||
my_display->buffer + my_display->capacity - my_display->cursor;
|
||||
|
||||
if (left < CAP_FREE) {
|
||||
my_display->buffer =
|
||||
realloc(my_display->buffer, my_display->capacity + CAP_INCR);
|
||||
if (!my_display->buffer) abort();
|
||||
my_display->capacity += CAP_INCR;
|
||||
}
|
||||
}
|
||||
|
||||
//========
|
||||
// main.c
|
||||
//========
|
||||
|
||||
void example_main()
|
||||
{
|
||||
struct MyDisplay my_display = MyDisplay_create();
|
||||
|
||||
KernAux_Display_putc(&my_display.display, '@');
|
||||
KernAux_Display_print(&my_display.display, "print");
|
||||
KernAux_Display_println(&my_display.display, "println");
|
||||
KernAux_Display_write(&my_display.display, "write!!!", 5);
|
||||
KernAux_Display_writeln(&my_display.display, "writeln!!!", 7);
|
||||
KernAux_Display_printf(&my_display.display, "printf(%d)", 123);
|
||||
KernAux_Display_printlnf(&my_display.display, "printfln(%d)", 123);
|
||||
|
||||
assert(
|
||||
strcmp(
|
||||
my_display.buffer,
|
||||
"@printprintln\nwritewriteln\nprintf(123)printfln(123)\n"
|
||||
) == 0
|
||||
);
|
||||
}
|
|
@ -1,93 +0,0 @@
|
|||
//=============
|
||||
// my_malloc.c
|
||||
//=============
|
||||
|
||||
// To not always use macro "KERNAUX_PROTECTED_FIELD" around the names of
|
||||
// structure fields you may define "KERNAUX_ACCESS_PROTECTED" before including
|
||||
// any other headers, but ONLY in the file where you implement a generic type.
|
||||
//
|
||||
#define KERNAUX_ACCESS_PROTECTED
|
||||
|
||||
//=============
|
||||
// my_malloc.h
|
||||
//=============
|
||||
|
||||
#include <kernaux/generic/malloc.h>
|
||||
|
||||
typedef struct MyMalloc {
|
||||
struct KernAux_Malloc malloc;
|
||||
} *MyMalloc;
|
||||
|
||||
struct MyMalloc MyMalloc_create();
|
||||
|
||||
//=============
|
||||
// my_malloc.c
|
||||
//=============
|
||||
|
||||
#include <kernaux/generic/malloc.h>
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
static void *MyMalloc_calloc (void *malloc, size_t nmemb, size_t size);
|
||||
static void MyMalloc_free (void *malloc, void *ptr);
|
||||
static void *MyMalloc_malloc (void *malloc, size_t size);
|
||||
static void *MyMalloc_realloc(void *malloc, void *ptr, size_t size);
|
||||
|
||||
struct MyMalloc MyMalloc_create()
|
||||
{
|
||||
return (struct MyMalloc){
|
||||
.malloc = {
|
||||
.calloc = MyMalloc_calloc,
|
||||
.free = MyMalloc_free,
|
||||
.malloc = MyMalloc_malloc,
|
||||
.realloc = MyMalloc_realloc,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
void *MyMalloc_calloc(void *const malloc, const size_t nmemb, const size_t size)
|
||||
{
|
||||
(void)malloc; // unused
|
||||
return calloc(nmemb, size);
|
||||
}
|
||||
|
||||
void MyMalloc_free(void *const malloc, void *const ptr)
|
||||
{
|
||||
(void)malloc; // unused
|
||||
free(ptr);
|
||||
}
|
||||
|
||||
void *MyMalloc_malloc(void *const malloc_, const size_t size)
|
||||
{
|
||||
(void)malloc_; // unused
|
||||
return malloc(size);
|
||||
}
|
||||
|
||||
void *MyMalloc_realloc(void *const malloc, void *const ptr, const size_t size)
|
||||
{
|
||||
(void)malloc; // unused
|
||||
return realloc(ptr, size);
|
||||
}
|
||||
|
||||
//========
|
||||
// main.c
|
||||
//========
|
||||
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
|
||||
void example_main()
|
||||
{
|
||||
// Create memory allocator
|
||||
struct MyMalloc my_malloc = MyMalloc_create();
|
||||
|
||||
// Allocate memory
|
||||
void *ptr = KernAux_Malloc_malloc(&my_malloc.malloc, 1000);
|
||||
|
||||
// Use memory
|
||||
strcpy(ptr, "Hello, World!");
|
||||
assert(strcmp(ptr, "Hello, World!") == 0);
|
||||
|
||||
// Free memory
|
||||
KernAux_Malloc_free(&my_malloc.malloc, ptr);
|
||||
}
|
|
@ -1,21 +0,0 @@
|
|||
#include <kernaux/macro.h>
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
void example_main()
|
||||
{
|
||||
assert(KERNAUX_BITS(0) == 0x1);
|
||||
assert(KERNAUX_BITS(31) == 0x80000000);
|
||||
|
||||
assert(KERNAUX_BITS8(0) == 0x1);
|
||||
assert(KERNAUX_BITS8(7) == 0x80);
|
||||
|
||||
assert(KERNAUX_BITS16(0) == 0x1);
|
||||
assert(KERNAUX_BITS16(15) == 0x8000);
|
||||
|
||||
assert(KERNAUX_BITS32(0) == 0x1);
|
||||
assert(KERNAUX_BITS32(31) == 0x80000000);
|
||||
|
||||
assert(KERNAUX_BITS64(0) == 0x1);
|
||||
assert(KERNAUX_BITS64(63) == 0x8000000000000000);
|
||||
}
|
|
@ -1,25 +0,0 @@
|
|||
#include <kernaux/macro.h>
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdint.h>
|
||||
|
||||
void example_main()
|
||||
{
|
||||
const uint32_t value = 123;
|
||||
|
||||
// const unsigned long ul = (unsigned long)value;
|
||||
KERNAUX_CAST_CONST(unsigned long, ul, value);
|
||||
assert(ul == 123);
|
||||
|
||||
// unsigned long long ull = (unsigned long long)value;
|
||||
KERNAUX_CAST_VAR(unsigned long long, ull, value);
|
||||
assert(ull == 123);
|
||||
|
||||
// const [signed] long sl = ([signed] long)value;
|
||||
KERNAUX_CAST_CONST(long, sl, value);
|
||||
assert(sl == 123);
|
||||
|
||||
// [signed] long long sll = ([signed] long long)value;
|
||||
KERNAUX_CAST_VAR(long long, sll, value);
|
||||
assert(sll == 123);
|
||||
}
|
|
@ -1,35 +0,0 @@
|
|||
#include <kernaux/macro.h>
|
||||
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
|
||||
struct Foo {
|
||||
const char *hello;
|
||||
};
|
||||
|
||||
struct Bar {
|
||||
unsigned char a;
|
||||
unsigned int b;
|
||||
struct Foo foo;
|
||||
unsigned short c;
|
||||
};
|
||||
|
||||
static const struct Bar bar = {
|
||||
.a = 143,
|
||||
.b = 820794098,
|
||||
.foo = {
|
||||
.hello = "Hello, World!",
|
||||
},
|
||||
.c = 10981,
|
||||
};
|
||||
|
||||
void example_main()
|
||||
{
|
||||
const struct Bar *const bar_ptr =
|
||||
KERNAUX_CONTAINER_OF(&bar.foo, struct Bar, foo);
|
||||
|
||||
assert(bar_ptr->a == 143);
|
||||
assert(bar_ptr->b == 820794098);
|
||||
assert(strcmp(bar_ptr->foo.hello, "Hello, World!") == 0);
|
||||
assert(bar_ptr->c == 10981);
|
||||
}
|
|
@ -1,25 +0,0 @@
|
|||
#include <kernaux/macro.h>
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdint.h>
|
||||
|
||||
struct Foo {
|
||||
uint8_t a;
|
||||
uint32_t b;
|
||||
};
|
||||
|
||||
#include <kernaux/macro/packing_start.run>
|
||||
|
||||
struct Bar {
|
||||
uint8_t a;
|
||||
uint32_t b;
|
||||
}
|
||||
KERNAUX_PACKED;
|
||||
|
||||
#include <kernaux/macro/packing_end.run>
|
||||
|
||||
void example_main()
|
||||
{
|
||||
assert(sizeof(struct Foo) > 5);
|
||||
assert(sizeof(struct Bar) == 5);
|
||||
}
|
|
@ -1,28 +0,0 @@
|
|||
#include <kernaux/macro.h>
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
KERNAUX_STATIC_TEST(uint8_t_size, sizeof(uint8_t) == 1);
|
||||
KERNAUX_STATIC_TEST(uint16_t_size, sizeof(uint16_t) == 2);
|
||||
KERNAUX_STATIC_TEST(uint32_t_size, sizeof(uint32_t) == 4);
|
||||
KERNAUX_STATIC_TEST(uint64_t_size, sizeof(uint64_t) == 8);
|
||||
|
||||
#include <kernaux/macro/packing_start.run>
|
||||
|
||||
struct Foo {
|
||||
uint8_t a;
|
||||
uint32_t b;
|
||||
} KERNAUX_PACKED;
|
||||
|
||||
KERNAUX_STATIC_TEST_STRUCT_SIZE(Foo, 5);
|
||||
|
||||
union Bar {
|
||||
uint8_t a;
|
||||
uint16_t b;
|
||||
} KERNAUX_PACKED;
|
||||
|
||||
KERNAUX_STATIC_TEST_UNION_SIZE(Bar, 2);
|
||||
|
||||
#include <kernaux/macro/packing_end.run>
|
||||
|
||||
void example_main() {}
|
|
@ -1,26 +0,0 @@
|
|||
#ifdef HAVE_CONFIG_H
|
||||
#include "config.h"
|
||||
#endif
|
||||
|
||||
#include <kernaux/runtime.h>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
void example_main();
|
||||
|
||||
static void assert_cb(
|
||||
const char *const file,
|
||||
const int line,
|
||||
const char *const msg
|
||||
) {
|
||||
fprintf(stderr, "%s:%d:%s\n", file, line, msg);
|
||||
abort();
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
kernaux_assert_cb = assert_cb;
|
||||
example_main();
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
|
@ -1,60 +0,0 @@
|
|||
#define KERNAUX_ACCESS_PROTECTED
|
||||
|
||||
#include <kernaux/free_list.h>
|
||||
#include <kernaux/generic/display.h>
|
||||
#include <kernaux/macro.h>
|
||||
#include <kernaux/memmap.h>
|
||||
|
||||
#include <assert.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
|
||||
static char malloc_memory[8192];
|
||||
|
||||
static void my_putc(void *const display KERNAUX_UNUSED, const char c)
|
||||
{
|
||||
putchar(c);
|
||||
}
|
||||
|
||||
static void my_vprintf(
|
||||
void *const display KERNAUX_UNUSED,
|
||||
const char *const format,
|
||||
va_list va
|
||||
) {
|
||||
vprintf(format, va);
|
||||
}
|
||||
|
||||
static const struct KernAux_Display display = {
|
||||
.putc = my_putc,
|
||||
.vprintf = my_vprintf,
|
||||
};
|
||||
|
||||
void example_main()
|
||||
{
|
||||
struct KernAux_FreeList malloc = KernAux_FreeList_create(NULL);
|
||||
KernAux_FreeList_add_zone(&malloc, malloc_memory, sizeof(malloc_memory));
|
||||
|
||||
KernAux_Memmap_Builder memmap_builder =
|
||||
KernAux_Memmap_Builder_new(&malloc.malloc);
|
||||
assert(memmap_builder);
|
||||
|
||||
assert(KernAux_Memmap_Builder_add(memmap_builder, NULL, 0x0, 654336, "available"));
|
||||
assert(KernAux_Memmap_Builder_add(memmap_builder, NULL, 0x9fc00, 1024, "reserved"));
|
||||
assert(KernAux_Memmap_Builder_add(memmap_builder, NULL, 0xf0000, 65536, "reserved"));
|
||||
KernAux_Memmap_Node kernel_node =
|
||||
KernAux_Memmap_Builder_add (memmap_builder, NULL, 0x100000, 133038080, "available");
|
||||
assert(kernel_node);
|
||||
assert(KernAux_Memmap_Builder_add(memmap_builder, NULL, 0x7fe0000, 131072, "reserved"));
|
||||
assert(KernAux_Memmap_Builder_add(memmap_builder, NULL, 0xfffc0000, 262144, "reserved"));
|
||||
|
||||
assert(KernAux_Memmap_Builder_add(memmap_builder, kernel_node, 0x400000, 8192, "kernel code"));
|
||||
assert(KernAux_Memmap_Builder_add(memmap_builder, kernel_node, 0x402000, 4096, "kernel data"));
|
||||
|
||||
KernAux_Memmap memmap =
|
||||
KernAux_Memmap_Builder_finish_and_free(memmap_builder);
|
||||
assert(memmap);
|
||||
|
||||
KernAux_Memmap_print(memmap, &display);
|
||||
|
||||
KERNAUX_MEMMAP_FREE(memmap);
|
||||
}
|
|
@ -1,104 +0,0 @@
|
|||
#include <kernaux/multiboot2.h>
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
#include <kernaux/macro/packing_start.run>
|
||||
|
||||
KERNAUX_ALIGNED(KERNAUX_MULTIBOOT2_HEADER_ALIGN)
|
||||
static const struct {
|
||||
struct KernAux_Multiboot2_Header header;
|
||||
// This macro may be used to create the tag
|
||||
// of type "KernAux_Multiboot2_HTag_InfoReq"
|
||||
// when the number of requested information
|
||||
// tag types is even (n % 2 == 0).
|
||||
//
|
||||
// cppcheck-suppress unknownMacro
|
||||
KERNAUX_MULTIBOOT2_HFIELDS_INFO_REQ_EVEN(
|
||||
// This is the name of the structure field.
|
||||
tag_info_req_even,
|
||||
// This is the number of requested information tag types.
|
||||
// IT MUST BE EVEN!!! (n % 2 == 0)
|
||||
2
|
||||
)
|
||||
// This macro may be used to create the tag
|
||||
// of type "KernAux_Multiboot2_HTag_InfoReq"
|
||||
// when the number of requested information
|
||||
// tag types is odd (n % 2 == 1).
|
||||
//
|
||||
// cppcheck-suppress unknownMacro
|
||||
KERNAUX_MULTIBOOT2_HFIELDS_INFO_REQ_ODD(
|
||||
// This is the name of the structure field.
|
||||
tag_info_req_odd,
|
||||
// This is the number of requested information tag types.
|
||||
// IT MUST BE ODD!!! (n % 2 == 1)
|
||||
1,
|
||||
// This is the name of the additional structure field
|
||||
// which will be used to align the following tags properly.
|
||||
// You may keep it unassigned.
|
||||
_align1
|
||||
)
|
||||
// This macro may be used for all other header tag types.
|
||||
//
|
||||
// cppcheck-suppress unknownMacro
|
||||
KERNAUX_MULTIBOOT2_HFIELDS_COMMON(
|
||||
// This is the name of the structure field.
|
||||
tag_none,
|
||||
// This is the type of the structure field
|
||||
// without the "KernAux_Multiboot2_HTag_" prefix.
|
||||
None
|
||||
)
|
||||
}
|
||||
KERNAUX_PACKED
|
||||
multiboot2_header = {
|
||||
.header = {
|
||||
.magic = KERNAUX_MULTIBOOT2_HEADER_MAGIC,
|
||||
.arch = KERNAUX_MULTIBOOT2_HEADER_ARCH_I386,
|
||||
.total_size = sizeof(multiboot2_header),
|
||||
// This macro helps to calculate the checksum.
|
||||
.checksum = KERNAUX_MULTIBOOT2_HEADER_CHECKSUM(
|
||||
KERNAUX_MULTIBOOT2_HEADER_ARCH_I386,
|
||||
sizeof(multiboot2_header)
|
||||
),
|
||||
},
|
||||
.tag_info_req_even = {
|
||||
.tag = {
|
||||
.base = {
|
||||
.type = KERNAUX_MULTIBOOT2_HTAG_INFO_REQ,
|
||||
.flags = KERNAUX_MULTIBOOT2_HTAG_BASE_FLAG_OPTIONAL,
|
||||
.size = sizeof(multiboot2_header.tag_info_req_even),
|
||||
},
|
||||
},
|
||||
.mbi_tag_types = {
|
||||
KERNAUX_MULTIBOOT2_ITAG_BOOT_CMD_LINE,
|
||||
KERNAUX_MULTIBOOT2_ITAG_BOOT_LOADER_NAME,
|
||||
},
|
||||
},
|
||||
.tag_info_req_odd = {
|
||||
.tag = {
|
||||
.base = {
|
||||
.type = KERNAUX_MULTIBOOT2_HTAG_INFO_REQ,
|
||||
.flags = KERNAUX_MULTIBOOT2_HTAG_BASE_FLAG_OPTIONAL,
|
||||
.size = sizeof(multiboot2_header.tag_info_req_odd),
|
||||
},
|
||||
},
|
||||
.mbi_tag_types = {
|
||||
KERNAUX_MULTIBOOT2_ITAG_ELF_SYMBOLS,
|
||||
},
|
||||
},
|
||||
.tag_none = {
|
||||
.tag = {
|
||||
.base = {
|
||||
.type = KERNAUX_MULTIBOOT2_HTAG_NONE,
|
||||
.flags = 0,
|
||||
.size = sizeof(multiboot2_header.tag_none),
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
#include <kernaux/macro/packing_end.run>
|
||||
|
||||
void example_main()
|
||||
{
|
||||
assert(KernAux_Multiboot2_Header_is_valid(&multiboot2_header.header));
|
||||
}
|
281
examples/ntoa.c
281
examples/ntoa.c
|
@ -1,281 +0,0 @@
|
|||
#include <kernaux/ntoa.h>
|
||||
|
||||
#include <assert.h>
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
|
||||
static const char *str_end(const char *str)
|
||||
{
|
||||
for (;; ++str) if (*str == '\0') return str;
|
||||
}
|
||||
|
||||
void example_main()
|
||||
{
|
||||
// kernaux_utoa
|
||||
{
|
||||
char buffer[KERNAUX_UTOA_MIN_BUFFER_SIZE + 3]; // more space for prefix
|
||||
const char *end;
|
||||
|
||||
// decimal: 10
|
||||
// no prefix (NULL)
|
||||
end = kernaux_utoa(123, buffer, 10, NULL);
|
||||
assert(strcmp(buffer, "123") == 0);
|
||||
assert(end == str_end(buffer));
|
||||
|
||||
// decimal: 10
|
||||
// with prefix ("foo")
|
||||
end = kernaux_utoa(123, buffer, 10, "foo");
|
||||
assert(strcmp(buffer, "foo123") == 0);
|
||||
assert(end == str_end(buffer));
|
||||
|
||||
// decimal, character alias: 'd'
|
||||
// no prefix (NULL)
|
||||
end = kernaux_utoa(123, buffer, 'd', NULL);
|
||||
assert(strcmp(buffer, "123") == 0);
|
||||
assert(end == str_end(buffer));
|
||||
|
||||
// binary: 2
|
||||
// no prefix (NULL)
|
||||
end = kernaux_utoa(123, buffer, 2, NULL);
|
||||
assert(strcmp(buffer, "1111011") == 0);
|
||||
assert(end == str_end(buffer));
|
||||
|
||||
// binary: 2
|
||||
// with prefix ("0b")
|
||||
end = kernaux_utoa(123, buffer, 2, "0b");
|
||||
assert(strcmp(buffer, "0b1111011") == 0);
|
||||
assert(end == str_end(buffer));
|
||||
|
||||
// binary, character alias: 'b'
|
||||
// no prefix (NULL)
|
||||
end = kernaux_utoa(123, buffer, 'b', NULL);
|
||||
assert(strcmp(buffer, "1111011") == 0);
|
||||
assert(end == str_end(buffer));
|
||||
|
||||
// binary, character alias: 'b'
|
||||
// with prefix ("0b")
|
||||
end = kernaux_utoa(123, buffer, 'b', "0b");
|
||||
assert(strcmp(buffer, "0b1111011") == 0);
|
||||
assert(end == str_end(buffer));
|
||||
|
||||
// octal: 8
|
||||
// no prefix (NULL)
|
||||
end = kernaux_utoa(0123, buffer, 8, NULL);
|
||||
assert(strcmp(buffer, "123") == 0);
|
||||
assert(end == str_end(buffer));
|
||||
|
||||
// octal: 8
|
||||
// with prefix ("0o")
|
||||
end = kernaux_utoa(0123, buffer, 8, "0o");
|
||||
assert(strcmp(buffer, "0o123") == 0);
|
||||
assert(end == str_end(buffer));
|
||||
|
||||
// octal, character alias: 'o'
|
||||
// no prefix (NULL)
|
||||
end = kernaux_utoa(0123, buffer, 'o', NULL);
|
||||
assert(strcmp(buffer, "123") == 0);
|
||||
assert(end == str_end(buffer));
|
||||
|
||||
// octal, character alias: 'o'
|
||||
// with prefix ("0o")
|
||||
end = kernaux_utoa(0123, buffer, 'o', "0o");
|
||||
assert(strcmp(buffer, "0o123") == 0);
|
||||
assert(end == str_end(buffer));
|
||||
|
||||
// hex: 16
|
||||
// no prefix (NULL)
|
||||
end = kernaux_utoa(0x123cafe, buffer, 16, NULL);
|
||||
assert(strcmp(buffer, "123cafe") == 0);
|
||||
assert(end == str_end(buffer));
|
||||
|
||||
// hex: 16
|
||||
// with prefix ("0x")
|
||||
end = kernaux_utoa(0x123cafe, buffer, 16, "0x");
|
||||
assert(strcmp(buffer, "0x123cafe") == 0);
|
||||
assert(end == str_end(buffer));
|
||||
|
||||
// hex, character alias: 'x'
|
||||
// no prefix (NULL)
|
||||
end = kernaux_utoa(0x123cafe, buffer, 'x', NULL);
|
||||
assert(strcmp(buffer, "123cafe") == 0);
|
||||
assert(end == str_end(buffer));
|
||||
|
||||
// hex, character alias: 'x'
|
||||
// with prefix ("0x")
|
||||
end = kernaux_utoa(0x123cafe, buffer, 'x', "0x");
|
||||
assert(strcmp(buffer, "0x123cafe") == 0);
|
||||
assert(end == str_end(buffer));
|
||||
|
||||
// hex, character alias: 'h'
|
||||
// no prefix (NULL)
|
||||
end = kernaux_utoa(0x123cafe, buffer, 'h', NULL);
|
||||
assert(strcmp(buffer, "123cafe") == 0);
|
||||
assert(end == str_end(buffer));
|
||||
|
||||
// hex, uppercase: -16
|
||||
// no prefix (NULL)
|
||||
end = kernaux_utoa(0x123cafe, buffer, -16, NULL);
|
||||
assert(strcmp(buffer, "123CAFE") == 0);
|
||||
assert(end == str_end(buffer));
|
||||
|
||||
// hex, uppercase, character alias: 'X'
|
||||
// no prefix (NULL)
|
||||
end = kernaux_utoa(0x123cafe, buffer, 'X', NULL);
|
||||
assert(strcmp(buffer, "123CAFE") == 0);
|
||||
assert(end == str_end(buffer));
|
||||
|
||||
// hex, uppercase, character alias: 'H'
|
||||
// no prefix (NULL)
|
||||
end = kernaux_utoa(0x123cafe, buffer, 'H', NULL);
|
||||
assert(strcmp(buffer, "123CAFE") == 0);
|
||||
assert(end == str_end(buffer));
|
||||
|
||||
// random base: 14
|
||||
// no prefix (NULL)
|
||||
end = kernaux_utoa(123456, buffer, 14, NULL);
|
||||
assert(strcmp(buffer, "32dc4") == 0);
|
||||
assert(end == str_end(buffer));
|
||||
|
||||
// random base: 14
|
||||
// with prefix ("foo")
|
||||
end = kernaux_utoa(123456, buffer, 14, "foo");
|
||||
assert(strcmp(buffer, "foo32dc4") == 0);
|
||||
assert(end == str_end(buffer));
|
||||
|
||||
// random base, uppercase: -14
|
||||
// no prefix (NULL)
|
||||
end = kernaux_utoa(123456, buffer, -14, NULL);
|
||||
assert(strcmp(buffer, "32DC4") == 0);
|
||||
assert(end == str_end(buffer));
|
||||
}
|
||||
|
||||
// kernaux_itoa - similar to kernaux_utoa
|
||||
{
|
||||
char buffer[KERNAUX_ITOA_MIN_BUFFER_SIZE + 3]; // more space for prefix
|
||||
const char *end;
|
||||
|
||||
// sign: +
|
||||
// decimal: 10
|
||||
// no prefix (NULL)
|
||||
end = kernaux_itoa(123, buffer, 10, NULL);
|
||||
assert(strcmp(buffer, "123") == 0);
|
||||
assert(end == str_end(buffer));
|
||||
|
||||
// sign: -
|
||||
// decimal: 10
|
||||
// no prefix (NULL)
|
||||
end = kernaux_itoa(-123, buffer, 10, NULL);
|
||||
assert(strcmp(buffer, "-123") == 0);
|
||||
assert(end == str_end(buffer));
|
||||
|
||||
// sign: +
|
||||
// decimal: 10
|
||||
// with prefix ("foo")
|
||||
end = kernaux_itoa(123, buffer, 10, "foo");
|
||||
assert(strcmp(buffer, "foo123") == 0);
|
||||
assert(end == str_end(buffer));
|
||||
|
||||
// sign: -
|
||||
// decimal: 10
|
||||
// with prefix ("foo")
|
||||
end = kernaux_itoa(-123, buffer, 10, "foo");
|
||||
assert(strcmp(buffer, "-foo123") == 0);
|
||||
assert(end == str_end(buffer));
|
||||
}
|
||||
|
||||
// kernaux_utoa2
|
||||
{
|
||||
char buffer[KERNAUX_UTOA2_BUFFER_SIZE];
|
||||
const char *end;
|
||||
|
||||
end = kernaux_utoa2(123, buffer);
|
||||
assert(strcmp(buffer, "0b1111011") == 0);
|
||||
assert(end == str_end(buffer));
|
||||
}
|
||||
|
||||
// kernaux_itoa2
|
||||
{
|
||||
char buffer[KERNAUX_ITOA2_BUFFER_SIZE];
|
||||
const char *end;
|
||||
|
||||
end = kernaux_itoa2(123, buffer);
|
||||
assert(strcmp(buffer, "0b1111011") == 0);
|
||||
assert(end == str_end(buffer));
|
||||
|
||||
end = kernaux_itoa2(-123, buffer);
|
||||
assert(strcmp(buffer, "-0b1111011") == 0);
|
||||
assert(end == str_end(buffer));
|
||||
}
|
||||
|
||||
// kernaux_utoa8
|
||||
{
|
||||
char buffer[KERNAUX_UTOA8_BUFFER_SIZE];
|
||||
const char *end;
|
||||
|
||||
end = kernaux_utoa8(0123, buffer);
|
||||
assert(strcmp(buffer, "0o123") == 0);
|
||||
assert(end == str_end(buffer));
|
||||
}
|
||||
|
||||
// kernaux_itoa8
|
||||
{
|
||||
char buffer[KERNAUX_ITOA8_BUFFER_SIZE];
|
||||
const char *end;
|
||||
|
||||
end = kernaux_itoa8(0123, buffer);
|
||||
assert(strcmp(buffer, "0o123") == 0);
|
||||
assert(end == str_end(buffer));
|
||||
|
||||
end = kernaux_itoa8(-0123, buffer);
|
||||
assert(strcmp(buffer, "-0o123") == 0);
|
||||
assert(end == str_end(buffer));
|
||||
}
|
||||
|
||||
// kernaux_utoa10
|
||||
{
|
||||
char buffer[KERNAUX_UTOA10_BUFFER_SIZE];
|
||||
const char *end;
|
||||
|
||||
end = kernaux_utoa10(123, buffer);
|
||||
assert(strcmp(buffer, "123") == 0);
|
||||
assert(end == str_end(buffer));
|
||||
}
|
||||
|
||||
// kernaux_itoa10
|
||||
{
|
||||
char buffer[KERNAUX_ITOA10_BUFFER_SIZE];
|
||||
const char *end;
|
||||
|
||||
end = kernaux_itoa10(123, buffer);
|
||||
assert(strcmp(buffer, "123") == 0);
|
||||
assert(end == str_end(buffer));
|
||||
|
||||
end = kernaux_itoa10(-123, buffer);
|
||||
assert(strcmp(buffer, "-123") == 0);
|
||||
assert(end == str_end(buffer));
|
||||
}
|
||||
|
||||
// kernaux_utoa16
|
||||
{
|
||||
char buffer[KERNAUX_UTOA16_BUFFER_SIZE];
|
||||
const char *end;
|
||||
|
||||
end = kernaux_utoa16(0x123, buffer);
|
||||
assert(strcmp(buffer, "0x123") == 0);
|
||||
assert(end == str_end(buffer));
|
||||
}
|
||||
|
||||
// kernaux_itoa16
|
||||
{
|
||||
char buffer[KERNAUX_ITOA16_BUFFER_SIZE];
|
||||
const char *end;
|
||||
|
||||
end = kernaux_itoa16(0x123, buffer);
|
||||
assert(strcmp(buffer, "0x123") == 0);
|
||||
assert(end == str_end(buffer));
|
||||
|
||||
end = kernaux_itoa16(-0x123, buffer);
|
||||
assert(strcmp(buffer, "-0x123") == 0);
|
||||
assert(end == str_end(buffer));
|
||||
}
|
||||
}
|
|
@ -1,71 +0,0 @@
|
|||
#include <kernaux/pfa.h>
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
// Create PFA in some static memory area because typically you don't have memory
|
||||
// management in kernel without PFA.
|
||||
struct KernAux_PFA pfa;
|
||||
|
||||
void example_main()
|
||||
{
|
||||
// In the earliest stage of kernel initialization mark all pages as
|
||||
// unavailable because you don't have memory map yet.
|
||||
KernAux_PFA_initialize(&pfa);
|
||||
|
||||
// Let's assume that you have the following memory map now:
|
||||
// * [0 B; 64 KiB)
|
||||
// * [1 MiB; 4 MiB)
|
||||
// * [6 MiB; 128 MiB)
|
||||
// Mark these pages as available. Note that page is identified by any
|
||||
// address inside it. This is why you can subtract 1, not only
|
||||
// KERNAUX_PFA_PAGE_SIZE.
|
||||
KernAux_PFA_mark_available(&pfa, 0, 1024 * 64 - 1);
|
||||
KernAux_PFA_mark_available(&pfa, 1024 * 1024, 1024 * 1024 * 4 - 1);
|
||||
KernAux_PFA_mark_available(&pfa, 1024 * 1024 * 6, 1024 * 1024 * 128 - 1);
|
||||
|
||||
// You can test page availability.
|
||||
assert(KernAux_PFA_is_available(&pfa, 1024 * 32)); // 32 KiB
|
||||
assert(!KernAux_PFA_is_available(&pfa, 1024 * 64)); // 64 KiB
|
||||
assert(KernAux_PFA_is_available(&pfa, 1024 * 1024)); // 1 MiB
|
||||
assert(!KernAux_PFA_is_available(&pfa, 1024 * 1024 * 6 // 6 MiB - 4 KiB
|
||||
- KERNAUX_PFA_PAGE_SIZE));
|
||||
assert(KernAux_PFA_is_available(&pfa, 1024 * 1024 * 128 // 128 MiB - 4 KiB
|
||||
- KERNAUX_PFA_PAGE_SIZE));
|
||||
assert(!KernAux_PFA_is_available(&pfa, 1024 * 1024 * 128)); // 128 MiB
|
||||
|
||||
// When you have configured PFA, you can use it to allocate and free pages.
|
||||
{
|
||||
const size_t page_addr =
|
||||
KernAux_PFA_alloc_pages(&pfa, KERNAUX_PFA_PAGE_SIZE);
|
||||
assert(!KernAux_PFA_is_available(&pfa, page_addr));
|
||||
KernAux_PFA_free_pages(&pfa, page_addr, KERNAUX_PFA_PAGE_SIZE);
|
||||
assert(KernAux_PFA_is_available(&pfa, page_addr));
|
||||
}
|
||||
|
||||
// You can also allocate multiple continuous pages.
|
||||
{
|
||||
const size_t page_addr =
|
||||
KernAux_PFA_alloc_pages(&pfa, 10 * KERNAUX_PFA_PAGE_SIZE);
|
||||
|
||||
for (size_t index = 0, addr = page_addr; index < 10; ++index) {
|
||||
assert(!KernAux_PFA_is_available(&pfa, addr));
|
||||
addr += KERNAUX_PFA_PAGE_SIZE;
|
||||
}
|
||||
|
||||
KernAux_PFA_free_pages(&pfa, page_addr, 10 * KERNAUX_PFA_PAGE_SIZE);
|
||||
|
||||
for (size_t index = 0, addr = page_addr; index < 10; ++index) {
|
||||
assert(KernAux_PFA_is_available(&pfa, addr));
|
||||
addr += KERNAUX_PFA_PAGE_SIZE;
|
||||
}
|
||||
}
|
||||
|
||||
// You can also request amounts of memory which are not factors of page
|
||||
// size.
|
||||
{
|
||||
const size_t page_addr = KernAux_PFA_alloc_pages(&pfa, 123);
|
||||
assert(!KernAux_PFA_is_available(&pfa, page_addr));
|
||||
KernAux_PFA_free_pages(&pfa, page_addr, 123);
|
||||
assert(KernAux_PFA_is_available(&pfa, page_addr));
|
||||
}
|
||||
}
|
|
@ -1,33 +0,0 @@
|
|||
#include <kernaux/printf.h>
|
||||
|
||||
#include <assert.h>
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#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 *const arg)
|
||||
{
|
||||
assert(arg == data);
|
||||
if (buffer_index >= BUFFER_SIZE) abort();
|
||||
buffer[buffer_index++] = chr;
|
||||
}
|
||||
|
||||
void example_main()
|
||||
{
|
||||
const int result = kernaux_fprintf(
|
||||
my_putchar,
|
||||
(void*)data,
|
||||
"Hello, %s! Session ID: %u.",
|
||||
"Alex",
|
||||
123
|
||||
);
|
||||
assert((size_t)result == strlen(buffer));
|
||||
assert(strcmp(buffer, "Hello, Alex! Session ID: 123.") == 0);
|
||||
}
|
|
@ -1,36 +0,0 @@
|
|||
#include <kernaux/printf.h>
|
||||
|
||||
#include <assert.h>
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#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 *const arg)
|
||||
{
|
||||
assert(arg == data);
|
||||
if (buffer_index >= BUFFER_SIZE) abort();
|
||||
buffer[buffer_index++] = chr;
|
||||
}
|
||||
|
||||
static int my_printf(const char *const format, ...)
|
||||
{
|
||||
va_list va;
|
||||
va_start(va, format);
|
||||
const int result = kernaux_vfprintf(my_putchar, (void*)data, format, va);
|
||||
va_end(va);
|
||||
return result;
|
||||
}
|
||||
|
||||
void example_main()
|
||||
{
|
||||
const int result = my_printf("Hello, %s! Session ID: %u.", "Alex", 123);
|
||||
assert((size_t)result == strlen(buffer));
|
||||
assert(strcmp(buffer, "Hello, Alex! Session ID: 123.") == 0);
|
||||
}
|
|
@ -1,101 +0,0 @@
|
|||
#include <kernaux/printf_fmt.h>
|
||||
|
||||
#include <assert.h>
|
||||
#include <stddef.h>
|
||||
|
||||
void example_main()
|
||||
{
|
||||
{
|
||||
const char *const format = "s";
|
||||
|
||||
struct KernAux_PrintfFmt_Spec spec = KernAux_PrintfFmt_Spec_create(format);
|
||||
|
||||
if (spec.set_width) {
|
||||
// Actually this line won't be executed.
|
||||
KernAux_PrintfFmt_Spec_set_width(&spec, 0);
|
||||
}
|
||||
if (spec.set_precision) {
|
||||
// Actually this line won't be executed.
|
||||
KernAux_PrintfFmt_Spec_set_precision(&spec, 0);
|
||||
}
|
||||
|
||||
assert(spec.format_start == format);
|
||||
assert(spec.format_limit == &format[1]);
|
||||
|
||||
assert(spec.flags == 0);
|
||||
assert(spec.width == 0);
|
||||
assert(spec.precision == 0);
|
||||
assert(spec.type == KERNAUX_PRINTF_FMT_TYPE_STR);
|
||||
assert(spec.base == 0);
|
||||
|
||||
assert(!spec.set_width);
|
||||
assert(!spec.set_precision);
|
||||
}
|
||||
|
||||
{
|
||||
const char *format = "012.34f";
|
||||
|
||||
struct KernAux_PrintfFmt_Spec spec = KernAux_PrintfFmt_Spec_create_out(&format);
|
||||
|
||||
if (spec.set_width) {
|
||||
// Actually this line won't be executed.
|
||||
KernAux_PrintfFmt_Spec_set_width(&spec, 0);
|
||||
}
|
||||
if (spec.set_precision) {
|
||||
// Actually this line won't be executed.
|
||||
KernAux_PrintfFmt_Spec_set_precision(&spec, 0);
|
||||
}
|
||||
|
||||
assert(spec.format_start == &format[-7]);
|
||||
assert(spec.format_limit == format);
|
||||
|
||||
assert(
|
||||
spec.flags ==
|
||||
(
|
||||
KERNAUX_PRINTF_FMT_FLAGS_ZEROPAD |
|
||||
KERNAUX_PRINTF_FMT_FLAGS_PRECISION
|
||||
)
|
||||
);
|
||||
assert(spec.width == 12);
|
||||
assert(spec.precision == 34);
|
||||
assert(spec.type == KERNAUX_PRINTF_FMT_TYPE_FLOAT);
|
||||
assert(spec.base == 0);
|
||||
|
||||
assert(!spec.set_width);
|
||||
assert(!spec.set_precision);
|
||||
}
|
||||
|
||||
{
|
||||
const char *const format = " *.*ld";
|
||||
const char *new_format = NULL;
|
||||
|
||||
struct KernAux_PrintfFmt_Spec spec = KernAux_PrintfFmt_Spec_create_out_new(format, &new_format);
|
||||
|
||||
if (spec.set_width) {
|
||||
KernAux_PrintfFmt_Spec_set_width(&spec, 12);
|
||||
}
|
||||
if (spec.set_precision) {
|
||||
KernAux_PrintfFmt_Spec_set_precision(&spec, 34);
|
||||
}
|
||||
|
||||
assert(spec.format_start == format);
|
||||
assert(spec.format_limit == &format[6]);
|
||||
assert(new_format == &format[6]);
|
||||
|
||||
assert(
|
||||
spec.flags ==
|
||||
(
|
||||
KERNAUX_PRINTF_FMT_FLAGS_SPACE |
|
||||
KERNAUX_PRINTF_FMT_FLAGS_LONG |
|
||||
KERNAUX_PRINTF_FMT_FLAGS_PRECISION
|
||||
)
|
||||
);
|
||||
assert(spec.width == 12);
|
||||
assert(spec.precision == 34);
|
||||
assert(spec.type == KERNAUX_PRINTF_FMT_TYPE_INT);
|
||||
assert(spec.base == 10);
|
||||
|
||||
assert(spec.set_width);
|
||||
assert(spec.set_precision);
|
||||
}
|
||||
}
|
|
@ -1,21 +0,0 @@
|
|||
#include <kernaux/printf.h>
|
||||
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
|
||||
#define BUFFER_SIZE 1024
|
||||
|
||||
static char buffer[BUFFER_SIZE];
|
||||
|
||||
void example_main()
|
||||
{
|
||||
const int result = kernaux_snprintf(
|
||||
buffer,
|
||||
sizeof(buffer),
|
||||
"Hello, %s! Session ID: %u.",
|
||||
"Alex",
|
||||
123
|
||||
);
|
||||
assert((size_t)result == strlen(buffer));
|
||||
assert(strcmp(buffer, "Hello, Alex! Session ID: 123.") == 0);
|
||||
}
|
|
@ -1,24 +0,0 @@
|
|||
#include <kernaux/printf.h>
|
||||
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
|
||||
#define BUFFER_SIZE 1024
|
||||
|
||||
static char buffer[BUFFER_SIZE];
|
||||
|
||||
static int my_snprintf(const char *const format, ...)
|
||||
{
|
||||
va_list va;
|
||||
va_start(va, format);
|
||||
const int result = kernaux_vsnprintf(buffer, sizeof(buffer), format, va);
|
||||
va_end(va);
|
||||
return result;
|
||||
}
|
||||
|
||||
void example_main()
|
||||
{
|
||||
const int result = my_snprintf("Hello, %s! Session ID: %u.", "Alex", 123);
|
||||
assert((size_t)result == strlen(buffer));
|
||||
assert(strcmp(buffer, "Hello, Alex! Session ID: 123.") == 0);
|
||||
}
|
|
@ -1,45 +0,0 @@
|
|||
#include <kernaux/units.h>
|
||||
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
|
||||
void example_main()
|
||||
{
|
||||
char buffer[256];
|
||||
|
||||
kernaux_units_human_raw(123, KERNAUX_UNIT_BIT, buffer, sizeof(buffer));
|
||||
assert(strcmp(buffer, "123 bit") == 0);
|
||||
|
||||
kernaux_units_human_dec(123, KERNAUX_UNIT_BIT, KERNAUX_UNITPFX_KILO, buffer, sizeof(buffer));
|
||||
assert(strcmp(buffer, "123 kbit") == 0);
|
||||
kernaux_units_human_bin(123, KERNAUX_UNIT_BIT, KERNAUX_UNITPFX_KIBI, buffer, sizeof(buffer));
|
||||
assert(strcmp(buffer, "123 Kibit") == 0);
|
||||
|
||||
kernaux_units_human_dec(123, KERNAUX_UNIT_BIT, KERNAUX_UNITPFX_MEGA, buffer, sizeof(buffer));
|
||||
assert(strcmp(buffer, "123 Mbit") == 0);
|
||||
kernaux_units_human_bin(123, KERNAUX_UNIT_BIT, KERNAUX_UNITPFX_MEBI, buffer, sizeof(buffer));
|
||||
assert(strcmp(buffer, "123 Mibit") == 0);
|
||||
|
||||
kernaux_units_human_dec(123, KERNAUX_UNIT_BIT, KERNAUX_UNITPFX_GIGA, buffer, sizeof(buffer));
|
||||
assert(strcmp(buffer, "123 Gbit") == 0);
|
||||
kernaux_units_human_bin(123, KERNAUX_UNIT_BIT, KERNAUX_UNITPFX_GIBI, buffer, sizeof(buffer));
|
||||
assert(strcmp(buffer, "123 Gibit") == 0);
|
||||
|
||||
kernaux_units_human_raw(123, KERNAUX_UNIT_BYTE, buffer, sizeof(buffer));
|
||||
assert(strcmp(buffer, "123 Byte") == 0);
|
||||
|
||||
kernaux_units_human_dec(123, KERNAUX_UNIT_BYTE, KERNAUX_UNITPFX_KILO, buffer, sizeof(buffer));
|
||||
assert(strcmp(buffer, "123 kB") == 0);
|
||||
kernaux_units_human_bin(123, KERNAUX_UNIT_BYTE, KERNAUX_UNITPFX_KIBI, buffer, sizeof(buffer));
|
||||
assert(strcmp(buffer, "123 KiB") == 0);
|
||||
|
||||
kernaux_units_human_dec(123, KERNAUX_UNIT_BYTE, KERNAUX_UNITPFX_MEGA, buffer, sizeof(buffer));
|
||||
assert(strcmp(buffer, "123 MB") == 0);
|
||||
kernaux_units_human_bin(123, KERNAUX_UNIT_BYTE, KERNAUX_UNITPFX_MEBI, buffer, sizeof(buffer));
|
||||
assert(strcmp(buffer, "123 MiB") == 0);
|
||||
|
||||
kernaux_units_human_dec(123, KERNAUX_UNIT_BYTE, KERNAUX_UNITPFX_GIGA, buffer, sizeof(buffer));
|
||||
assert(strcmp(buffer, "123 GB") == 0);
|
||||
kernaux_units_human_bin(123, KERNAUX_UNIT_BYTE, KERNAUX_UNITPFX_GIBI, buffer, sizeof(buffer));
|
||||
assert(strcmp(buffer, "123 GiB") == 0);
|
||||
}
|
|
@ -20,15 +20,10 @@ CPPCHECK_INC = \
|
|||
-I$(top_srcdir)/include
|
||||
|
||||
CPPCHECK_SUPPRESS = \
|
||||
--suppress='constArgument:$(top_srcdir)/examples/macro_cast.c' \
|
||||
--suppress='constParameter:$(top_srcdir)/examples/printf_file*.c' \
|
||||
--suppress='unusedStructMember:$(top_srcdir)/examples/*.c' \
|
||||
--suppress='unusedStructMember:$(top_srcdir)/tests/test_multiboot2_info_*.c' \
|
||||
--suppress='unusedVariable'
|
||||
|
||||
CPPCHECK_PATHS = \
|
||||
$(top_builddir)/examples \
|
||||
$(top_srcdir)/examples \
|
||||
$(top_builddir)/include \
|
||||
$(top_srcdir)/include \
|
||||
$(top_builddir)/libc \
|
||||
|
|
|
@ -2,5 +2,4 @@
|
|||
|
||||
set -eux
|
||||
|
||||
test -f examples/test-suite.log && cat examples/test-suite.log || true
|
||||
test -f tests/test-suite.log && cat tests/test-suite.log || true
|
||||
test -f tests/test-suite.log && cat tests/test-suite.log || true
|
||||
|
|
Loading…
Reference in a new issue