[PATCH:libX11 1/2] Replace Xmalloc+bzero pairs with Xcalloc calls

Alan Coopersmith alan.coopersmith at oracle.com
Thu Apr 14 21:38:24 PDT 2011


Signed-off-by: Alan Coopersmith <alan.coopersmith at oracle.com>
---
 modules/im/ximcp/imDefIc.c     |    3 +--
 modules/im/ximcp/imInt.c       |    3 +--
 modules/im/ximcp/imLcIc.c      |    3 +--
 modules/im/ximcp/imRm.c        |   18 ++++++------------
 modules/im/ximcp/imRmAttr.c    |   15 ++++-----------
 modules/im/ximcp/imThaiIc.c    |    3 +--
 modules/im/ximcp/imTrX.c       |    3 +--
 modules/im/ximcp/imTrans.c     |    4 +---
 modules/lc/gen/lcGenConv.c     |    3 +--
 modules/lc/xlocale/lcJis.c     |    3 +--
 modules/om/generic/omGeneric.c |    9 +++------
 src/Quarks.c                   |    3 +--
 src/Xrm.c                      |    6 ++----
 src/xlibi18n/XDefaultOMIF.c    |   16 +++++-----------
 src/xlibi18n/lcCharSet.c       |    3 +--
 src/xlibi18n/lcDB.c            |    6 ++----
 src/xlibi18n/lcGeneric.c       |   15 +++++----------
 src/xlibi18n/lcPublic.c        |    6 ++----
 18 files changed, 39 insertions(+), 83 deletions(-)

diff --git a/modules/im/ximcp/imDefIc.c b/modules/im/ximcp/imDefIc.c
index 9283c49..a962c1b 100644
--- a/modules/im/ximcp/imDefIc.c
+++ b/modules/im/ximcp/imDefIc.c
@@ -1425,10 +1425,9 @@ _XimProtoCreateIC(
     if (!(_XimGetInputStyle(arg, &input_style)))
 	return (XIC)NULL;
 
-    if ((ic = (Xic)Xmalloc(sizeof(XicRec))) == (Xic)NULL)
+    if ((ic = Xcalloc(1, sizeof(XicRec))) == (Xic)NULL)
 	return (XIC)NULL;
 
-    bzero((char *)ic, sizeof(XicRec));
     ic->methods = &ic_methods;
     ic->core.im = (XIM)im;
     ic->core.input_style = input_style;
diff --git a/modules/im/ximcp/imInt.c b/modules/im/ximcp/imInt.c
index b198092..449a42d 100644
--- a/modules/im/ximcp/imInt.c
+++ b/modules/im/ximcp/imInt.c
@@ -204,9 +204,8 @@ _XimOpenIM(
     Xim			 im;
     register int	 i;
 
-    if (!(im = (Xim)Xmalloc(sizeof(XimRec))))
+    if (!(im = Xcalloc(1, sizeof(XimRec))))
 	return (XIM)NULL;
-    bzero(im, sizeof(XimRec));
 
     im->core.lcd       = lcd;
     im->core.ic_chain  = (XIC)NULL;
diff --git a/modules/im/ximcp/imLcIc.c b/modules/im/ximcp/imLcIc.c
index c072808..4933885 100644
--- a/modules/im/ximcp/imLcIc.c
+++ b/modules/im/ximcp/imLcIc.c
@@ -143,10 +143,9 @@ _XimLocalCreateIC(
     unsigned int	 num;
     int			 len;
 
-    if((ic = (Xic)Xmalloc(sizeof(XicRec))) == (Xic)NULL) {
+    if((ic = Xcalloc(1, sizeof(XicRec))) == (Xic)NULL) {
 	return ((XIC)NULL);
     }
-    bzero((char *)ic, sizeof(XicRec));
 
     ic->methods = &Local_ic_methods;
     ic->core.im = im;
diff --git a/modules/im/ximcp/imRm.c b/modules/im/ximcp/imRm.c
index 3d09b81..3449f91 100644
--- a/modules/im/ximcp/imRm.c
+++ b/modules/im/ximcp/imRm.c
@@ -360,10 +360,9 @@ _XimDefaultStyles(
 
     n = XIMNumber(supported_local_styles) - 1;
     len = sizeof(XIMStyles) + sizeof(XIMStyle) * n;
-    if(!(tmp = (XPointer)Xmalloc(len))) {
+    if(!(tmp = Xcalloc(1, len))) {
 	return False;
     }
-    bzero(tmp, len);
 
     styles = (XIMStyles *)tmp;
     if (n > 0) {
@@ -396,10 +395,9 @@ _XimDefaultIMValues(
 
     n = XIMNumber(supported_local_im_values_list);
     len = sizeof(XIMValuesList) + sizeof(char **) * n;
-    if(!(tmp = (XPointer)Xmalloc(len))) {
+    if(!(tmp = Xcalloc(1, len))) {
 	return False;
     }
-    bzero(tmp, len);
 
     values_list = (XIMValuesList *)tmp;
     if (n > 0) {
@@ -433,10 +431,9 @@ _XimDefaultICValues(
 
     n = XIMNumber(supported_local_ic_values_list);
     len = sizeof(XIMValuesList) + sizeof(char **) * n;
-    if(!(tmp = (XPointer)Xmalloc(len))) {
+    if(!(tmp = Xcalloc(1, len))) {
 	return False;
     }
-    bzero(tmp, len);
 
     values_list = (XIMValuesList *)tmp;
     if (n > 0) {
@@ -1085,10 +1082,9 @@ _XimDecodeStyles(
     num = styles->count_styles;
 
     len = sizeof(XIMStyles) + sizeof(XIMStyle) * num;
-    if(!(tmp = (XPointer)Xmalloc(len))) {
+    if(!(tmp = Xcalloc(1, len))) {
 	return False;
     }
-    bzero(tmp, len);
 
     out = (XIMStyles *)tmp;
     if(num >0) {
@@ -1124,10 +1120,9 @@ _XimDecodeValues(
     num = values_list->count_values;
 
     len = sizeof(XIMValuesList) + sizeof(char **) * num;
-    if(!(tmp = (char *)Xmalloc(len))) {
+    if(!(tmp = Xcalloc(1, len))) {
 	return False;
     }
-    bzero(tmp, len);
 
     out = (XIMValuesList *)tmp;
     if(num) {
@@ -2143,10 +2138,9 @@ _XimSetResourceList(
     XIMResourceList	  res;
 
     len = sizeof(XIMResource) * num_resource;
-    if(!(res = (XIMResourceList)Xmalloc(len))) {
+    if(!(res = Xcalloc(1, len))) {
 	return False;
     }
-    bzero((char *)res, len);
 
     for(i = 0; i < num_resource; i++, id++) {
 	res[i]    = resource[i];
diff --git a/modules/im/ximcp/imRmAttr.c b/modules/im/ximcp/imRmAttr.c
index 27dcbc9..2e73265 100644
--- a/modules/im/ximcp/imRmAttr.c
+++ b/modules/im/ximcp/imRmAttr.c
@@ -1408,7 +1408,6 @@ _XimGetAttributeID(
 {
     unsigned int	  n;
     XIMResourceList	  res;
-    int			  res_len;
     char		 *names;
     int			  names_len;
     XPointer		  tmp;
@@ -1426,18 +1425,15 @@ _XimGetAttributeID(
 
     if (!(n = _XimCountNumberOfAttr(buf[0], &buf[1], &names_len)))
 	return False;
-    res_len = sizeof(XIMResource) * n;
 
-    if (!(res = (XIMResourceList)Xmalloc(res_len)))
+    if (!(res = Xcalloc(n, sizeof(XIMResource))))
 	return False;
-    bzero((char *)res, res_len);
 
     values_len = sizeof(XIMValuesList) + (sizeof(char **) * n) + names_len;
-    if (!(tmp = (XPointer)Xmalloc(values_len))) {
+    if (!(tmp = Xcalloc(1, values_len))) {
 	Xfree(res);
 	return False;
     }
-    bzero(tmp, values_len);
 
     values_list = (XIMValuesList *)tmp;
     values = (char **)((char *)tmp + sizeof(XIMValuesList));
@@ -1475,18 +1471,15 @@ _XimGetAttributeID(
 
     if (!(n = _XimCountNumberOfAttr(buf[0], &buf[2], &names_len)))
 	return False;
-    res_len = sizeof(XIMResource) * n;
 
-    if (!(res = (XIMResourceList)Xmalloc(res_len)))
+    if (!(res = Xcalloc(n, sizeof(XIMResource))))
 	return False;
-    bzero((char *)res, res_len);
 
     values_len = sizeof(XIMValuesList) + (sizeof(char **) * n) + names_len;
-    if (!(tmp = (XPointer)Xmalloc(values_len))) {
+    if (!(tmp = Xcalloc(1, values_len))) {
 	Xfree(res);
 	return False;
     }
-    bzero(tmp, values_len);
 
     values_list = (XIMValuesList *)tmp;
     values = (char **)((char *)tmp + sizeof(XIMValuesList));
diff --git a/modules/im/ximcp/imThaiIc.c b/modules/im/ximcp/imThaiIc.c
index d1cb22a..95433f3 100644
--- a/modules/im/ximcp/imThaiIc.c
+++ b/modules/im/ximcp/imThaiIc.c
@@ -157,10 +157,9 @@ _XimThaiCreateIC(
     int			 len;
     DefTree             *tree;
 
-    if((ic = (Xic)Xmalloc(sizeof(XicRec))) == (Xic)NULL) {
+    if((ic = Xcalloc(1, sizeof(XicRec))) == (Xic)NULL) {
 	return ((XIC)NULL);
     }
-    bzero((char *)ic,      sizeof(XicRec));
 
     ic->methods = &Thai_ic_methods;
     ic->core.im = im;
diff --git a/modules/im/ximcp/imTrX.c b/modules/im/ximcp/imTrX.c
index edcaf08..d85d1d1 100644
--- a/modules/im/ximcp/imTrX.c
+++ b/modules/im/ximcp/imTrX.c
@@ -496,9 +496,8 @@ _XimXConf(Xim im, char *address)
 {
     XSpecRec	*spec;
 
-    if (!(spec = (XSpecRec *)Xmalloc(sizeof(XSpecRec))))
+    if (!(spec = Xcalloc(1, sizeof(XSpecRec))))
 	return False;
-    bzero(spec, sizeof(XSpecRec));
 
     spec->improtocolid = XInternAtom(im->core.display, _XIM_PROTOCOL, False);
     spec->imconnectid  = XInternAtom(im->core.display, _XIM_XCONNECT, False);
diff --git a/modules/im/ximcp/imTrans.c b/modules/im/ximcp/imTrans.c
index dfef765..e321d59 100644
--- a/modules/im/ximcp/imTrans.c
+++ b/modules/im/ximcp/imTrans.c
@@ -293,13 +293,11 @@ _XimTransConf(
     if (!(paddr = (char *)Xmalloc(strlen(address) + 1)))
 	return False;
 
-    if (!(spec = (TransSpecRec *) Xmalloc(sizeof(TransSpecRec)))) {
+    if (!(spec = Xcalloc(1, sizeof(TransSpecRec)))) {
 	Xfree(paddr);
 	return False;
     }
 
-    bzero(spec, sizeof(TransSpecRec));
-
     (void)strcpy(paddr, address);
     spec->address   = paddr;
 
diff --git a/modules/lc/gen/lcGenConv.c b/modules/lc/gen/lcGenConv.c
index baac73a..7a113a7 100644
--- a/modules/lc/gen/lcGenConv.c
+++ b/modules/lc/gen/lcGenConv.c
@@ -2660,10 +2660,9 @@ create_conv(
     *conv->methods = *methods;
     conv->methods->reset = init_state;
 
-    conv->state = (XPointer) Xmalloc(sizeof(StateRec));
+    conv->state = Xcalloc(1, sizeof(StateRec));
     if (conv->state == NULL)
 	goto err;
-    bzero((char *) conv->state, sizeof(StateRec));
 
     state = (State) conv->state;
     state->lcd = lcd;
diff --git a/modules/lc/xlocale/lcJis.c b/modules/lc/xlocale/lcJis.c
index 594e636..551862d 100644
--- a/modules/lc/xlocale/lcJis.c
+++ b/modules/lc/xlocale/lcJis.c
@@ -545,10 +545,9 @@ create_conv(
     if (XLC_PUBLIC(lcd, is_state_depend))
 	conv->methods->reset = init_state;
 
-    conv->state = (XPointer) Xmalloc(sizeof(StateRec));
+    conv->state = Xcalloc(1, sizeof(StateRec));
     if (conv->state == NULL)
 	goto err;
-    bzero((char *) conv->state, sizeof(StateRec));
 
     state = (State) conv->state;
     state->lcd = lcd;
diff --git a/modules/om/generic/omGeneric.c b/modules/om/generic/omGeneric.c
index 44143ac..e358796 100644
--- a/modules/om/generic/omGeneric.c
+++ b/modules/om/generic/omGeneric.c
@@ -1654,10 +1654,9 @@ create_oc(
     XOCMethodsList methods_list = oc_methods_list;
     int count;
 
-    oc = (XOC) Xmalloc(sizeof(XOCGenericRec));
+    oc = Xcalloc(1, sizeof(XOCGenericRec));
     if (oc == NULL)
 	return (XOC) NULL;
-    bzero((char *) oc, sizeof(XOCGenericRec));
 
     oc->core.om = om;
 
@@ -1842,10 +1841,9 @@ create_om(
 {
     XOM om;
 
-    om = (XOM) Xmalloc(sizeof(XOMGenericRec));
+    om = Xcalloc(1, sizeof(XOMGenericRec));
     if (om == NULL)
 	return (XOM) NULL;
-    bzero((char *) om, sizeof(XOMGenericRec));
 
     om->methods = &methods;
     om->core.lcd = lcd;
@@ -1913,10 +1911,9 @@ read_EncodingInfo(
     FontData font_data,ret;
     char *buf, *bufptr,*scp;
     int len;
-    font_data = (FontData) Xmalloc(sizeof(FontDataRec) * count);
+    font_data = Xcalloc(count, sizeof(FontDataRec));
     if (font_data == NULL)
         return NULL;
-    bzero((char *) font_data, sizeof(FontDataRec) * count);
 
     ret = font_data;
     for ( ; count-- > 0; font_data++) {
diff --git a/src/Quarks.c b/src/Quarks.c
index d0eb690..7a704b1 100644
--- a/src/Quarks.c
+++ b/src/Quarks.c
@@ -210,10 +210,9 @@ ExpandQuarkTable(void)
 #endif
 	newmask = 0x1ff;
     }
-    entries = (Entry *)Xmalloc(sizeof(Entry) * (newmask + 1));
+    entries = Xcalloc(newmask + 1, sizeof(Entry));
     if (!entries)
 	return False;
-    bzero((char *)entries, sizeof(Entry) * (newmask + 1));
     quarkTable = entries;
     quarkMask = newmask;
     quarkRehash = quarkMask - 2;
diff --git a/src/Xrm.c b/src/Xrm.c
index c466cae..53467ae 100644
--- a/src/Xrm.c
+++ b/src/Xrm.c
@@ -581,23 +581,21 @@ static void GrowTable(
 	ltable = (LTable)table;
 	/* cons up a copy to make MoveValues look symmetric */
 	otable = *ltable;
-	ltable->buckets = (VEntry *)Xmalloc(i * sizeof(VEntry));
+	ltable->buckets = Xcalloc(i, sizeof(VEntry));
 	if (!ltable->buckets) {
 	    ltable->buckets = otable.buckets;
 	    return;
 	}
 	ltable->table.mask = i - 1;
-	bzero((char *)ltable->buckets, i * sizeof(VEntry));
 	MoveValues(&otable, ltable);
     } else {
 	register NTable ntable;
 
-	ntable = (NTable)Xmalloc(sizeof(NTableRec) + i * sizeof(NTable));
+	ntable = Xcalloc(1, sizeof(NTableRec) + (i * sizeof(NTable)));
 	if (!ntable)
 	    return;
 	*ntable = *table;
 	ntable->mask = i - 1;
-	bzero((char *)NodeBuckets(ntable), i * sizeof(NTable));
 	*prev = ntable;
 	MoveTables(table, ntable);
     }
diff --git a/src/xlibi18n/XDefaultOMIF.c b/src/xlibi18n/XDefaultOMIF.c
index ae8ac79..85d63e8 100644
--- a/src/xlibi18n/XDefaultOMIF.c
+++ b/src/xlibi18n/XDefaultOMIF.c
@@ -128,10 +128,9 @@ init_fontset(
 
     data = XOM_GENERIC(oc->core.om)->data;
 
-    font_set = (FontSet) Xmalloc(sizeof(FontSetRec));
+    font_set = Xcalloc(1, sizeof(FontSetRec));
     if (font_set == NULL)
 	return False;
-    bzero((char *) font_set, sizeof(FontSetRec));
 
     gen = XOC_GENERIC(oc);
     gen->font_set = font_set;
@@ -1005,10 +1004,9 @@ create_oc(
 {
     XOC oc;
 
-    oc = (XOC) Xmalloc(sizeof(XOCGenericRec));
+    oc = Xcalloc(1, sizeof(XOCGenericRec));
     if (oc == NULL)
 	return (XOC) NULL;
-    bzero((char *) oc, sizeof(XOCGenericRec));
 
     oc->core.om = om;
 
@@ -1126,15 +1124,13 @@ add_data(
     XOMGenericPart *gen = XOM_GENERIC(om);
     OMData new;
 
-    new = (OMData) Xmalloc(sizeof(OMDataRec));
+    new = Xcalloc(1, sizeof(OMDataRec));
 
     if (new == NULL)
         return NULL;
 
     gen->data = new;
 
-    bzero((char *) new, sizeof(OMDataRec));
-
     return new;
 }
 
@@ -1168,10 +1164,9 @@ init_om(
     if (data == NULL)
 	return False;
 
-    font_data = (FontData) Xmalloc(sizeof(FontDataRec) * count);
+    font_data = Xcalloc(count, sizeof(FontDataRec));
     if (font_data == NULL)
 	return False;
-    bzero((char *) font_data, sizeof(FontDataRec) * count);
     data->font_data = font_data;
     data->font_data_count = count;
 
@@ -1237,10 +1232,9 @@ _XDefaultOpenOM(XLCd lcd, Display *dpy, XrmDatabase rdb,
 {
     XOM om;
 
-    om = (XOM) Xmalloc(sizeof(XOMGenericRec));
+    om = Xcalloc(1, sizeof(XOMGenericRec));
     if (om == NULL)
 	return (XOM) NULL;
-    bzero((char *) om, sizeof(XOMGenericRec));
 
     om->methods = (XOMMethods)&methods;
     om->core.lcd = lcd;
diff --git a/src/xlibi18n/lcCharSet.c b/src/xlibi18n/lcCharSet.c
index 0fa39d8..5d28781 100644
--- a/src/xlibi18n/lcCharSet.c
+++ b/src/xlibi18n/lcCharSet.c
@@ -176,10 +176,9 @@ _XlcCreateDefaultCharSet(
     const char *colon;
     char *tmp;
 
-    charset = (XlcCharSet) Xmalloc(sizeof(XlcCharSetRec));
+    charset = Xcalloc(1, sizeof(XlcCharSetRec));
     if (charset == NULL)
 	return (XlcCharSet) NULL;
-    bzero((char *) charset, sizeof(XlcCharSetRec));
 
     name_len = strlen(name);
     ct_sequence_len = strlen(ct_sequence);
diff --git a/src/xlibi18n/lcDB.c b/src/xlibi18n/lcDB.c
index afff174..c6eb7f0 100644
--- a/src/xlibi18n/lcDB.c
+++ b/src/xlibi18n/lcDB.c
@@ -591,11 +591,10 @@ store_to_database(
 	goto err;
     }
 
-    new = (Database)Xmalloc(sizeof(DatabaseRec));
+    new = Xcalloc(1, sizeof(DatabaseRec));
     if (new == (Database)NULL) {
 	goto err;
     }
-    bzero(new, sizeof(DatabaseRec));
 
     new->category = (char *)Xmalloc(strlen(parse_info.category) + 1);
     if (new->category == NULL) {
@@ -1309,10 +1308,9 @@ _XlcCreateLocaleDataBase(
 	return (XPointer)NULL;
     }
     n = CountDatabase(database);
-    lc_db = (XlcDatabase)Xmalloc(sizeof(XlcDatabaseRec) * (n + 1));
+    lc_db = Xcalloc(n + 1, sizeof(XlcDatabaseRec));
     if (lc_db == (XlcDatabase)NULL)
 	goto err;
-    bzero(lc_db, sizeof(XlcDatabaseRec) * (n + 1));
     for (p = database, i = 0; p && i < n; p = p->next, ++i) {
 	lc_db[i].category_q = XrmStringToQuark(p->category);
 	lc_db[i].name_q = XrmStringToQuark(p->name);
diff --git a/src/xlibi18n/lcGeneric.c b/src/xlibi18n/lcGeneric.c
index 688a4cf..df392e9 100644
--- a/src/xlibi18n/lcGeneric.c
+++ b/src/xlibi18n/lcGeneric.c
@@ -60,15 +60,13 @@ create(
     XLCd lcd;
     XLCdPublicMethods new;
 
-    lcd = (XLCd) Xmalloc(sizeof(XLCdRec));
+    lcd = Xcalloc(1, sizeof(XLCdRec));
     if (lcd == NULL)
         return (XLCd) NULL;
-    bzero((char *) lcd, sizeof(XLCdRec));
 
-    lcd->core = (XLCdCore) Xmalloc(sizeof(XLCdGenericRec));
+    lcd->core = Xcalloc(1, sizeof(XLCdGenericRec));
     if (lcd->core == NULL)
 	goto err;
-    bzero((char *) lcd->core, sizeof(XLCdGenericRec));
 
     new = (XLCdPublicMethods) Xmalloc(sizeof(XLCdPublicMethodsRec));
     if (new == NULL)
@@ -180,10 +178,9 @@ add_codeset(
     CodeSet new, *new_list;
     int num;
 
-    new = (CodeSet) Xmalloc(sizeof(CodeSetRec));
+    new = Xcalloc(1, sizeof(CodeSetRec));
     if (new == NULL)
         return NULL;
-    bzero((char *) new, sizeof(CodeSetRec));
 
     if ((num = gen->codeset_num))
         new_list = (CodeSet *) Xrealloc(gen->codeset_list,
@@ -223,16 +220,14 @@ add_parse_list(
         return False;
     strcpy(str, encoding);
 
-    new = (ParseInfo) Xmalloc(sizeof(ParseInfoRec));
+    new = Xcalloc(1, sizeof(ParseInfoRec));
     if (new == NULL)
         goto err;
-    bzero((char *) new, sizeof(ParseInfoRec));
 
     if (gen->mb_parse_table == NULL) {
-        gen->mb_parse_table = (unsigned char *) Xmalloc(256); /* 2^8 */
+        gen->mb_parse_table = Xcalloc(1, 256); /* 2^8 */
         if (gen->mb_parse_table == NULL)
             goto err;
-        bzero((char *) gen->mb_parse_table, 256);
     }
 
     if ((num = gen->mb_parse_list_num))
diff --git a/src/xlibi18n/lcPublic.c b/src/xlibi18n/lcPublic.c
index 2c02f5f..43b9286 100644
--- a/src/xlibi18n/lcPublic.c
+++ b/src/xlibi18n/lcPublic.c
@@ -80,15 +80,13 @@ create(
     XLCd lcd;
     XLCdPublicMethods new;
 
-    lcd = (XLCd) Xmalloc(sizeof(XLCdRec));
+    lcd = Xcalloc(1, sizeof(XLCdRec));
     if (lcd == NULL)
         return (XLCd) NULL;
-    bzero((char *) lcd, sizeof(XLCdRec));
 
-    lcd->core = (XLCdCore) Xmalloc(sizeof(XLCdPublicRec));
+    lcd->core = Xcalloc(1, sizeof(XLCdPublicRec));
     if (lcd->core == NULL)
 	goto err;
-    bzero((char *) lcd->core, sizeof(XLCdPublicRec));
 
     new = (XLCdPublicMethods) Xmalloc(sizeof(XLCdPublicMethodsRec));
     if (new == NULL)
-- 
1.7.3.2



More information about the xorg-devel mailing list