Advertisement
NaZaRa

memset

Dec 28th, 2018
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.27 KB | None | 0 0
  1. static inline void *memsetq(void *ptr, ullong uval, size_t qwords)
  2. {
  3.     size_t n;
  4.     ullong *uptr = (ullong *)ptr;
  5.  
  6.     for (n = 0; n < qwords; n++) {
  7.         *(uptr + n) = uval;
  8.     }
  9.  
  10.     return ptr;
  11. }
  12.  
  13. void *memset(void *ptr, int val, size_t bytes)
  14. {
  15.     uchar *uptr = (uchar *)ptr;
  16.     const size_t qwords = bytes/QWORD_SIZE;
  17.    
  18.     // get rid of everything after the first byte
  19.     val = val & 0xFF;
  20.    
  21.     // deal with bytes before start of the first qword
  22.     while (((ullong)uptr % QWORD_ALIGN) > 0 && bytes--) {
  23.         *uptr++ = (uchar)val;
  24.     }
  25.  
  26.     // move qword by qword
  27.     if (qwords) {
  28.         const ullong uval = ((ullong)val << 56) | ((ullong)val << 48)
  29.                           | ((ullong)val << 40) | ((ullong)val << 32)
  30.                           | ((ullong)val << 24) | ((ullong)val << 16)
  31.                           | ((ullong)val <<  8) | ((ullong)val);
  32.  
  33.         memsetq(uptr, uval, qwords);
  34.        
  35.         uptr += qwords * QWORD_SIZE;
  36.         bytes %= QWORD_SIZE;
  37.     }
  38.    
  39.     // deal with what's left
  40.     while (bytes--) {
  41.         *uptr++ = (uchar)val;
  42.     }
  43.    
  44.     return ptr;
  45. }
  46.  
  47. //
  48. //  Set "bytes"-many bytes starting from ptr to 0
  49. //
  50. void *memzero(void *ptr, size_t bytes)
  51. {
  52.     return memset(ptr, 0, bytes);
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement