HelenOS sources
This source file includes following definitions.
- memset
- unaligned_memcpy
- memcpy
- memmove
- memcmp
- memchr
#include "../include/mem.h"
#include <stdlib.h>
#include <stddef.h>
#include <stdint.h>
#include "cc.h"
#undef memset
#undef memcpy
#undef memcmp
#undef memmove
#undef memchr
DO_NOT_DISCARD
ATTRIBUTE_OPTIMIZE_NO_TLDP
void *memset(void *dest, int b, size_t n)
{
char *pb;
unsigned long *pw;
size_t word_size;
size_t n_words;
unsigned long pattern;
size_t i;
size_t fill;
word_size = sizeof(unsigned long);
fill = word_size - ((uintptr_t) dest & (word_size - 1));
if (fill > n)
fill = n;
pb = dest;
i = fill;
while (i-- != 0)
*pb++ = b;
n -= fill;
if (n == 0)
return dest;
n_words = n / word_size;
n = n % word_size;
pw = (unsigned long *) pb;
pattern = 0;
i = word_size;
while (i-- != 0)
pattern = (pattern << 8) | (uint8_t) b;
i = n_words;
while (i-- != 0)
*pw++ = pattern;
pb = (char *) pw;
i = n;
while (i-- != 0)
*pb++ = b;
return dest;
}
struct along {
unsigned long n;
} __attribute__((packed));
ATTRIBUTE_OPTIMIZE_NO_TLDP
static void *unaligned_memcpy(void *dst, const void *src, size_t n)
{
size_t i, j;
struct along *adst = dst;
const struct along *asrc = src;
for (i = 0; i < n / sizeof(unsigned long); i++)
adst[i].n = asrc[i].n;
for (j = 0; j < n % sizeof(unsigned long); j++)
((unsigned char *) (((unsigned long *) dst) + i))[j] =
((unsigned char *) (((unsigned long *) src) + i))[j];
return (char *) dst;
}
DO_NOT_DISCARD
ATTRIBUTE_OPTIMIZE_NO_TLDP
void *memcpy(void *dst, const void *src, size_t n)
{
size_t i;
size_t mod, fill;
size_t word_size;
size_t n_words;
const unsigned long *srcw;
unsigned long *dstw;
const uint8_t *srcb;
uint8_t *dstb;
word_size = sizeof(unsigned long);
if (((uintptr_t) dst & (word_size - 1)) !=
((uintptr_t) src & (word_size - 1)))
return unaligned_memcpy(dst, src, n);
mod = (uintptr_t) dst & (word_size - 1);
fill = word_size - mod;
if (fill > n)
fill = n;
srcb = src;
dstb = dst;
i = fill;
while (i-- != 0)
*dstb++ = *srcb++;
n -= fill;
if (n == 0)
return dst;
dstw = (unsigned long *) dstb;
srcw = (const unsigned long *) srcb;
n_words = n / word_size;
n -= n_words * word_size;
i = n_words;
while (i-- != 0)
*dstw++ = *srcw++;
srcb = (const uint8_t *) srcw;
dstb = (uint8_t *) dstw;
i = n;
while (i-- != 0)
*dstb++ = *srcb++;
return dst;
}
DO_NOT_DISCARD
ATTRIBUTE_OPTIMIZE_NO_TLDP
void *memmove(void *dst, const void *src, size_t n)
{
const uint8_t *sp;
uint8_t *dp;
if (src == dst)
return dst;
if (dst >= src + n || src >= dst + n) {
return memcpy(dst, src, n);
}
if (src > dst) {
sp = src;
dp = dst;
while (n-- != 0)
*dp++ = *sp++;
} else {
sp = src + (n - 1);
dp = dst + (n - 1);
while (n-- != 0)
*dp-- = *sp--;
}
return dst;
}
DO_NOT_DISCARD
ATTRIBUTE_OPTIMIZE_NO_TLDP
int memcmp(const void *s1, const void *s2, size_t len)
{
uint8_t *u1 = (uint8_t *) s1;
uint8_t *u2 = (uint8_t *) s2;
size_t i;
for (i = 0; i < len; i++) {
if (*u1 != *u2)
return (int)(*u1) - (int)(*u2);
++u1;
++u2;
}
return 0;
}
DO_NOT_DISCARD
ATTRIBUTE_OPTIMIZE_NO_TLDP
void *memchr(const void *s, int c, size_t n)
{
uint8_t *u = (uint8_t *) s;
unsigned char uc = (unsigned char) c;
size_t i;
for (i = 0; i < n; i++) {
if (u[i] == uc)
return (void *) &u[i];
}
return NULL;
}
HelenOS homepage, sources at GitHub