summaryrefslogtreecommitdiff
path: root/src/mem.c
blob: dd302fbb9c52c435ae2e5f43e54f52e650f9c5c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <mem.h>
#include <stdint.h>

void *memcpy(void *restrict dest, const void *restrict src, size_t n) {
	for (size_t i = 0; i < n; i++)
		((char *) dest)[i] = ((char *) src)[i];
	return dest;
}

void *memmove(void *dest, const void *src, size_t n) {
	for (size_t i = 0; i < n; i++)
		((char *) dest)[i] = ((char *) src)[i];
	return dest;
}

void *memset(void *s, int c, size_t n) {
	for (size_t i = 0; i < n; i++)
		((char *) s)[i] = c;
	return s;
}

int memcmp(const void *s1, const void *s2, size_t n) {
	while (n && *(uint8_t*)s1 == *(uint8_t*)s2)
		n--, s1++, s2++;
	return n ? *(uint8_t*)s1 - *(uint8_t*)s2 : 0;
}