Skip to content

Commit

Permalink
-Data - added a new duplicate method that will copy a data object but…
Browse files Browse the repository at this point in the history
… continue to use it's memory if source

 data is Shared or Borrowed.  Allows copying a data object with shared data without making a copy of the actual data.
  • Loading branch information
sgodin committed Apr 7, 2015
1 parent 09f4b6a commit 7c4c764
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
26 changes: 26 additions & 0 deletions rutil/Data.cxx
Expand Up @@ -663,6 +663,32 @@ Data::takeBuf(Data& other)
return *this;
}

Data&
Data::duplicate(const Data& other)
{
if (&other == this)
return *this;

if (mShareEnum == Data::Take)
delete[] mBuf;

if (other.mBuf == other.mPreBuffer)
{
// plus one picks up the terminating safety NULL
memcpy(mPreBuffer, other.mPreBuffer, other.mSize + 1);
mBuf = mPreBuffer;
}
else
{
mBuf = other.mBuf;
}
mSize = other.mSize;
mCapacity = other.mCapacity;
mShareEnum = other.mShareEnum;

return *this;
}

Data&
Data::copy(const char *buf, size_type length)
{
Expand Down
10 changes: 9 additions & 1 deletion rutil/Data.hxx
Expand Up @@ -347,7 +347,15 @@ class Data

/**
Functional equivalent of: *this = Data(buf, length)
but avoid the intermediate allocation and free. Also,
and Data& copy(const char *buf, size_type length)
but avoids an actual copy of the data if {other} is Shared
or Borrowed. Will have the same storage mode as {other}.
**/
Data& duplicate(const Data& other);

/**
Functional equivalent of: *this = Data(buf, length)
but avoids the intermediate allocation and free. Also,
will never decrease capacity. Safe to call even if {buf}
is part of {this}.
Expand Down

0 comments on commit 7c4c764

Please sign in to comment.