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/compat/linuxkpi/common/include/linux/string.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  * Copyright (c) 2010 Isilon Systems, Inc.
    3  * Copyright (c) 2010 iX Systems, Inc.
    4  * Copyright (c) 2010 Panasas, Inc.
    5  * Copyright (c) 2013-2017 Mellanox Technologies, Ltd.
    6  * All rights reserved.
    7  *
    8  * Redistribution and use in source and binary forms, with or without
    9  * modification, are permitted provided that the following conditions
   10  * are met:
   11  * 1. Redistributions of source code must retain the above copyright
   12  *    notice unmodified, this list of conditions, and the following
   13  *    disclaimer.
   14  * 2. Redistributions in binary form must reproduce the above copyright
   15  *    notice, this list of conditions and the following disclaimer in the
   16  *    documentation and/or other materials provided with the distribution.
   17  *
   18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
   19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
   21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
   22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
   23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
   24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
   25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
   26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
   27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   28  *
   29  * $FreeBSD$
   30  */
   31 #ifndef _LINUXKPI_LINUX_STRING_H_
   32 #define _LINUXKPI_LINUX_STRING_H_
   33 
   34 #include <sys/ctype.h>
   35 
   36 #include <linux/types.h>
   37 #include <linux/gfp.h>
   38 #include <linux/slab.h>
   39 #include <linux/uaccess.h>
   40 #include <linux/err.h>
   41 #include <linux/bitops.h> /* for BITS_PER_LONG */
   42 
   43 #include <sys/libkern.h>
   44 
   45 #define strnicmp(...) strncasecmp(__VA_ARGS__)
   46 
   47 static inline int
   48 match_string(const char *const *table, int n, const char *key)
   49 {
   50         int i;
   51 
   52         for (i = 0; i != n && table[i] != NULL; i++) {
   53                 if (strcmp(table[i], key) == 0)
   54                         return (i);
   55         }
   56         return (-EINVAL);
   57 }
   58 
   59 static inline void *
   60 memdup_user(const void *ptr, size_t len)
   61 {
   62         void *retval;
   63         int error;
   64 
   65         retval = malloc(len, M_KMALLOC, M_WAITOK);
   66         error = linux_copyin(ptr, retval, len);
   67         if (error != 0) {
   68                 free(retval, M_KMALLOC);
   69                 return (ERR_PTR(error));
   70         }
   71         return (retval);
   72 }
   73 
   74 static inline void *
   75 memdup_user_nul(const void *ptr, size_t len)
   76 {
   77         char *retval;
   78         int error;
   79 
   80         retval = malloc(len + 1, M_KMALLOC, M_WAITOK);
   81         error = linux_copyin(ptr, retval, len);
   82         if (error != 0) {
   83                 free(retval, M_KMALLOC);
   84                 return (ERR_PTR(error));
   85         }
   86         retval[len] = '\0';
   87         return (retval);
   88 }
   89 
   90 static inline void *
   91 kmemdup(const void *src, size_t len, gfp_t gfp)
   92 {
   93         void *dst;
   94 
   95         dst = kmalloc(len, gfp);
   96         if (dst != NULL)
   97                 memcpy(dst, src, len);
   98         return (dst);
   99 }
  100 
  101 static inline char *
  102 strndup_user(const char __user *ustr, long n)
  103 {
  104         if (n < 1)
  105                 return (ERR_PTR(-EINVAL));
  106 
  107         return (memdup_user_nul(ustr, n - 1));
  108 }
  109 
  110 static inline char *
  111 kstrdup(const char *string, gfp_t gfp)
  112 {
  113         char *retval;
  114         size_t len;
  115 
  116         if (string == NULL)
  117                 return (NULL);
  118         len = strlen(string) + 1;
  119         retval = kmalloc(len, gfp);
  120         if (retval != NULL)
  121                 memcpy(retval, string, len);
  122         return (retval);
  123 }
  124 
  125 static inline char *
  126 kstrndup(const char *string, size_t len, gfp_t gfp)
  127 {
  128         char *retval;
  129 
  130         if (string == NULL)
  131                 return (NULL);
  132         retval = kmalloc(len + 1, gfp);
  133         if (retval != NULL)
  134                 strncpy(retval, string, len);
  135         return (retval);
  136 }
  137 
  138 static inline const char *
  139 kstrdup_const(const char *src, gfp_t gfp)
  140 {
  141         return (kmemdup(src, strlen(src) + 1, gfp));
  142 }
  143 
  144 static inline char *
  145 skip_spaces(const char *str)
  146 {
  147         while (isspace(*str))
  148                 ++str;
  149         return (__DECONST(char *, str));
  150 }
  151 
  152 static inline void *
  153 memchr_inv(const void *start, int c, size_t length)
  154 {
  155         const u8 *ptr;
  156         const u8 *end;
  157         u8 ch;
  158 
  159         ch = c;
  160         ptr = start;
  161         end = ptr + length;
  162 
  163         while (ptr != end) {
  164                 if (*ptr != ch)
  165                         return (__DECONST(void *, ptr));
  166                 ptr++;
  167         }
  168         return (NULL);
  169 }
  170 
  171 static inline size_t
  172 str_has_prefix(const char *str, const char *prefix)
  173 {
  174         size_t len;
  175 
  176         len = strlen(prefix);
  177         return (strncmp(str, prefix, len) == 0 ? len : 0);
  178 }
  179 
  180 static inline char *
  181 strreplace(char *str, char old, char new)
  182 {
  183         char *p;
  184 
  185         p = strchrnul(str, old);
  186         while (p != NULL && *p != '\0') {
  187                 *p = new;
  188                 p = strchrnul(str, old);
  189         }
  190         return (p);
  191 }
  192 
  193 static inline ssize_t
  194 strscpy(char* dst, const char* src, size_t len)
  195 {
  196         size_t i;
  197 
  198         if (len <= INT_MAX) {
  199                 for (i = 0; i < len; i++)
  200                         if ('\0' == (dst[i] = src[i]))
  201                                 return ((ssize_t)i);
  202                 if (i != 0)
  203                         dst[--i] = '\0';
  204         }
  205 
  206         return (-E2BIG);
  207 }
  208 
  209 static inline void *
  210 memset32(uint32_t *b, uint32_t c, size_t len)
  211 {
  212         uint32_t *dst = b;
  213 
  214         while (len--)
  215                 *dst++ = c;
  216         return (b);
  217 }
  218 
  219 static inline void *
  220 memset64(uint64_t *b, uint64_t c, size_t len)
  221 {
  222         uint64_t *dst = b;
  223 
  224         while (len--)
  225                 *dst++ = c;
  226         return (b);
  227 }
  228 
  229 static inline void *
  230 memset_p(void **p, void *v, size_t n)
  231 {
  232 
  233         if (BITS_PER_LONG == 32)
  234                 return (memset32((uint32_t *)p, (uintptr_t)v, n));
  235         else
  236                 return (memset64((uint64_t *)p, (uintptr_t)v, n));
  237 }
  238 
  239 static inline void
  240 memcpy_and_pad(void *dst, size_t dstlen, const void *src, size_t len, int ch)
  241 {
  242 
  243         if (len >= dstlen) {
  244                 memcpy(dst, src, dstlen);
  245         } else {
  246                 memcpy(dst, src, len);
  247                 /* Pad with given padding character. */
  248                 memset((char *)dst + len, ch, dstlen - len);
  249         }
  250 }
  251 
  252 #define memset_startat(ptr, bytepat, smember)                           \
  253 ({                                                                      \
  254         uint8_t *_ptr = (uint8_t *)(ptr);                               \
  255         int _c = (int)(bytepat);                                        \
  256         size_t _o = offsetof(typeof(*(ptr)), smember);                  \
  257         memset(_ptr + _o, _c, sizeof(*(ptr)) - _o);                     \
  258 })
  259 
  260 #endif  /* _LINUXKPI_LINUX_STRING_H_ */

Cache object: 6ab5fd8cebd986c433b0966c5a1615ea


[ 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.