From 89f0f64c05733fc9d5fea2ced2e6dc7e6450d334 Mon Sep 17 00:00:00 2001 From: Alexey Kopytov Date: Wed, 17 Dec 2008 20:00:08 +0000 Subject: [PATCH] Got rid of unnecessary calloc() in db_driver.c by reusing a per-connection preallocated structure. --- sysbench/db_driver.c | 22 ++++++------------- sysbench/db_driver.h | 51 ++++++++++++++++++++++---------------------- 2 files changed, 32 insertions(+), 41 deletions(-) diff --git a/sysbench/db_driver.c b/sysbench/db_driver.c index a5ec35e..d3698e3 100644 --- a/sysbench/db_driver.c +++ b/sysbench/db_driver.c @@ -363,7 +363,7 @@ int db_bind_result(db_stmt_t *stmt, db_bind_t *results, unsigned int len) db_result_set_t *db_execute(db_stmt_t *stmt) { db_conn_t *con = stmt->connection; - db_result_set_t *rs; + db_result_set_t *rs = &con->rs; if (con == NULL || con->driver == NULL) { @@ -371,19 +371,14 @@ db_result_set_t *db_execute(db_stmt_t *stmt) return NULL; } - rs = (db_result_set_t *)calloc(1, sizeof(db_result_set_t)); - if (rs == NULL) - { - log_text(LOG_DEBUG, "ERROR: exiting db_execute(), memory allocation failure"); - return NULL; - } + memset(rs, 0, sizeof(db_result_set_t)); + rs->statement = stmt; rs->connection = con; con->db_errno = con->driver->ops.execute(stmt, rs); if (con->db_errno != SB_DB_ERROR_NONE) { - free(rs); log_text(LOG_DEBUG, "ERROR: exiting db_execute(), driver's execute method failed"); if (con->db_errno == SB_DB_ERROR_DEADLOCK) @@ -469,22 +464,18 @@ db_row_t *db_fetch_row(db_result_set_t *rs) db_result_set_t *db_query(db_conn_t *con, const char *query) { - db_result_set_t *rs; + db_result_set_t *rs = &con->rs; if (con->driver == NULL) return NULL; - rs = (db_result_set_t *)calloc(1, sizeof(db_result_set_t)); - if (rs == NULL) - return NULL; + memset(rs, 0, sizeof(db_result_set_t)); + rs->connection = con; con->db_errno = con->driver->ops.query(con, query, rs); if (con->db_errno != SB_DB_ERROR_NONE) - { - free(rs); return NULL; - } return rs; } @@ -505,7 +496,6 @@ int db_free_results(db_result_set_t *rs) if (rs->row != NULL) db_free_row(rs->row); - free(rs); return rc; } diff --git a/sysbench/db_driver.h b/sysbench/db_driver.h index b4999eb..30e6204 100644 --- a/sysbench/db_driver.h +++ b/sysbench/db_driver.h @@ -177,25 +177,38 @@ typedef enum { DB_CONN_TYPE_MYSQL } db_conn_type_t; +/* Result set definition */ + +typedef struct db_result_set +{ + struct db_conn *connection; /* Connection which this result set belongs to */ + struct db_stmt *statement; /* Statement for this result set (if any) */ + + struct db_row *row; /* Last row fetched by db_fetch_row */ + void *ptr; /* Pointer to driver-specific data */ + unsigned long long nrows; /* Number of rows in a result set */ +} db_result_set_t; + /* Database connection structure */ typedef struct db_conn { - db_driver_t *driver; /* DB driver for this connection */ - db_conn_type_t type; - void *ptr; - db_error_t db_errno; + db_driver_t *driver; /* DB driver for this connection */ + db_conn_type_t type; + void *ptr; + db_error_t db_errno; /* Internal fields */ - char bulk_supported; /* 1, if multi-row inserts are supported by the driver */ - unsigned int bulk_cnt; /* Current number of rows in bulk insert buffer */ - char * bulk_buffer; /* Bulk insert query buffer */ - unsigned int bulk_buflen; /* Current length of bulk_buffer */ - unsigned int bulk_ptr; /* Current position in bulk_buffer */ - unsigned int bulk_ptr_orig; /* Save value of bulk_ptr */ - unsigned int bulk_commit_cnt; /* Current value of uncommitted rows */ - unsigned int bulk_commit_max; /* Maximum value of uncommitted rows */ - int thread_id; /* Assiciated thread id (required to collect per-thread stats */ + char bulk_supported; /* 1, if multi-row inserts are supported by the driver */ + unsigned int bulk_cnt; /* Current number of rows in bulk insert buffer */ + char * bulk_buffer; /* Bulk insert query buffer */ + unsigned int bulk_buflen; /* Current length of bulk_buffer */ + unsigned int bulk_ptr; /* Current position in bulk_buffer */ + unsigned int bulk_ptr_orig; /* Save value of bulk_ptr */ + unsigned int bulk_commit_cnt; /* Current value of uncommitted rows */ + unsigned int bulk_commit_max; /* Maximum value of uncommitted rows */ + int thread_id; /* Assiciated thread id (required to collect per-thread stats */ + db_result_set_t rs; /* Result set */ } db_conn_t; typedef enum { @@ -220,18 +233,6 @@ typedef struct db_stmt void *ptr; /* Pointer to driver-specific data structure */ } db_stmt_t; -/* Result set definition */ - -typedef struct db_result_set -{ - db_conn_t *connection; /* Connection which this result set belongs to */ - db_stmt_t *statement; /* Statement for this result set (if any) */ - - struct db_row *row; /* Last row fetched by db_fetch_row */ - void *ptr; /* Pointer to driver-specific data */ - unsigned long long nrows; /* Number of rows in a result set */ -} db_result_set_t; - /* Result set row definition */ typedef struct db_row