From 44707ad2e00a91f459e80efbe8f362b5853b0a62 Mon Sep 17 00:00:00 2001 From: Gerald Carter Date: Mon, 29 Aug 2005 14:55:40 +0000 Subject: r9739: conver the reg_objects (REGSUBKEY_CTR & REGVAL_CTR) to use the new talloc() features: Note that the REGSUB_CTR and REGVAL_CTR objects *must* be talloc()'d since the methods use the object pointer as the talloc context for internal private data. There is no longer a regXXX_ctr_intit() and regXXX_ctr_destroy() pair of functions. Simply TALLOC_ZERO_P() and TALLOC_FREE() the object. Also had to convert the printer_info_2->NT_PRINTER_DATA field to be talloc()'d as well. This is just a stop on the road to cleaning up the printer memory management. (This used to be commit ef721333ab9639cb5346067497e99fbd0d4425dd) --- source3/registry/reg_db.c | 78 +++++++++++++++++++++++------------- source3/registry/reg_frontend.c | 58 ++++++++++++++++----------- source3/registry/reg_objects.c | 88 ++++++++++------------------------------- source3/registry/reg_printing.c | 20 +++++----- 4 files changed, 116 insertions(+), 128 deletions(-) (limited to 'source3/registry') diff --git a/source3/registry/reg_db.c b/source3/registry/reg_db.c index 64a863ceac..19f9abd80d 100644 --- a/source3/registry/reg_db.c +++ b/source3/registry/reg_db.c @@ -31,7 +31,7 @@ static TDB_CONTEXT *tdb_reg; /* List the deepest path into the registry. All part components will be created.*/ -/* If you want to have a part of the path controlled by the tdb abd part by +/* If you want to have a part of the path controlled by the tdb and part by a virtual registry db (e.g. printing), then you have to list the deepest path. For example,"HKLM/SOFTWARE/Microsoft/Windows NT/CurrentVersion/Print" allows the reg_db backend to handle everything up to @@ -89,12 +89,20 @@ static BOOL init_registry_data( void ) { pstring path, base, remaining; fstring keyname, subkeyname; - REGSUBKEY_CTR subkeys; - REGVAL_CTR values; + REGSUBKEY_CTR *subkeys; + REGVAL_CTR *values; + uint32 *ctx; int i; const char *p, *p2; UNISTR2 data; + /* create a new top level talloc ctx */ + + if ( !(ctx = TALLOC_P( NULL, uint32 )) ) { + DEBUG(0,("init_registry_data: top level talloc() failure!\n")); + return False; + } + /* loop over all of the predefined paths and add each component */ for ( i=0; builtin_registry_paths[i] != NULL; i++ ) { @@ -131,27 +139,33 @@ static BOOL init_registry_data( void ) we are about to update the record. We just want any subkeys already present */ - regsubkey_ctr_init( &subkeys ); - - regdb_fetch_keys( base, &subkeys ); + if ( !(subkeys = TALLOC_ZERO_P( ctx, REGSUBKEY_CTR )) ) { + DEBUG(0,("talloc() failure!\n")); + return False; + } + + regdb_fetch_keys( base, subkeys ); if ( *subkeyname ) - regsubkey_ctr_addkey( &subkeys, subkeyname ); - if ( !regdb_store_keys( base, &subkeys )) + regsubkey_ctr_addkey( subkeys, subkeyname ); + if ( !regdb_store_keys( base, subkeys )) return False; - regsubkey_ctr_destroy( &subkeys ); + TALLOC_FREE( subkeys ); } } /* loop over all of the predefined values and add each component */ for ( i=0; builtin_registry_values[i].path != NULL; i++ ) { - regval_ctr_init( &values ); - - regdb_fetch_values( builtin_registry_values[i].path, &values ); + if ( !(values = TALLOC_ZERO_P( ctx, REGVAL_CTR )) ) { + DEBUG(0,("talloc() failure!\n")); + return False; + } + + regdb_fetch_values( builtin_registry_values[i].path, values ); switch( builtin_registry_values[i].type ) { case REG_DWORD: - regval_ctr_addvalue( &values, + regval_ctr_addvalue( values, builtin_registry_values[i].valuename, REG_DWORD, (char*)&builtin_registry_values[i].data.dw_value, @@ -160,7 +174,7 @@ static BOOL init_registry_data( void ) case REG_SZ: init_unistr2( &data, builtin_registry_values[i].data.string, UNI_STR_TERMINATE); - regval_ctr_addvalue( &values, + regval_ctr_addvalue( values, builtin_registry_values[i].valuename, REG_SZ, (char*)data.buffer, @@ -171,9 +185,9 @@ static BOOL init_registry_data( void ) DEBUG(0,("init_registry_data: invalid value type in builtin_registry_values [%d]\n", builtin_registry_values[i].type)); } - regdb_store_values( builtin_registry_values[i].path, &values ); + regdb_store_values( builtin_registry_values[i].path, values ); - regval_ctr_destroy( &values ); + TALLOC_FREE( values ); } return True; @@ -297,13 +311,17 @@ BOOL regdb_store_keys( const char *key, REGSUBKEY_CTR *ctr ) { int num_subkeys, i; pstring path; - REGSUBKEY_CTR subkeys, old_subkeys; + REGSUBKEY_CTR *subkeys, *old_subkeys; char *oldkeyname; /* fetch a list of the old subkeys so we can determine if any were deleted */ - regsubkey_ctr_init( &old_subkeys ); - regdb_fetch_keys( key, &old_subkeys ); + if ( !(old_subkeys = TALLOC_ZERO_P( ctr, REGSUBKEY_CTR )) ) { + DEBUG(0,("regdb_store_keys: talloc() failure!\n")); + return False; + } + + regdb_fetch_keys( key, old_subkeys ); /* store the subkey list for the parent */ @@ -314,9 +332,9 @@ BOOL regdb_store_keys( const char *key, REGSUBKEY_CTR *ctr ) /* now delete removed keys */ - num_subkeys = regsubkey_ctr_numkeys( &old_subkeys ); + num_subkeys = regsubkey_ctr_numkeys( old_subkeys ); for ( i=0; iname)); - if ( !ctr_init ) { + if ( !ctr ) { DEBUG(8,("fetch_reg_keys_specific: Initializing cache of subkeys for [%s]\n", key->name)); - regsubkey_ctr_init( &ctr ); + + if ( !(ctr = TALLOC_ZERO_P( NULL, REGSUBKEY_CTR )) ) { + DEBUG(0,("fetch_reg_keys_specific: talloc() failed!\n")); + return False; + } pstrcpy( save_path, key->name ); - if ( fetch_reg_keys( key, &ctr) == -1 ) + if ( fetch_reg_keys( key, ctr) == -1 ) return False; - ctr_init = True; } /* clear the cache when key_index == 0 or the path has changed */ else if ( !key_index || StrCaseCmp( save_path, key->name) ) { DEBUG(8,("fetch_reg_keys_specific: Updating cache of subkeys for [%s]\n", key->name)); - regsubkey_ctr_destroy( &ctr ); - regsubkey_ctr_init( &ctr ); + TALLOC_FREE( ctr ); + + if ( !(ctr = TALLOC_ZERO_P( NULL, REGSUBKEY_CTR )) ) { + DEBUG(0,("fetch_reg_keys_specific: talloc() failed!\n")); + return False; + } pstrcpy( save_path, key->name ); - if ( fetch_reg_keys( key, &ctr) == -1 ) + if ( fetch_reg_keys( key, ctr) == -1 ) return False; } - if ( !(s = regsubkey_ctr_specific_key( &ctr, key_index )) ) + if ( !(s = regsubkey_ctr_specific_key( ctr, key_index )) ) return False; *subkey = SMB_STRDUP( s ); @@ -198,42 +206,46 @@ int fetch_reg_values( REGISTRY_KEY *key, REGVAL_CTR *val ) BOOL fetch_reg_values_specific( REGISTRY_KEY *key, REGISTRY_VALUE **val, uint32 val_index ) { - static REGVAL_CTR ctr; + static REGVAL_CTR *ctr = NULL; static pstring save_path; - static BOOL ctr_init = False; REGISTRY_VALUE *v; *val = NULL; /* simple caching for performance; very basic heuristic */ - if ( !ctr_init ) { + if ( !ctr ) { DEBUG(8,("fetch_reg_values_specific: Initializing cache of values for [%s]\n", key->name)); - regval_ctr_init( &ctr ); - + if ( !(ctr = TALLOC_ZERO_P( NULL, REGVAL_CTR )) ) { + DEBUG(0,("fetch_reg_values_specific: talloc() failed!\n")); + return False; + } + pstrcpy( save_path, key->name ); - if ( fetch_reg_values( key, &ctr) == -1 ) + if ( fetch_reg_values( key, ctr) == -1 ) return False; - - ctr_init = True; } /* clear the cache when val_index == 0 or the path has changed */ else if ( !val_index || !strequal(save_path, key->name) ) { DEBUG(8,("fetch_reg_values_specific: Updating cache of values for [%s]\n", key->name)); - regval_ctr_destroy( &ctr ); - regval_ctr_init( &ctr ); - + TALLOC_FREE( ctr ); + + if ( !(ctr = TALLOC_ZERO_P( NULL, REGVAL_CTR )) ) { + DEBUG(0,("fetch_reg_values_specific: talloc() failed!\n")); + return False; + } + pstrcpy( save_path, key->name ); - if ( fetch_reg_values( key, &ctr) == -1 ) + if ( fetch_reg_values( key, ctr) == -1 ) return False; } - if ( !(v = regval_ctr_specific_value( &ctr, val_index )) ) + if ( !(v = regval_ctr_specific_value( ctr, val_index )) ) return False; *val = dup_registry_value( v ); diff --git a/source3/registry/reg_objects.c b/source3/registry/reg_objects.c index b5753fc688..70410a6740 100644 --- a/source3/registry/reg_objects.c +++ b/source3/registry/reg_objects.c @@ -25,17 +25,17 @@ #undef DBGC_CLASS #define DBGC_CLASS DBGC_RPC_SRV +/********************************************************************** -/*********************************************************************** - Init the talloc context held by a REGSUBKEY_CTR structure - This now zero's the structure - **********************************************************************/ + Note that the REGSUB_CTR and REGVAL_CTR objects *must* be talloc()'d + since the methods use the object pointer as the talloc context for + internal private data. -void regsubkey_ctr_init( REGSUBKEY_CTR *ctr ) -{ - ZERO_STRUCTP( ctr ); - ctr->ctx = talloc_init("regsubkey_ctr_init for ctr %p", ctr); -} + There is no longer a regXXX_ctr_intit() and regXXX_ctr_destroy() + pair of functions. Simply TALLOC_ZERO_P() and TALLOC_FREE() the + object. + + **********************************************************************/ /*********************************************************************** Add a new key to the array @@ -56,16 +56,16 @@ int regsubkey_ctr_addkey( REGSUBKEY_CTR *ctr, const char *keyname ) /* allocate a space for the char* in the array */ if ( ctr->subkeys == 0 ) - ctr->subkeys = TALLOC_P( ctr->ctx, char *); + ctr->subkeys = TALLOC_P( ctr, char *); else { - pp = TALLOC_REALLOC_ARRAY( ctr->ctx, ctr->subkeys, char *, ctr->num_subkeys+1); + pp = TALLOC_REALLOC_ARRAY( ctr, ctr->subkeys, char *, ctr->num_subkeys+1); if ( pp ) ctr->subkeys = pp; } /* allocate the string and save it in the array */ - ctr->subkeys[ctr->num_subkeys] = talloc_strdup( ctr->ctx, keyname ); + ctr->subkeys[ctr->num_subkeys] = talloc_strdup( ctr, keyname ); ctr->num_subkeys++; return ctr->num_subkeys; @@ -137,34 +137,10 @@ char* regsubkey_ctr_specific_key( REGSUBKEY_CTR *ctr, uint32 key_index ) return ctr->subkeys[key_index]; } -/*********************************************************************** - free memory held by a REGSUBKEY_CTR structure - **********************************************************************/ - -void regsubkey_ctr_destroy( REGSUBKEY_CTR *ctr ) -{ - if ( ctr ) { - talloc_destroy( ctr->ctx ); - ZERO_STRUCTP( ctr ); - } -} - - /* * Utility functions for REGVAL_CTR */ -/*********************************************************************** - Init the talloc context held by a REGSUBKEY_CTR structure - This now zero's the structure - **********************************************************************/ - -void regval_ctr_init( REGVAL_CTR *ctr ) -{ - ZERO_STRUCTP( ctr ); - ctr->ctx = talloc_init("regval_ctr_init for ctr %p", ctr); -} - /*********************************************************************** How many keys does the container hold ? **********************************************************************/ @@ -271,17 +247,6 @@ REGISTRY_VALUE* regval_ctr_specific_value( REGVAL_CTR *ctr, uint32 idx ) return ctr->values[idx]; } -/*********************************************************************** - Retrive the TALLOC_CTX associated with a REGISTRY_VALUE - **********************************************************************/ - -TALLOC_CTX* regval_ctr_getctx( REGVAL_CTR *val ) -{ - if ( !val ) - return NULL; - - return val->ctx; } - /*********************************************************************** Check for the existance of a value **********************************************************************/ @@ -316,22 +281,22 @@ int regval_ctr_addvalue( REGVAL_CTR *ctr, const char *name, uint16 type, /* allocate a slot in the array of pointers */ if ( ctr->num_values == 0 ) - ctr->values = TALLOC_P( ctr->ctx, REGISTRY_VALUE *); + ctr->values = TALLOC_P( ctr, REGISTRY_VALUE *); else { - ppreg = TALLOC_REALLOC_ARRAY( ctr->ctx, ctr->values, REGISTRY_VALUE *, ctr->num_values+1 ); + ppreg = TALLOC_REALLOC_ARRAY( ctr, ctr->values, REGISTRY_VALUE *, ctr->num_values+1 ); if ( ppreg ) ctr->values = ppreg; } /* allocate a new value and store the pointer in the arrya */ - ctr->values[ctr->num_values] = TALLOC_P( ctr->ctx, REGISTRY_VALUE); + ctr->values[ctr->num_values] = TALLOC_P( ctr, REGISTRY_VALUE); /* init the value */ fstrcpy( ctr->values[ctr->num_values]->valuename, name ); ctr->values[ctr->num_values]->type = type; - ctr->values[ctr->num_values]->data_p = TALLOC_MEMDUP( ctr->ctx, data_p, size ); + ctr->values[ctr->num_values]->data_p = TALLOC_MEMDUP( ctr, data_p, size ); ctr->values[ctr->num_values]->size = size; ctr->num_values++; @@ -351,22 +316,22 @@ int regval_ctr_copyvalue( REGVAL_CTR *ctr, REGISTRY_VALUE *val ) /* allocate a slot in the array of pointers */ if ( ctr->num_values == 0 ) - ctr->values = TALLOC_P( ctr->ctx, REGISTRY_VALUE *); + ctr->values = TALLOC_P( ctr, REGISTRY_VALUE *); else { - ppreg = TALLOC_REALLOC_ARRAY( ctr->ctx, ctr->values, REGISTRY_VALUE *, ctr->num_values+1 ); + ppreg = TALLOC_REALLOC_ARRAY( ctr, ctr->values, REGISTRY_VALUE *, ctr->num_values+1 ); if ( ppreg ) ctr->values = ppreg; } /* allocate a new value and store the pointer in the arrya */ - ctr->values[ctr->num_values] = TALLOC_P( ctr->ctx, REGISTRY_VALUE); + ctr->values[ctr->num_values] = TALLOC_P( ctr, REGISTRY_VALUE); /* init the value */ fstrcpy( ctr->values[ctr->num_values]->valuename, val->valuename ); ctr->values[ctr->num_values]->type = val->type; - ctr->values[ctr->num_values]->data_p = TALLOC_MEMDUP( ctr->ctx, val->data_p, val->size ); + ctr->values[ctr->num_values]->data_p = TALLOC_MEMDUP( ctr, val->data_p, val->size ); ctr->values[ctr->num_values]->size = val->size; ctr->num_values++; } @@ -420,16 +385,3 @@ REGISTRY_VALUE* regval_ctr_getvalue( REGVAL_CTR *ctr, const char *name ) return NULL; } -/*********************************************************************** - free memory held by a REGVAL_CTR structure - **********************************************************************/ - -void regval_ctr_destroy( REGVAL_CTR *ctr ) -{ - if ( ctr ) { - talloc_destroy( ctr->ctx ); - ZERO_STRUCTP( ctr ); - } -} - - diff --git a/source3/registry/reg_printing.c b/source3/registry/reg_printing.c index c1f7d3925f..d0f7daa926 100644 --- a/source3/registry/reg_printing.c +++ b/source3/registry/reg_printing.c @@ -261,7 +261,7 @@ static int key_printers_fetch_keys( const char *key, REGSUBKEY_CTR *subkeys ) return -1; } - num_subkeys = get_printer_subkeys( &printer->info_2->data, printerdatakey?printerdatakey:"", &subkey_names ); + num_subkeys = get_printer_subkeys( printer->info_2->data, printerdatakey?printerdatakey:"", &subkey_names ); for ( i=0; iinfo_2->data, "", &existing_subkeys ); + num_existing_keys = get_printer_subkeys( printer->info_2->data, "", &existing_subkeys ); for ( i=0; iinfo_2->data, existing_subkeys[i] ); + delete_printer_key( printer->info_2->data, existing_subkeys[i] ); } } @@ -362,10 +362,10 @@ static BOOL key_printers_store_keys( const char *key, REGSUBKEY_CTR *subkeys ) for ( i=0; iinfo_2->data, subkeyname) == -1 ) { + if ( lookup_printerkey(printer->info_2->data, subkeyname) == -1 ) { DEBUG(5,("key_printers_store_keys: adding key %s\n", existing_subkeys[i])); - if ( add_new_printer_key( &printer->info_2->data, subkeyname ) == -1 ) + if ( add_new_printer_key( printer->info_2->data, subkeyname ) == -1 ) return False; } } @@ -445,7 +445,7 @@ static void fill_in_printer_values( NT_PRINTER_INFO_LEVEL_2 *info2, REGVAL_CTR * /* use a prs_struct for converting the devmode and security descriptor to REG_BINARY */ - prs_init( &prs, MAX_PDU_FRAG_LEN, regval_ctr_getctx(values), MARSHALL); + prs_init( &prs, MAX_PDU_FRAG_LEN, values, MARSHALL); /* stream the device mode */ @@ -508,7 +508,7 @@ static int key_printers_fetch_values( const char *key, REGVAL_CTR *values ) /* iterate over all printer data keys and fill the regval container */ - p_data = &printer->info_2->data; + p_data = printer->info_2->data; if ( (key_index = lookup_printerkey( p_data, printerdatakey )) == -1 ) { /* failure....should never happen if the client has a valid open handle first */ DEBUG(10,("key_printers_fetch_values: Unknown keyname [%s]\n", printerdatakey)); @@ -517,9 +517,9 @@ static int key_printers_fetch_values( const char *key, REGVAL_CTR *values ) return -1; } - num_values = regval_ctr_numvals( &p_data->keys[key_index].values ); + num_values = regval_ctr_numvals( p_data->keys[key_index].values ); for ( i=0; ikeys[key_index].values, i) ); + regval_ctr_copyvalue( values, regval_ctr_specific_value(p_data->keys[key_index].values, i) ); done: @@ -702,7 +702,7 @@ static BOOL key_printers_store_values( const char *key, REGVAL_CTR *values ) int i; REGISTRY_VALUE *val; - delete_printer_key( &printer->info_2->data, keyname ); + delete_printer_key( printer->info_2->data, keyname ); /* deal with any subkeys */ for ( i=0; i