Create a function to determine the table's row size and use it.

Fix a memory leak in the table code.
This commit is contained in:
Mike McCormack 2005-09-26 13:25:48 +00:00 committed by Alexandre Julliard
parent f0f179dd0d
commit fbbb32d6d1
1 changed files with 21 additions and 11 deletions

View File

@ -62,6 +62,7 @@ static UINT table_get_column_info( MSIDATABASE *db, LPCWSTR name,
MSICOLUMNINFO **pcols, UINT *pcount ); MSICOLUMNINFO **pcols, UINT *pcount );
static UINT get_tablecolumns( MSIDATABASE *db, static UINT get_tablecolumns( MSIDATABASE *db,
LPCWSTR szTableName, MSICOLUMNINFO *colinfo, UINT *sz); LPCWSTR szTableName, MSICOLUMNINFO *colinfo, UINT *sz);
static void msi_free_colinfo( MSICOLUMNINFO *colinfo, UINT count );
static inline UINT bytes_per_column( const MSICOLUMNINFO *col ) static inline UINT bytes_per_column( const MSICOLUMNINFO *col )
{ {
@ -404,6 +405,14 @@ static void free_table( MSITABLE *table )
msi_free( table ); msi_free( table );
} }
static UINT msi_table_get_row_size( const MSICOLUMNINFO *cols, UINT count )
{
const MSICOLUMNINFO *last_col = &cols[count-1];
if (!count)
return 0;
return last_col->offset + bytes_per_column( last_col );
}
/* add this table to the list of cached tables in the database */ /* add this table to the list of cached tables in the database */
static MSITABLE *read_table_from_storage( IStorage *stg, LPCWSTR name, static MSITABLE *read_table_from_storage( IStorage *stg, LPCWSTR name,
const MSICOLUMNINFO *cols, UINT num_cols ) const MSICOLUMNINFO *cols, UINT num_cols )
@ -411,7 +420,6 @@ static MSITABLE *read_table_from_storage( IStorage *stg, LPCWSTR name,
MSITABLE *t; MSITABLE *t;
USHORT *rawdata = NULL; USHORT *rawdata = NULL;
UINT rawsize = 0, i, j, row_size = 0; UINT rawsize = 0, i, j, row_size = 0;
const MSICOLUMNINFO *last_col;
TRACE("%s\n",debugstr_w(name)); TRACE("%s\n",debugstr_w(name));
@ -420,8 +428,7 @@ static MSITABLE *read_table_from_storage( IStorage *stg, LPCWSTR name,
if( !t ) if( !t )
return t; return t;
last_col = &cols[num_cols-1]; row_size = msi_table_get_row_size( cols, num_cols );
row_size = last_col->offset + bytes_per_column( last_col );
t->row_count = 0; t->row_count = 0;
t->data = NULL; t->data = NULL;
@ -559,7 +566,7 @@ static UINT save_table( MSIDATABASE *db, MSITABLE *t )
{ {
USHORT *rawdata = NULL, *p; USHORT *rawdata = NULL, *p;
UINT rawsize, r, i, j, row_size, num_cols = 0; UINT rawsize, r, i, j, row_size, num_cols = 0;
MSICOLUMNINFO *cols, *last_col; MSICOLUMNINFO *cols = NULL;
TRACE("Saving %s\n", debugstr_w( t->name ) ); TRACE("Saving %s\n", debugstr_w( t->name ) );
@ -567,13 +574,15 @@ static UINT save_table( MSIDATABASE *db, MSITABLE *t )
if( r != ERROR_SUCCESS ) if( r != ERROR_SUCCESS )
return r; return r;
last_col = &cols[num_cols-1]; row_size = msi_table_get_row_size( cols, num_cols );
row_size = last_col->offset + bytes_per_column( last_col );
rawsize = t->row_count * row_size; rawsize = t->row_count * row_size;
rawdata = msi_alloc_zero( rawsize ); rawdata = msi_alloc_zero( rawsize );
if( !rawdata ) if( !rawdata )
return ERROR_NOT_ENOUGH_MEMORY; {
r = ERROR_NOT_ENOUGH_MEMORY;
goto err;
}
p = rawdata; p = rawdata;
for( i=0; i<num_cols; i++ ) for( i=0; i<num_cols; i++ )
@ -591,6 +600,9 @@ static UINT save_table( MSIDATABASE *db, MSITABLE *t )
TRACE("writing %d bytes\n", rawsize); TRACE("writing %d bytes\n", rawsize);
r = write_stream_data( db->storage, t->name, rawdata, rawsize ); r = write_stream_data( db->storage, t->name, rawdata, rawsize );
err:
msi_free_colinfo( cols, num_cols );
msi_free( cols );
msi_free( rawdata ); msi_free( rawdata );
return r; return r;
@ -1436,7 +1448,7 @@ UINT TABLE_CreateView( MSIDATABASE *db, LPCWSTR name, MSIVIEW **view )
{ {
MSITABLEVIEW *tv ; MSITABLEVIEW *tv ;
UINT r, sz, column_count; UINT r, sz, column_count;
MSICOLUMNINFO *columns, *last_col; MSICOLUMNINFO *columns;
TRACE("%p %s %p\n", db, debugstr_w(name), view ); TRACE("%p %s %p\n", db, debugstr_w(name), view );
@ -1474,15 +1486,13 @@ UINT TABLE_CreateView( MSIDATABASE *db, LPCWSTR name, MSIVIEW **view )
TRACE("Table has %d columns\n", column_count); TRACE("Table has %d columns\n", column_count);
last_col = &columns[column_count-1];
/* fill the structure */ /* fill the structure */
tv->view.ops = &table_ops; tv->view.ops = &table_ops;
tv->db = db; tv->db = db;
tv->columns = columns; tv->columns = columns;
tv->num_cols = column_count; tv->num_cols = column_count;
tv->table = NULL; tv->table = NULL;
tv->row_size = last_col->offset + bytes_per_column( last_col ); tv->row_size = msi_table_get_row_size( columns, column_count );
TRACE("%s one row is %d bytes\n", debugstr_w(name), tv->row_size ); TRACE("%s one row is %d bytes\n", debugstr_w(name), tv->row_size );