Skip to content

Instantly share code, notes, and snippets.

@ptarjan
Created February 1, 2014 09:22
Show Gist options
  • Save ptarjan/8750016 to your computer and use it in GitHub Desktop.
Save ptarjan/8750016 to your computer and use it in GitHub Desktop.
diff --git a/hphp/runtime/ext/ext_pdo.cpp b/hphp/runtime/ext/ext_pdo.cpp
index c49fd8f..81a4d73 100644
--- a/hphp/runtime/ext/ext_pdo.cpp
+++ b/hphp/runtime/ext/ext_pdo.cpp
@@ -2025,7 +2025,7 @@ static bool do_fetch(sp_PDOStatement stmt, bool do_bind, Variant &ret,
static int register_bound_param(CVarRef paramno, VRefParam param, int64_t type,
int64_t max_value_len, CVarRef driver_params,
sp_PDOStatement stmt, bool is_param) {
- SmartResource<PDOBoundParam> p(new PDOBoundParam);
+ SmartResource<PDOBoundParam> p(NEWOBJ(PDOBoundParam));
// need to make sure this is NULL, in case a fatal errors occurs before its set
// inside really_register_bound_param
p->stmt = NULL;
@@ -2640,7 +2640,7 @@ Variant c_PDOStatement::t_execute(CArrRef params /* = null_array */) {
if (!params.empty()) {
m_stmt->bound_params.reset();
for (ArrayIter iter(params); iter; ++iter) {
- SmartResource<PDOBoundParam> param(new PDOBoundParam);
+ SmartResource<PDOBoundParam> param(NEWOBJ(PDOBoundParam));
param->param_type = PDO_PARAM_STR;
param->parameter = iter.second();
param->stmt = NULL;
diff --git a/hphp/runtime/ext/pdo_driver.cpp b/hphp/runtime/ext/pdo_driver.cpp
index 9b527dc..21b87d3 100644
--- a/hphp/runtime/ext/pdo_driver.cpp
+++ b/hphp/runtime/ext/pdo_driver.cpp
@@ -75,7 +75,6 @@ PDOConnection::~PDOConnection() {
void PDOConnection::sweep() {
assert(!is_persistent);
def_stmt_ctor_args.asTypedValue()->m_type = KindOfNull;
- delete this;
}
void PDOConnection::persistentSave() {
@@ -180,6 +179,10 @@ PDOBoundParam::PDOBoundParam()
}
PDOBoundParam::~PDOBoundParam() {
+ sweep();
+}
+
+void PDOBoundParam::sweep() {
/* tell the driver that it is going away */
if (stmt && stmt->support(PDOStatement::MethodParamHook)) {
stmt->paramHook(this, PDO_PARAM_EVT_FREE);
@@ -203,6 +206,10 @@ PDOStatement::~PDOStatement() {
}
}
+void PDOStatement::sweep() {
+ // nothing, but kids can overwrite
+}
+
bool PDOStatement::support(SupportedMethod method) {
return false;
}
diff --git a/hphp/runtime/ext/pdo_driver.h b/hphp/runtime/ext/pdo_driver.h
index 93829e4..61dcc33 100644
--- a/hphp/runtime/ext/pdo_driver.h
+++ b/hphp/runtime/ext/pdo_driver.h
@@ -237,6 +237,7 @@ typedef SmartResource<PDOStatement> sp_PDOStatement;
/* represents a connection to a database */
class PDOConnection : public SweepableResourceData {
public:
+ DECLARE_RESOURCE_ALLOCATION(PDOConnection);
static const char *PersistentKey;
enum SupportedMethod {
@@ -259,7 +260,6 @@ public:
PDOConnection();
virtual ~PDOConnection();
virtual bool create(CArrRef options) = 0;
- virtual void sweep();
CLASSNAME_IS("PDOConnection")
// overriding ResourceData
@@ -320,8 +320,8 @@ public:
public:
/* credentials */
- std::string username;
- std::string password;
+ String username;
+ String password;
/* if true, then data stored and pointed at by this handle must all be
* persistently allocated */
@@ -356,7 +356,7 @@ public:
unsigned _reserved_flags:21;
/* data source string used to open this handle */
- std::string data_source;
+ String data_source;
/* the global error code. */
PDOErrorType error_code;
@@ -366,13 +366,13 @@ public:
PDOCaseConversion native_case, desired_case;
/* persistent hash key associated with this handle */
- std::string persistent_id;
+ String persistent_id;
PDODriver *driver;
- std::string def_stmt_clsname;
+ String def_stmt_clsname;
- std::string serialized_def_stmt_ctor_args;
+ String serialized_def_stmt_ctor_args;
Variant def_stmt_ctor_args;
/* when calling PDO::query(), we need to keep the error
@@ -391,6 +391,7 @@ typedef SmartResource<PDOConnection> sp_PDOConnection;
/* describes a column */
class PDOColumn : public ResourceData {
public:
+ DECLARE_RESOURCE_ALLOCATION_NO_SWEEP(PDOColumn);
PDOColumn();
~PDOColumn();
@@ -408,8 +409,9 @@ public:
///////////////////////////////////////////////////////////////////////////////
/* describes a bound parameter */
-class PDOBoundParam : public ResourceData {
+class PDOBoundParam : public SweepableResourceData {
public:
+ DECLARE_RESOURCE_ALLOCATION(PDOBoundParam);
PDOBoundParam();
~PDOBoundParam();
@@ -441,8 +443,9 @@ class c_pdo;
typedef SmartObject<c_pdo> sp_pdo;
/* represents a prepared statement */
-class PDOStatement : public ResourceData {
+class PDOStatement : public SweepableResourceData {
public:
+ DECLARE_RESOURCE_ALLOCATION(PDOStatement);
enum SupportedMethod {
MethodExecuter,
MethodFetcher,
diff --git a/hphp/runtime/ext/pdo_mysql.cpp b/hphp/runtime/ext/pdo_mysql.cpp
index f0452b1..9fb4386 100644
--- a/hphp/runtime/ext/pdo_mysql.cpp
+++ b/hphp/runtime/ext/pdo_mysql.cpp
@@ -47,6 +47,7 @@ public:
class PDOMySqlStatement;
class PDOMySqlConnection : public PDOConnection {
public:
+ DECLARE_RESOURCE_ALLOCATION(PDOMySqlConnection);
PDOMySqlConnection();
virtual ~PDOMySqlConnection();
virtual bool create(CArrRef options);
@@ -84,6 +85,7 @@ private:
class PDOMySqlStatement : public PDOStatement {
public:
+ DECLARE_RESOURCE_ALLOCATION(PDOMySqlStatement);
PDOMySqlStatement(PDOMySqlConnection *conn, MYSQL *server);
virtual ~PDOMySqlStatement();
@@ -231,6 +233,10 @@ PDOMySqlConnection::PDOMySqlConnection()
}
PDOMySqlConnection::~PDOMySqlConnection() {
+ sweep();
+}
+
+void PDOMySqlConnection::sweep() {
if (m_server) {
mysql_close(m_server);
}
@@ -474,7 +480,7 @@ int PDOMySqlConnection::handleError(const char *file, int line,
bool PDOMySqlConnection::preparer(const String& sql, sp_PDOStatement *stmt,
CVarRef options) {
- PDOMySqlStatement *s = new PDOMySqlStatement(this, m_server);
+ PDOMySqlStatement *s = NEWOBJ(PDOMySqlStatement)(this, m_server);
*stmt = s;
if (m_emulate_prepare) {
@@ -808,6 +814,10 @@ PDOMySqlStatement::PDOMySqlStatement(PDOMySqlConnection *conn, MYSQL *server)
}
PDOMySqlStatement::~PDOMySqlStatement() {
+ sweep();
+}
+
+void PDOMySqlStatement::sweep() {
if (m_result) {
/* free the resource */
mysql_free_result(m_result);
@@ -1001,7 +1011,7 @@ bool PDOMySqlStatement::describer(int colno) {
if (columns.empty()) {
for (int i = 0; i < column_count; i++) {
- columns.set(i, Resource(new PDOColumn()));
+ columns.set(i, Resource(NEWOBJ(PDOColumn)));
}
}
@@ -1276,7 +1286,7 @@ PDOMySql::PDOMySql() : PDODriver("mysql") {
}
PDOConnection *PDOMySql::createConnectionObject() {
- return new PDOMySqlConnection();
+ return NEWOBJ(PDOMySqlConnection);
}
///////////////////////////////////////////////////////////////////////////////
diff --git a/hphp/runtime/ext/pdo_sqlite.cpp b/hphp/runtime/ext/pdo_sqlite.cpp
index 9af121d..d3f7dda 100644
--- a/hphp/runtime/ext/pdo_sqlite.cpp
+++ b/hphp/runtime/ext/pdo_sqlite.cpp
@@ -30,6 +30,7 @@ IMPLEMENT_DEFAULT_EXTENSION_VERSION(pdo_sqlite, 1.0.1);
class PDOSqliteStatement : public PDOStatement {
public:
+ DECLARE_RESOURCE_ALLOCATION(PDOSqliteStatement);
PDOSqliteStatement(sqlite3 *db, sqlite3_stmt* stmt);
virtual ~PDOSqliteStatement();
@@ -82,16 +83,11 @@ PDOSqliteConnection::PDOSqliteConnection() : m_db(NULL) {
}
PDOSqliteConnection::~PDOSqliteConnection() {
- if (m_db) {
- sqlite3_close(m_db);
- }
- if (m_einfo.errmsg) {
- free(m_einfo.errmsg);
- }
+ sweep();
}
bool PDOSqliteConnection::create(CArrRef options) {
- String filename = data_source.substr(0,1) == ":" ? String(data_source) :
+ String filename = data_source.charAt(0) == ':' ? String(data_source) :
File::TranslatePath(data_source);
if (filename.empty()) {
throw_pdo_exception(0, Array(),
@@ -122,6 +118,12 @@ void PDOSqliteConnection::sweep() {
udf->step.asTypedValue()->m_type = KindOfNull;
udf->fini.asTypedValue()->m_type = KindOfNull;
}
+ if (m_db) {
+ sqlite3_close(m_db);
+ }
+ if (m_einfo.errmsg) {
+ free(m_einfo.errmsg);
+ }
PDOConnection::sweep();
}
@@ -188,7 +190,7 @@ bool PDOSqliteConnection::preparer(const String& sql, sp_PDOStatement *stmt,
if (sqlite3_prepare(m_db, sql.data(), sql.size(), &rawstmt, &tail)
== SQLITE_OK) {
- PDOSqliteStatement *s = new PDOSqliteStatement(m_db, rawstmt);
+ PDOSqliteStatement *s = NEWOBJ(PDOSqliteStatement)(m_db, rawstmt);
*stmt = s;
return true;
}
@@ -317,6 +319,10 @@ PDOSqliteStatement::PDOSqliteStatement(sqlite3 *db, sqlite3_stmt* stmt)
}
PDOSqliteStatement::~PDOSqliteStatement() {
+ sweep();
+}
+
+void PDOSqliteStatement::sweep() {
if (m_stmt) {
sqlite3_finalize(m_stmt);
}
@@ -406,7 +412,7 @@ bool PDOSqliteStatement::describer(int colno) {
if (columns.empty()) {
for (int i = 0; i < column_count; i++) {
- columns.set(i, Resource(new PDOColumn()));
+ columns.set(i, Resource(NEWOBJ(PDOColumn)));
}
}
@@ -614,7 +620,7 @@ PDOSqlite::PDOSqlite() : PDODriver("sqlite") {
}
PDOConnection *PDOSqlite::createConnectionObject() {
- return new PDOSqliteConnection();
+ return NEWOBJ(PDOSqliteConnection);
}
///////////////////////////////////////////////////////////////////////////////
diff --git a/hphp/runtime/ext/pdo_sqlite.h b/hphp/runtime/ext/pdo_sqlite.h
index a14d5de..1f749ae 100644
--- a/hphp/runtime/ext/pdo_sqlite.h
+++ b/hphp/runtime/ext/pdo_sqlite.h
@@ -40,10 +40,10 @@ struct PDOSqliteError {
class PDOSqliteConnection : public PDOConnection {
public:
+ DECLARE_RESOURCE_ALLOCATION(PDOSqliteConnection);
PDOSqliteConnection();
virtual ~PDOSqliteConnection();
virtual bool create(CArrRef options);
- virtual void sweep();
int handleError(const char *file, int line, PDOStatement *stmt = nullptr);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment