From 47896c88372ce388f03ff47dca13fadfa5b032f1 Mon Sep 17 00:00:00 2001 From: Andy C Date: Sun, 1 Jan 2023 21:24:37 -0500 Subject: [PATCH] [mycpp] MaybeShrink() replaces SetObjLenFromStrLen() It will also be used for SmallStr. --- cpp/core.cc | 4 ++-- cpp/libc.cc | 2 +- cpp/qsn.h | 2 +- cpp/stdlib.cc | 2 +- cpp/stdlib.h | 2 +- mycpp/gc_builtins.cc | 2 +- mycpp/gc_mylib.cc | 2 +- mycpp/gc_str.h | 29 +++++++++++++++++++---------- mycpp/gc_str_test.cc | 2 +- 9 files changed, 28 insertions(+), 19 deletions(-) diff --git a/cpp/core.cc b/cpp/core.cc index 9381266cd8..295fb977c5 100644 --- a/cpp/core.cc +++ b/cpp/core.cc @@ -38,7 +38,7 @@ Tuple2 Read(int fd, int n, List* chunks) { } // Now we know how much data we got back - s->SetObjLenFromStrLen(length); + s->MaybeShrink(length); chunks->append(s); return Tuple2(length, 0); @@ -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; } diff --git a/cpp/libc.cc b/cpp/libc.cc index 3174abe589..32c4adbbb2 100644 --- a/cpp/libc.cc +++ b/cpp/libc.cc @@ -20,7 +20,7 @@ Str* gethostname() { throw Alloc(errno); } // Important: set the length of the string! - result->SetObjLenFromC(); + result->MaybeShrink(strlen(result->data_)); return result; } diff --git a/cpp/qsn.h b/cpp/qsn.h index 0a12435691..f23e0116e3 100644 --- a/cpp/qsn.h +++ b/cpp/qsn.h @@ -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; } diff --git a/cpp/stdlib.cc b/cpp/stdlib.cc index e5f3e93634..38e7e5275a 100644 --- a/cpp/stdlib.cc +++ b/cpp/stdlib.cc @@ -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; } diff --git a/cpp/stdlib.h b/cpp/stdlib.h index 865eb0c71f..91753eed86 100644 --- a/cpp/stdlib.h +++ b/cpp/stdlib.h @@ -33,7 +33,7 @@ inline Str* getcwd() { throw Alloc(errno); } // Important: set the length of the string! - result->SetObjLenFromC(); + result->MaybeShrink(strlen(result->data_)); return result; } diff --git a/mycpp/gc_builtins.cc b/mycpp/gc_builtins.cc index b6b16bea08..5337b836a9 100644 --- a/mycpp/gc_builtins.cc +++ b/mycpp/gc_builtins.cc @@ -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; } diff --git a/mycpp/gc_mylib.cc b/mycpp/gc_mylib.cc index cad5add209..a0afdb26a7 100644 --- a/mycpp/gc_mylib.cc +++ b/mycpp/gc_mylib.cc @@ -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; diff --git a/mycpp/gc_str.h b/mycpp/gc_str.h index fa87f02293..7308754dfe 100644 --- a/mycpp/gc_str.h +++ b/mycpp/gc_str.h @@ -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); @@ -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: @@ -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; } diff --git a/mycpp/gc_str_test.cc b/mycpp/gc_str_test.cc index 1f7f64efb0..988027e863 100644 --- a/mycpp/gc_str_test.cc +++ b/mycpp/gc_str_test.cc @@ -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_));