Main: rename package "malloc" to "alloc"

This commit is contained in:
Alex Kotov 2022-06-18 00:58:03 +03:00
parent de58477551
commit 87296c0d12
Signed by: kotovalexarian
GPG Key ID: 553C0EBBEB5D5F08
12 changed files with 95 additions and 95 deletions

2
.gitignore vendored
View File

@ -127,7 +127,7 @@
/tests/test_cmdline_gen
/tests/test_cmdline_gen.c
/tests/test_elf
/tests/test_malloc
/tests/test_alloc
/tests/test_mbr
/tests/test_memmap
/tests/test_multiboot2_common_packing

View File

@ -1,7 +1,7 @@
2022-06-17 Alex Kotov <kotovalexarian@gmail.com>
* configure.ac: Added package "malloc"
* include/kernaux/malloc.h: Added
* configure.ac: Added package "alloc"
* include/kernaux/alloc.h: Added
2022-06-16 Alex Kotov <kotovalexarian@gmail.com>

View File

@ -51,8 +51,8 @@ endif
if WITH_LIBC
libkernaux_la_LIBADD += libc/libc.la
endif
if WITH_MALLOC
libkernaux_la_SOURCES += src/malloc.c
if WITH_ALLOC
libkernaux_la_SOURCES += src/alloc.c
endif
if WITH_MBR
libkernaux_la_SOURCES += src/mbr.c

View File

@ -65,7 +65,7 @@ zero). Work-in-progress APIs can change at any time.
* Utilities
* [Measurement units utils](/include/kernaux/units.h) (*work in progress*)
* [Example: To human](/examples/units_human.c)
* [Memory allocator](/include/kernaux/malloc.h) (*work in progress*)
* [Memory allocator](/include/kernaux/alloc.h) (*work in progress*)
* [Memory map](/include/kernaux/memmap.h.in) (*non-breaking since* **0.4.0**)
* [Example](/examples/memmap.c)
* [printf format parser](/include/kernaux/printf_fmt.h) (*work in progress*)
@ -147,9 +147,9 @@ stable options.
All packages are included by default. To exclude all packages except those
explicitly included, use `--without-all`.
* `--with[out]-alloc` - memory allocator
* `--with[out]-cmdline` - command line parser
* `--with[out]-io` - input/output
* `--with[out]-malloc` - memory allocator
* `--with[out]-memmap` - memory map
* `--with[out]-ntoa` - itoa/ftoa
* `--with[out]-printf` - printf

View File

@ -57,12 +57,12 @@ AC_ARG_ENABLE([tests-python], AS_HELP_STRING([--enable-tests-python], [enable te
dnl Packages (enabled by default)
AC_ARG_WITH( [all], AS_HELP_STRING([--without-all], [without all default packages]))
AC_ARG_WITH( [alloc], AS_HELP_STRING([--without-alloc], [without memory allocator]))
AC_ARG_WITH( [cmdline], AS_HELP_STRING([--without-cmdline], [without command line parser]))
AC_ARG_WITH( [console], AS_HELP_STRING([--without-console], [without serial console]))
AC_ARG_WITH( [elf], AS_HELP_STRING([--without-elf], [without ELF utils]))
AC_ARG_WITH( [framebuffer], AS_HELP_STRING([--without-framebuffer], [without framebuffer]))
AC_ARG_WITH( [io], AS_HELP_STRING([--without-io], [without input/output]))
AC_ARG_WITH( [malloc], AS_HELP_STRING([--without-malloc], [without memory allocator]))
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]))
@ -90,12 +90,12 @@ AS_IF([test "$enable_tests_all" = yes], do_enable_tests_all)
AC_DEFUN([do_without_all],
[
if test -z "$with_alloc"; then with_alloc=no; fi
if test -z "$with_cmdline"; then with_cmdline=no; fi
if test -z "$with_console"; then with_console=no; fi
if test -z "$with_elf"; then with_elf=no; fi
if test -z "$with_framebuffer"; then with_framebuffer=no; fi
if test -z "$with_io"; then with_io=no; fi
if test -z "$with_malloc"; then with_malloc=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
@ -127,12 +127,12 @@ AS_IF([test "$enable_tests_python" = yes], [enable_tests_python=yes], [enable_te
dnl Packages (enabled by default)
AS_IF([test "$with_all" = no ], [with_all=no], [with_all=yes])
AS_IF([test "$with_alloc" = no ], [with_alloc=no], [with_alloc=yes])
AS_IF([test "$with_cmdline" = no ], [with_cmdline=no], [with_cmdline=yes])
AS_IF([test "$with_console" = no ], [with_console=no], [with_console=yes])
AS_IF([test "$with_elf" = no ], [with_elf=no], [with_elf=yes])
AS_IF([test "$with_framebuffer" = no ], [with_framebuffer=no], [with_framebuffer=yes])
AS_IF([test "$with_io" = no ], [with_io=no], [with_io=yes])
AS_IF([test "$with_malloc" = no ], [with_malloc=no], [with_malloc=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])
@ -185,12 +185,12 @@ AM_CONDITIONAL([ENABLE_TESTS], [test "$enable_tests" = yes])
AM_CONDITIONAL([ENABLE_TESTS_PYTHON], [test "$enable_tests_python" = yes])
dnl Packages (enabled by default)
AM_CONDITIONAL([WITH_ALLOC], [test "$with_alloc" = yes])
AM_CONDITIONAL([WITH_CMDLINE], [test "$with_cmdline" = yes])
AM_CONDITIONAL([WITH_CONSOLE], [test "$with_console" = yes])
AM_CONDITIONAL([WITH_ELF], [test "$with_elf" = yes])
AM_CONDITIONAL([WITH_FRAMEBUFFER], [test "$with_framebuffer" = yes])
AM_CONDITIONAL([WITH_IO], [test "$with_io" = yes])
AM_CONDITIONAL([WITH_MALLOC], [test "$with_malloc" = 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])
@ -226,12 +226,12 @@ AS_IF([test "$enable_tests" = yes], [AC_DEFINE([ENABLE_TESTS], [1]
AS_IF([test "$enable_tests_python" = yes], [AC_DEFINE([ENABLE_TESTS_PYTHON], [1], [enabled tests that require Python 3 with YAML and Jinja2])])
dnl Packages (enabled by default)
AS_IF([test "$with_alloc" = yes], [AC_DEFINE([WITH_ALLOC], [1], [with memory allocator])])
AS_IF([test "$with_cmdline" = yes], [AC_DEFINE([WITH_CMDLINE], [1], [with command line parser])])
AS_IF([test "$with_console" = yes], [AC_DEFINE([WITH_CONSOLE], [1], [with serial console])])
AS_IF([test "$with_elf" = yes], [AC_DEFINE([WITH_ELF], [1], [with ELF utils])])
AS_IF([test "$with_framebuffer" = yes], [AC_DEFINE([WITH_FRAMEBUFFER], [1], [with framebuffer])])
AS_IF([test "$with_io" = yes], [AC_DEFINE([WITH_IO], [1], [with input/output])])
AS_IF([test "$with_malloc" = yes], [AC_DEFINE([WITH_MALLOC], [1], [with memory allocator])])
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])])
@ -254,12 +254,12 @@ AS_IF([test "$enable_debug" = yes], [AC_DEFINE([KERNAUX_DEBUG], [1]
##########################
dnl Packages (enabled by default)
AS_IF([test "$with_alloc" = no], [AC_SUBST([comment_line_alloc], [//])])
AS_IF([test "$with_cmdline" = no], [AC_SUBST([comment_line_cmdline], [//])])
AS_IF([test "$with_console" = no], [AC_SUBST([comment_line_console], [//])])
AS_IF([test "$with_elf" = no], [AC_SUBST([comment_line_elf], [//])])
AS_IF([test "$with_framebuffer" = no], [AC_SUBST([comment_line_framebuffer], [//])])
AS_IF([test "$with_io" = no], [AC_SUBST([comment_line_io], [//])])
AS_IF([test "$with_malloc" = no], [AC_SUBST([comment_line_malloc], [//])])
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

@ -16,6 +16,9 @@ if ASM_X86_64
nobase_include_HEADERS += kernaux/asm/x86_64.h
endif
if WITH_ALLOC
nobase_include_HEADERS += kernaux/alloc.h
endif
if WITH_CMDLINE
nobase_include_HEADERS += kernaux/cmdline.h
endif
@ -31,9 +34,6 @@ endif
if WITH_IO
nobase_include_HEADERS += kernaux/io.h
endif
if WITH_MALLOC
nobase_include_HEADERS += kernaux/malloc.h
endif
if WITH_MBR
nobase_include_HEADERS += kernaux/mbr.h
endif

View File

@ -6,12 +6,12 @@
#include <kernaux/assert.h>
#include <kernaux/version.h>
@comment_line_alloc@#include <kernaux/alloc.h>
@comment_line_cmdline@#include <kernaux/cmdline.h>
@comment_line_console@#include <kernaux/console.h>
@comment_line_elf@#include <kernaux/elf.h>
@comment_line_framebuffer@#include <kernaux/framebuffer.h>
@comment_line_io@#include <kernaux/io.h>
@comment_line_malloc@#include <kernaux/malloc.h>
@comment_line_mbr@#include <kernaux/mbr.h>
@comment_line_memmap@#include <kernaux/memmap.h>
@comment_line_multiboot2@#include <kernaux/multiboot2.h>

35
include/kernaux/alloc.h Normal file
View File

@ -0,0 +1,35 @@
#ifndef KERNAUX_INCLUDED_ALLOC
#define KERNAUX_INCLUDED_ALLOC
#ifdef __cplusplus
extern "C" {
#endif
#include <stdbool.h>
#include <stddef.h>
typedef struct KernAux_Alloc_Node {
struct KernAux_Alloc_Node *next;
bool free;
size_t actual_size, user_size;
char *block;
} *KernAux_Alloc_Node;
typedef struct KernAux_Alloc {
KernAux_Alloc_Node head;
} *KernAux_Alloc;
struct KernAux_Alloc KernAux_Alloc_create();
void KernAux_Alloc_init(KernAux_Alloc alloc);
void
KernAux_Alloc_add_memory_block(KernAux_Alloc alloc, void *ptr, size_t size);
void *KernAux_Alloc_malloc(KernAux_Alloc alloc, size_t size);
void KernAux_Alloc_free (KernAux_Alloc alloc, void *ptr);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -1,35 +0,0 @@
#ifndef KERNAUX_INCLUDED_MALLOC
#define KERNAUX_INCLUDED_MALLOC
#ifdef __cplusplus
extern "C" {
#endif
#include <stdbool.h>
#include <stddef.h>
typedef struct KernAux_Malloc_Node {
struct KernAux_Malloc_Node *next;
bool free;
size_t actual_size, user_size;
char *block;
} *KernAux_Malloc_Node;
typedef struct KernAux_Malloc {
KernAux_Malloc_Node head;
} *KernAux_Malloc;
struct KernAux_Malloc KernAux_Malloc_create();
void KernAux_Malloc_init(KernAux_Malloc malloc);
void
KernAux_Malloc_add_memory_block(KernAux_Malloc malloc, void *ptr, size_t size);
void *KernAux_Malloc_malloc(KernAux_Malloc malloc, size_t size);
void KernAux_Malloc_free (KernAux_Malloc malloc, void *ptr);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -8,8 +8,8 @@
#include "config.h"
#endif
#include <kernaux/alloc.h>
#include <kernaux/assert.h>
#include <kernaux/malloc.h>
#include <stddef.h>
#include <stdint.h>
@ -19,55 +19,55 @@
#define PTR_ALIGNMENT (sizeof(void*)) // TODO: align node to this value
#define ALLOC_HEADER_SIZE (offsetof(struct KernAux_Malloc_Node, block))
#define ALLOC_HEADER_SIZE (offsetof(struct KernAux_Alloc_Node, block))
#define MIN_ALLOC_SIZE (ALLOC_HEADER_SIZE + 16)
struct KernAux_Malloc KernAux_Malloc_create()
struct KernAux_Alloc KernAux_Alloc_create()
{
struct KernAux_Malloc malloc;
KernAux_Malloc_init(&malloc);
return malloc;
struct KernAux_Alloc alloc;
KernAux_Alloc_init(&alloc);
return alloc;
}
void KernAux_Malloc_init(const KernAux_Malloc malloc)
void KernAux_Alloc_init(const KernAux_Alloc alloc)
{
KERNAUX_ASSERT(malloc);
KERNAUX_ASSERT(alloc);
malloc->head = NULL;
alloc->head = NULL;
}
void KernAux_Malloc_add_memory_block(
const KernAux_Malloc malloc,
void KernAux_Alloc_add_memory_block(
const KernAux_Alloc alloc,
void *const ptr,
const size_t size
) {
KERNAUX_ASSERT(malloc);
KERNAUX_ASSERT(alloc);
KERNAUX_ASSERT(ptr);
KERNAUX_ASSERT(size >= 2 * sizeof(struct KernAux_Malloc_Node));
KERNAUX_ASSERT(size >= 2 * sizeof(struct KernAux_Alloc_Node));
KernAux_Malloc_Node new_node = ptr;
KernAux_Alloc_Node new_node = ptr;
new_node->free = true;
new_node->actual_size = size;
new_node->user_size = size - sizeof(struct KernAux_Malloc_Node);
new_node->user_size = size - sizeof(struct KernAux_Alloc_Node);
// TODO: lock
new_node->next = malloc->head;
malloc->head = new_node;
new_node->next = alloc->head;
alloc->head = new_node;
// TODO: unlock
}
void *KernAux_Malloc_malloc(const KernAux_Malloc malloc, const size_t size)
void *KernAux_Alloc_malloc(const KernAux_Alloc alloc, const size_t size)
{
KERNAUX_ASSERT(malloc);
KERNAUX_ASSERT(alloc);
if (size == 0) return NULL;
KernAux_Malloc_Node node = NULL;
KernAux_Alloc_Node node = NULL;
void *ptr = NULL;
// TODO: lock
for (
KernAux_Malloc_Node item_node = malloc->head;
KernAux_Alloc_Node item_node = alloc->head;
item_node;
item_node = item_node->next
) {
@ -80,8 +80,8 @@ void *KernAux_Malloc_malloc(const KernAux_Malloc malloc, const size_t size)
if (node) {
// Can we split the block?
if (node->actual_size - size >= MIN_ALLOC_SIZE) {
KernAux_Malloc_Node new_node =
(KernAux_Malloc_Node)(((uintptr_t)&node->block) + size);
KernAux_Alloc_Node new_node =
(KernAux_Alloc_Node)(((uintptr_t)&node->block) + size);
new_node->free = true;
new_node->actual_size = node->actual_size - size - ALLOC_HEADER_SIZE;
new_node->user_size = node->user_size - size - ALLOC_HEADER_SIZE;
@ -98,12 +98,12 @@ void *KernAux_Malloc_malloc(const KernAux_Malloc malloc, const size_t size)
return ptr;
}
void KernAux_Malloc_free(const KernAux_Malloc malloc, void *const ptr)
void KernAux_Alloc_free(const KernAux_Alloc alloc, void *const ptr)
{
KERNAUX_ASSERT(malloc);
KERNAUX_ASSERT(alloc);
// TODO: implement this
(void)malloc;
(void)alloc;
(void)ptr;
}

View File

@ -52,6 +52,16 @@ multiboot2_info_print2_SOURCES = \
multiboot2_info_example2.h
endif
##############
# test_alloc #
##############
if WITH_ALLOC
TESTS += test_alloc
test_alloc_LDADD = $(top_builddir)/libkernaux.la
test_alloc_SOURCES = test_alloc.c
endif
################
# test_cmdline #
################
@ -93,16 +103,6 @@ test_elf_LDADD = $(top_builddir)/libkernaux.la
test_elf_SOURCES = test_elf.c
endif
###############
# test_malloc #
###############
if WITH_MALLOC
TESTS += test_malloc
test_malloc_LDADD = $(top_builddir)/libkernaux.la
test_malloc_SOURCES = test_malloc.c
endif
############
# test_mbr #
############

View File

@ -2,32 +2,32 @@
#include "config.h"
#endif
#include <kernaux/malloc.h>
#include <kernaux/alloc.h>
#include <assert.h>
int main()
{
char memory_block[1000];
struct KernAux_Malloc alloc = KernAux_Malloc_create();
KernAux_Malloc_add_memory_block(&alloc, memory_block, sizeof(memory_block));
struct KernAux_Alloc alloc = KernAux_Alloc_create();
KernAux_Alloc_add_memory_block(&alloc, memory_block, sizeof(memory_block));
char *const ptr1 = KernAux_Malloc_malloc(&alloc, 100);
char *const ptr1 = KernAux_Alloc_malloc(&alloc, 100);
assert(ptr1);
assert(ptr1 > memory_block);
assert(ptr1 < &memory_block[1000]);
char *const ptr2 = KernAux_Malloc_malloc(&alloc, 100);
char *const ptr2 = KernAux_Alloc_malloc(&alloc, 100);
assert(ptr2);
assert(ptr2 > ptr1);
assert(ptr2 < &memory_block[1000]);
char *const ptr3 = KernAux_Malloc_malloc(&alloc, 100);
char *const ptr3 = KernAux_Alloc_malloc(&alloc, 100);
assert(ptr3);
assert(ptr3 > ptr2);
assert(ptr3 < &memory_block[1000]);
char *const ptr4 = KernAux_Malloc_malloc(&alloc, 100);
char *const ptr4 = KernAux_Alloc_malloc(&alloc, 100);
assert(ptr4);
assert(ptr4 > ptr3);
assert(ptr4 < &memory_block[1000]);