|
| 1 | +// Copyright (c) 2014-2015 Dropbox, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#ifndef PYSTON_GC_GC_H |
| 16 | +#define PYSTON_GC_GC_H |
| 17 | + |
| 18 | +namespace pyston { |
| 19 | +namespace gc { |
| 20 | + |
| 21 | +// GOAL: Eventually, move any declaration that needs to be visible outside the gc/ folder |
| 22 | +// to this file and only expose this header. |
| 23 | + |
| 24 | +class TraceStack; |
| 25 | +class GCVisitor { |
| 26 | +private: |
| 27 | + bool isValid(void* p); |
| 28 | + |
| 29 | +public: |
| 30 | + TraceStack* stack; |
| 31 | + GCVisitor(TraceStack* stack) : stack(stack) {} |
| 32 | + |
| 33 | + // These all work on *user* pointers, ie pointers to the user_data section of GCAllocations |
| 34 | + void visitIf(void* p) { |
| 35 | + if (p) |
| 36 | + visit(p); |
| 37 | + } |
| 38 | + void visit(void* p); |
| 39 | + void visitRange(void* const* start, void* const* end); |
| 40 | + void visitPotential(void* p); |
| 41 | + void visitPotentialRange(void* const* start, void* const* end); |
| 42 | +}; |
| 43 | + |
| 44 | +enum class GCKind : uint8_t { |
| 45 | + // Any Python object (e.g. any Box) that can be visited precisely, using |
| 46 | + // a GC handler function. |
| 47 | + PYTHON = 1, |
| 48 | + |
| 49 | + // An arbitrary block of memory that may contain pointers. |
| 50 | + CONSERVATIVE = 2, |
| 51 | + |
| 52 | + // An arbitrary block of memory with contiguous pointers. |
| 53 | + PRECISE = 3, |
| 54 | + |
| 55 | + // An arbitrary block of memory that does not contain pointers. |
| 56 | + UNTRACKED = 4, |
| 57 | + |
| 58 | + // C++ objects that we need to manage with our own heap and GC, either |
| 59 | + // because it contains pointers into our heap or our heap points to these |
| 60 | + // objects. These objects inherit from GCAllocatedRuntime. |
| 61 | + RUNTIME = 5, |
| 62 | + |
| 63 | + // A Python object where we don't have a way to visit precisely with a GC |
| 64 | + // handler function. These are usually Python objects allocated in C extensions. |
| 65 | + CONSERVATIVE_PYTHON = 6, |
| 66 | +}; |
| 67 | + |
| 68 | +extern "C" void* gc_alloc(size_t nbytes, GCKind kind); |
| 69 | +extern "C" void* gc_realloc(void* ptr, size_t bytes); |
| 70 | +extern "C" void gc_free(void* ptr); |
| 71 | + |
| 72 | +// Use this if a C++ object needs to be allocated in our heap. |
| 73 | +class GCAllocatedRuntime { |
| 74 | +public: |
| 75 | + virtual ~GCAllocatedRuntime() {} |
| 76 | + |
| 77 | + void* operator new(size_t size) __attribute__((visibility("default"))) { return gc_alloc(size, GCKind::RUNTIME); } |
| 78 | + void operator delete(void* ptr) __attribute__((visibility("default"))) { gc_free(ptr); } |
| 79 | + |
| 80 | + virtual void gc_visit(GCVisitor* visitor) = 0; |
| 81 | +}; |
| 82 | +} // namespace gc |
| 83 | +} |
| 84 | + |
| 85 | +#endif |
0 commit comments