boot2

Playing with the boostrap
git clone https://git.ryansepassi.com/git/boot2.git
Log | Files | Refs | README

mem.c (1496B)


      1 /* Tiny runtime providing the mem* helpers tcc emits calls to for
      2    struct copies and bulk zero-init past its inline thresholds.
      3    tcc's own libtcc1 (lib-arm64.o) does not define these — upstream
      4    assumes they come from libc, but the tcc-cc suite links with
      5    -nostdlib so we ship them here. memcmp lives here too as a plain
      6    compiler-builtin for fixtures that reach it via `extern int
      7    memcmp(...)`. */
      8 
      9 typedef unsigned long size_t;
     10 
     11 void *memcpy(void *dst, const void *src, size_t n) {
     12     unsigned char *d = (unsigned char *)dst;
     13     const unsigned char *s = (const unsigned char *)src;
     14     while (n--) *d++ = *s++;
     15     return dst;
     16 }
     17 
     18 void *memmove(void *dst, const void *src, size_t n) {
     19     unsigned char *d = (unsigned char *)dst;
     20     const unsigned char *s = (const unsigned char *)src;
     21     if (d == s || n == 0) return dst;
     22     if (d < s) {
     23         while (n--) *d++ = *s++;
     24     } else {
     25         d += n;
     26         s += n;
     27         while (n--) *--d = *--s;
     28     }
     29     return dst;
     30 }
     31 
     32 void *memset(void *dst, int c, size_t n) {
     33     unsigned char *d = (unsigned char *)dst;
     34     unsigned char b = (unsigned char)c;
     35     while (n--) *d++ = b;
     36     return dst;
     37 }
     38 
     39 int memcmp(const void *a, const void *b, size_t n) {
     40     const unsigned char *p = (const unsigned char *)a;
     41     const unsigned char *q = (const unsigned char *)b;
     42     while (n--) {
     43         unsigned char x = *p++;
     44         unsigned char y = *q++;
     45         if (x < y) return -1;
     46         if (x > y) return 1;
     47     }
     48     return 0;
     49 }