c++: Update minimal libc headers

Updates the minimal libc headers for differences between the C and C++
languages.  This includes ...
   1. Conditionally defining "bool", "true" and "false" as they are
      already keywords in C++.
   2. Making the definition of NULL language dependent.
   3. Using the _Restrict macro instead of the restrict keyword as
      restrict exists in C, but not in C++.
   4. Changing the definition of size_t so that it is compatible with
      what the compiler expects when building the new operator stubs
      (as it varies by architecture).

Change-Id: I37ff058a60b90a05f96e9dd6f61d454d143041ce
Signed-off-by: Peter Mitsis <peter.mitsis@windriver.com>
This commit is contained in:
Peter Mitsis 2015-12-17 11:04:31 -05:00 committed by Anas Nashif
commit df3c6b13ab
10 changed files with 79 additions and 29 deletions

View file

@ -19,5 +19,9 @@
*/
#if !defined(NULL)
#ifdef __cplusplus
#define NULL 0
#else
#define NULL (void *)0
#endif
#endif

View file

@ -0,0 +1,34 @@
/*
* Copyright (c) 2016 Wind River Systems, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @file _Restrict definition
*
* The macro "_Restrict" is intended to be private to the minimal libc library.
* It evaluates to the "restrict" keyword when a C99 compiler is used, and
* to "__restrict__" when a C++ compiler is used.
*/
#if !defined(_Restrict_defined)
#define _Restrict_defined
#ifdef __cplusplus
#define _Restrict __restrict__
#else
#define _Restrict restrict
#endif
#endif

View file

@ -21,6 +21,14 @@
#if !defined(__size_t_defined)
#define __size_t_defined
#ifdef __i386
typedef unsigned long int size_t;
#elif defined(__ARM_ARCH)
typedef unsigned int size_t;
#elif defined(__arc__)
typedef unsigned int size_t;
#else
#error "The minimal libc library does not recognize the architecture!\n"
#endif
#endif

View file

@ -19,9 +19,11 @@
#ifndef __INC_stdbool_h__
#define __INC_stdbool_h__
#ifndef __cplusplus
#define bool _Bool
#define true 1
#define false 0
#endif
#define __bool_true_false_are_defined 1

View file

@ -22,6 +22,7 @@
#include <stdarg.h> /* Needed to get definition of va_list */
#include <bits/null.h>
#include <bits/size_t.h>
#include <bits/restrict.h>
#ifdef __cplusplus
extern "C" {
@ -45,22 +46,22 @@ typedef int FILE;
* declared below.
*/
int printf(const char *restrict fmt, ...);
int snprintf(char *restrict s, size_t len, const char *restrict fmt, ...);
int sprintf(char *restrict s, const char *restrict fmt, ...);
int fprintf(FILE *restrict stream, const char *restrict format, ...);
int printf(const char *_Restrict fmt, ...);
int snprintf(char *_Restrict s, size_t len, const char *_Restrict fmt, ...);
int sprintf(char *_Restrict s, const char *_Restrict fmt, ...);
int fprintf(FILE *_Restrict stream, const char *_Restrict format, ...);
int vprintf(const char *restrict fmt, va_list list);
int vsnprintf(char *restrict s, size_t len, const char *restrict fmt, va_list list);
int vsprintf(char *restrict s, const char *restrict fmt, va_list list);
int vfprintf(FILE *restrict stream, const char *restrict format, va_list ap);
int vprintf(const char *_Restrict fmt, va_list list);
int vsnprintf(char *_Restrict s, size_t len, const char *_Restrict fmt, va_list list);
int vsprintf(char *_Restrict s, const char *_Restrict fmt, va_list list);
int vfprintf(FILE *_Restrict stream, const char *_Restrict format, va_list ap);
int puts(const char *s);
int fputc(int c, FILE *stream);
int fputs(const char *restrict s, FILE *restrict stream);
size_t fwrite(const void *restrict ptr, size_t size, size_t nitems, FILE *restrict stream);
int fputs(const char *_Restrict s, FILE *_Restrict stream);
size_t fwrite(const void *_Restrict ptr, size_t size, size_t nitems, FILE *_Restrict stream);
#ifdef __cplusplus
}

View file

@ -21,22 +21,23 @@
#include <bits/null.h>
#include <bits/size_t.h>
#include <bits/restrict.h>
#ifdef __cplusplus
extern "C" {
#endif
extern char *strcpy(char *restrict d, const char *restrict s);
extern char *strncpy(char *restrict d, const char *restrict s, size_t n);
extern char *strcpy(char *_Restrict d, const char *_Restrict s);
extern char *strncpy(char *_Restrict d, const char *_Restrict s, size_t n);
extern char *strchr(const char *s, int c);
extern size_t strlen(const char *s);
extern int strcmp(const char *s1, const char *s2);
extern int strncmp(const char *s1, const char *s2, size_t n);
extern char *strcat(char *restrict dest, const char *restrict src);
extern char *strcat(char *_Restrict dest, const char *_Restrict src);
extern int memcmp(const void *m1, const void *m2, size_t n);
extern void *memmove(void *d, const void *s, size_t n);
extern void *memcpy(void *restrict d, const void *restrict s, size_t n);
extern void *memcpy(void *_Restrict d, const void *_Restrict s, size_t n);
extern void *memset(void *buf, int c, size_t n);
extern void *memchr(const void *s, unsigned char c, size_t n);

View file

@ -24,7 +24,7 @@
extern int _prf(int (*func)(), void *dest,
const char *format, va_list vargs);
int fprintf(FILE * restrict F, const char *restrict format, ...)
int fprintf(FILE *_Restrict F, const char *_Restrict format, ...)
{
va_list vargs;
int r;
@ -36,7 +36,7 @@ int fprintf(FILE * restrict F, const char *restrict format, ...)
return r;
}
int vfprintf(FILE * restrict F, const char *restrict format, va_list vargs)
int vfprintf(FILE *_Restrict F, const char *_Restrict format, va_list vargs)
{
int r;
@ -45,7 +45,7 @@ int vfprintf(FILE * restrict F, const char *restrict format, va_list vargs)
return r;
}
int printf(const char *restrict format, ...)
int printf(const char *_Restrict format, ...)
{
va_list vargs;
int r;
@ -57,7 +57,7 @@ int printf(const char *restrict format, ...)
return r;
}
int vprintf(const char *restrict format, va_list vargs)
int vprintf(const char *_Restrict format, va_list vargs)
{
int r;

View file

@ -37,7 +37,7 @@ static int sprintf_out(int c, struct emitter *p)
return 0; /* indicate keep going so we get the total count */
}
int snprintf(char *restrict s, size_t len, const char *restrict format, ...)
int snprintf(char *_Restrict s, size_t len, const char *_Restrict format, ...)
{
va_list vargs;
@ -64,7 +64,7 @@ int snprintf(char *restrict s, size_t len, const char *restrict format, ...)
return r;
}
int sprintf(char *restrict s, const char *restrict format, ...)
int sprintf(char *_Restrict s, const char *_Restrict format, ...)
{
va_list vargs;
@ -82,7 +82,7 @@ int sprintf(char *restrict s, const char *restrict format, ...)
return r;
}
int vsnprintf(char *restrict s, size_t len, const char *restrict format, va_list vargs)
int vsnprintf(char *_Restrict s, size_t len, const char *_Restrict format, va_list vargs)
{
struct emitter p;
int r;
@ -105,7 +105,7 @@ int vsnprintf(char *restrict s, size_t len, const char *restrict format, va_list
return r;
}
int vsprintf(char *restrict s, const char *restrict format, va_list vargs)
int vsprintf(char *_Restrict s, const char *_Restrict format, va_list vargs)
{
struct emitter p;
int r;

View file

@ -37,7 +37,7 @@ int fputc(int c, FILE *stream)
return (stdout == stream) ? _stdout_hook(c) : EOF;
}
int fputs(const char *restrict string, FILE *restrict stream)
int fputs(const char *_Restrict string, FILE *_Restrict stream)
{
if (stream != stdout) {
return EOF;
@ -53,8 +53,8 @@ int fputs(const char *restrict string, FILE *restrict stream)
return 0;
}
size_t fwrite(const void *restrict ptr, size_t size, size_t nitems,
FILE *restrict stream)
size_t fwrite(const void *_Restrict ptr, size_t size, size_t nitems,
FILE *_Restrict stream)
{
size_t i;
size_t j;

View file

@ -25,7 +25,7 @@
* @return pointer to destination buffer <d>
*/
char *strcpy(char *restrict d, const char *restrict s)
char *strcpy(char *_Restrict d, const char *_Restrict s)
{
char *dest = d;
@ -47,7 +47,7 @@ char *strcpy(char *restrict d, const char *restrict s)
* @return pointer to destination buffer <d>
*/
char *strncpy(char *restrict d, const char *restrict s, size_t n)
char *strncpy(char *_Restrict d, const char *_Restrict s, size_t n)
{
char *dest = d;
@ -138,7 +138,7 @@ int strncmp(const char *s1, const char *s2, size_t n)
return (n == 0) ? 0 : (*s1 - *s2);
}
char *strcat(char *restrict dest, const char *restrict src)
char *strcat(char *_Restrict dest, const char *_Restrict src)
{
strcpy(dest + strlen(dest), src);
return dest;
@ -208,7 +208,7 @@ void *memmove(void *d, const void *s, size_t n)
* @return pointer to start of destination buffer
*/
void *memcpy(void *restrict d, const void *restrict s, size_t n)
void *memcpy(void *_Restrict d, const void *_Restrict s, size_t n)
{
/* attempt word-sized copying only if buffers have identical alignment */