libc: Introduce cdefs.h, assert.h and strlen.c
authorAntonio Nino Diaz <antonio.ninodiaz@arm.com>
Mon, 13 Aug 2018 18:51:26 +0000 (19:51 +0100)
committerAntonio Nino Diaz <antonio.ninodiaz@arm.com>
Wed, 22 Aug 2018 09:26:04 +0000 (10:26 +0100)
Change-Id: I76091d52571f1950111c4b1670d5fc3883607715
Signed-off-by: Antonio Nino Diaz <antonio.ninodiaz@arm.com>
include/lib/libc/assert.h [new file with mode: 0644]
include/lib/libc/cdefs.h [new file with mode: 0644]
lib/libc/strlen.c [new file with mode: 0644]

diff --git a/include/lib/libc/assert.h b/include/lib/libc/assert.h
new file mode 100644 (file)
index 0000000..2cca6a0
--- /dev/null
@@ -0,0 +1,39 @@
+/*
+ * Copyright (c) 2018, ARM Limited and Contributors. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+
+#ifndef ASSERT_H
+#define ASSERT_H
+
+#include <cdefs.h>
+#include <debug.h>
+#include <platform_def.h>
+
+#ifndef PLAT_LOG_LEVEL_ASSERT
+#define PLAT_LOG_LEVEL_ASSERT  LOG_LEVEL
+#endif
+
+#if ENABLE_ASSERTIONS
+# if PLAT_LOG_LEVEL_ASSERT >= LOG_LEVEL_VERBOSE
+#  define assert(e)    ((e) ? (void)0 : __assert(__FILE__, __LINE__, #e))
+# elif PLAT_LOG_LEVEL_ASSERT >= LOG_LEVEL_INFO
+#  define assert(e)    ((e) ? (void)0 : __assert(__FILE__, __LINE__))
+# else
+#  define assert(e)    ((e) ? (void)0 : __assert())
+# endif
+#else
+#define assert(e)      ((void)0)
+#endif /* ENABLE_ASSERTIONS */
+
+#if PLAT_LOG_LEVEL_ASSERT >= LOG_LEVEL_VERBOSE
+__dead2 void __assert(const char *file, unsigned int line,
+                     const char *assertion);
+#elif PLAT_LOG_LEVEL_ASSERT >= LOG_LEVEL_INFO
+__dead2 void __assert(const char *file, unsigned int line);
+#else
+__dead2 void __assert(void);
+#endif
+
+#endif /* ASSERT_H */
diff --git a/include/lib/libc/cdefs.h b/include/lib/libc/cdefs.h
new file mode 100644 (file)
index 0000000..b1d10cc
--- /dev/null
@@ -0,0 +1,28 @@
+/*
+ * Copyright (c) 2018, ARM Limited and Contributors. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+
+#ifndef CDEFS_H
+#define CDEFS_H
+
+#define __dead2                __attribute__((__noreturn__))
+#define __deprecated   __attribute__((__deprecated__))
+#define __packed       __attribute__((__packed__))
+#define __used         __attribute__((__used__))
+#define __unused       __attribute__((__unused__))
+#define __aligned(x)   __attribute__((__aligned__(x)))
+#define __section(x)   __attribute__((__section__(x)))
+
+#define __printflike(fmtarg, firstvararg) \
+               __attribute__((__format__ (__printf__, fmtarg, firstvararg)))
+
+#define __weak_reference(sym, alias)   \
+       __asm__(".weak alias");         \
+       __asm__(".equ alias, sym")
+
+#define __STRING(x)    #x
+#define __XSTRING(x)   __STRING(x)
+
+#endif /* CDEFS_H */
diff --git a/lib/libc/strlen.c b/lib/libc/strlen.c
new file mode 100644 (file)
index 0000000..3c27630
--- /dev/null
@@ -0,0 +1,17 @@
+/*
+ * Copyright (c) 2018, ARM Limited and Contributors. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+
+#include <string.h>
+
+size_t strlen(const char *s)
+{
+       const char *cursor = s;
+
+       while (*cursor)
+               cursor++;
+
+       return cursor - s;
+}