gfx/2d/Preferences.cpp
author Wes Kocher <wkocher@mozilla.com>
Fri, 24 Jun 2016 14:43:41 -0700
changeset 342662 c6bb3db5e1b837a3caa622d673bdc326b171e566
child 342676 c2da34d96746288b5fee27bf6542a12c9f410988
permissions -rw-r--r--
Backed out 4 changesets (bug 1280822) for leaking nsTArray_base Backed out changeset b9a0567cd7c1 (bug 1280822) Backed out changeset 93d483583ffa (bug 1280822) Backed out changeset 91715d8e468f (bug 1280822) Backed out changeset 3db77f57cef4 (bug 1280822)

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "Preferences.h"

#include "mozilla/Assertions.h"
#include "mozilla/Vector.h"

namespace mozilla {
namespace gfx {

static PreferenceAccess* sAccess = nullptr;

struct Int32Pref
{
  const char* name;
  int32_t* varPtr;
};

static Vector<Int32Pref>& Int32Prefs()
{
  static Vector<Int32Pref>* sInt32Prefs = new Vector<Int32Pref>();
  return *sInt32Prefs;
}

/* static */
int32_t
PreferenceAccess::RegisterLivePref(const char* aName, int32_t* aVar,
                                   int32_t aDefault)
{
  if (!Int32Prefs().append(Int32Pref{ aName, aVar })) {
    MOZ_CRASH("GFX: RegisterLivePref failure");
  }
  return aDefault;
}

/* static */
void
PreferenceAccess::SetAccess(PreferenceAccess* aAccess)
{
  sAccess = aAccess;
  if (!sAccess) {
    return;
  }

#if defined(DEBUG)
  static uint32_t sProvideAccessCount;
  MOZ_ASSERT(!sProvideAccessCount++,
             "ProvideAccess must only be called with non-nullptr once.");
#endif

  for (Int32Pref pref : Int32Prefs()) {
    sAccess->LivePref(pref.name, pref.varPtr, *pref.varPtr);
  }
  Int32Prefs().clearAndFree();
}

} // namespace gfx
} // namespace mozilla