Skip to content

Commit b923576

Browse files
committed
Move GC-related declarations to gc folder and add comments.
1 parent b0964a8 commit b923576

File tree

3 files changed

+88
-53
lines changed

3 files changed

+88
-53
lines changed

src/core/types.h

Lines changed: 2 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "core/common.h"
3030
#include "core/stats.h"
3131
#include "core/stringpool.h"
32+
#include "gc/gc.h"
3233

3334
namespace llvm {
3435
class Function;
@@ -38,29 +39,6 @@ class Value;
3839

3940
namespace pyston {
4041

41-
namespace gc {
42-
43-
class TraceStack;
44-
class GCVisitor {
45-
private:
46-
bool isValid(void* p);
47-
48-
public:
49-
TraceStack* stack;
50-
GCVisitor(TraceStack* stack) : stack(stack) {}
51-
52-
// These all work on *user* pointers, ie pointers to the user_data section of GCAllocations
53-
void visitIf(void* p) {
54-
if (p)
55-
visit(p);
56-
}
57-
void visit(void* p);
58-
void visitRange(void* const* start, void* const* end);
59-
void visitPotential(void* p);
60-
void visitPotentialRange(void* const* start, void* const* end);
61-
};
62-
63-
} // namespace gc
6442
using gc::GCVisitor;
6543

6644
enum class EffortLevel {
@@ -459,35 +437,7 @@ class BinopIC;
459437

460438
class Box;
461439

462-
namespace gc {
463-
464-
enum class GCKind : uint8_t {
465-
PYTHON = 1,
466-
CONSERVATIVE = 2,
467-
PRECISE = 3,
468-
UNTRACKED = 4,
469-
RUNTIME = 5,
470-
CONSERVATIVE_PYTHON = 6,
471-
};
472-
473-
extern "C" void* gc_alloc(size_t nbytes, GCKind kind);
474-
extern "C" void* gc_realloc(void* ptr, size_t bytes);
475-
extern "C" void gc_free(void* ptr);
476-
}
477-
478-
class GCAllocatedRuntime {
479-
public:
480-
virtual ~GCAllocatedRuntime() {}
481-
482-
void* operator new(size_t size) __attribute__((visibility("default"))) {
483-
return gc::gc_alloc(size, gc::GCKind::RUNTIME);
484-
}
485-
void operator delete(void* ptr) __attribute__((visibility("default"))) { gc::gc_free(ptr); }
486-
487-
virtual void gc_visit(GCVisitor* visitor) = 0;
488-
};
489-
490-
class BoxIteratorImpl : public GCAllocatedRuntime {
440+
class BoxIteratorImpl : public gc::GCAllocatedRuntime {
491441
public:
492442
virtual ~BoxIteratorImpl() = default;
493443
virtual void next() = 0;

src/gc/gc.h

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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

src/runtime/hiddenclass.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
namespace pyston {
2727

28-
class HiddenClass : public GCAllocatedRuntime {
28+
class HiddenClass : public gc::GCAllocatedRuntime {
2929
public:
3030
// We have a couple different storage strategies for attributes, which
3131
// are distinguished by having a different hidden class type.

0 commit comments

Comments
 (0)