Sorted out proc refcounting with ARCs
[akaros.git] / kern / src / string.c
index 5c2e53b..8cde851 100644 (file)
@@ -5,6 +5,8 @@
 #endif
 
 #include <string.h>
+#include <ros/memlayout.h>
+#include <assert.h>
 
 int
 strlen(const char *s)
@@ -106,6 +108,16 @@ strchr(const char *s, char c)
        return 0;
 }
 
+void *
+memchr(void* mem, int chr, int len)
+{
+       char* s = (char*)mem;
+       for(int i = 0; i < len; i++)
+               if(s[i] == (char)chr)
+                       return s+i;
+       return NULL;
+}
+
 // Return a pointer to the first occurrence of 'c' in 's',
 // or a pointer to the string-ending null character if the string has no 'c'.
 char *
@@ -129,6 +141,19 @@ memset16(uint32_t *COUNT(n/sizeof(uint32_t)) _v, uint32_t c, size_t n)
        v = _v;
        c = c | c<<8 | c<<16 | c<<24;
 
+       if(n >= 64 && ((uintptr_t)v) % 8 == 0)
+       {
+               uint64_t* v64 = (uint64_t*)v;
+               uint64_t c64 = c | ((uint64_t)c)<<32;
+               while(v64 < (uint64_t*)end-7)
+               {
+                       v64[3] = v64[2] = v64[1] = v64[0] = c64;
+                       v64[7] = v64[6] = v64[5] = v64[4] = c64;
+                       v64 += 8;
+               }
+               v = (uint32_t*)v64;
+       }
+
        while(v < end)
        {
                v[3] = v[2] = v[1] = v[0] = c;
@@ -169,6 +194,24 @@ memcpy16(uint32_t *COUNT(n/sizeof(uint32_t)) _dst,
 }
 
 void *
+pagecopy(void* d, void* s)
+{
+       static_assert(PGSIZE % 64 == 0);
+       for(int i = 0; i < PGSIZE; i += 64)
+       {
+               *((uint64_t*)(d+i+0)) = *((uint64_t*)(s+i+0));
+               *((uint64_t*)(d+i+8)) = *((uint64_t*)(s+i+8));
+               *((uint64_t*)(d+i+16)) = *((uint64_t*)(s+i+16));
+               *((uint64_t*)(d+i+24)) = *((uint64_t*)(s+i+24));
+               *((uint64_t*)(d+i+32)) = *((uint64_t*)(s+i+32));
+               *((uint64_t*)(d+i+40)) = *((uint64_t*)(s+i+40));
+               *((uint64_t*)(d+i+48)) = *((uint64_t*)(s+i+48));
+               *((uint64_t*)(d+i+56)) = *((uint64_t*)(s+i+56));
+       }
+       return d;
+}
+
+void *
 memset(void *COUNT(_n) v, int c, size_t _n)
 {
        char *BND(v,v+_n) p;
@@ -179,10 +222,16 @@ memset(void *COUNT(_n) v, int c, size_t _n)
 
        p = v;
 
-       if(n >= 16 && ((uintptr_t)v & 3) == 0)
+    while (n > 0 && ((uintptr_t)p & 7))
+       {
+               *p++ = c;
+               n--;
+       }
+
+       if(n >= 16 && ((uintptr_t)p & 3) == 0)
        {
                n0 = (n/16)*16;
-               memset16((uint32_t*COUNT(n0/sizeof(uint32_t)))v,c,n0);
+               memset16((uint32_t*COUNT(n0/sizeof(uint32_t)))p,c,n0);
                n -= n0;
                p += n0;
        }
@@ -317,3 +366,9 @@ strtol(const char *s, char **endptr, int base)
        return (neg ? -val : val);
 }
 
+int
+atoi(const char* s)
+{
+       // no overflow detection
+       return (int)strtol(s,NULL,10);
+}