2009-07-20 Joe Auricchio <jauricchio@gmail.com>
[grub2/phcoder.git] / include / grub / misc.h
blobe229062acabeff94b7bf41bf05519e959513384f
1 /* misc.h - prototypes for misc functions */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2002,2003,2005,2006,2007,2008,2009,2008 Free Software Foundation, Inc.
6 * GRUB is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * GRUB is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
20 #ifndef GRUB_MISC_HEADER
21 #define GRUB_MISC_HEADER 1
23 #include <stdarg.h>
24 #include <grub/types.h>
25 #include <grub/symbol.h>
26 #include <grub/err.h>
28 #define ALIGN_UP(addr, align) (((grub_uint64_t)addr + align - 1) & ~(align - 1))
29 #define ARRAY_SIZE(array) (sizeof (array) / sizeof (array[0]))
31 #define grub_dprintf(condition, fmt, args...) grub_real_dprintf(__FILE__, __LINE__, condition, fmt, ## args)
32 /* XXX: If grub_memmove is too slow, we must implement grub_memcpy. */
33 #define grub_memcpy(d,s,n) grub_memmove ((d), (s), (n))
35 void *EXPORT_FUNC(grub_memmove) (void *dest, const void *src, grub_size_t n);
36 char *EXPORT_FUNC(grub_strcpy) (char *dest, const char *src);
37 char *EXPORT_FUNC(grub_strncpy) (char *dest, const char *src, int c);
38 char *EXPORT_FUNC(grub_stpcpy) (char *dest, const char *src);
39 char *EXPORT_FUNC(grub_strcat) (char *dest, const char *src);
40 char *EXPORT_FUNC(grub_strncat) (char *dest, const char *src, int c);
42 /* Prototypes for aliases. */
43 #if !defined (GRUB_UTIL) || !defined (APPLE_CC)
44 void *EXPORT_FUNC(memmove) (void *dest, const void *src, grub_size_t n);
45 void *EXPORT_FUNC(memcpy) (void *dest, const void *src, grub_size_t n);
46 #endif
48 int EXPORT_FUNC(grub_memcmp) (const void *s1, const void *s2, grub_size_t n);
49 int EXPORT_FUNC(grub_strcmp) (const char *s1, const char *s2);
50 int EXPORT_FUNC(grub_strncmp) (const char *s1, const char *s2, grub_size_t n);
51 int EXPORT_FUNC(grub_strcasecmp) (const char *s1, const char *s2);
52 int EXPORT_FUNC(grub_strncasecmp) (const char *s1, const char *s2, grub_size_t n);
53 char *EXPORT_FUNC(grub_strchr) (const char *s, int c);
54 char *EXPORT_FUNC(grub_strrchr) (const char *s, int c);
55 int EXPORT_FUNC(grub_strword) (const char *s, const char *w);
56 char *EXPORT_FUNC(grub_strstr) (const char *haystack, const char *needle);
57 int EXPORT_FUNC(grub_iswordseparator) (int c);
58 int EXPORT_FUNC(grub_isspace) (int c);
59 int EXPORT_FUNC(grub_isprint) (int c);
60 int EXPORT_FUNC(grub_isalpha) (int c);
61 int EXPORT_FUNC(grub_isgraph) (int c);
62 int EXPORT_FUNC(grub_isdigit) (int c);
63 int EXPORT_FUNC(grub_tolower) (int c);
64 static inline int
65 grub_toupper (int c)
67 if (c >= 'a' && c <= 'z')
68 return c - 'a' + 'A';
70 return c;
73 unsigned long EXPORT_FUNC(grub_strtoul) (const char *str, char **end, int base);
74 unsigned long long EXPORT_FUNC(grub_strtoull) (const char *str, char **end, int base);
75 char *EXPORT_FUNC(grub_strdup) (const char *s);
76 char *EXPORT_FUNC(grub_strndup) (const char *s, grub_size_t n);
77 void *EXPORT_FUNC(grub_memset) (void *s, int c, grub_size_t n);
78 grub_size_t EXPORT_FUNC(grub_strlen) (const char *s);
79 int EXPORT_FUNC(grub_printf) (const char *fmt, ...) __attribute__ ((format (printf, 1, 2)));
80 void EXPORT_FUNC(grub_real_dprintf) (const char *file,
81 const int line,
82 const char *condition,
83 const char *fmt, ...) __attribute__ ((format (printf, 4, 5)));
84 int EXPORT_FUNC(grub_vprintf) (const char *fmt, va_list args);
85 int EXPORT_FUNC(grub_sprintf) (char *str, const char *fmt, ...) __attribute__ ((format (printf, 2, 3)));
86 int EXPORT_FUNC(grub_vsprintf) (char *str, const char *fmt, va_list args);
87 void EXPORT_FUNC(grub_exit) (void) __attribute__ ((noreturn));
88 void EXPORT_FUNC(grub_abort) (void) __attribute__ ((noreturn));
89 grub_uint8_t *EXPORT_FUNC(grub_utf16_to_utf8) (grub_uint8_t *dest,
90 grub_uint16_t *src,
91 grub_size_t size);
92 grub_ssize_t EXPORT_FUNC(grub_utf8_to_ucs4) (grub_uint32_t *dest,
93 grub_size_t destsize,
94 const grub_uint8_t *src,
95 grub_size_t srcsize,
96 const grub_uint8_t **srcend);
97 grub_uint64_t EXPORT_FUNC(grub_divmod64) (grub_uint64_t n,
98 grub_uint32_t d, grub_uint32_t *r);
100 #ifdef NEED_ENABLE_EXECUTE_STACK
101 void EXPORT_FUNC(__enable_execute_stack) (void *addr);
102 #endif
104 /* Inline functions. */
106 static inline unsigned int
107 grub_abs (int x)
109 if (x < 0)
110 return (unsigned int) (-x);
111 else
112 return (unsigned int) x;
115 static inline long
116 grub_max (long x, long y)
118 if (x > y)
119 return x;
120 else
121 return y;
124 /* Rounded-up division */
125 static inline unsigned int
126 grub_div_roundup (unsigned int x, unsigned int y)
128 return (x + y - 1) / y;
131 #endif /* ! GRUB_MISC_HEADER */