More memory stuff

This commit is contained in:
Julian Barathieu 2019-01-02 17:19:13 +01:00
parent 6b9ea44f09
commit 8998b2d62d
6 changed files with 59 additions and 34 deletions

View File

@ -1,11 +1,11 @@
//----------------------------------------------------------------------------# //----------------------------------------------------------------------------//
// GNU GPL OS/K # // GNU GPL OS/K //
// # // //
// Authors: spectral` # // Authors: spectral` //
// NeoX # // NeoX //
// # // //
// Desc: Project Makefile # // Desc: Project Makefile //
//----------------------------------------------------------------------------# //----------------------------------------------------------------------------//
// The madman's Makefile // The madman's Makefile
#include "preproc.h" #include "preproc.h"

View File

@ -4,7 +4,7 @@
// Authors: spectral` // // Authors: spectral` //
// NeoX // // NeoX //
// // // //
// Desc: Conversion utilities // // Desc: Conversion utilities - itoa family //
//----------------------------------------------------------------------------// //----------------------------------------------------------------------------//
#include <kaleid.h> #include <kaleid.h>

View File

@ -17,7 +17,7 @@
// //
// Set "bytes"-many bytes starting from ptr to val // Set "bytes"-many bytes starting from ptr to val
// //
void *memsetb(void *ptr, int val, size_t bytes) void *memset(void *ptr, int val, size_t bytes)
{ {
uchar *uptr = (uchar *)ptr; uchar *uptr = (uchar *)ptr;
@ -45,9 +45,7 @@ void *memsetb(void *ptr, int val, size_t bytes)
} }
// deal with what's left // deal with what's left
while (bytes--) { while (bytes--) *uptr++ = (uchar)val;
*uptr++ = (uchar)val;
}
return ptr; return ptr;
} }
@ -61,12 +59,7 @@ void *memsetw(void *ptr, int val, size_t words)
// can't we do this an aligned way? // can't we do this an aligned way?
if unlikely (((ulong)uptr % WORD_ALIGN) > 0) { if unlikely (((ulong)uptr % WORD_ALIGN) > 0) {
// no, we can't align ourselves while (words--) *uptr++ = (ushort)val;
while (words--) {
// do it the hard way
*uptr++ = (ushort)val;
}
// too bad '-'
return uptr; return uptr;
} }
@ -92,9 +85,7 @@ void *memsetw(void *ptr, int val, size_t words)
} }
// deal with what's left // deal with what's left
while (words--) { while (words--) *uptr++ = (ushort)val;
*uptr++ = (ushort)val;
}
return ptr; return ptr;
} }
@ -135,7 +126,6 @@ void *memzero(void *ptr, size_t bytes)
return memsetb(ptr, 0, bytes); return memsetb(ptr, 0, bytes);
} }
// //
// Copy "bytes"-many bytes of src to dst // Copy "bytes"-many bytes of src to dst
// Does not deal with overlapping blocks (memmove's job) // Does not deal with overlapping blocks (memmove's job)
@ -185,10 +175,45 @@ void *memcpy(void *dst, const void *src, size_t bytes)
ushort *ubdst = (ushort *)udst; ushort *ubdst = (ushort *)udst;
// deal with what's left // deal with what's left
while (bytes--) { while (bytes--) *ubdst ++ = *ubsrc++;
*ubdst ++ = *ubsrc++;
}
return dst; return dst;
} }
//
// Move memory from src to dest
// To be made more efficient
//
void *memmove(void *dst, const void *src, size_t bytes)
{
const uchar *usrc = src;
uchar *udst = dst;
if (udst < usrc) {
while (bytes--) *udst++ = *usrc++;
return dst;
}
uchar *usrc_end = (uchar *)usrc + bytes - 1;
uchar *udst_end = udst + bytes - 1;
while (bytes--) *udst_end-- = *usrc_end--;
return dst;
}
//
// Compare memory areas
//
int memcmp(const void *ptr1, const void *ptr2, size_t bytes)
{
const uchar *uptr1 = ptr1;
const uchar *uptr2 = ptr2;
while (bytes--) {
if (*uptr1++ != *uptr2++) {
return uptr1[-1] < uptr2[-1] ? -1 : 1;
}
}
return 0;
}

View File

@ -71,20 +71,20 @@ typedef struct { long quot, rem; } ldiv_t;
// Memory management utilities // // Memory management utilities //
//------------------------------------------// //------------------------------------------//
#ifndef memset #ifndef memsetb
#define memset memsetb #define memsetb memset
#endif #endif
#ifndef memchr #ifndef memchrb
#define memchr memchrb #define memchrb memchr
#endif #endif
void *memsetb(void *, int, size_t); void *memset(void *, int, size_t);
void *memsetw(void *, int, size_t); void *memsetw(void *, int, size_t);
void *memsetd(void *, int, size_t); void *memsetd(void *, int, size_t);
void *memsetq(void *, long, size_t); void *memsetq(void *, long, size_t);
void *memchrb(const void *, int, size_t); void *memchr(const void *, int, size_t);
void *memchrw(const void *, int, size_t); void *memchrw(const void *, int, size_t);
void *memchrd(const void *, int, size_t); void *memchrd(const void *, int, size_t);
void *memchrq(const void *, long, size_t); void *memchrq(const void *, long, size_t);

View File

@ -20,7 +20,7 @@
#define memsetd _osk_memsetd #define memsetd _osk_memsetd
#define memsetq _osk_memsetq #define memsetq _osk_memsetq
#define memchrb _osk_memchrb #define memchr _osk_memchrb
#define memchrw _osk_memchrw #define memchrw _osk_memchrw
#define memchrd _osk_memchrd #define memchrd _osk_memchrd
#define memchrq _osk_memchrq #define memchrq _osk_memchrq

View File

@ -16,7 +16,7 @@
# define LINK_KERNEL(out) $(KCC) $(CLDSCR) $(COMMOBJS) $(KERNOBJS) -o $(BINDIR)/out # define LINK_KERNEL(out) $(KCC) $(CLDSCR) $(COMMOBJS) $(KERNOBJS) -o $(BINDIR)/out
#endif #endif
#define COMPILE_CONVRT(file) $(CCC) _CSPREF $(COMMDIR)/convert.c -o $(COBJDIR)/file._OUTFIX #define COMPILE_CONVRT(file) $(CCC) _CSPREF $(COMMDIR)/itoa.c -o $(COBJDIR)/file._OUTFIX
#define COMPILE_COMMON(file) $(CCC) _CSPREF $(COMMDIR)/file.c -o $(COBJDIR)/file._OUTFIX #define COMPILE_COMMON(file) $(CCC) _CSPREF $(COMMDIR)/file.c -o $(COBJDIR)/file._OUTFIX
#define COMPILE_KERNEL(file) $(KCC) _CSPREF $(KERNDIR)/file.c -o $(KOBJDIR)/file._OUTFIX #define COMPILE_KERNEL(file) $(KCC) _CSPREF $(KERNDIR)/file.c -o $(KOBJDIR)/file._OUTFIX