Userspace dlmalloc port, supporting syscalls mman_map()/mman_unmap()
This commit is contained in:
@ -9,3 +9,105 @@ size_t string_len(const char *s) {
|
||||
}
|
||||
return l;
|
||||
}
|
||||
|
||||
void *string_memset(void *p, int c, size_t n) {
|
||||
char *cp = p;
|
||||
for (size_t i = 0; i < n; i++) cp[i] = c;
|
||||
return p;
|
||||
}
|
||||
|
||||
void *string_memcpy(void *dst, const void *src, size_t n) {
|
||||
char *a = dst;
|
||||
const char *b = src;
|
||||
for (size_t i = 0; i < n; i++) a[i] = b[i];
|
||||
return dst;
|
||||
}
|
||||
|
||||
// https://aticleworld.com/memcmp-in-c/
|
||||
int string_memcmp(const void *s1, const void *s2, int len)
|
||||
{
|
||||
unsigned char *p = s1;
|
||||
unsigned char *q = s2;
|
||||
int charCompareStatus = 0;
|
||||
//If both pointer pointing same memory block
|
||||
if (s1 == s2)
|
||||
{
|
||||
return charCompareStatus;
|
||||
}
|
||||
while (len > 0)
|
||||
{
|
||||
if (*p != *q)
|
||||
{ //compare the mismatching character
|
||||
charCompareStatus = (*p >*q)?1:-1;
|
||||
break;
|
||||
}
|
||||
len--;
|
||||
p++;
|
||||
q++;
|
||||
}
|
||||
return charCompareStatus;
|
||||
}
|
||||
|
||||
int string_strcmp(const char *a, const char *b) {
|
||||
while (*a && (*a == *b)) {
|
||||
a++, b++;
|
||||
}
|
||||
return (unsigned char)*a - (unsigned char)*b;
|
||||
}
|
||||
|
||||
size_t string_strcspn(const char *s, const char *reject) {
|
||||
size_t count = 0;
|
||||
for (; *s != '\0'; ++s) {
|
||||
const char *r = reject;
|
||||
while (*r != '\0') {
|
||||
if (*s == *r) {
|
||||
return count;
|
||||
}
|
||||
r++;
|
||||
}
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
size_t string_strspn(const char *s, const char *accept) {
|
||||
size_t count = 0;
|
||||
for (; *s != '\0'; ++s) {
|
||||
const char *a = accept;
|
||||
int matched = 0;
|
||||
while (*a != '\0') {
|
||||
if (*s == *a) {
|
||||
matched = 1;
|
||||
break;
|
||||
}
|
||||
a++;
|
||||
}
|
||||
if (!matched) {
|
||||
return count;
|
||||
}
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
char *string_strcpy(char *dest, const char *src) {
|
||||
char *d = dest;
|
||||
while ((*d++ = *src++) != '\0') {
|
||||
;
|
||||
}
|
||||
return dest;
|
||||
}
|
||||
|
||||
char *string_strchr(const char *s, int c) {
|
||||
char ch = (char)c;
|
||||
while (*s != '\0') {
|
||||
if (*s == ch) {
|
||||
return (char *)s;
|
||||
}
|
||||
s++;
|
||||
}
|
||||
if (ch == '\0') {
|
||||
return (char *)s;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
@ -1,8 +1,24 @@
|
||||
#ifndef ULIB_STRING_STRING_H_
|
||||
#define ULIB_STRING_STRING_H_
|
||||
|
||||
#if !defined(__ASSEMBLER__)
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#define memset string_memset
|
||||
#define memcpy string_memcpy
|
||||
|
||||
size_t string_len(const char *s);
|
||||
void *string_memset(void *p, int c, size_t n);
|
||||
void *string_memcpy(void *dst, const void *src, size_t n);
|
||||
int string_memcmp(const void *s1, const void *s2, int len);
|
||||
int string_strcmp(const char *a, const char *b);
|
||||
size_t string_strcspn(const char *s, const char *reject);
|
||||
size_t string_strspn(const char *s, const char *accept);
|
||||
char *string_strcpy(char *dest, const char *src);
|
||||
char *string_strchr(const char *s, int c);
|
||||
|
||||
#endif
|
||||
|
||||
#endif // ULIB_STRING_STRING_H_
|
||||
|
Reference in New Issue
Block a user