Backported some Util::ResizeablePointer improvements from 3.0
This commit is contained in:
parent
679ff219db
commit
260dce0953
2 changed files with 34 additions and 0 deletions
28
lib/util.cpp
28
lib/util.cpp
|
@ -184,13 +184,32 @@ namespace Util{
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool ResizeablePointer::assign(const std::string & str){
|
||||||
|
return assign(str.data(), str.length());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
bool ResizeablePointer::append(const void * p, uint32_t l){
|
bool ResizeablePointer::append(const void * p, uint32_t l){
|
||||||
|
//We're writing to ourselves or from null pointer - assume outside write (e.g. fread or socket operation) and update the size
|
||||||
|
if (!p || p == ((char*)ptr)+currSize){
|
||||||
|
if (currSize+l > maxSize){
|
||||||
|
FAIL_MSG("Pointer write went beyond allocated size! Memory corruption likely.");
|
||||||
|
BACKTRACE;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
currSize += l;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
if (!allocate(l+currSize)){return false;}
|
if (!allocate(l+currSize)){return false;}
|
||||||
memcpy(((char*)ptr)+currSize, p, l);
|
memcpy(((char*)ptr)+currSize, p, l);
|
||||||
currSize += l;
|
currSize += l;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool ResizeablePointer::append(const std::string & str){
|
||||||
|
return append(str.data(), str.length());
|
||||||
|
}
|
||||||
|
|
||||||
bool ResizeablePointer::allocate(uint32_t l){
|
bool ResizeablePointer::allocate(uint32_t l){
|
||||||
if (l > maxSize){
|
if (l > maxSize){
|
||||||
void *tmp = realloc(ptr, l);
|
void *tmp = realloc(ptr, l);
|
||||||
|
@ -204,6 +223,15 @@ namespace Util{
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns amount of space currently reserved for this pointer
|
||||||
|
uint32_t ResizeablePointer::rsize(){
|
||||||
|
return maxSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void ResizeablePointer::truncate(const size_t newLen){
|
||||||
|
if (currSize > newLen){currSize = newLen;}
|
||||||
|
}
|
||||||
|
|
||||||
/// Redirects stderr to log parser, writes log parser to the old stderr.
|
/// Redirects stderr to log parser, writes log parser to the old stderr.
|
||||||
/// Does nothing if the MIST_CONTROL environment variable is set.
|
/// Does nothing if the MIST_CONTROL environment variable is set.
|
||||||
|
|
|
@ -37,10 +37,16 @@ namespace Util{
|
||||||
ResizeablePointer();
|
ResizeablePointer();
|
||||||
~ResizeablePointer();
|
~ResizeablePointer();
|
||||||
inline size_t& size(){return currSize;}
|
inline size_t& size(){return currSize;}
|
||||||
|
inline const size_t size() const{return currSize;}
|
||||||
bool assign(const void * p, uint32_t l);
|
bool assign(const void * p, uint32_t l);
|
||||||
|
bool assign(const std::string & str);
|
||||||
bool append(const void * p, uint32_t l);
|
bool append(const void * p, uint32_t l);
|
||||||
|
bool append(const std::string & str);
|
||||||
bool allocate(uint32_t l);
|
bool allocate(uint32_t l);
|
||||||
|
uint32_t rsize();
|
||||||
|
void truncate(const size_t newLen);
|
||||||
inline operator char*(){return (char*)ptr;}
|
inline operator char*(){return (char*)ptr;}
|
||||||
|
inline operator const char *() const{return (const char *)ptr;}
|
||||||
inline operator void*(){return ptr;}
|
inline operator void*(){return ptr;}
|
||||||
private:
|
private:
|
||||||
void * ptr;
|
void * ptr;
|
||||||
|
|
Loading…
Add table
Reference in a new issue