os-workshop

same materials and sample source for RV32 OS projects
git clone http://frotz.net/git/os-workshop.git
Log | Files | Refs

memcpy.c (1288B)


      1 /*
      2 ** Copyright 2001, Travis Geiselbrecht. All rights reserved.
      3 ** Distributed under the terms of the NewOS License.
      4 */
      5 /*
      6  * Copyright (c) 2008 Travis Geiselbrecht
      7  *
      8  * Use of this source code is governed by a MIT-style
      9  * license that can be found in the LICENSE file or at
     10  * https://opensource.org/licenses/MIT
     11  */
     12 #include <string.h>
     13 #include <stdint.h>
     14 
     15 
     16 #if !_ASM_MEMCPY
     17 
     18 typedef long word;
     19 
     20 #define lsize sizeof(word)
     21 #define lmask (lsize - 1)
     22 
     23 void *memcpy(void *dest, const void *src, size_t count) {
     24     char *d = (char *)dest;
     25     const char *s = (const char *)src;
     26     int len;
     27 
     28     if (count == 0 || dest == src)
     29         return dest;
     30 
     31     if (((long)d | (long)s) & lmask) {
     32         // src and/or dest do not align on word boundary
     33         if ((((long)d ^ (long)s) & lmask) || (count < lsize))
     34             len = count; // copy the rest of the buffer with the byte mover
     35         else
     36             len = lsize - ((long)d & lmask); // move the ptrs up to a word boundary
     37 
     38         count -= len;
     39         for (; len > 0; len--)
     40             *d++ = *s++;
     41     }
     42     for (len = count / lsize; len > 0; len--) {
     43         *(word *)d = *(word *)s;
     44         d += lsize;
     45         s += lsize;
     46     }
     47     for (len = count & lmask; len > 0; len--)
     48         *d++ = *s++;
     49 
     50     return dest;
     51 }
     52 
     53 #endif