From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1Me3Vy-0000bj-PV for qemu-devel@nongnu.org; Thu, 20 Aug 2009 05:00:58 -0400 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1Me3Vt-0000bW-UD for qemu-devel@nongnu.org; Thu, 20 Aug 2009 05:00:58 -0400 Received: from [199.232.76.173] (port=34957 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Me3Vt-0000bT-Ko for qemu-devel@nongnu.org; Thu, 20 Aug 2009 05:00:53 -0400 Received: from ey-out-1920.google.com ([74.125.78.146]:60722) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1Me3Vt-0004vc-2t for qemu-devel@nongnu.org; Thu, 20 Aug 2009 05:00:53 -0400 Received: by ey-out-1920.google.com with SMTP id 5so967666eyb.4 for ; Thu, 20 Aug 2009 02:00:51 -0700 (PDT) Sender: Paolo Bonzini Message-ID: <4A8D10BE.8030804@gnu.org> Date: Thu, 20 Aug 2009 11:00:46 +0200 From: Paolo Bonzini MIME-Version: 1.0 References: <1250723280-3509-1-git-send-email-lcapitulino@redhat.com> <1250723280-3509-5-git-send-email-lcapitulino@redhat.com> In-Reply-To: <1250723280-3509-5-git-send-email-lcapitulino@redhat.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Subject: [Qemu-devel] Re: [PATCH 04/29] Introduce QDict List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Luiz Capitulino Cc: aliguori@us.ibm.com, qemu-devel@nongnu.org, avi@redhat.com > + > +/** > + * tdb_hash(): based on the hash agorithm from gdbm, via tdb > + * (from module-init-tools) > + */ > +static unsigned int tdb_hash(const char *name) > +{ > + unsigned value; /* Used to compute the hash value. */ > + unsigned i; /* Used to cycle through random values. */ > + > + /* Set the initial value from the key size. */ > + for (value = 0x238F13AF * strlen(name), i=0; name[i]; i++) > + value = (value + (((const unsigned char *)name)[i]<< (i*5 % 24))); This is a _bad_ hash function, especially because 5 is exactly the bit that is flipped between lowercase and uppercase. So "aa" would collide with "Ab". Besides, since the function is additive, you can initialize the value to 0 and add "0x238F13AF * i" at the end (but this is just showing off...). Here is a very simple but good hash function: uintptr_t hashVal = 1497032417; /* arbitrary value */ while (len--) { hashVal += *str++; hashVal += (hashVal << 10); hashVal ^= (hashVal >> 6); } return 1103515243 * value; > +/** > + * qdict_add(): Add a new object into the dictionary > + * > + * Add the pair 'key:value' into qdict. Does nothing if 'key' already > + * exist. > + * > + * NOTE: this function 'steals' a reference to 'value' Using my proposed nomenclature, "ownership of value is transferred to the QDict". > +void qdict_add_qint(QDict *qdict, const char *key, QInt *qi) I agree with Avi that these do not add much. If anything, make wrappers that accept C datatypes. > +/** > + * qdict_get(): Lookup for a given 'key' > + * > + * Return borrowed reference to QObject if 'key' exists, > + * NULL otherwise. Return a weak reference to the QObject associated with 'key' if the key is present in the dictionary, NULL otherwise. > +/** > + * qdict_del(): Delete a 'key:value' pair from the dictionary > + * > + * This will destroy all data allocated by this entry. ... decrementing the reference count of the QObject associated to 'key'. Paolo