Author: Armin Rigo <[email protected]>
Branch:
Changeset: r1008:c459d0dc116d
Date: 2014-03-14 09:12 +0100
http://bitbucket.org/pypy/stmgc/changeset/c459d0dc116d/
Log: Weakrefs, copied from c7/
diff --git a/gil-c7/stmgc.c b/gil-c7/stmgc.c
--- a/gil-c7/stmgc.c
+++ b/gil-c7/stmgc.c
@@ -117,10 +117,12 @@
#define GCFLAG_WRITE_BARRIER _STM_GCFLAG_WRITE_BARRIER
static struct list_s *objects_pointing_to_nursery;
+static struct list_s *young_weakrefs;
void stm_setup(void)
{
objects_pointing_to_nursery = list_create();
+ young_weakrefs = list_create();
}
void stm_teardown(void)
@@ -271,11 +273,58 @@
memset(_stm_nursery_base, 0, NURSERY_SIZE);
}
+#define WEAKREF_PTR(wr, sz) ((object_t * TLPREFIX *)(((char *)(wr)) + (sz) -
sizeof(void*)))
+
+static void move_young_weakrefs(void)
+{
+ LIST_FOREACH_R(
+ young_weakrefs,
+ object_t * /*item*/,
+ ({
+ assert(_is_in_nursery(item));
+ object_t *TLPREFIX *pforwarded_array = (object_t *TLPREFIX *)item;
+
+ /* the following checks are done like in nursery.c: */
+ if (pforwarded_array[0] != GCWORD_MOVED) {
+ /* weakref dies */
+ continue;
+ }
+
+ item = pforwarded_array[1]; /* moved location */
+
+ assert(!_is_in_nursery(item));
+
+ ssize_t size = 16;
+ object_t *pointing_to = *WEAKREF_PTR(item, size);
+ assert(pointing_to != NULL);
+
+ if (_is_in_nursery(pointing_to)) {
+ object_t *TLPREFIX *pforwarded_array = (object_t *TLPREFIX
*)pointing_to;
+ /* the following checks are done like in nursery.c: */
+ if (pforwarded_array[0] != GCWORD_MOVED) {
+ /* pointing_to dies */
+ *WEAKREF_PTR(item, size) = NULL;
+ continue; /* no need to remember in old_weakrefs */
+ }
+ else {
+ /* moved location */
+ *WEAKREF_PTR(item, size) = pforwarded_array[1];
+ }
+ }
+ else {
+ /* pointing_to was already old */
+ }
+ //LIST_APPEND(STM_PSEGMENT->old_weakrefs, item);
+ }));
+ list_clear(young_weakrefs);
+}
+
void stm_collect(long level)
{
/* 'level' is ignored, only minor collections are implemented */
collect_roots_in_nursery();
collect_oldrefs_to_nursery();
+ move_young_weakrefs();
throw_away_nursery();
}
@@ -291,3 +340,11 @@
_stm_nursery_current = end;
return (object_t *)p;
}
+
+object_t *stm_allocate_weakref(ssize_t size_rounded_up)
+{
+ assert(size_rounded_up == 16);
+ object_t *obj = stm_allocate(size_rounded_up);
+ LIST_APPEND(young_weakrefs, obj);
+ return obj;
+}
diff --git a/gil-c7/stmgc.h b/gil-c7/stmgc.h
--- a/gil-c7/stmgc.h
+++ b/gil-c7/stmgc.h
@@ -47,6 +47,7 @@
object_t *_stm_allocate_external(ssize_t);
object_t *_stm_allocate_slowpath(ssize_t);
+object_t *stm_allocate_weakref(ssize_t size_rounded_up);
inline static object_t *stm_allocate(ssize_t size_rounded_up) {
OPT_ASSERT(size_rounded_up >= 16);
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit