Skip to content

Commit

Permalink
[mycpp] MaybeShrink() replaces SetObjLenFromStrLen()
Browse files Browse the repository at this point in the history
It will also be used for SmallStr.
  • Loading branch information
Andy C committed Jan 2, 2023
1 parent 78f7313 commit 47896c8
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 19 deletions.
4 changes: 2 additions & 2 deletions cpp/core.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Tuple2<int, int> Read(int fd, int n, List<Str*>* chunks) {
}

// Now we know how much data we got back
s->SetObjLenFromStrLen(length);
s->MaybeShrink(length);
chunks->append(s);

return Tuple2<int, int>(length, 0);
Expand Down Expand Up @@ -296,7 +296,7 @@ Str* BackslashEscape(Str* s, Str* meta_chars) {
}
*p++ = c;
}
buf->SetObjLenFromStrLen(p - buf->data_);
buf->MaybeShrink(p - buf->data_);
return buf;
}

Expand Down
2 changes: 1 addition & 1 deletion cpp/libc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Str* gethostname() {
throw Alloc<OSError>(errno);
}
// Important: set the length of the string!
result->SetObjLenFromC();
result->MaybeShrink(strlen(result->data_));
return result;
}

Expand Down
2 changes: 1 addition & 1 deletion cpp/qsn.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ inline Str* UEscape(int codepoint) {
// 1 for }
Str* result = OverAllocatedStr(10);
int n = sprintf(result->data(), "\\u{%x}", codepoint);
result->SetObjLenFromStrLen(n); // truncate to what we wrote
result->MaybeShrink(n); // truncate to what we wrote
return result;
}

Expand Down
2 changes: 1 addition & 1 deletion cpp/stdlib.cc
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ Str* strftime(Str* s, time_t ts) {
// No error location info, but leaving it out points reliably to 'printf'
e_die(StrFromC("strftime() result exceeds 1024 bytes"));
}
result->SetObjLenFromStrLen(n);
result->MaybeShrink(n);
return result;
}

Expand Down
2 changes: 1 addition & 1 deletion cpp/stdlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ inline Str* getcwd() {
throw Alloc<OSError>(errno);
}
// Important: set the length of the string!
result->SetObjLenFromC();
result->MaybeShrink(strlen(result->data_));
return result;
}

Expand Down
2 changes: 1 addition & 1 deletion mycpp/gc_builtins.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ void println_stderr(Str* s) {
Str* str(int i) {
Str* s = OverAllocatedStr(kIntBufSize);
int length = snprintf(s->data(), kIntBufSize, "%d", i);
s->SetObjLenFromStrLen(length);
s->MaybeShrink(length);
return s;
}

Expand Down
2 changes: 1 addition & 1 deletion mycpp/gc_mylib.cc
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ Str* BufWriter::getvalue() {
return kEmptyString;
} else {
Str* s = str_;
s->SetObjLenFromStrLen(len_);
s->MaybeShrink(len_);
str_ = nullptr;
len_ = -1;
return s;
Expand Down
29 changes: 19 additions & 10 deletions mycpp/gc_str.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,8 @@ class Str {
return data_;
}

void SetObjLenFromStrLen(int str_len);

// Useful for getcwd() + PATH_MAX, gethostname() + HOSTNAME_MAX, etc.
void SetObjLenFromC() {
SetObjLenFromStrLen(strlen(data_));
}
// Call this after writing into buffer created by OverAllocatedStr()
void MaybeShrink(int str_len);

Str* index_(int i);

Expand Down Expand Up @@ -83,15 +79,23 @@ class Str {

constexpr int kStrHeaderSize = offsetof(Str, data_);

inline void Str::SetObjLenFromStrLen(int str_len) {
// header_.obj_len = kStrHeaderSize + str_len + 1;
// Note: for SmallStr, we might copy into the VALUE
inline void Str::MaybeShrink(int str_len) {
#ifdef MARK_SWEEP
STR_LEN(header_) = str_len;
#else
// reversed in len() to derive string length
header_.obj_len = kStrHeaderSize + str_len + 1;
#endif
}

inline int len(const Str* s) {
// DCHECK(s->header_.obj_len >= kStrHeaderSize - 1);
// return s->header_.obj_len - kStrHeaderSize - 1;
#ifdef MARK_SWEEP
return STR_LEN(s->header_);
#else
DCHECK(s->header_.obj_len >= kStrHeaderSize - 1);
return s->header_.obj_len - kStrHeaderSize - 1;
#endif
}

// Notes:
Expand All @@ -112,7 +116,12 @@ inline Str* NewStr(int len) {
void* place = gHeap.Allocate(obj_len);

auto s = new (place) Str();
#ifdef MARK_SWEEP
STR_LEN(s->header_) = len;
#else
// reversed in len() to derive string length
header_.obj_len = kStrHeaderSize + str_len + 1;
#endif
return s;
}

Expand Down
2 changes: 1 addition & 1 deletion mycpp/gc_str_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ TEST test_str_creation() {
// Hm annoying that we have to do a const_cast
memcpy(s4->data(), "foo", 3);
strcpy(s4->data(), "foo");
s4->SetObjLenFromStrLen(3);
s4->MaybeShrink(3);

ASSERT_EQ(3, len(s4));
ASSERT_EQ(0, strcmp("foo", s4->data_));
Expand Down

0 comments on commit 47896c8

Please sign in to comment.