Skip to content

Commit 54458c4

Browse files
Statically initialize PyInterpreterState.gc.generations.
1 parent e60c802 commit 54458c4

File tree

3 files changed

+20
-14
lines changed

3 files changed

+20
-14
lines changed

Include/internal/pycore_gc.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ struct _gc_runtime_state {
134134
/* Current call-stack depth of tp_dealloc calls. */
135135
int trash_delete_nesting;
136136

137+
/* Is automatic collection enabled? */
137138
int enabled;
138139
int debug;
139140
/* linked lists of container objects */
@@ -161,6 +162,7 @@ struct _gc_runtime_state {
161162
Py_ssize_t long_lived_pending;
162163
};
163164

165+
164166
extern void _PyGC_InitState(struct _gc_runtime_state *);
165167

166168
extern Py_ssize_t _PyGC_CollectNoFail(PyThreadState *tstate);

Include/internal/pycore_runtime_init.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,13 @@ extern "C" {
4040
.recursion_limit = Py_DEFAULT_RECURSION_LIMIT, \
4141
}, \
4242
.gc = { \
43-
.enabled = 1, /* automatic collection enabled? */ \
43+
.enabled = 1, \
44+
.generations = { \
45+
/* .head is set in _PyGC_InitState(). */ \
46+
{ .threshold = 700, }, \
47+
{ .threshold = 10, }, \
48+
{ .threshold = 10, }, \
49+
}, \
4450
}, \
4551
._initial_thread = _PyThreadState_INIT, \
4652
}

Modules/gcmodule.c

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -139,22 +139,20 @@ get_gc_state(void)
139139
void
140140
_PyGC_InitState(GCState *gcstate)
141141
{
142-
#define _GEN_HEAD(n) GEN_HEAD(gcstate, n)
143-
struct gc_generation generations[NUM_GENERATIONS] = {
144-
/* PyGC_Head, threshold, count */
145-
{{(uintptr_t)_GEN_HEAD(0), (uintptr_t)_GEN_HEAD(0)}, 700, 0},
146-
{{(uintptr_t)_GEN_HEAD(1), (uintptr_t)_GEN_HEAD(1)}, 10, 0},
147-
{{(uintptr_t)_GEN_HEAD(2), (uintptr_t)_GEN_HEAD(2)}, 10, 0},
148-
};
142+
#define INIT_HEAD(GEN) \
143+
do { \
144+
GEN.head._gc_next = (uintptr_t)&GEN.head; \
145+
GEN.head._gc_prev = (uintptr_t)&GEN.head; \
146+
} while (0)
147+
149148
for (int i = 0; i < NUM_GENERATIONS; i++) {
150-
gcstate->generations[i] = generations[i];
149+
assert(gcstate->generations[i].count == 0);
150+
INIT_HEAD(gcstate->generations[i]);
151151
};
152152
gcstate->generation0 = GEN_HEAD(gcstate, 0);
153-
struct gc_generation permanent_generation = {
154-
{(uintptr_t)&gcstate->permanent_generation.head,
155-
(uintptr_t)&gcstate->permanent_generation.head}, 0, 0
156-
};
157-
gcstate->permanent_generation = permanent_generation;
153+
INIT_HEAD(gcstate->permanent_generation);
154+
155+
#undef INIT_HEAD
158156
}
159157

160158

0 commit comments

Comments
 (0)