Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1. /*
  2. FUNCTION
  3.         <<memccpy>>---copy memory regions with end-token check
  4.  
  5. ANSI_SYNOPSIS
  6.         #include <string.h>
  7.         void* memccpy(void *restrict <[out]>, const void *restrict <[in]>,
  8.                       int <[endchar]>, size_t <[n]>);
  9.  
  10. TRAD_SYNOPSIS
  11.         void *memccpy(<[out]>, <[in]>, <[endchar]>, <[n]>
  12.         void *<[out]>;
  13.         void *<[in]>;
  14.         int <[endchar]>;
  15.         size_t <[n]>;
  16.  
  17. DESCRIPTION
  18.         This function copies up to <[n]> bytes from the memory region
  19.         pointed to by <[in]> to the memory region pointed to by
  20.         <[out]>.  If a byte matching the <[endchar]> is encountered,
  21.         the byte is copied and copying stops.
  22.  
  23.         If the regions overlap, the behavior is undefined.
  24.  
  25. RETURNS
  26.         <<memccpy>> returns a pointer to the first byte following the
  27.         <[endchar]> in the <[out]> region.  If no byte matching
  28.         <[endchar]> was copied, then <<NULL>> is returned.
  29.  
  30. PORTABILITY
  31. <<memccpy>> is a GNU extension.
  32.  
  33. <<memccpy>> requires no supporting OS subroutines.
  34.  
  35.         */
  36.  
  37. #include <_ansi.h>
  38. #include <stddef.h>
  39. #include <string.h>
  40. #include <limits.h>
  41.  
  42. /* Nonzero if either X or Y is not aligned on a "long" boundary.  */
  43. #define UNALIGNED(X, Y) \
  44.   (((long)X & (sizeof (long) - 1)) | ((long)Y & (sizeof (long) - 1)))
  45.  
  46. /* How many bytes are copied each iteration of the word copy loop.  */
  47. #define LITTLEBLOCKSIZE (sizeof (long))
  48.  
  49. /* Threshhold for punting to the byte copier.  */
  50. #define TOO_SMALL(LEN)  ((LEN) < LITTLEBLOCKSIZE)
  51.  
  52. /* Macros for detecting endchar */
  53. #if LONG_MAX == 2147483647L
  54. #define DETECTNULL(X) (((X) - 0x01010101) & ~(X) & 0x80808080)
  55. #else
  56. #if LONG_MAX == 9223372036854775807L
  57. /* Nonzero if X (a long int) contains a NULL byte. */
  58. #define DETECTNULL(X) (((X) - 0x0101010101010101) & ~(X) & 0x8080808080808080)
  59. #else
  60. #error long int is not a 32bit or 64bit type.
  61. #endif
  62. #endif
  63.  
  64.  
  65. _PTR
  66. _DEFUN (memccpy, (dst0, src0, endchar, len0),
  67.         _PTR __restrict dst0 _AND
  68.         _CONST _PTR __restrict src0 _AND
  69.         int endchar0 _AND
  70.         size_t len0)
  71. {
  72.  
  73. #if defined(PREFER_SIZE_OVER_SPEED) || defined(__OPTIMIZE_SIZE__)
  74.   _PTR ptr = NULL;
  75.   char *dst = (char *) dst0;
  76.   char *src = (char *) src0;
  77.   char endchar = endchar0 & 0xff;
  78.  
  79.   while (len0--)
  80.     {
  81.       if ((*dst++ = *src++) == endchar)
  82.         {
  83.           ptr = dst;
  84.           break;
  85.         }
  86.     }
  87.  
  88.   return ptr;
  89. #else
  90.   _PTR ptr = NULL;
  91.   char *dst = dst0;
  92.   _CONST char *src = src0;
  93.   long *aligned_dst;
  94.   _CONST long *aligned_src;
  95.   char endchar = endchar0 & 0xff;
  96.  
  97.   /* If the size is small, or either SRC or DST is unaligned,
  98.      then punt into the byte copy loop.  This should be rare.  */
  99.   if (!TOO_SMALL(len0) && !UNALIGNED (src, dst))
  100.     {
  101.       unsigned int i;
  102.       unsigned long mask = 0;
  103.  
  104.       aligned_dst = (long*)dst;
  105.       aligned_src = (long*)src;
  106.  
  107.       /* The fast code reads the ASCII one word at a time and only
  108.          performs the bytewise search on word-sized segments if they
  109.          contain the search character, which is detected by XORing
  110.          the word-sized segment with a word-sized block of the search
  111.          character and then detecting for the presence of NULL in the
  112.          result.  */
  113.       for (i = 0; i < LITTLEBLOCKSIZE; i++)
  114.         mask = (mask << 8) + endchar;
  115.  
  116.  
  117.       /* Copy one long word at a time if possible.  */
  118.       while (len0 >= LITTLEBLOCKSIZE)
  119.         {
  120.           unsigned long buffer = (unsigned long)(*aligned_src);
  121.           buffer ^=  mask;
  122.           if (DETECTNULL (buffer))
  123.             break; /* endchar is found, go byte by byte from here */
  124.           *aligned_dst++ = *aligned_src++;
  125.           len0 -= LITTLEBLOCKSIZE;
  126.         }
  127.  
  128.        /* Pick up any residual with a byte copier.  */
  129.       dst = (char*)aligned_dst;
  130.       src = (char*)aligned_src;
  131.     }
  132.  
  133.   while (len0--)
  134.     {
  135.       if ((*dst++ = *src++) == endchar)
  136.         {
  137.           ptr = dst;
  138.           break;
  139.         }
  140.     }
  141.  
  142.   return ptr;
  143. #endif /* not PREFER_SIZE_OVER_SPEED */
  144. }
  145.