From 92cd60799c045157523765d42a5683b7850cae32 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Fri, 28 Aug 2015 22:01:03 +0200 Subject: add stddef.h MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- include/bits/types.h | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++++ include/stddef.h | 72 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 164 insertions(+) create mode 100644 include/bits/types.h create mode 100644 include/stddef.h (limited to 'include') diff --git a/include/bits/types.h b/include/bits/types.h new file mode 100644 index 0000000..1fffa0c --- /dev/null +++ b/include/bits/types.h @@ -0,0 +1,92 @@ +/** + * slibc — Yet another C library + * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +/* That's right. No inclusion-guard; we want multiple + * headers to be able to define types by including this + * header without the need of keeping in mind which + * order headers are included. */ + + +/** + * Signed integer type of the result of subtracting two pointers. + * May not be greater than the with of type long. + */ +#if defined(__NEED_ptrdiff_t) && !defined(__DEFINED_ptrdiff_t) +# define __DEFINED_ptrdiff_t +typedef signed long int ptrdiff_t; +#endif + + +/** + * Unsigned version of `ptrdiff_t` + */ +#if defined(__NEED_uptrdiff_t) && !defined(__DEFINED_uptrdiff_t) +# define __DEFINED_uptrdiff_t +typedef unsigned long int uptrdiff_t; +#endif + + +/** + * Integer type which can represent any character. + * May not be greater than the with of type long. + * + * Unicode originally had an, inclusive upper limit + * of U+FFFF. UTF-8 however had 31-bits (U+7FFFFFFF). + * Unicode is now limited to 0x10FFFF. + * + * 32 bits should be sufficient (long is at least 32 + * bits), but why limit further than POSIX does. + */ +#if defined(__NEED_wchar_t) && !defined(__DEFINED_wchar_t) +# define __DEFINED_wchar_t +typedef long int wchar_t; +#endif + + +/** + * Unsigned integer type of the result of the + * `sizeof` operator. May not be greater than the + * with of type long. + */ +#if defined(__NEED_size_t) && !defined(__DEFINED_size_t) +# define __DEFINED_size_t +typedef unsigned long int size_t; +#endif + + +/** + * Signed version of `size_t` + */ +#if defined(__NEED_ssize_t) && !defined(__DEFINED_ssize_t) +# define __DEFINED_ssize_t +typedef signed long int ssize_t; +#endif + + +/** + * A type, of unspecified construct, whose alignment requirement + * is at least as strict as that of every scalar type. + */ +#if defined(__NEED_max_align_t) && !defined(__DEFINED_max_align_t) +# define __DEFINED_max_align_t +typedef struct +{ + long long int __lli __attribute__((__aligned__(__alignof__(long long int)))); + long double __ld __attribute__((__aligned__(__alignof__(long double)))); +} max_align_t; +#endif + diff --git a/include/stddef.h b/include/stddef.h new file mode 100644 index 0000000..b8ad4ea --- /dev/null +++ b/include/stddef.h @@ -0,0 +1,72 @@ +/** + * slibc — Yet another C library + * Copyright © 2015 Mattias Andrée (maandree@member.fsf.org) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#ifndef _STDDEF_H +#define _STDDEF_H + + + +/** + * `NULL` is a zero-values constant, conventionally + * a `void`-pointer (it is specified this way in + * POSIX, it does not really matter and ISO C does + * not specify it this way), use to indicate that + * a pointer does not point to anything. + */ +#define NULL ((void*)0) + + +/** + * Get the offset of a member in a `struct`, in bytes. + * + * For example. Consider the structure + * ``` + * struct example + * { + * char offset_is_0[16]; + * char offset_is_16[16]; + * } + * ``` + * `offsetof(struct example, offset_is_0)` evaluates to + * 0 because it is at the top of the structure. + * `offsetof(struct example, offset_is_16)` evaluates to + * 16 because the member above it in the structure + * has offset 0 and size 16. 0 + 16 = 16. + * + * @param type:identifier The identifier for a structure. + * @param member:identifier The identifier for a member, direct or indirect, + * of the structure. + * @return :size_t The offset of the member. + */ +#define offsetof(type, member) \ + ((size_t)((char*)&(((type*)NULL)->member) - (char*)NULL)) + + + +#define __NEED_ptrdiff_t +#define __NEED_wchar_t +#define __NEED_size_t +#if __STDC_VERSION__ >= 201112L +# define __NEED_max_align_t +#endif + +#include + + + +#endif + -- cgit v1.2.3-70-g09d2