libc.h (5531B)
1 /* 2 * 3 * This license is set out in https://raw.githubusercontent.com/Broadcom-Network-Switching-Software/OpenBCM/master/Legal/LICENSE file. 4 * 5 * Copyright 2007-2019 Broadcom Inc. All rights reserved. 6 * 7 * File: libc.h 8 * Purpose: Some C library functions to remove dependencies 9 * of the driver on external libraries. 10 * 11 * The compile-time flag -DRTOS_STRINGS should be used if your RTOS 12 * supports the standard <string.h> routines. The RTOS library or 13 * built-in versions of these routines are likely to be much more 14 * efficient than the stand-in versions below. 15 */ 16 17 #ifndef _SAL_LIBC_H 18 #define _SAL_LIBC_H 19 20 #ifdef __KERNEL__ 21 #include <linux/types.h> 22 #include <linux/ctype.h> 23 #include <linux/kernel.h> 24 #endif 25 26 #include <sal/types.h> 27 #include <stdarg.h> 28 29 #ifndef NULL 30 #define NULL 0 31 #endif 32 33 #if defined(UNIX) || defined(__KERNEL__) || \ 34 defined(LINUX) 35 #define RTOS_STRINGS 36 #endif 37 38 #if defined(RTOS_STRINGS) && defined(__KERNEL__) 39 #include <linux/string.h> 40 #else 41 #include <string.h> 42 #endif 43 44 #ifdef RTOS_STRINGS 45 /* { */ 46 47 #define sal_strlen strlen 48 #define sal_strcpy strcpy 49 #define sal_strncpy strncpy 50 #define sal_strcmp strcmp 51 #define sal_strstr strstr 52 #define sal_strcat strcat 53 #define sal_strncat strncat 54 #define sal_strncmp strncmp 55 #define sal_strchr strchr 56 #define sal_strrchr strrchr 57 #define sal_strspn strspn 58 #define sal_strtok strtok 59 #define sal_strtoul strtoul 60 #define sal_strtol strtol 61 #define sal_toupper toupper 62 #define sal_tolower tolower 63 #define sal_atoi atoi 64 65 #define sal_abort abort 66 #define sal_fgets fgets 67 #define sal_fgetc fgetc 68 #define sal_fputs fputs 69 #define sal_fputc fputc 70 #define sal_fwrite fwrite 71 #define sal_fprintf fprintf 72 #define sal_fflush fflush 73 #define sal_vfprintf vfprintf 74 75 76 77 #if defined(memcpy) 78 void *sal_memcpy_wrapper(void *, const void *, size_t); 79 #define sal_memcpy sal_memcpy_wrapper 80 #else 81 #define sal_memcpy memcpy 82 #endif 83 84 #define sal_memset memset 85 #define sal_memmove memmove 86 87 /* } */ 88 #else /* !RTOS_STRINGS */ 89 /* { */ 90 extern int sal_strlen(const char *s); 91 extern char *sal_strcpy(char *, const char *); 92 extern char *sal_strncpy(char *, const char *, size_t); 93 extern int sal_strcmp(const char *, const char *); 94 extern int sal_strncmp(const char *, const char *, size_t); 95 96 extern void *sal_memcpy(void *, const void *, size_t); 97 extern void *sal_memmove(void *, const void *, size_t); 98 extern void *sal_memset(void *, int, size_t); 99 extern char *sal_strstr(const char *haystack, const char *needle); 100 extern char *sal_strcat(const char *dest, const char *src); 101 extern char *sal_strncat(const char *dest, const char *src, size_t n); 102 extern int sal_strncmp(const char *s1, const char *s2, size_t n); 103 extern char *sal_strchr(const char *s, int c); 104 extern char *sal_strrchr(const char *s, int c); 105 extern size_t sal_strspn(const char *s, const char *accept); 106 /* Should be avoided, not thread safe !!! */ 107 extern char *sal_strtok(char *str, const char *delim); 108 extern unsigned long int sal_strtoul(const char *nptr, 109 char **endptr, int base); 110 extern long int sal_strtol(const char *nptr, char **endptr, int base); 111 extern int sal_toupper(int c); 112 extern int sal_tolower(int c); 113 extern int sal_atoi(const char *nptr); 114 extern void sal_abort(void); 115 /* } */ 116 #endif /* !RTOS_STRINGS */ 117 118 /* 119 * Do exactly as sal_strncpy() but always set NULL ('0') as last character 120 * of the destination string. 121 * Unlike strncpy(), the destination here is ended with NULL regardless 122 * of the comntents of the source. 123 */ 124 extern char *sal_strncpy_s(char *dst, const char *src, size_t length); 125 /* 126 * Always use our version of strnlen, since the original strnlen() 127 * depends on '_GNU_SOURCE' definition which is not available on all 128 * the various compilations. 129 */ 130 extern int sal_strnlen(const char *, size_t); 131 /* Always use our version of memcmp, since it is broken 132 * in certain OS-versions (e..g Linux 2.4.18) 133 */ 134 extern int sal_memcmp(const void *, const void *, size_t); 135 136 /* Always use our version of strdup, strndup, which uses 137 * sal_alloc() instead of malloc(), and can be freed by sal_free() 138 */ 139 extern char *sal_strdup(const char *s); 140 extern char *sal_strndup(const char *s, size_t); 141 142 extern char *sal_strtok_r(char *s1, const char *delim, char **s2); 143 144 extern int sal_ctoi(const char *s, char **end); /* C constant to integer */ 145 extern void sal_itoa(char *buf, uint32 num, 146 int base, int caps, int prec); 147 148 #ifdef COMPILER_HAS_DOUBLE 149 extern void sal_ftoa(char *buf, double f, int decimals); 150 #endif 151 152 extern int sal_vsnprintf(char *buf, size_t bufsize, 153 const char *fmt, va_list ap) 154 COMPILER_ATTRIBUTE ((format (printf, 3, 0))); 155 extern int sal_vsprintf(char *buf, const char *fmt, va_list ap) 156 COMPILER_ATTRIBUTE ((format (printf, 2, 0))); 157 extern int sal_snprintf(char *buf, size_t bufsize, const char *fmt, ...) 158 COMPILER_ATTRIBUTE ((format (printf, 3, 4))); 159 extern int sal_sprintf(char *buf, const char *fmt, ...) 160 COMPILER_ATTRIBUTE ((format (printf, 2, 3))); 161 extern void sal_free_safe(void *ptr); 162 163 #define SAL_RAND_MAX 32767 164 165 extern int sal_rand(void); 166 extern void sal_srand(unsigned seed); 167 extern uint32 sal_ceil_func(uint32 numerators , uint32 denominator); 168 extern uint32 sal_floor_func(uint32 numerators , uint32 denominator); 169 170 #endif /* !_SAL_LIBC_H */