Subject: Re: Making a common API for cpu frequency drivers
To: Juan RP <juan@xtrarom.org>
From: Jason Thorpe <thorpej@shagadelic.org>
List: tech-kern
Date: 09/01/2006 14:48:25
On Sep 1, 2006, at 2:19 PM, Juan RP wrote:

>> Please make sure the dictionary key names have meaningful namespace
>> prefixes to them.
>
> Does that mean that the key of the dictionary must not have names like
> cpu0?

I would say:

void
cpu_attach(device_t parent, device_t self, void *aux)
{
	prop_array_t array;

	...

	array = /* build the supported frequencies array */;

	prop_dictionary_set(device_properties(self),
			    "cpu-supported-clock-frequencies", array);
	prop_object_release(array);

	...
}

Make sense?  Or maybe I'm not understanding how you want to group the  
information together.

>
>> Glad someone is taking in interest in cleaning this all up!  Yay
>> sysmon!  Yay proplib! :-)
>
> I'm starting to love it... I've attached the test code to create the  
> template.
> <prop_dictionary.c>

A few comments about your test program:

  --- snip ---

	array = prop_array_create_with_capacity(MAX_FREQS);

  --- snip ---

You don't have to specify the capacity -- the array with automatically  
expand as necessary.  That said, if you know that it will contain  
exactly two entries and no more, then create with that capacity.  But  
your example creates an array with a larger capacity than entries that  
you add.



  --- snip ---

	if (array == NULL ||
	    !prop_dictionary_set(dict, tm->device, array))
		return 1;

  --- snip ---

You need to prop_object_release() the array, like so:

	if (array == NULL ||
	    !prop_dictionary_set(dict, tm->device, array)) {
		prop_object_release(array);
		return 1;
	}
	prop_object_release(array);

Reasons:
1- On failure, you need to free the array.

2- On success, the dictionary has retained it (bumped the ref count),  
and you need to release your reference now that you're done with the  
array, else you'll leak the reference and thus the memory.


And finally, construct the array BEFORE storing it in the dictionary.   
That way the "I am done with the array" is more clear, and you also  
avoid having partially constructed data visible in the dictionary.

-- thorpej