Commit 258a8ced authored by Alan Coopersmith's avatar Alan Coopersmith 🛫

Replace _XkbTypedRealloc & _XkbClearElems with new _XkbResizeArray

Makes resizing & clearing more consistent and gets rid of some weird
quirks like always subtracting 1 from the size passed to _XkbClearElems
so it could always add 1 to the size passed in.
Signed-off-by: Alan Coopersmith's avatarAlan Coopersmith <alan.coopersmith@oracle.com>
parent f94273e2
......@@ -44,7 +44,6 @@ Status
XkbAllocCompatMap(XkbDescPtr xkb, unsigned which, unsigned nSI)
{
XkbCompatMapPtr compat;
XkbSymInterpretRec *prev_interpret;
if (!xkb)
return BadMatch;
......@@ -55,18 +54,12 @@ XkbAllocCompatMap(XkbDescPtr xkb, unsigned which, unsigned nSI)
compat->size_si = nSI;
if (compat->sym_interpret == NULL)
compat->num_si = 0;
prev_interpret = compat->sym_interpret;
compat->sym_interpret = _XkbTypedRealloc(compat->sym_interpret,
nSI, XkbSymInterpretRec);
_XkbResizeArray(compat->sym_interpret, compat->num_si,
nSI, XkbSymInterpretRec);
if (compat->sym_interpret == NULL) {
_XkbFree(prev_interpret);
compat->size_si = compat->num_si = 0;
return BadAlloc;
}
if (compat->num_si != 0) {
_XkbClearElems(compat->sym_interpret, compat->num_si,
compat->size_si - 1, XkbSymInterpretRec);
}
return Success;
}
compat = _XkbTypedCalloc(1, XkbCompatMapRec);
......@@ -150,22 +143,10 @@ XkbAllocNames(XkbDescPtr xkb, unsigned which, int nTotalRG, int nTotalAliases)
return BadAlloc;
}
if ((which & XkbKeyAliasesMask) && (nTotalAliases > 0)) {
if (names->key_aliases == NULL) {
names->key_aliases = _XkbTypedCalloc(nTotalAliases, XkbKeyAliasRec);
}
else if (nTotalAliases > names->num_key_aliases) {
XkbKeyAliasRec *prev_aliases = names->key_aliases;
names->key_aliases = _XkbTypedRealloc(names->key_aliases,
nTotalAliases,
XkbKeyAliasRec);
if (names->key_aliases != NULL) {
_XkbClearElems(names->key_aliases, names->num_key_aliases,
nTotalAliases - 1, XkbKeyAliasRec);
}
else {
_XkbFree(prev_aliases);
}
if ((names->key_aliases == NULL) ||
(nTotalAliases > names->num_key_aliases)) {
_XkbResizeArray(names->key_aliases, names->num_key_aliases,
nTotalAliases, XkbKeyAliasRec);
}
if (names->key_aliases == NULL) {
names->num_key_aliases = 0;
......@@ -174,24 +155,13 @@ XkbAllocNames(XkbDescPtr xkb, unsigned which, int nTotalRG, int nTotalAliases)
names->num_key_aliases = nTotalAliases;
}
if ((which & XkbRGNamesMask) && (nTotalRG > 0)) {
if (names->radio_groups == NULL) {
names->radio_groups = _XkbTypedCalloc(nTotalRG, Atom);
if ((names->radio_groups == NULL) || (nTotalRG > names->num_rg)) {
_XkbResizeArray(names->radio_groups, names->num_rg, nTotalRG, Atom);
}
else if (nTotalRG > names->num_rg) {
Atom *prev_radio_groups = names->radio_groups;
names->radio_groups =
_XkbTypedRealloc(names->radio_groups, nTotalRG, Atom);
if (names->radio_groups != NULL) {
_XkbClearElems(names->radio_groups, names->num_rg, nTotalRG - 1,
Atom);
}
else {
_XkbFree(prev_radio_groups);
}
}
if (names->radio_groups == NULL)
if (names->radio_groups == NULL) {
names->num_rg = 0;
return BadAlloc;
}
names->num_rg = nTotalRG;
}
return Success;
......@@ -350,16 +320,13 @@ XkbAddDeviceLedInfo(XkbDeviceInfoPtr devi, unsigned ledClass, unsigned ledId)
return devli;
}
if (devi->num_leds >= devi->sz_leds) {
XkbDeviceLedInfoRec *prev_leds = devi->leds;
if (devi->sz_leds > 0)
devi->sz_leds *= 2;
else
devi->sz_leds = 1;
devi->leds = _XkbTypedRealloc(devi->leds, devi->sz_leds,
XkbDeviceLedInfoRec);
_XkbResizeArray(devi->leds, devi->num_leds, devi->sz_leds,
XkbDeviceLedInfoRec);
if (!devi->leds) {
_XkbFree(prev_leds);
devi->sz_leds = devi->num_leds = 0;
return NULL;
}
......@@ -380,8 +347,6 @@ XkbAddDeviceLedInfo(XkbDeviceInfoPtr devi, unsigned ledClass, unsigned ledId)
Status
XkbResizeDeviceButtonActions(XkbDeviceInfoPtr devi, unsigned newTotal)
{
XkbAction *prev_btn_acts;
if ((!devi) || (newTotal > 255))
return BadValue;
if ((devi->btn_acts != NULL) && (newTotal == devi->num_btns))
......@@ -394,10 +359,8 @@ XkbResizeDeviceButtonActions(XkbDeviceInfoPtr devi, unsigned newTotal)
devi->num_btns = 0;
return Success;
}
prev_btn_acts = devi->btn_acts;
devi->btn_acts = _XkbTypedRealloc(devi->btn_acts, newTotal, XkbAction);
_XkbResizeArray(devi->btn_acts, devi->num_btns, newTotal, XkbAction);
if (devi->btn_acts == NULL) {
_XkbFree(prev_btn_acts);
devi->num_btns = 0;
return BadAlloc;
}
......
......@@ -63,12 +63,9 @@ _XkbReadKeyTypes(XkbReadBufferPtr buf, XkbDescPtr xkb, xkbGetMapReply *rep)
if (desc->nMapEntries > 0) {
if ((type->map == NULL) ||
(desc->nMapEntries > type->map_count)) {
XkbKTMapEntryRec *prev_map = type->map;
type->map = _XkbTypedRealloc(type->map, desc->nMapEntries,
XkbKTMapEntryRec);
_XkbResizeArray(type->map, type->map_count,
desc->nMapEntries, XkbKTMapEntryRec);
if (type->map == NULL) {
_XkbFree(prev_map);
return BadAlloc;
}
}
......@@ -81,13 +78,9 @@ _XkbReadKeyTypes(XkbReadBufferPtr buf, XkbDescPtr xkb, xkbGetMapReply *rep)
if (desc->preserve && (desc->nMapEntries > 0)) {
if ((!type->preserve) || (desc->nMapEntries > lastMapCount)) {
XkbModsRec *prev_preserve = type->preserve;
type->preserve = _XkbTypedRealloc(type->preserve,
desc->nMapEntries,
XkbModsRec);
_XkbResizeArray(type->preserve, lastMapCount,
desc->nMapEntries, XkbModsRec);
if (type->preserve == NULL) {
_XkbFree(prev_preserve);
return BadAlloc;
}
}
......@@ -188,12 +181,10 @@ _XkbReadKeySyms(XkbReadBufferPtr buf, XkbDescPtr xkb, xkbGetMapReply *rep)
oldMap->offset = offset;
if (offset + newMap->nSyms >= map->size_syms) {
register int sz;
KeySym *prev_syms = map->syms;
sz = map->size_syms + 128;
map->syms = _XkbTypedRealloc(map->syms, sz, KeySym);
_XkbResizeArray(map->syms, map->size_syms, sz, KeySym);
if (map->syms == NULL) {
_XkbFree(prev_syms);
map->size_syms = 0;
return BadAlloc;
}
......
......@@ -69,25 +69,17 @@ XkbAllocClientMap(XkbDescPtr xkb, unsigned which, unsigned nTotalTypes)
if ((which & XkbKeyTypesMask) && (nTotalTypes > 0)) {
if (map->types == NULL) {
map->types = _XkbTypedCalloc(nTotalTypes, XkbKeyTypeRec);
if (map->types == NULL)
return BadAlloc;
map->num_types = 0;
map->size_types = nTotalTypes;
map->num_types = map->size_types = 0;
}
else if (map->size_types < nTotalTypes) {
XkbKeyTypeRec *prev_types = map->types;
if ((map->types == NULL) || (map->size_types < nTotalTypes)) {
_XkbResizeArray(map->types, map->size_types, nTotalTypes,
XkbKeyTypeRec);
map->types =
_XkbTypedRealloc(map->types, nTotalTypes, XkbKeyTypeRec);
if (map->types == NULL) {
_XkbFree(prev_types);
map->num_types = map->size_types = 0;
return BadAlloc;
}
map->size_types = nTotalTypes;
bzero(&map->types[map->num_types],
((map->size_types - map->num_types) * sizeof(XkbKeyTypeRec)));
}
}
if (which & XkbKeySymsMask) {
......@@ -164,26 +156,20 @@ XkbAllocServerMap(XkbDescPtr xkb, unsigned which, unsigned nNewActions)
if (nNewActions < 1)
nNewActions = 1;
if (map->acts == NULL) {
map->acts = _XkbTypedCalloc((nNewActions + 1), XkbAction);
if (map->acts == NULL)
return BadAlloc;
map->num_acts = 1;
map->size_acts = nNewActions + 1;
map->size_acts = 0;
}
else if ((map->size_acts - map->num_acts) < nNewActions) {
if ((map->acts == NULL) ||
((map->size_acts - map->num_acts) < nNewActions)) {
unsigned need;
XkbAction *prev_acts = map->acts;
need = map->num_acts + nNewActions;
map->acts = _XkbTypedRealloc(map->acts, need, XkbAction);
_XkbResizeArray(map->acts, map->size_acts, need, XkbAction);
if (map->acts == NULL) {
_XkbFree(prev_acts);
map->num_acts = map->size_acts = 0;
return BadAlloc;
}
map->size_acts = need;
bzero(&map->acts[map->num_acts],
((map->size_acts - map->num_acts) * sizeof(XkbAction)));
}
if (map->key_acts == NULL) {
i = xkb->max_key_code + 1;
......@@ -390,24 +376,18 @@ XkbResizeKeyType(XkbDescPtr xkb,
type->map_count = 0;
}
else {
XkbKTMapEntryRec *prev_map = type->map;
if ((map_count > type->map_count) || (type->map == NULL))
type->map =
_XkbTypedRealloc(type->map, map_count, XkbKTMapEntryRec);
_XkbResizeArray(type->map, type->map_count, map_count,
XkbKTMapEntryRec);
if (!type->map) {
_XkbFree(prev_map);
return BadAlloc;
}
if (want_preserve) {
XkbModsRec *prev_preserve = type->preserve;
if ((map_count > type->map_count) || (type->preserve == NULL)) {
type->preserve = _XkbTypedRealloc(type->preserve, map_count,
XkbModsRec);
_XkbResizeArray(type->preserve, type->map_count, map_count,
XkbModsRec);
}
if (!type->preserve) {
_XkbFree(prev_preserve);
return BadAlloc;
}
}
......@@ -419,12 +399,8 @@ XkbResizeKeyType(XkbDescPtr xkb,
}
if ((new_num_lvls > type->num_levels) || (type->level_names == NULL)) {
Atom *prev_level_names = type->level_names;
type->level_names =
_XkbTypedRealloc(type->level_names, new_num_lvls, Atom);
_XkbResizeArray(type->level_names, type->num_levels, new_num_lvls, Atom);
if (!type->level_names) {
_XkbFree(prev_level_names);
return BadAlloc;
}
}
......@@ -728,16 +704,11 @@ XkbChangeKeycodeRange(XkbDescPtr xkb,
tmp = maxKC - xkb->max_key_code;
if (xkb->map) {
if (xkb->map->key_sym_map) {
XkbSymMapRec *prev_key_sym_map = xkb->map->key_sym_map;
xkb->map->key_sym_map = _XkbTypedRealloc(xkb->map->key_sym_map,
(maxKC + 1), XkbSymMapRec);
_XkbResizeArray(xkb->map->key_sym_map, xkb->max_key_code + 1,
(maxKC + 1), XkbSymMapRec);
if (!xkb->map->key_sym_map) {
_XkbFree(prev_key_sym_map);
return BadAlloc;
}
bzero((char *) &xkb->map->key_sym_map[xkb->max_key_code],
tmp * sizeof(XkbSymMapRec));
if (changes) {
changes->map.changed = _ExtendRange(changes->map.changed,
XkbKeySymsMask, maxKC,
......@@ -746,15 +717,11 @@ XkbChangeKeycodeRange(XkbDescPtr xkb,
}
}
if (xkb->map->modmap) {
unsigned char *prev_modmap = xkb->map->modmap;
xkb->map->modmap = _XkbTypedRealloc(xkb->map->modmap,
(maxKC + 1), unsigned char);
_XkbResizeArray(xkb->map->modmap, xkb->max_key_code + 1,
(maxKC + 1), unsigned char);
if (!xkb->map->modmap) {
_XkbFree(prev_modmap);
return BadAlloc;
}
bzero((char *) &xkb->map->modmap[xkb->max_key_code], tmp);
if (changes) {
changes->map.changed = _ExtendRange(changes->map.changed,
XkbModifierMapMask, maxKC,
......@@ -765,17 +732,11 @@ XkbChangeKeycodeRange(XkbDescPtr xkb,
}
if (xkb->server) {
if (xkb->server->behaviors) {
XkbBehavior *prev_behaviors = xkb->server->behaviors;
xkb->server->behaviors =
_XkbTypedRealloc(xkb->server->behaviors, (maxKC + 1),
XkbBehavior);
_XkbResizeArray(xkb->server->behaviors, xkb->max_key_code + 1,
(maxKC + 1), XkbBehavior);
if (!xkb->server->behaviors) {
_XkbFree(prev_behaviors);
return BadAlloc;
}
bzero((char *) &xkb->server->behaviors[xkb->max_key_code],
tmp * sizeof(XkbBehavior));
if (changes) {
changes->map.changed = _ExtendRange(changes->map.changed,
XkbKeyBehaviorsMask, maxKC,
......@@ -784,16 +745,11 @@ XkbChangeKeycodeRange(XkbDescPtr xkb,
}
}
if (xkb->server->key_acts) {
unsigned short *prev_key_acts = xkb->server->key_acts;
xkb->server->key_acts = _XkbTypedRealloc(xkb->server->key_acts,
(maxKC + 1), unsigned short);
_XkbResizeArray(xkb->server->key_acts, xkb->max_key_code + 1,
(maxKC + 1), unsigned short);
if (!xkb->server->key_acts) {
_XkbFree(prev_key_acts);
return BadAlloc;
}
bzero((char *) &xkb->server->key_acts[xkb->max_key_code],
tmp * sizeof(unsigned short));
if (changes) {
changes->map.changed = _ExtendRange(changes->map.changed,
XkbKeyActionsMask, maxKC,
......@@ -802,16 +758,11 @@ XkbChangeKeycodeRange(XkbDescPtr xkb,
}
}
if (xkb->server->vmodmap) {
unsigned short *prev_vmodmap = xkb->server->vmodmap;
xkb->server->vmodmap = _XkbTypedRealloc(xkb->server->vmodmap,
(maxKC + 1), unsigned short);
_XkbResizeArray(xkb->server->vmodmap, xkb->max_key_code + 1,
(maxKC + 1), unsigned short);
if (!xkb->server->vmodmap) {
_XkbFree(prev_vmodmap);
return BadAlloc;
}
bzero((char *) &xkb->server->vmodmap[xkb->max_key_code],
tmp * sizeof(unsigned short));
if (changes) {
changes->map.changed = _ExtendRange(changes->map.changed,
XkbVirtualModMapMask, maxKC,
......@@ -821,16 +772,11 @@ XkbChangeKeycodeRange(XkbDescPtr xkb,
}
}
if ((xkb->names) && (xkb->names->keys)) {
XkbKeyNameRec *prev_keys = xkb->names->keys;
xkb->names->keys = _XkbTypedRealloc(xkb->names->keys,
(maxKC + 1), XkbKeyNameRec);
_XkbResizeArray(xkb->names->keys, xkb->max_key_code + 1,
(maxKC + 1), XkbKeyNameRec);
if (!xkb->names->keys) {
_XkbFree(prev_keys);
return BadAlloc;
}
bzero((char *) &xkb->names->keys[xkb->max_key_code],
tmp * sizeof(XkbKeyNameRec));
if (changes) {
changes->names.changed = _ExtendRange(changes->names.changed,
XkbKeyNamesMask, maxKC,
......
......@@ -205,14 +205,14 @@ _XkbReadGetNamesReply(Display *dpy,
if (rep->nRadioGroups > 0) {
Atom *rgNames;
if (names->radio_groups == NULL)
names->radio_groups = _XkbTypedCalloc(rep->nRadioGroups, Atom);
else if (names->num_rg < rep->nRadioGroups) {
names->radio_groups = _XkbTypedRealloc(names->radio_groups,
rep->nRadioGroups, Atom);
if ((names->radio_groups == NULL) ||
(names->num_rg < rep->nRadioGroups)) {
_XkbResizeArray(names->radio_groups, names->num_rg,
rep->nRadioGroups, Atom);
}
rgNames = names->radio_groups;
if (!rgNames) {
names->num_rg = 0;
goto BAILOUT;
}
if (!_XkbReadBufferCopy32
......
......@@ -120,11 +120,26 @@ typedef struct _XkbReadBuffer {
#define _XkbRealloc(o,s) Xrealloc((o),(s))
#define _XkbTypedAlloc(t) ((t *)Xmalloc(sizeof(t)))
#define _XkbTypedCalloc(n,t) ((t *)Xcalloc((n),sizeof(t)))
#define _XkbTypedRealloc(o,n,t) \
((o) ? (t *)Xreallocarray((o), (n), sizeof(t)) : _XkbTypedCalloc(n,t))
#define _XkbClearElems(a,f,l,t) bzero(&(a)[f], ((l)-(f)+1) * sizeof(t))
#define _XkbFree(p) Xfree(p)
/* Resizes array to hold new_num elements, zeroing newly added entries.
Destroys old array on failure. */
#define _XkbResizeArray(array, old_num, new_num, type) \
do { \
if (array == NULL) { \
array = _XkbTypedCalloc(new_num, type); \
} else { \
type *prev_array = array; \
array = Xreallocarray(array, new_num, sizeof(type)); \
if (_X_UNLIKELY((array) == NULL)) { \
_XkbFree(prev_array); \
} else if ((new_num) > (old_num)) { \
bzero(&array[old_num], \
((new_num) - (old_num)) * sizeof(type)); \
} \
} \
} while(0)
_XFUNCPROTOBEGIN
extern void _XkbReloadDpy(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment