Skip to content

Commit

Permalink
Remove AttachDeprecated mode for StringData
Browse files Browse the repository at this point in the history
In many places we were doing malloc(size) followed
by String(ptr, len, AttachDeprecated). Update this
to use the StringData(int reserve) API.
  • Loading branch information
aravind authored and sgolemon committed Oct 5, 2012
1 parent 62236bf commit ca6070b
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 132 deletions.
2 changes: 1 addition & 1 deletion src/runtime/base/server/http_protocol.cpp
Expand Up @@ -179,7 +179,7 @@ void HttpProtocol::PrepareSystemVariables(Transport *transport,
if (needDelete) {
if (RuntimeOption::AlwaysPopulateRawPostData &&
uint32_t(size) <= StringData::MaxSize) {
g->GV(HTTP_RAW_POST_DATA) = String((char*)data, size, AttachDeprecated);
g->GV(HTTP_RAW_POST_DATA) = String((char*)data, size, AttachString);
} else {
free((void *)data);
}
Expand Down
20 changes: 0 additions & 20 deletions src/runtime/base/string_data.cpp
Expand Up @@ -87,26 +87,6 @@ void StringData::initLiteral(const char* data, int len) {
TAINT_OBSERVER_REGISTER_MUTATED(m_taint_data, rawdata());
}

void StringData::initAttachDeprecated(const char* data) {
return initAttachDeprecated(data, strlen(data));
}

void StringData::initAttachDeprecated(const char* data, int len) {
if (uint32_t(len) > MaxSize) {
throw InvalidArgumentException("len > 2^31-2", len);
}
// Don't copy small strings here either because the caller sometimes
// assumes he can mess with data while this string is still alive,
// and we want to free it eagerly. Sketchy!
m_hash = 0;
_count = 0;
m_len = len;
m_cdata = data;
m_big.cap = len | IsMalloc;
ASSERT(checkSane());
TAINT_OBSERVER_REGISTER_MUTATED(m_taint_data, rawdata());
}

HOT_FUNC
void StringData::initAttach(const char* data) {
return initAttach(data, strlen(data));
Expand Down
19 changes: 3 additions & 16 deletions src/runtime/base/string_data.h
Expand Up @@ -57,14 +57,6 @@ typedef Slice<char> MutableSlice;
// deprecated to enable copying of small strings.
enum AttachLiteralMode { AttachLiteral };

// DEPRECATED. const char* points to malloc'd memory that will be freed
// sometime after the the StringData is constructed (in case of mutation),
// but no later than when ~StringData is called. Really, the new StringData
// owns data from the point of construction -- see AttachStringMode.
// This is deprecated because callers should assume the buffer can be
// freed immediately when a String[Data] is constructed.
enum AttachDeprecatedMode { AttachDeprecated };

// Aggressively copy small strings and free the passed-in buffer immediately;
// otherwise keep the buffer for long strings, and free it when the string
// is mutated or released.
Expand All @@ -75,6 +67,9 @@ enum AttachStringMode { AttachString };
// itself was smart-allocated.
enum CopyStringMode { CopyString };

// reserve space for buffer that will be filled in by client.
enum ReserveStringMode { ReserveString };

// const char* points to client-owned memory, StringData will copy it
// at construct-time using malloc. This works for any String but is
// meant for StringData instances which are not smart-allocated (e.g.
Expand Down Expand Up @@ -163,9 +158,6 @@ class StringData {
StringData(const char *data, AttachLiteralMode) {
initLiteral(data);
}
StringData(const char *data, AttachDeprecatedMode) {
initAttachDeprecated(data);
}
StringData(const char *data, AttachStringMode) {
initAttach(data);
}
Expand All @@ -176,9 +168,6 @@ class StringData {
StringData(const char *data, int len, AttachLiteralMode) {
initLiteral(data, len);
}
StringData(const char* data, int len, AttachDeprecatedMode) {
initAttachDeprecated(data, len);
}
StringData(const char* data, int len, AttachStringMode) {
initAttach(data, len);
}
Expand Down Expand Up @@ -398,11 +387,9 @@ class StringData {
* Helpers.
*/
void initLiteral(const char* data);
void initAttachDeprecated(const char* data);
void initAttach(const char* data);
void initCopy(const char* data);
void initLiteral(const char* data, int len);
void initAttachDeprecated(const char* data, int len);
void initAttach(const char* data, int len);
void initCopy(const char* data, int len);
void initMalloc(const char* data, int len);
Expand Down
27 changes: 13 additions & 14 deletions src/runtime/base/type_string.h
Expand Up @@ -142,13 +142,6 @@ class String : protected StringBase {
m_px->setRefCount(1);
}
}
// attach to null terminated malloc'ed string
String(const char *s, AttachDeprecatedMode mode) {
if (s) {
m_px = NEW(StringData)(s, mode);
m_px->setRefCount(1);
}
}
// attach to null terminated malloc'ed string, maybe free it now.
String(const char *s, AttachStringMode mode) {
if (s) {
Expand All @@ -171,13 +164,6 @@ class String : protected StringBase {
}
}
// attach to binary malloc'ed string
String(const char *s, int length, AttachDeprecatedMode mode) {
if (s) {
m_px = NEW(StringData)(s, length, mode);
m_px->setRefCount(1);
}
}
// attach to binary malloc'ed string
String(const char *s, int length, AttachStringMode mode) {
if (s) {
m_px = NEW(StringData)(s, length, mode);
Expand All @@ -191,6 +177,11 @@ class String : protected StringBase {
m_px->setRefCount(1);
}
}
// make an empty string with cap reserve bytes, plus 1 for '\0'
String(int cap, ReserveStringMode mode) {
m_px = NEW(StringData)(cap);
m_px->setRefCount(1);
}

void clear() { reset();}
/**
Expand All @@ -211,6 +202,11 @@ class String : protected StringBase {
return m_px ? m_px->dataIgnoreTaint() : "";
}
public:
CStrRef setSize(int len) {
ASSERT(m_px);
m_px->setSize(len);
return *this;
}
const char *c_str() const {
return m_px ? m_px->data() : "";
}
Expand All @@ -226,6 +222,9 @@ class String : protected StringBase {
StringSlice slice() const {
return m_px ? m_px->slice() : StringSlice("", 0);
}
MutableSlice mutableSlice() {
return m_px ? m_px->mutableSlice() : MutableSlice("", 0);
}
bool isNull() const {
return m_px == NULL;
}
Expand Down

0 comments on commit ca6070b

Please sign in to comment.