From: ljsebald <ljs...@us...> - 2023-08-19 03:14:05
|
This is an automated email from the git hooks/post-receive script. It was generated because a ref change was pushed to the repository containing the project "A pseudo Operating System for the Dreamcast.". The branch, master has been updated via 89148902083e680415afe1976458551d3ed321a7 (commit) from ff68b986d96786cadf25838d77f651989033b9c4 (commit) Those revisions listed above that are new to this repository have not appeared on any other notification email; so we list those revisions in full, below. - Log ----------------------------------------------------------------- commit 89148902083e680415afe1976458551d3ed321a7 Author: Falco Girgis <gyr...@gm...> Date: Fri Aug 18 22:06:58 2023 -0500 Implemented posix_memalign() addition to stdlib.h (#268) * Implemented posix_memalign() addition to stdlib.h - created kos/stdlib.h as the KOS stdlib private include/extension - modified sys/_types.h to conditionally include kos/stdlib.h when including the regular stdlib.h (following the pattern established with kos/time.h) - added POSIX directory and Makefile - added posix_memalign.c with the implementation - updated CHANGELOG * Fixed a typo in kos/stdlib.h header. * Removed C11 check, fixed warnings and indentation - No longer checking for a particular C or C++ version before including posix_memalign - Fixed warnings in posix_memalign.c arising from missing includes and not building with C11 for aligned_alloc() - Fixed indentation in posix_memalign.c * Update include/kos/stdlib.h Co-authored-by: Lawrence Sebald <ljs...@us...> * Update kernel/libc/posix/posix_memalign.c Co-authored-by: Lawrence Sebald <ljs...@us...> * Update kernel/libc/posix/posix_memalign.c Co-authored-by: Lawrence Sebald <ljs...@us...> --------- Co-authored-by: Lawrence Sebald <ljs...@us...> ----------------------------------------------------------------------- Summary of changes: doc/CHANGELOG | 1 + include/kos/stdlib.h | 31 ++++++++++++++++++++++++++ include/sys/_types.h | 5 +++++ kernel/libc/Makefile | 2 +- kernel/libc/posix/Makefile | 16 ++++++++++++++ kernel/libc/posix/posix_memalign.c | 45 ++++++++++++++++++++++++++++++++++++++ 6 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 include/kos/stdlib.h create mode 100644 kernel/libc/posix/Makefile create mode 100644 kernel/libc/posix/posix_memalign.c diff --git a/doc/CHANGELOG b/doc/CHANGELOG index 20bd0bd..68bd32c 100644 --- a/doc/CHANGELOG +++ b/doc/CHANGELOG @@ -178,6 +178,7 @@ KallistiOS version 2.1.0 ----------------------------------------------- - DC Add example for VMU speaker use [DH && FG] - DC Add example for rumble accessory use [DH] - *** Split init flags from <arch/arch.h> into <kos/init.h> [LS] +- *** Implemented posix_memalign() from stdlib.h [FG] KallistiOS version 2.0.0 ----------------------------------------------- - DC Broadband Adapter driver fixes [Megan Potter == MP] diff --git a/include/kos/stdlib.h b/include/kos/stdlib.h new file mode 100644 index 0000000..9d5562f --- /dev/null +++ b/include/kos/stdlib.h @@ -0,0 +1,31 @@ +/* KallistiOS ##version## + + kos/stdlib.h + Copyright (C) 2023 Falco Girgis +*/ + +/* + Add select POSIX extensions to stdlib.h which are not present within Newlib. +*/ + +#ifndef _STDLIB_H_ +#error "Do not include this file directly. Use <stdlib.h> instead." +#endif /* !_STDLIB_H_ */ + +#ifndef __KOS_STDLIB_H +#define __KOS_STDLIB_H + +#if !defined(__STRICT_ANSI__) || (_POSIX_C_SOURCE >= 200112L) || \ + (_XOPEN_SOURCE >= 600) + +#include <kos/cdefs.h> + +__BEGIN_DECLS + +extern int posix_memalign(void **memptr, size_t alignment, size_t size); + +__END_DECLS + +#endif /* !defined(__STRICT_ANSI__) || (_POSIX_C_SOURCE >= 200112L) || + (_XOPEN_SOURCE >= 600) */ +#endif /* !__KOS_STDLIB_H */ diff --git a/include/sys/_types.h b/include/sys/_types.h index 664907a..42ab393 100644 --- a/include/sys/_types.h +++ b/include/sys/_types.h @@ -227,3 +227,8 @@ __END_DECLS #ifdef _TIME_H_ #include <kos/time.h> #endif + +#ifdef _STDLIB_H_ +#include <kos/stdlib.h> +#endif + diff --git a/kernel/libc/Makefile b/kernel/libc/Makefile index 3078b9d..e20fcff 100644 --- a/kernel/libc/Makefile +++ b/kernel/libc/Makefile @@ -4,7 +4,7 @@ # Copyright (C)2004 Megan Potter # -SUBDIRS = koslib newlib pthreads c11 +SUBDIRS = koslib newlib pthreads c11 posix all: subdirs clean: clean_subdirs diff --git a/kernel/libc/posix/Makefile b/kernel/libc/posix/Makefile new file mode 100644 index 0000000..8e07e19 --- /dev/null +++ b/kernel/libc/posix/Makefile @@ -0,0 +1,16 @@ +# KallistiOS ##version## +# +# kernel/libc/posix/Makefile +# Copyright (C) 2023 Falco Girgis +# + +# +# This dir contains the implementation of POSIX-specific +# C and C++ extensions which are implemented by KOS and +# are not provided as part of Newlib. +# + +CFLAGS += -std=c11 +OBJS = posix_memalign.o + +include $(KOS_BASE)/Makefile.prefab diff --git a/kernel/libc/posix/posix_memalign.c b/kernel/libc/posix/posix_memalign.c new file mode 100644 index 0000000..a5b2591 --- /dev/null +++ b/kernel/libc/posix/posix_memalign.c @@ -0,0 +1,45 @@ +/* KallistiOS ##version## + + posix_memalign.c + Copyright (C) 2023 Falco Girgis +*/ + +#include <stdlib.h> +#include <errno.h> +#include <stdlib.h> +#include <assert.h> + +static inline int is_power_of_two(size_t x) { + return (x & (x - 1)) == 0; +} + +static inline size_t aligned_size(size_t size, size_t alignment) { + const size_t align_rem = size % alignment; + size_t new_size = size; + + if(align_rem) + new_size += (alignment - align_rem); + + return new_size; +} + +int posix_memalign(void **memptr, size_t alignment, size_t size) { + if(!memptr) { + return EFAULT; + } + + if(!alignment || !is_power_of_two(alignment) || alignment % sizeof(void*)) { + *memptr = NULL; + return EINVAL; + } + + if(!size) { + *memptr = NULL; + return 0; + } + + size = aligned_size(size, alignment); + *memptr = aligned_alloc(alignment, size); + + return *memptr ? 0 : ENOMEM; +} hooks/post-receive -- A pseudo Operating System for the Dreamcast. |