The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/sys/libkern.h

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * SPDX-License-Identifier: BSD-3-Clause
    3  *
    4  * Copyright (c) 1992, 1993
    5  *      The Regents of the University of California.  All rights reserved.
    6  *
    7  * Redistribution and use in source and binary forms, with or without
    8  * modification, are permitted provided that the following conditions
    9  * are met:
   10  * 1. Redistributions of source code must retain the above copyright
   11  *    notice, this list of conditions and the following disclaimer.
   12  * 2. Redistributions in binary form must reproduce the above copyright
   13  *    notice, this list of conditions and the following disclaimer in the
   14  *    documentation and/or other materials provided with the distribution.
   15  * 3. Neither the name of the University nor the names of its contributors
   16  *    may be used to endorse or promote products derived from this software
   17  *    without specific prior written permission.
   18  *
   19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   29  * SUCH DAMAGE.
   30  *
   31  *      @(#)libkern.h   8.1 (Berkeley) 6/10/93
   32  * $FreeBSD: releng/12.0/sys/sys/libkern.h 334534 2018-06-02 18:03:35Z mjg $
   33  */
   34 
   35 #ifndef _SYS_LIBKERN_H_
   36 #define _SYS_LIBKERN_H_
   37 
   38 #include <sys/cdefs.h>
   39 #include <sys/types.h>
   40 #ifdef _KERNEL
   41 #include <sys/systm.h>
   42 #endif
   43 
   44 #ifndef LIBKERN_INLINE
   45 #define LIBKERN_INLINE  static __inline
   46 #define LIBKERN_BODY
   47 #endif
   48 
   49 /* BCD conversions. */
   50 extern u_char const     bcd2bin_data[];
   51 extern u_char const     bin2bcd_data[];
   52 extern char const       hex2ascii_data[];
   53 
   54 #define LIBKERN_LEN_BCD2BIN     154
   55 #define LIBKERN_LEN_BIN2BCD     100
   56 #define LIBKERN_LEN_HEX2ASCII   36
   57 
   58 static inline u_char
   59 bcd2bin(int bcd)
   60 {
   61 
   62         KASSERT(bcd >= 0 && bcd < LIBKERN_LEN_BCD2BIN,
   63             ("invalid bcd %d", bcd));
   64         return (bcd2bin_data[bcd]);
   65 }
   66 
   67 static inline u_char
   68 bin2bcd(int bin)
   69 {
   70 
   71         KASSERT(bin >= 0 && bin < LIBKERN_LEN_BIN2BCD,
   72             ("invalid bin %d", bin));
   73         return (bin2bcd_data[bin]);
   74 }
   75 
   76 static inline char
   77 hex2ascii(int hex)
   78 {
   79 
   80         KASSERT(hex >= 0 && hex < LIBKERN_LEN_HEX2ASCII,
   81             ("invalid hex %d", hex));
   82         return (hex2ascii_data[hex]);
   83 }
   84 
   85 static inline bool
   86 validbcd(int bcd)
   87 {
   88 
   89         return (bcd == 0 || (bcd > 0 && bcd <= 0x99 && bcd2bin_data[bcd] != 0));
   90 }
   91 
   92 static __inline int imax(int a, int b) { return (a > b ? a : b); }
   93 static __inline int imin(int a, int b) { return (a < b ? a : b); }
   94 static __inline long lmax(long a, long b) { return (a > b ? a : b); }
   95 static __inline long lmin(long a, long b) { return (a < b ? a : b); }
   96 static __inline u_int max(u_int a, u_int b) { return (a > b ? a : b); }
   97 static __inline u_int min(u_int a, u_int b) { return (a < b ? a : b); }
   98 static __inline quad_t qmax(quad_t a, quad_t b) { return (a > b ? a : b); }
   99 static __inline quad_t qmin(quad_t a, quad_t b) { return (a < b ? a : b); }
  100 static __inline u_quad_t uqmax(u_quad_t a, u_quad_t b) { return (a > b ? a : b); }
  101 static __inline u_quad_t uqmin(u_quad_t a, u_quad_t b) { return (a < b ? a : b); }
  102 static __inline u_long ulmax(u_long a, u_long b) { return (a > b ? a : b); }
  103 static __inline u_long ulmin(u_long a, u_long b) { return (a < b ? a : b); }
  104 static __inline __uintmax_t ummax(__uintmax_t a, __uintmax_t b)
  105 {
  106 
  107         return (a > b ? a : b);
  108 }
  109 static __inline __uintmax_t ummin(__uintmax_t a, __uintmax_t b)
  110 {
  111 
  112         return (a < b ? a : b);
  113 }
  114 static __inline off_t omax(off_t a, off_t b) { return (a > b ? a : b); }
  115 static __inline off_t omin(off_t a, off_t b) { return (a < b ? a : b); }
  116 
  117 static __inline int abs(int a) { return (a < 0 ? -a : a); }
  118 static __inline long labs(long a) { return (a < 0 ? -a : a); }
  119 static __inline quad_t qabs(quad_t a) { return (a < 0 ? -a : a); }
  120 
  121 #define ARC4_ENTR_NONE  0       /* Don't have entropy yet. */
  122 #define ARC4_ENTR_HAVE  1       /* Have entropy. */
  123 #define ARC4_ENTR_SEED  2       /* Reseeding. */
  124 extern int arc4rand_iniseed_state;
  125 
  126 /* Prototypes for non-quad routines. */
  127 struct malloc_type;
  128 uint32_t arc4random(void);
  129 void     arc4random_buf(void *, size_t);
  130 void     arc4rand(void *, u_int, int);
  131 int      timingsafe_bcmp(const void *, const void *, size_t);
  132 void    *bsearch(const void *, const void *, size_t,
  133             size_t, int (*)(const void *, const void *));
  134 #ifndef HAVE_INLINE_FFS
  135 int      ffs(int);
  136 #endif
  137 #ifndef HAVE_INLINE_FFSL
  138 int      ffsl(long);
  139 #endif
  140 #ifndef HAVE_INLINE_FFSLL
  141 int      ffsll(long long);
  142 #endif
  143 #ifndef HAVE_INLINE_FLS
  144 int      fls(int);
  145 #endif
  146 #ifndef HAVE_INLINE_FLSL
  147 int      flsl(long);
  148 #endif
  149 #ifndef HAVE_INLINE_FLSLL
  150 int      flsll(long long);
  151 #endif
  152 #define bitcount64(x)   __bitcount64((uint64_t)(x))
  153 #define bitcount32(x)   __bitcount32((uint32_t)(x))
  154 #define bitcount16(x)   __bitcount16((uint16_t)(x))
  155 #define bitcountl(x)    __bitcountl((u_long)(x))
  156 #define bitcount(x)     __bitcount((u_int)(x))
  157 
  158 int      fnmatch(const char *, const char *, int);
  159 int      locc(int, char *, u_int);
  160 void    *memchr(const void *s, int c, size_t n);
  161 void    *memcchr(const void *s, int c, size_t n);
  162 void    *memmem(const void *l, size_t l_len, const void *s, size_t s_len);
  163 void     qsort(void *base, size_t nmemb, size_t size,
  164             int (*compar)(const void *, const void *));
  165 void     qsort_r(void *base, size_t nmemb, size_t size, void *thunk,
  166             int (*compar)(void *, const void *, const void *));
  167 u_long   random(void);
  168 int      scanc(u_int, const u_char *, const u_char *, int);
  169 void     srandom(u_long);
  170 int      strcasecmp(const char *, const char *);
  171 char    *strcat(char * __restrict, const char * __restrict);
  172 char    *strchr(const char *, int);
  173 int      strcmp(const char *, const char *);
  174 char    *strcpy(char * __restrict, const char * __restrict);
  175 size_t   strcspn(const char * __restrict, const char * __restrict) __pure;
  176 char    *strdup(const char *__restrict, struct malloc_type *);
  177 char    *strncat(char *, const char *, size_t);
  178 char    *strndup(const char *__restrict, size_t, struct malloc_type *);
  179 size_t   strlcat(char *, const char *, size_t);
  180 size_t   strlcpy(char *, const char *, size_t);
  181 size_t   strlen(const char *);
  182 int      strncasecmp(const char *, const char *, size_t);
  183 int      strncmp(const char *, const char *, size_t);
  184 char    *strncpy(char * __restrict, const char * __restrict, size_t);
  185 size_t   strnlen(const char *, size_t);
  186 char    *strrchr(const char *, int);
  187 char    *strsep(char **, const char *delim);
  188 size_t   strspn(const char *, const char *);
  189 char    *strstr(const char *, const char *);
  190 int      strvalid(const char *, size_t);
  191 
  192 extern const uint32_t crc32_tab[];
  193 
  194 static __inline uint32_t
  195 crc32_raw(const void *buf, size_t size, uint32_t crc)
  196 {
  197         const uint8_t *p = (const uint8_t *)buf;
  198 
  199         while (size--)
  200                 crc = crc32_tab[(crc ^ *p++) & 0xFF] ^ (crc >> 8);
  201         return (crc);
  202 }
  203 
  204 static __inline uint32_t
  205 crc32(const void *buf, size_t size)
  206 {
  207         uint32_t crc;
  208 
  209         crc = crc32_raw(buf, size, ~0U);
  210         return (crc ^ ~0U);
  211 }
  212 
  213 uint32_t
  214 calculate_crc32c(uint32_t crc32c, const unsigned char *buffer,
  215     unsigned int length);
  216 #ifdef _KERNEL
  217 #if defined(__amd64__) || defined(__i386__)
  218 uint32_t sse42_crc32c(uint32_t, const unsigned char *, unsigned);
  219 #endif
  220 #if defined(__aarch64__)
  221 uint32_t armv8_crc32c(uint32_t, const unsigned char *, unsigned int);
  222 #endif
  223 #endif
  224 
  225 static __inline char *
  226 index(const char *p, int ch)
  227 {
  228 
  229         return (strchr(p, ch));
  230 }
  231 
  232 static __inline char *
  233 rindex(const char *p, int ch)
  234 {
  235 
  236         return (strrchr(p, ch));
  237 }
  238 
  239 /* fnmatch() return values. */
  240 #define FNM_NOMATCH     1       /* Match failed. */
  241 
  242 /* fnmatch() flags. */
  243 #define FNM_NOESCAPE    0x01    /* Disable backslash escaping. */
  244 #define FNM_PATHNAME    0x02    /* Slash must be matched by slash. */
  245 #define FNM_PERIOD      0x04    /* Period must be matched by period. */
  246 #define FNM_LEADING_DIR 0x08    /* Ignore /<tail> after Imatch. */
  247 #define FNM_CASEFOLD    0x10    /* Case insensitive search. */
  248 #define FNM_IGNORECASE  FNM_CASEFOLD
  249 #define FNM_FILE_NAME   FNM_PATHNAME
  250 
  251 #endif /* !_SYS_LIBKERN_H_ */

Cache object: c2d0361a3f4fc4979ef9088a1805d3e7


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.