Commit 3b02cdf5 authored by Victor Costan's avatar Victor Costan Committed by Commit Bot

sql: Make Connection::StatementRef use 1-based ref-counting.

0-based ref-counting is deprecated. 1-based counting is described in
//base/memory/ref_counted.h. In summary, instaces must be created by
base::MakeRefCounted<T> or (less ideal) base::AdoptRef(new T).

This CL switches Connection::StatementRef to 1-based ref-counting, and

std::move()'s scoped_refptr<StatementRef> in a few places where that
avoids refcount churn.

Bug: none
Change-Id: I14755dc2482f18c8a9582066104f346c2873941d
Reviewed-on: https://chromium-review.googlesource.com/1137848
Commit-Queue: Victor Costan <pwnall@chromium.org>
Reviewed-by: default avatarChris Mumford <cmumford@chromium.org>
Cr-Commit-Position: refs/heads/master@{#575876}
parent 83cb1c00
...@@ -1435,7 +1435,7 @@ scoped_refptr<Connection::StatementRef> Connection::GetStatementImpl( ...@@ -1435,7 +1435,7 @@ scoped_refptr<Connection::StatementRef> Connection::GetStatementImpl(
// Return inactive statement. // Return inactive statement.
if (!db_) if (!db_)
return new StatementRef(NULL, NULL, poisoned_); return base::MakeRefCounted<StatementRef>(nullptr, nullptr, poisoned_);
sqlite3_stmt* stmt = NULL; sqlite3_stmt* stmt = NULL;
int rc = sqlite3_prepare_v2(db_, sql, -1, &stmt, NULL); int rc = sqlite3_prepare_v2(db_, sql, -1, &stmt, NULL);
...@@ -1445,9 +1445,9 @@ scoped_refptr<Connection::StatementRef> Connection::GetStatementImpl( ...@@ -1445,9 +1445,9 @@ scoped_refptr<Connection::StatementRef> Connection::GetStatementImpl(
// It could also be database corruption. // It could also be database corruption.
OnSqliteError(rc, NULL, sql); OnSqliteError(rc, NULL, sql);
return new StatementRef(NULL, NULL, false); return base::MakeRefCounted<StatementRef>(nullptr, nullptr, false);
} }
return new StatementRef(tracking_db, stmt, true); return base::MakeRefCounted<StatementRef>(tracking_db, stmt, true);
} }
scoped_refptr<Connection::StatementRef> Connection::GetUntrackedStatement( scoped_refptr<Connection::StatementRef> Connection::GetUntrackedStatement(
......
...@@ -581,6 +581,8 @@ class SQL_EXPORT Connection { ...@@ -581,6 +581,8 @@ class SQL_EXPORT Connection {
// should always check validity before using. // should always check validity before using.
class SQL_EXPORT StatementRef : public base::RefCounted<StatementRef> { class SQL_EXPORT StatementRef : public base::RefCounted<StatementRef> {
public: public:
REQUIRE_ADOPTION_FOR_REFCOUNTED_TYPE();
// |connection| is the sql::Connection instance associated with // |connection| is the sql::Connection instance associated with
// the statement, and is used for tracking outstanding statements // the statement, and is used for tracking outstanding statements
// and for error handling. Set to NULL for invalid or untracked // and for error handling. Set to NULL for invalid or untracked
......
...@@ -18,16 +18,14 @@ namespace sql { ...@@ -18,16 +18,14 @@ namespace sql {
// we don't have to NULL-check the ref_ to see if the statement is valid: we // we don't have to NULL-check the ref_ to see if the statement is valid: we
// only have to check the ref's validity bit. // only have to check the ref's validity bit.
Statement::Statement() Statement::Statement()
: ref_(new Connection::StatementRef(NULL, NULL, false)), : ref_(base::MakeRefCounted<Connection::StatementRef>(nullptr,
nullptr,
false)),
stepped_(false), stepped_(false),
succeeded_(false) { succeeded_(false) {}
}
Statement::Statement(scoped_refptr<Connection::StatementRef> ref) Statement::Statement(scoped_refptr<Connection::StatementRef> ref)
: ref_(ref), : ref_(std::move(ref)), stepped_(false), succeeded_(false) {}
stepped_(false),
succeeded_(false) {
}
Statement::~Statement() { Statement::~Statement() {
// Free the resources associated with this statement. We assume there's only // Free the resources associated with this statement. We assume there's only
...@@ -38,11 +36,12 @@ Statement::~Statement() { ...@@ -38,11 +36,12 @@ Statement::~Statement() {
void Statement::Assign(scoped_refptr<Connection::StatementRef> ref) { void Statement::Assign(scoped_refptr<Connection::StatementRef> ref) {
Reset(true); Reset(true);
ref_ = ref; ref_ = std::move(ref);
} }
void Statement::Clear() { void Statement::Clear() {
Assign(new Connection::StatementRef(NULL, NULL, false)); Assign(
base::MakeRefCounted<Connection::StatementRef>(nullptr, nullptr, false));
succeeded_ = false; succeeded_ = false;
} }
......
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