| 1 | // gc_mylib.h - corresponds to mycpp/mylib.py
|
| 2 |
|
| 3 | #ifndef MYCPP_GC_MYLIB_H
|
| 4 | #define MYCPP_GC_MYLIB_H
|
| 5 |
|
| 6 | #include "mycpp/gc_alloc.h" // gHeap
|
| 7 | #include "mycpp/gc_dict.h" // for dict_erase()
|
| 8 | #include "mycpp/gc_mops.h"
|
| 9 | #include "mycpp/gc_tuple.h"
|
| 10 |
|
| 11 | #include <sys/stat.h>
|
| 12 |
|
| 13 | template <class K, class V>
|
| 14 | class Dict;
|
| 15 |
|
| 16 | namespace mylib {
|
| 17 |
|
| 18 | bool isinf_(double f);
|
| 19 | bool isnan_(double f);
|
| 20 |
|
| 21 | void InitCppOnly();
|
| 22 |
|
| 23 | // Wrappers around our C++ APIs
|
| 24 |
|
| 25 | inline void MaybeCollect() {
|
| 26 | gHeap.MaybeCollect();
|
| 27 | }
|
| 28 |
|
| 29 | inline void PrintGcStats() {
|
| 30 | gHeap.PrintShortStats(); // print to stderr
|
| 31 | }
|
| 32 |
|
| 33 | void print_stderr(BigStr* s);
|
| 34 |
|
| 35 | inline int ByteAt(BigStr* s, int i) {
|
| 36 | DCHECK(0 <= i);
|
| 37 | DCHECK(i <= len(s));
|
| 38 |
|
| 39 | return static_cast<unsigned char>(s->data_[i]);
|
| 40 | }
|
| 41 |
|
| 42 | inline int ByteEquals(int byte, BigStr* ch) {
|
| 43 | DCHECK(0 <= byte);
|
| 44 | DCHECK(byte < 256);
|
| 45 |
|
| 46 | DCHECK(len(ch) == 1);
|
| 47 |
|
| 48 | return byte == static_cast<unsigned char>(ch->data_[0]);
|
| 49 | }
|
| 50 |
|
| 51 | inline int ByteInSet(int byte, BigStr* byte_set) {
|
| 52 | DCHECK(0 <= byte);
|
| 53 | DCHECK(byte < 256);
|
| 54 |
|
| 55 | int n = len(byte_set);
|
| 56 | for (int i = 0; i < n; ++i) {
|
| 57 | int b = static_cast<unsigned char>(byte_set->data_[i]);
|
| 58 | if (byte == b) {
|
| 59 | return true;
|
| 60 | }
|
| 61 | }
|
| 62 | return false;
|
| 63 | }
|
| 64 |
|
| 65 | BigStr* JoinBytes(List<int>* byte_list);
|
| 66 |
|
| 67 | void BigIntSort(List<mops::BigInt>* keys);
|
| 68 |
|
| 69 | // const int kStdout = 1;
|
| 70 | // const int kStderr = 2;
|
| 71 |
|
| 72 | // void writeln(BigStr* s, int fd = kStdout);
|
| 73 |
|
| 74 | Tuple2<BigStr*, BigStr*> split_once(BigStr* s, BigStr* delim);
|
| 75 |
|
| 76 | template <typename K, typename V>
|
| 77 | void dict_erase(Dict<K, V>* haystack, K needle) {
|
| 78 | DCHECK(haystack->obj_header().heap_tag != HeapTag::Global);
|
| 79 |
|
| 80 | int pos = haystack->hash_and_probe(needle);
|
| 81 | if (pos == kTooSmall) {
|
| 82 | return;
|
| 83 | }
|
| 84 | DCHECK(pos >= 0);
|
| 85 | int kv_index = haystack->index_->items_[pos];
|
| 86 | if (kv_index < 0) {
|
| 87 | return;
|
| 88 | }
|
| 89 |
|
| 90 | int last_kv_index = haystack->len_ - 1;
|
| 91 | DCHECK(kv_index <= last_kv_index);
|
| 92 |
|
| 93 | // Swap the target entry with the most recently inserted one before removing
|
| 94 | // it. This has two benefits.
|
| 95 | // (1) It keeps the entry arrays compact. All valid entries occupy a
|
| 96 | // contiguous region in memory.
|
| 97 | // (2) It prevents holes in the entry arrays. This makes iterating over
|
| 98 | // entries (e.g. in keys() or DictIter()) trivial and doesn't require
|
| 99 | // any extra validity state (like a bitset of unusable slots). This is
|
| 100 | // important because keys and values wont't always be pointers, so we
|
| 101 | // can't rely on NULL checks for validity. We also can't wrap the slab
|
| 102 | // entry types in some other type without modifying the garbage
|
| 103 | // collector to trace through unmanaged types (or paying the extra
|
| 104 | // allocations for the outer type).
|
| 105 | if (kv_index != last_kv_index) {
|
| 106 | K last_key = haystack->keys_->items_[last_kv_index];
|
| 107 | V last_val = haystack->values_->items_[last_kv_index];
|
| 108 | int last_pos = haystack->hash_and_probe(last_key);
|
| 109 | DCHECK(last_pos != kNotFound);
|
| 110 | haystack->keys_->items_[kv_index] = last_key;
|
| 111 | haystack->values_->items_[kv_index] = last_val;
|
| 112 | haystack->index_->items_[last_pos] = kv_index;
|
| 113 | }
|
| 114 |
|
| 115 | // Zero out for GC. These could be nullptr or 0
|
| 116 | haystack->keys_->items_[last_kv_index] = 0;
|
| 117 | haystack->values_->items_[last_kv_index] = 0;
|
| 118 | haystack->index_->items_[pos] = kDeletedEntry;
|
| 119 | haystack->len_--;
|
| 120 | DCHECK(haystack->len_ < haystack->capacity_);
|
| 121 | }
|
| 122 |
|
| 123 | inline BigStr* hex_lower(int i) {
|
| 124 | // Note: Could also use OverAllocatedStr, but most strings are small?
|
| 125 | char buf[kIntBufSize];
|
| 126 | int len = snprintf(buf, kIntBufSize, "%x", i);
|
| 127 | return ::StrFromC(buf, len);
|
| 128 | }
|
| 129 |
|
| 130 | // Abstract type: Union of LineReader and Writer
|
| 131 | class File {
|
| 132 | public:
|
| 133 | File() {
|
| 134 | }
|
| 135 | // Writer
|
| 136 | virtual void write(BigStr* s) = 0;
|
| 137 | virtual void flush() = 0;
|
| 138 |
|
| 139 | // Reader
|
| 140 | virtual BigStr* readline() = 0;
|
| 141 |
|
| 142 | // Both
|
| 143 | virtual bool isatty() = 0;
|
| 144 | virtual void close() = 0;
|
| 145 |
|
| 146 | static constexpr ObjHeader obj_header() {
|
| 147 | return ObjHeader::ClassFixed(field_mask(), sizeof(File));
|
| 148 | }
|
| 149 |
|
| 150 | static constexpr uint32_t field_mask() {
|
| 151 | return kZeroMask;
|
| 152 | }
|
| 153 | };
|
| 154 |
|
| 155 | // Wrap a FILE* for read and write
|
| 156 | class CFile : public File {
|
| 157 | public:
|
| 158 | explicit CFile(FILE* f) : File(), f_(f) {
|
| 159 | }
|
| 160 | // Writer
|
| 161 | void write(BigStr* s) override;
|
| 162 | void flush() override;
|
| 163 |
|
| 164 | // Reader
|
| 165 | BigStr* readline() override;
|
| 166 |
|
| 167 | // Both
|
| 168 | bool isatty() override;
|
| 169 | void close() override;
|
| 170 |
|
| 171 | static constexpr ObjHeader obj_header() {
|
| 172 | return ObjHeader::ClassFixed(field_mask(), sizeof(CFile));
|
| 173 | }
|
| 174 |
|
| 175 | static constexpr uint32_t field_mask() {
|
| 176 | // not mutating field_mask because FILE* isn't a GC object
|
| 177 | return File::field_mask();
|
| 178 | }
|
| 179 |
|
| 180 | private:
|
| 181 | FILE* f_;
|
| 182 |
|
| 183 | DISALLOW_COPY_AND_ASSIGN(CFile)
|
| 184 | };
|
| 185 |
|
| 186 | // Abstract File we can only read from.
|
| 187 | // TODO: can we get rid of DCHECK() and reinterpret_cast?
|
| 188 | class LineReader : public File {
|
| 189 | public:
|
| 190 | LineReader() : File() {
|
| 191 | }
|
| 192 | void write(BigStr* s) override {
|
| 193 | CHECK(false); // should not happen
|
| 194 | }
|
| 195 | void flush() override {
|
| 196 | CHECK(false); // should not happen
|
| 197 | }
|
| 198 |
|
| 199 | static constexpr ObjHeader obj_header() {
|
| 200 | return ObjHeader::ClassFixed(field_mask(), sizeof(LineReader));
|
| 201 | }
|
| 202 |
|
| 203 | static constexpr uint32_t field_mask() {
|
| 204 | return kZeroMask;
|
| 205 | }
|
| 206 | };
|
| 207 |
|
| 208 | class BufLineReader : public LineReader {
|
| 209 | public:
|
| 210 | explicit BufLineReader(BigStr* s) : LineReader(), s_(s), pos_(0) {
|
| 211 | }
|
| 212 | virtual BigStr* readline();
|
| 213 | virtual bool isatty() {
|
| 214 | return false;
|
| 215 | }
|
| 216 | virtual void close() {
|
| 217 | }
|
| 218 |
|
| 219 | BigStr* s_;
|
| 220 | int pos_;
|
| 221 |
|
| 222 | static constexpr ObjHeader obj_header() {
|
| 223 | return ObjHeader::ClassFixed(field_mask(), sizeof(LineReader));
|
| 224 | }
|
| 225 |
|
| 226 | static constexpr uint32_t field_mask() {
|
| 227 | return LineReader::field_mask() | maskbit(offsetof(BufLineReader, s_));
|
| 228 | }
|
| 229 |
|
| 230 | DISALLOW_COPY_AND_ASSIGN(BufLineReader)
|
| 231 | };
|
| 232 |
|
| 233 | extern LineReader* gStdin;
|
| 234 |
|
| 235 | inline LineReader* Stdin() {
|
| 236 | if (gStdin == nullptr) {
|
| 237 | gStdin = reinterpret_cast<LineReader*>(Alloc<CFile>(stdin));
|
| 238 | }
|
| 239 | return gStdin;
|
| 240 | }
|
| 241 |
|
| 242 | LineReader* open(BigStr* path);
|
| 243 |
|
| 244 | // Abstract File we can only write to.
|
| 245 | // TODO: can we get rid of DCHECK() and reinterpret_cast?
|
| 246 | class Writer : public File {
|
| 247 | public:
|
| 248 | Writer() : File() {
|
| 249 | }
|
| 250 | BigStr* readline() override {
|
| 251 | CHECK(false); // should not happen
|
| 252 | }
|
| 253 |
|
| 254 | static constexpr ObjHeader obj_header() {
|
| 255 | return ObjHeader::ClassFixed(field_mask(), sizeof(Writer));
|
| 256 | }
|
| 257 |
|
| 258 | static constexpr uint32_t field_mask() {
|
| 259 | return kZeroMask;
|
| 260 | }
|
| 261 | };
|
| 262 |
|
| 263 | class MutableStr;
|
| 264 |
|
| 265 | class BufWriter : public Writer {
|
| 266 | public:
|
| 267 | BufWriter() : Writer(), str_(nullptr), len_(0) {
|
| 268 | }
|
| 269 | void write(BigStr* s) override;
|
| 270 | void write_spaces(int n);
|
| 271 | void clear() { // Reuse this instance
|
| 272 | str_ = nullptr;
|
| 273 | len_ = 0;
|
| 274 | is_valid_ = true;
|
| 275 | }
|
| 276 | void close() override {
|
| 277 | }
|
| 278 | void flush() override {
|
| 279 | }
|
| 280 | bool isatty() override {
|
| 281 | return false;
|
| 282 | }
|
| 283 | BigStr* getvalue(); // part of cStringIO API
|
| 284 |
|
| 285 | //
|
| 286 | // Low Level API for C++ usage only
|
| 287 | //
|
| 288 |
|
| 289 | // Convenient API that avoids BigStr*
|
| 290 | void WriteConst(const char* c_string);
|
| 291 |
|
| 292 | // Potentially resizes the buffer.
|
| 293 | void EnsureMoreSpace(int n);
|
| 294 | // After EnsureMoreSpace(42), you can write 42 more bytes safely.
|
| 295 | //
|
| 296 | // Note that if you call EnsureMoreSpace(42), write 5 byte, and then
|
| 297 | // EnsureMoreSpace(42) again, the amount of additional space reserved is 47.
|
| 298 |
|
| 299 | // (Similar to vector::reserve(n), but it takes an integer to ADD to the
|
| 300 | // capacity.)
|
| 301 |
|
| 302 | uint8_t* LengthPointer(); // start + length
|
| 303 | uint8_t* CapacityPointer(); // start + capacity
|
| 304 | void SetLengthFrom(uint8_t* length_ptr);
|
| 305 |
|
| 306 | int Length() {
|
| 307 | return len_;
|
| 308 | }
|
| 309 |
|
| 310 | // Rewind to earlier position, future writes start there
|
| 311 | void Truncate(int length);
|
| 312 |
|
| 313 | static constexpr ObjHeader obj_header() {
|
| 314 | return ObjHeader::ClassFixed(field_mask(), sizeof(BufWriter));
|
| 315 | }
|
| 316 |
|
| 317 | static constexpr unsigned field_mask() {
|
| 318 | // maskvit_v() because BufWriter has virtual methods
|
| 319 | return Writer::field_mask() | maskbit(offsetof(BufWriter, str_));
|
| 320 | }
|
| 321 |
|
| 322 | private:
|
| 323 | void WriteRaw(char* s, int n);
|
| 324 |
|
| 325 | MutableStr* str_; // getvalue() turns this directly into Str*, no copying
|
| 326 | int len_; // how many bytes have been written so far
|
| 327 | bool is_valid_ = true; // It becomes invalid after getvalue() is called
|
| 328 | };
|
| 329 |
|
| 330 | extern Writer* gStdout;
|
| 331 |
|
| 332 | inline Writer* Stdout() {
|
| 333 | if (gStdout == nullptr) {
|
| 334 | gStdout = reinterpret_cast<Writer*>(Alloc<CFile>(stdout));
|
| 335 | gHeap.RootGlobalVar(gStdout);
|
| 336 | }
|
| 337 | return gStdout;
|
| 338 | }
|
| 339 |
|
| 340 | extern Writer* gStderr;
|
| 341 |
|
| 342 | inline Writer* Stderr() {
|
| 343 | if (gStderr == nullptr) {
|
| 344 | gStderr = reinterpret_cast<Writer*>(Alloc<CFile>(stderr));
|
| 345 | gHeap.RootGlobalVar(gStderr);
|
| 346 | }
|
| 347 | return gStderr;
|
| 348 | }
|
| 349 |
|
| 350 | class UniqueObjects {
|
| 351 | // Can't be expressed in typed Python because we don't have uint64_t for
|
| 352 | // addresses
|
| 353 |
|
| 354 | public:
|
| 355 | UniqueObjects() {
|
| 356 | }
|
| 357 | void Add(void* obj) {
|
| 358 | }
|
| 359 | int Get(void* obj) {
|
| 360 | return -1;
|
| 361 | }
|
| 362 |
|
| 363 | static constexpr ObjHeader obj_header() {
|
| 364 | return ObjHeader::ClassFixed(field_mask(), sizeof(UniqueObjects));
|
| 365 | }
|
| 366 |
|
| 367 | // SPECIAL CASE? We should never have a unique reference to an object? So
|
| 368 | // don't bother tracing
|
| 369 | static constexpr uint32_t field_mask() {
|
| 370 | return kZeroMask;
|
| 371 | }
|
| 372 |
|
| 373 | private:
|
| 374 | // address -> small integer ID
|
| 375 | Dict<void*, int> addresses_;
|
| 376 | };
|
| 377 |
|
| 378 |
|
| 379 | class StatResult {
|
| 380 | public:
|
| 381 | bool isreg();
|
| 382 |
|
| 383 | static constexpr ObjHeader obj_header() {
|
| 384 | return ObjHeader::ClassFixed(field_mask(), sizeof(StatResult));
|
| 385 | }
|
| 386 |
|
| 387 | static constexpr uint32_t field_mask() {
|
| 388 | return kZeroMask;
|
| 389 | }
|
| 390 |
|
| 391 | struct stat stat_result_;
|
| 392 | };
|
| 393 |
|
| 394 | StatResult* stat(BigStr* filename);
|
| 395 |
|
| 396 | } // namespace mylib
|
| 397 |
|
| 398 | #endif // MYCPP_GC_MYLIB_H
|