Add memmove and bzero
authorweiyu <weiyuluo1232@gmail.com>
Wed, 16 Sep 2020 04:02:00 +0000 (21:02 -0700)
committerweiyu <weiyuluo1232@gmail.com>
Wed, 16 Sep 2020 04:02:00 +0000 (21:02 -0700)
mymemory.cc

index cda63dc..20c2962 100644 (file)
@@ -178,6 +178,84 @@ void * memcpy(void * dst, const void * src, size_t n) {
        return dst;
 }
 
+void * memmove(void * dst, const void * src, size_t n) {
+       if (model && !inside_model) {
+               thread_id_t tid = thread_current_id();
+               if (((uintptr_t)src&7) == 0 && ((uintptr_t)dst&7) == 0 && (n&7) == 0) {
+                       if (((uintptr_t)dst) < ((uintptr_t)src))
+                               for (uint i = 0; i < (n>>3); i++) {
+                                       raceCheckRead64(tid, (void *)(((uint64_t *)src) + i));
+                                       ((volatile uint64_t *)dst)[i] = ((uint64_t *)src)[i];
+                                       raceCheckWrite64(tid, (void *)(((uint64_t *)dst) + i));
+                               }
+                       else
+                               for (uint i = (n>>3); i != 0;) {
+                                       i--;
+                                       raceCheckRead64(tid, (void *)(((uint64_t *)src) + i));
+                                       ((volatile uint64_t *)dst)[i] = ((uint64_t *)src)[i];
+                                       raceCheckWrite64(tid, (void *)(((uint64_t *)dst) + i));
+                               }
+               } else if (((uintptr_t)src&3) == 0 && ((uintptr_t)dst&3) == 0 && (n&3) == 0) {
+                       if (((uintptr_t)dst) < ((uintptr_t)src))
+                               for (uint i = 0; i < (n>>2); i++) {
+                                       raceCheckRead32(tid, (void *)(((uint32_t *)src) + i));
+                                       ((volatile uint32_t *)dst)[i] = ((uint32_t *)src)[i];
+                                       raceCheckWrite32(tid, (void *)(((uint32_t *)dst) + i));
+                               }
+                       else
+                               for (uint i = (n>>2); i != 0;) {
+                                       i--;
+                                       raceCheckRead32(tid, (void *)(((uint32_t *)src) + i));
+                                       ((volatile uint32_t *)dst)[i] = ((uint32_t *)src)[i];
+                                       raceCheckWrite32(tid, (void *)(((uint32_t *)dst) + i));
+                               }
+               } else if (((uintptr_t)src&1) == 0 && ((uintptr_t)dst&1) == 0 && (n&1) == 0) {
+                       if (((uintptr_t)dst) < ((uintptr_t)src))
+                               for (uint i = 0; i < (n>>1); i++) {
+                                       raceCheckRead16(tid, (void *)(((uint16_t *)src) + i));
+                                       ((volatile uint16_t *)dst)[i] = ((uint16_t *)src)[i];
+                                       raceCheckWrite16(tid, (void *)(((uint16_t *)dst) + i));
+                               }
+                       else
+                               for (uint i = (n>>1); i != 0;) {
+                                       i--;
+                                       raceCheckRead16(tid, (void *)(((uint16_t *)src) + i));
+                                       ((volatile uint16_t *)dst)[i] = ((uint16_t *)src)[i];
+                                       raceCheckWrite16(tid, (void *)(((uint16_t *)dst) + i));
+                               }
+               } else {
+                       if (((uintptr_t)dst) < ((uintptr_t)src))
+                               for(uint i = 0; i < n; i++) {
+                                       raceCheckRead8(tid, (void *)(((char *)src) + i));
+                                       ((volatile char *)dst)[i] = ((char *)src)[i];
+                                       raceCheckWrite8(tid, (void *)(((char *)dst) + i));
+                               }
+                       else
+                               for(uint i = n; i != 0;) {
+                                       i--;
+                                       raceCheckRead8(tid, (void *)(((char *)src) + i));
+                                       ((volatile char *)dst)[i] = ((char *)src)[i];
+                                       raceCheckWrite8(tid, (void *)(((char *)dst) + i));
+                               }
+               }
+       } else {
+               if (((uintptr_t)real_memmove) < 2) {
+                       if (((uintptr_t)dst) < ((uintptr_t)src))
+                               for(uint i=0;i<n;i++) {
+                                       ((volatile char *)dst)[i] = ((char *)src)[i];
+                               }
+                       else
+                               for(uint i=n;i!=0; ) {
+                                       i--;
+                                       ((volatile char *)dst)[i] = ((char *)src)[i];
+                               }
+                       return dst;
+               }
+        return real_memmove(dst, src, n);
+       }
+       return dst;
+}
+
 void * memset(void *dst, int c, size_t n) {
        if (model && !inside_model) {
                thread_id_t tid = thread_current_id();
@@ -221,3 +299,38 @@ void * memset(void *dst, int c, size_t n) {
        }
        return dst;
 }
+
+void bzero(void *dst, size_t n) {
+       if (model && !inside_model) {
+               thread_id_t tid = thread_current_id();
+               if (((uintptr_t)dst&7) == 0 && (n&7) == 0) {
+                       for (uint i = 0; i < (n>>3); i++) {
+                               ((volatile uint64_t *)dst)[i] = 0;
+                               raceCheckWrite64(tid, (void *)(((uint64_t *)dst) + i));
+                       }
+               } else if (((uintptr_t)dst&3) == 0 && (n&3) == 0) {
+                       for (uint i = 0; i < (n>>2); i++) {
+                               ((volatile uint32_t *)dst)[i] = 0;
+                               raceCheckWrite32(tid, (void *)(((uint32_t *)dst) + i));
+                       }
+               } else if (((uintptr_t)dst&1) == 0 && (n&1) == 0) {
+                       for (uint i = 0; i < (n>>1); i++) {
+                               ((volatile uint16_t *)dst)[i] = 0;
+                               raceCheckWrite16(tid, (void *)(((uint16_t *)dst) + i));
+                       }
+               } else {
+                       for (uint i=0;i<n;i++) {
+                               ((volatile char *)dst)[i] = 0;
+                               raceCheckWrite8(tid, (void *)(((char *)dst) + i));
+                       }
+               }
+       } else {
+               if (((uintptr_t)real_bzero) < 2) {
+                       for(size_t s=0;s<n;s++) {
+                               ((volatile char *)dst)[s] = 0;
+                       }
+                       return;
+               }
+               real_bzero(dst, n);
+       }
+}