Add Memory::Copy overlap check and Memory::Move

This commit is contained in:
Ted John committed 2016-07-08 22:58:38 +01:00
1 parent 343623c815
commit d1dc6b528d
2 files changed
+17 -5

No files matched your search

+16 -4
View File
@@ -16,10 +16,8 @@
#pragma once
extern "C"
{
#include "../common.h"
}
#include "../common.h"
#include "Guard.hpp"
/**
* Utility methods for memory management. Typically helpers and wrappers around the C standard library.
@@ -80,9 +78,23 @@ namespace Memory
T * Copy(T * dst, const T * src, size_t size)
{
if (size == 0) return (T*)dst;
#ifdef DEBUG
uintptr_t srcBegin = (uintptr_t)src;
uintptr_t srcEnd = srcBegin + size;
uintptr_t dstBegin = (uintptr_t)dst;
uintptr_t dstEnd = dstBegin + size;
Guard::Assert(srcEnd <= dstBegin || srcBegin >= dstEnd, "Source overlaps destination, try using Memory::Move.");
#endif
return (T*)memcpy((void*)dst, (const void*)src, size);
}
template<typename T>
T * Move(T * dst, const T * src, size_t size)
{
if (size == 0) return (T*)dst;
return (T*)memmove((void*)dst, (const void*)src, size);
}
template<typename T>
T * Duplicate(const T * src, size_t size)
{
+1 -1
View File
@@ -251,7 +251,7 @@ namespace String
assert(newStringSize < currentStringSize);
#endif
Memory::Copy(str, firstNonWhitespace, newStringSize);
Memory::Move(str, firstNonWhitespace, newStringSize);
str[newStringSize] = '\0';
}
else