js/src/jshashutil.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/js/src/jshashutil.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,64 @@
     1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
     1.5 + * vim: set ts=8 sts=4 et sw=4 tw=99:
     1.6 + * This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +#ifndef jshashutil_h
    1.11 +#define jshashutil_h
    1.12 +
    1.13 +#include "jscntxt.h"
    1.14 +
    1.15 +namespace js {
    1.16 +
    1.17 +/*
    1.18 + * Used to add entries to a js::HashMap or HashSet where the key depends on a GC
    1.19 + * thing that may be moved by generational collection between the call to
    1.20 + * lookupForAdd() and relookupOrAdd().
    1.21 + */
    1.22 +template <class T>
    1.23 +struct DependentAddPtr
    1.24 +{
    1.25 +    typedef typename T::AddPtr AddPtr;
    1.26 +    typedef typename T::Entry Entry;
    1.27 +
    1.28 +    template <class Lookup>
    1.29 +    DependentAddPtr(const ExclusiveContext *cx, const T &table, const Lookup &lookup)
    1.30 +      : addPtr(table.lookupForAdd(lookup))
    1.31 +#ifdef JSGC_GENERATIONAL
    1.32 +      , originalGcNumber(cx->zone()->gcNumber())
    1.33 +#endif
    1.34 +        {}
    1.35 +
    1.36 +    template <class KeyInput, class ValueInput>
    1.37 +    bool add(const ExclusiveContext *cx, T &table, const KeyInput &key, const ValueInput &value) {
    1.38 +#ifdef JSGC_GENERATIONAL
    1.39 +        bool gcHappened = originalGcNumber != cx->zone()->gcNumber();
    1.40 +        if (gcHappened)
    1.41 +            addPtr = table.lookupForAdd(key);
    1.42 +#endif
    1.43 +        return table.relookupOrAdd(addPtr, key, value);
    1.44 +    }
    1.45 +
    1.46 +    typedef void (DependentAddPtr::* ConvertibleToBool)();
    1.47 +    void nonNull() {}
    1.48 +
    1.49 +    bool found() const                 { return addPtr.found(); }
    1.50 +    operator ConvertibleToBool() const { return found() ? &DependentAddPtr::nonNull : 0; }
    1.51 +    const Entry &operator*() const     { return *addPtr; }
    1.52 +    const Entry *operator->() const    { return &*addPtr; }
    1.53 +
    1.54 +  private:
    1.55 +    AddPtr addPtr ;
    1.56 +#ifdef JSGC_GENERATIONAL
    1.57 +    const uint64_t originalGcNumber;
    1.58 +#endif
    1.59 +
    1.60 +    DependentAddPtr() MOZ_DELETE;
    1.61 +    DependentAddPtr(const DependentAddPtr&) MOZ_DELETE;
    1.62 +    DependentAddPtr& operator=(const DependentAddPtr&) MOZ_DELETE;
    1.63 +};
    1.64 +
    1.65 +} // namespace js
    1.66 +
    1.67 +#endif

mercurial