Skip to content

Instantly share code, notes, and snippets.

@ptarjan
Created February 3, 2014 06:08
Show Gist options
  • Save ptarjan/8779502 to your computer and use it in GitHub Desktop.
Save ptarjan/8779502 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..4f06ed2 100644
--- a/hphp/runtime/ext/pdo_driver.cpp
+++ b/hphp/runtime/ext/pdo_driver.cpp
@@ -73,7 +73,9 @@ PDOConnection::~PDOConnection() {
}
void PDOConnection::sweep() {
+ using std::string;
assert(!is_persistent);
+ serialized_def_stmt_ctor_args.~string();
def_stmt_ctor_args.asTypedValue()->m_type = KindOfNull;
delete this;
}
@@ -180,6 +182,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 +209,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..9670933 100644
--- a/hphp/runtime/ext/pdo_driver.h
+++ b/hphp/runtime/ext/pdo_driver.h
@@ -237,6 +237,8 @@ typedef SmartResource<PDOStatement> sp_PDOStatement;
/* represents a connection to a database */
class PDOConnection : public SweepableResourceData {
public:
+ // This is special and doesn't use DECLARE_RESOURCE_ALLOCATION because it has
+ // to live across requests.
static const char *PersistentKey;
enum SupportedMethod {
@@ -320,8 +322,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 +358,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,11 +368,11 @@ 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;
Variant def_stmt_ctor_args;
@@ -391,6 +393,7 @@ typedef SmartResource<PDOConnection> sp_PDOConnection;
/* describes a column */
class PDOColumn : public ResourceData {
public:
+ DECLARE_RESOURCE_ALLOCATION_NO_SWEEP(PDOColumn);
PDOColumn();
~PDOColumn();
@@ -408,8 +411,9 @@ public:
///////////////////////////////////////////////////////////////////////////////
/* describes a bound parameter */
-class PDOBoundParam : public ResourceData {
+class PDOBoundParam : public SweepableResourceData {
public:
+ DECLARE_RESOURCE_ALLOCATION(PDOBoundParam);
PDOBoundParam();
~PDOBoundParam();
@@ -441,8 +445,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..0180e63 100644
--- a/hphp/runtime/ext/pdo_mysql.cpp
+++ b/hphp/runtime/ext/pdo_mysql.cpp
@@ -50,6 +50,7 @@ public:
PDOMySqlConnection();
virtual ~PDOMySqlConnection();
virtual bool create(CArrRef options);
+ virtual void sweep();
int handleError(const char *file, int line, PDOMySqlStatement *stmt = NULL);
@@ -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,6 +1286,7 @@ PDOMySql::PDOMySql() : PDODriver("mysql") {
}
PDOConnection *PDOMySql::createConnectionObject() {
+ // Doesn't use NEWOBJ because PDOConnection is malloced
return new PDOMySqlConnection();
}
diff --git a/hphp/runtime/ext/pdo_sqlite.cpp b/hphp/runtime/ext/pdo_sqlite.cpp
index 9af121d..fea7818 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,6 +620,7 @@ PDOSqlite::PDOSqlite() : PDODriver("sqlite") {
}
PDOConnection *PDOSqlite::createConnectionObject() {
+ // Doesn't use NEWOBJ because PDOConnection is malloced
return new PDOSqliteConnection();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment