Commit f69d1a99 authored by Chris Toshok's avatar Chris Toshok

Merge pull request #417 from toshok/stack-rooting

add StackRoot<> and #defines for DECREF
parents 8effb5a4 6a8be0a1
......@@ -826,7 +826,7 @@ PyAPI_FUNC(void) _Py_AddToAllObjects(PyObject *, int force) PYSTON_NOEXCEPT;
#define Py_INCREF(op) ((void)(op))
#define Py_DECREF(op) ((void)(op))
#define Py_DECREF(op) asm volatile("" : : "X"(op))
/* Safely decref `op` and set `op` to NULL, especially useful in tp_clear
* and tp_dealloc implementatons.
......@@ -874,7 +874,7 @@ PyAPI_FUNC(void) _Py_AddToAllObjects(PyObject *, int force) PYSTON_NOEXCEPT;
/* Macros to use in case the object pointer may be NULL: */
// Pyston change: made these noops as well
#define Py_XINCREF(op) ((void)(op))
#define Py_XDECREF(op) ((void)(op))
#define Py_XDECREF(op) asm volatile("" : : "X"(op))
/*
These are provided as conveniences to Python runtime embedders, so that
......
// Copyright (c) 2014-2015 Dropbox, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef PYSTON_GC_ROOTS_H
#define PYSTON_GC_ROOTS_H
#include "core/common.h"
#include "core/threading.h"
namespace pyston {
#define GC_KEEP_ALIVE(t) asm volatile("" : : "X"(t))
template <class T> class StackRoot {
public:
explicit StackRoot(T* t) : t(t) {}
StackRoot(const StackRoot& other) : t(other.t) {}
template <class... Args> StackRoot(Args&&... args) : t(new T(std::forward(args...))) {}
~StackRoot() { GC_KEEP_ALIVE(t); }
T& operator*() const { return *t; }
T* operator->() const { return t; }
operator T*() const { return t; }
private:
T* t;
};
class Box;
class BoxedString;
typedef StackRoot<Box> RootedBox;
typedef StackRoot<BoxedString> RootedBoxedString;
//
// the above types can be used whenever we want to explicitly root a Box subclass within some lexical scope.
//
// {
// RootedBoxedString sub("hello world");
// for (auto c : sub->s) {
// doSomethingThatCouldTriggerACollection();
// }
// callWithString(sub); // pass RootedBoxedString to a function taking BoxedString*
// // sub will be rooted conservatively until here
// }
//
}
#endif
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment