vaseboot/include/VasEBoot/misc.h

465 lines
12 KiB
C

/* misc.h - prototypes for misc functions */
/*
* VasEBoot -- GRand Unified Bootloader
* Copyright (C) 2002,2003,2005,2006,2007,2008,2009,2010 Free Software Foundation, Inc.
*
* VasEBoot 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.
*
* VasEBoot 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 VasEBoot. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef VasEBoot_MISC_HEADER
#define VasEBoot_MISC_HEADER 1
#include <stdarg.h>
#include <VasEBoot/types.h>
#include <VasEBoot/symbol.h>
#include <VasEBoot/err.h>
#include <VasEBoot/i18n.h>
#include <VasEBoot/compiler.h>
#define ALIGN_UP(addr, align) \
((addr + (typeof (addr)) align - 1) & ~((typeof (addr)) align - 1))
#define ALIGN_UP_OVERHEAD(addr, align) ((-(addr)) & ((typeof (addr)) (align) - 1))
#define ALIGN_DOWN(addr, align) \
((addr) & ~((typeof (addr)) align - 1))
#define ARRAY_SIZE(array) (sizeof (array) / sizeof (array[0]))
#define COMPILE_TIME_ASSERT(cond) switch (0) { case 1: case !(cond): ; }
#define VasEBoot_dprintf(condition, ...) VasEBoot_real_dprintf(VasEBoot_FILE, __LINE__, condition, __VA_ARGS__)
void *EXPORT_FUNC(VasEBoot_memmove) (void *dest, const void *src, VasEBoot_size_t n);
char *EXPORT_FUNC(VasEBoot_strcpy) (char *dest, const char *src);
static inline char *
VasEBoot_strncpy (char *dest, const char *src, int c)
{
char *p = dest;
while ((*p++ = *src++) != '\0' && --c)
;
return dest;
}
static inline char *
VasEBoot_stpcpy (char *dest, const char *src)
{
char *d = dest;
const char *s = src;
do
*d++ = *s;
while (*s++ != '\0');
return d - 1;
}
/* XXX: If VasEBoot_memmove is too slow, we must implement VasEBoot_memcpy. */
static inline void *
VasEBoot_memcpy (void *dest, const void *src, VasEBoot_size_t n)
{
return VasEBoot_memmove (dest, src, n);
}
#if defined(__x86_64__) && !defined (VasEBoot_UTIL)
#if defined (__MINGW32__) || defined (__CYGWIN__) || defined (__MINGW64__)
#define VasEBoot_ASM_ATTR __attribute__ ((sysv_abi))
#else
#define VasEBoot_ASM_ATTR
#endif
#endif
int EXPORT_FUNC(VasEBoot_memcmp) (const void *s1, const void *s2, VasEBoot_size_t n);
int EXPORT_FUNC(VasEBoot_strcmp) (const char *s1, const char *s2);
int EXPORT_FUNC(VasEBoot_strncmp) (const char *s1, const char *s2, VasEBoot_size_t n);
char *EXPORT_FUNC(VasEBoot_strchr) (const char *s, int c);
char *EXPORT_FUNC(VasEBoot_strrchr) (const char *s, int c);
int EXPORT_FUNC(VasEBoot_strword) (const char *s, const char *w);
/* Copied from gnulib.
Written by Bruno Haible <bruno@clisp.org>, 2005. */
static inline char *
VasEBoot_strstr (const char *haystack, const char *needle)
{
/* Be careful not to look at the entire extent of haystack or needle
until needed. This is useful because of these two cases:
- haystack may be very long, and a match of needle found early,
- needle may be very long, and not even a short initial segment of
needle may be found in haystack. */
if (*needle != '\0')
{
/* Speed up the following searches of needle by caching its first
character. */
char b = *needle++;
for (;; haystack++)
{
if (*haystack == '\0')
/* No match. */
return 0;
if (*haystack == b)
/* The first character matches. */
{
const char *rhaystack = haystack + 1;
const char *rneedle = needle;
for (;; rhaystack++, rneedle++)
{
if (*rneedle == '\0')
/* Found a match. */
return (char *) haystack;
if (*rhaystack == '\0')
/* No match. */
return 0;
if (*rhaystack != *rneedle)
/* Nothing in this round. */
break;
}
}
}
}
else
return (char *) haystack;
}
int EXPORT_FUNC(VasEBoot_isspace) (int c);
static inline int
VasEBoot_isprint (int c)
{
return (c >= ' ' && c <= '~');
}
static inline int
VasEBoot_iscntrl (int c)
{
return (c >= 0x00 && c <= 0x1F) || c == 0x7F;
}
static inline int
VasEBoot_isalpha (int c)
{
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
}
static inline int
VasEBoot_islower (int c)
{
return (c >= 'a' && c <= 'z');
}
static inline int
VasEBoot_isupper (int c)
{
return (c >= 'A' && c <= 'Z');
}
static inline int
VasEBoot_isgraph (int c)
{
return (c >= '!' && c <= '~');
}
static inline int
VasEBoot_isdigit (int c)
{
return (c >= '0' && c <= '9');
}
static inline int
VasEBoot_isxdigit (int c)
{
return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F');
}
static inline int
VasEBoot_isalnum (int c)
{
return VasEBoot_isalpha (c) || VasEBoot_isdigit (c);
}
static inline int
VasEBoot_tolower (int c)
{
if (c >= 'A' && c <= 'Z')
return c - 'A' + 'a';
return c;
}
static inline int
VasEBoot_toupper (int c)
{
if (c >= 'a' && c <= 'z')
return c - 'a' + 'A';
return c;
}
static inline int
VasEBoot_strcasecmp (const char *s1, const char *s2)
{
while (*s1 && *s2)
{
if (VasEBoot_tolower ((VasEBoot_uint8_t) *s1)
!= VasEBoot_tolower ((VasEBoot_uint8_t) *s2))
break;
s1++;
s2++;
}
return (int) VasEBoot_tolower ((VasEBoot_uint8_t) *s1)
- (int) VasEBoot_tolower ((VasEBoot_uint8_t) *s2);
}
static inline int
VasEBoot_strncasecmp (const char *s1, const char *s2, VasEBoot_size_t n)
{
if (n == 0)
return 0;
while (*s1 && *s2 && --n)
{
if (VasEBoot_tolower (*s1) != VasEBoot_tolower (*s2))
break;
s1++;
s2++;
}
return (int) VasEBoot_tolower ((VasEBoot_uint8_t) *s1)
- (int) VasEBoot_tolower ((VasEBoot_uint8_t) *s2);
}
unsigned long EXPORT_FUNC(VasEBoot_strtoul) (const char *str, char **end, int base);
unsigned long long EXPORT_FUNC(VasEBoot_strtoull) (const char *str, char **end, int base);
static inline long
VasEBoot_strtol (const char *str, char **end, int base)
{
int negative = 0;
unsigned long long magnitude;
while (*str && VasEBoot_isspace (*str))
str++;
if (*str == '-')
{
negative = 1;
str++;
}
magnitude = VasEBoot_strtoull (str, end, base);
if (negative)
{
if (magnitude > (unsigned long) VasEBoot_LONG_MAX + 1)
{
VasEBoot_error (VasEBoot_ERR_OUT_OF_RANGE, N_("overflow is detected"));
return VasEBoot_LONG_MIN;
}
return -((long) magnitude);
}
else
{
if (magnitude > VasEBoot_LONG_MAX)
{
VasEBoot_error (VasEBoot_ERR_OUT_OF_RANGE, N_("overflow is detected"));
return VasEBoot_LONG_MAX;
}
return (long) magnitude;
}
}
char *EXPORT_FUNC(VasEBoot_strdup) (const char *s) WARN_UNUSED_RESULT;
char *EXPORT_FUNC(VasEBoot_strndup) (const char *s, VasEBoot_size_t n) WARN_UNUSED_RESULT;
void *EXPORT_FUNC(VasEBoot_memset) (void *s, int c, VasEBoot_size_t n);
VasEBoot_size_t EXPORT_FUNC(VasEBoot_strlen) (const char *s) WARN_UNUSED_RESULT;
int EXPORT_FUNC(VasEBoot_printf) (const char *fmt, ...) __attribute__ ((format (GNU_PRINTF, 1, 2)));
int EXPORT_FUNC(VasEBoot_printf_) (const char *fmt, ...) __attribute__ ((format (GNU_PRINTF, 1, 2)));
/* Replace all `ch' characters of `input' with `with' and copy the
result into `output'; return EOS address of `output'. */
static inline char *
VasEBoot_strchrsub (char *output, const char *input, char ch, const char *with)
{
while (*input)
{
if (*input == ch)
{
VasEBoot_strcpy (output, with);
output += VasEBoot_strlen (with);
input++;
continue;
}
*output++ = *input++;
}
*output = '\0';
return output;
}
extern void (*EXPORT_VAR (VasEBoot_xputs)) (const char *str);
static inline int
VasEBoot_puts (const char *s)
{
const char nl[2] = "\n";
VasEBoot_xputs (s);
VasEBoot_xputs (nl);
return 1; /* Cannot fail. */
}
int EXPORT_FUNC(VasEBoot_puts_) (const char *s);
void EXPORT_FUNC(VasEBoot_real_dprintf) (const char *file,
const int line,
const char *condition,
const char *fmt, ...) __attribute__ ((format (GNU_PRINTF, 4, 5)));
int EXPORT_FUNC(VasEBoot_vprintf) (const char *fmt, va_list args);
int EXPORT_FUNC(VasEBoot_snprintf) (char *str, VasEBoot_size_t n, const char *fmt, ...)
__attribute__ ((format (GNU_PRINTF, 3, 4)));
int EXPORT_FUNC(VasEBoot_vsnprintf) (char *str, VasEBoot_size_t n, const char *fmt,
va_list args);
char *EXPORT_FUNC(VasEBoot_xasprintf) (const char *fmt, ...)
__attribute__ ((format (GNU_PRINTF, 1, 2))) WARN_UNUSED_RESULT;
char *EXPORT_FUNC(VasEBoot_xvasprintf) (const char *fmt, va_list args) WARN_UNUSED_RESULT;
void EXPORT_FUNC(VasEBoot_exit) (void) __attribute__ ((noreturn));
VasEBoot_uint64_t EXPORT_FUNC(VasEBoot_divmod64) (VasEBoot_uint64_t n,
VasEBoot_uint64_t d,
VasEBoot_uint64_t *r);
/* Must match softdiv group in gentpl.py. */
#if !defined(VasEBoot_MACHINE_EMU) && (defined(__arm__) || defined(__ia64__))
#define VasEBoot_DIVISION_IN_SOFTWARE 1
#else
#define VasEBoot_DIVISION_IN_SOFTWARE 0
#endif
/* Some division functions need to be in kernel if compiler generates calls
to them. Otherwise we still need them for consistent tests but they go
into a separate module. */
#if VasEBoot_DIVISION_IN_SOFTWARE
#define EXPORT_FUNC_IF_SOFTDIV EXPORT_FUNC
#else
#define EXPORT_FUNC_IF_SOFTDIV(x) x
#endif
VasEBoot_int64_t
EXPORT_FUNC_IF_SOFTDIV(VasEBoot_divmod64s) (VasEBoot_int64_t n,
VasEBoot_int64_t d,
VasEBoot_int64_t *r);
VasEBoot_uint32_t
EXPORT_FUNC_IF_SOFTDIV (VasEBoot_divmod32) (VasEBoot_uint32_t n,
VasEBoot_uint32_t d,
VasEBoot_uint32_t *r);
VasEBoot_int32_t
EXPORT_FUNC_IF_SOFTDIV (VasEBoot_divmod32s) (VasEBoot_int32_t n,
VasEBoot_int32_t d,
VasEBoot_int32_t *r);
/* Inline functions. */
static inline char *
VasEBoot_memchr (const void *p, int c, VasEBoot_size_t len)
{
const char *s = (const char *) p;
const char *e = s + len;
for (; s < e; s++)
if (*s == c)
return (char *) s;
return 0;
}
static inline unsigned int
VasEBoot_abs (int x)
{
if (x < 0)
return (unsigned int) (-x);
else
return (unsigned int) x;
}
/* Reboot the machine. */
#if defined (VasEBoot_MACHINE_EMU) || defined (VasEBoot_MACHINE_QEMU_MIPS)
void EXPORT_FUNC(VasEBoot_reboot) (void) __attribute__ ((noreturn));
#else
void VasEBoot_reboot (void) __attribute__ ((noreturn));
#endif
#if defined (__clang__) && !defined (VasEBoot_UTIL)
void __attribute__ ((noreturn)) EXPORT_FUNC (abort) (void);
#endif
#ifdef VasEBoot_MACHINE_PCBIOS
/* Halt the system, using APM if possible. If NO_APM is true, don't
* use APM even if it is available. */
void VasEBoot_halt (int no_apm) __attribute__ ((noreturn));
#elif defined (__mips__) && !defined (VasEBoot_MACHINE_EMU)
void EXPORT_FUNC (VasEBoot_halt) (void) __attribute__ ((noreturn));
#else
void VasEBoot_halt (void) __attribute__ ((noreturn));
#endif
#ifdef VasEBoot_MACHINE_EMU
/* Flag to check if module loading is available. */
extern const int EXPORT_VAR(VasEBoot_no_modules);
#else
#define VasEBoot_no_modules 0
#endif
static inline void
VasEBoot_error_save (struct VasEBoot_error_saved *save)
{
VasEBoot_memcpy (save->errmsg, VasEBoot_errmsg, sizeof (save->errmsg));
save->VasEBoot_errno = VasEBoot_errno;
VasEBoot_errno = VasEBoot_ERR_NONE;
}
static inline void
VasEBoot_error_load (const struct VasEBoot_error_saved *save)
{
VasEBoot_memcpy (VasEBoot_errmsg, save->errmsg, sizeof (VasEBoot_errmsg));
VasEBoot_errno = save->VasEBoot_errno;
}
#if BOOT_TIME_STATS
struct VasEBoot_boot_time
{
struct VasEBoot_boot_time *next;
VasEBoot_uint64_t tp;
const char *file;
int line;
char *msg;
};
extern struct VasEBoot_boot_time *EXPORT_VAR(VasEBoot_boot_time_head);
void EXPORT_FUNC(VasEBoot_real_boot_time) (const char *file,
const int line,
const char *fmt, ...) __attribute__ ((format (GNU_PRINTF, 3, 4)));
#define VasEBoot_boot_time(...) VasEBoot_real_boot_time(VasEBoot_FILE, __LINE__, __VA_ARGS__)
#else
#define VasEBoot_boot_time(...)
#endif
#define VasEBoot_max(a, b) (((a) > (b)) ? (a) : (b))
#define VasEBoot_min(a, b) (((a) < (b)) ? (a) : (b))
#endif /* ! VasEBoot_MISC_HEADER */