aboutsummaryrefslogtreecommitdiff
path: root/xorg-server/xkb/XKBGAlloc.c
diff options
context:
space:
mode:
authormarha <marha@users.sourceforge.net>2011-04-05 14:52:19 +0000
committermarha <marha@users.sourceforge.net>2011-04-05 14:52:19 +0000
commit6254e74e8b2ff8a2a1e8476a3300a7bdd65b7ad6 (patch)
tree02e0d5fd297602d526b582dc527e007fe20a326f /xorg-server/xkb/XKBGAlloc.c
parent1d142834bdb30308784a0654a39b7c0de7e37695 (diff)
parenteaedc21febeadad4cf0e370f5d97e7bdb4470870 (diff)
downloadvcxsrv-6254e74e8b2ff8a2a1e8476a3300a7bdd65b7ad6.tar.gz
vcxsrv-6254e74e8b2ff8a2a1e8476a3300a7bdd65b7ad6.tar.bz2
vcxsrv-6254e74e8b2ff8a2a1e8476a3300a7bdd65b7ad6.zip
svn merge ^/branches/released .
Diffstat (limited to 'xorg-server/xkb/XKBGAlloc.c')
-rw-r--r--xorg-server/xkb/XKBGAlloc.c64
1 files changed, 56 insertions, 8 deletions
diff --git a/xorg-server/xkb/XKBGAlloc.c b/xorg-server/xkb/XKBGAlloc.c
index db32dc8c5..ecdca5dd2 100644
--- a/xorg-server/xkb/XKBGAlloc.c
+++ b/xorg-server/xkb/XKBGAlloc.c
@@ -435,6 +435,57 @@ XkbFreeGeometry(XkbGeometryPtr geom,unsigned which,Bool freeMap)
/***====================================================================***/
+/**
+ * Resize and clear an XKB geometry item array. The array size may
+ * grow or shrink unlike in _XkbGeomAlloc.
+ *
+ * @param buffer[in,out] buffer to reallocate and clear
+ * @param szItems[in] currently allocated item count for "buffer"
+ * @param nrItems[in] required item count for "buffer"
+ * @param itemSize[in] size of a single item in "buffer"
+ * @param clearance[in] items to clear after reallocation
+ *
+ * @see _XkbGeomAlloc
+ *
+ * @return TRUE if reallocation succeeded. Otherwise FALSE is returned
+ * and contents of "buffer" aren't touched.
+ */
+Bool
+XkbGeomRealloc(void **buffer, int szItems, int nrItems,
+ int itemSize, XkbGeomClearance clearance)
+{
+ void *items;
+ int clearBegin;
+ /* Check validity of arguments. */
+ if (!buffer)
+ return FALSE;
+ items = *buffer;
+ if (!((items && (szItems > 0)) || (!items && !szItems)))
+ return FALSE;
+ /* Check if there is need to resize. */
+ if (nrItems != szItems)
+ if (!(items = realloc(items, nrItems * itemSize)))
+ return FALSE;
+ /* Clear specified items to zero. */
+ switch (clearance)
+ {
+ case XKB_GEOM_CLEAR_EXCESS:
+ clearBegin = szItems;
+ break;
+ case XKB_GEOM_CLEAR_ALL:
+ clearBegin = 0;
+ break;
+ case XKB_GEOM_CLEAR_NONE:
+ default:
+ clearBegin = nrItems;
+ break;
+ }
+ if (items && (clearBegin < nrItems))
+ memset((char *)items + (clearBegin * itemSize), 0, (nrItems - clearBegin) * itemSize);
+ *buffer = items;
+ return TRUE;
+}
+
static Status
_XkbGeomAlloc( void ** old,
unsigned short * num,
@@ -451,18 +502,15 @@ _XkbGeomAlloc( void ** old,
return Success;
*total= (*num)+num_new;
- if ((*old)!=NULL)
- (*old)= realloc((*old),(*total)*sz_elem);
- else (*old)= calloc((*total),sz_elem);
- if ((*old)==NULL) {
+
+ if (!XkbGeomRealloc(old, *num, *total, sz_elem, XKB_GEOM_CLEAR_EXCESS))
+ {
+ free(*old);
+ (*old)= NULL;
*total= *num= 0;
return BadAlloc;
}
- if (*num>0) {
- char *tmp= (char *)(*old);
- memset(&tmp[sz_elem*(*num)], 0, (num_new*sz_elem));
- }
return Success;
}