From: Christoph Egger Subject: [PATCH][TOOLS] xenstore: cleanups Date: Tue, 10 Jun 2008 13:15:28 +0200 Message-ID: <200806101315.28951.Christoph.Egger@amd.com> Mime-Version: 1.0 Content-Type: Multipart/Mixed; boundary="Boundary-00=_QJmTI5fxf8+MUA/" Return-path: List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xensource.com Errors-To: xen-devel-bounces@lists.xensource.com To: xen-devel@lists.xensource.com List-Id: xen-devel@lists.xenproject.org --Boundary-00=_QJmTI5fxf8+MUA/ Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Hi! Attached patch uses calloc() for hash allocation. This makes sure, the allocated memory is always initialized. Also cleanup error handling a bit. On *BSD avoid conflicts with BSD list macros. Signed-off-by: Christoph Egger =2D-=20 AMD Saxony, Dresden, Germany Operating System Research Center Legal Information: AMD Saxony Limited Liability Company & Co. KG Sitz (Gesch=E4ftsanschrift): Wilschdorfer Landstr. 101, 01109 Dresden, Deutschland Registergericht Dresden: HRA 4896 vertretungsberechtigter Komplement=E4r: AMD Saxony LLC (Sitz Wilmington, Delaware, USA) Gesch=E4ftsf=FChrer der AMD Saxony LLC: Dr. Hans-R. Deppe, Thomas McCoy --Boundary-00=_QJmTI5fxf8+MUA/ Content-Type: text/x-diff; charset="us-ascii"; name="tools_xenstore.diff" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="tools_xenstore.diff" diff -r 7a0f1bf86516 tools/xenstore/hashtable.c --- a/tools/xenstore/hashtable.c Tue Jun 10 09:27:01 2008 +0100 +++ b/tools/xenstore/hashtable.c Tue Jun 10 11:51:05 2008 +0200 @@ -33,17 +33,22 @@ create_hashtable(unsigned int minsize, { struct hashtable *h; unsigned int pindex, size = primes[0]; + /* Check requested hashtable isn't too large */ if (minsize > (1u << 30)) return NULL; + /* Enforce size as prime */ for (pindex=0; pindex < prime_table_length; pindex++) { if (primes[pindex] > minsize) { size = primes[pindex]; break; } } - h = (struct hashtable *)malloc(sizeof(struct hashtable)); - if (NULL == h) return NULL; /*oom*/ - h->table = (struct entry **)malloc(sizeof(struct entry*) * size); - if (NULL == h->table) { free(h); return NULL; } /*oom*/ - memset(h->table, 0, size * sizeof(struct entry *)); + + h = (struct hashtable *)calloc(1, sizeof(struct hashtable)); + if (NULL == h) + goto err0; + h->table = (struct entry **)calloc(size, sizeof(struct entry *)); + if (NULL == h->table) + goto err1; + h->tablelength = size; h->primeindex = pindex; h->entrycount = 0; @@ -51,6 +56,11 @@ create_hashtable(unsigned int minsize, h->eqfn = eqf; h->loadlimit = (unsigned int)(((uint64_t)size * max_load_factor) / 100); return h; + +err0: + free(h); +err1: + return NULL; } /*****************************************************************************/ @@ -80,10 +90,9 @@ hashtable_expand(struct hashtable *h) if (h->primeindex == (prime_table_length - 1)) return 0; newsize = primes[++(h->primeindex)]; - newtable = (struct entry **)malloc(sizeof(struct entry*) * newsize); + newtable = (struct entry **)calloc(newsize, sizeof(struct entry*)); if (NULL != newtable) { - memset(newtable, 0, newsize * sizeof(struct entry *)); /* This algorithm is not 'stable'. ie. it reverses the list * when it transfers entries between the tables */ for (i = 0; i < h->tablelength; i++) { @@ -149,7 +158,7 @@ hashtable_insert(struct hashtable *h, vo * element may be ok. Next time we insert, we'll try expanding again.*/ hashtable_expand(h); } - e = (struct entry *)malloc(sizeof(struct entry)); + e = (struct entry *)calloc(1, sizeof(struct entry)); if (NULL == e) { --(h->entrycount); return 0; } /*oom*/ e->h = hash(h,k); index = indexFor(h->tablelength,e->h); diff -r 7a0f1bf86516 tools/xenstore/list.h --- a/tools/xenstore/list.h Tue Jun 10 09:27:01 2008 +0100 +++ b/tools/xenstore/list.h Tue Jun 10 11:51:05 2008 +0200 @@ -2,6 +2,10 @@ #define _LINUX_LIST_H /* Taken from Linux kernel code, but de-kernelized for userspace. */ #include + +#undef LIST_HEAD_INIT +#undef LIST_HEAD +#undef INIT_LIST_HEAD /* * These are non-NULL pointers that will result in page faults --Boundary-00=_QJmTI5fxf8+MUA/ Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ Xen-devel mailing list Xen-devel@lists.xensource.com http://lists.xensource.com/xen-devel --Boundary-00=_QJmTI5fxf8+MUA/--