dbe: simplify temporary buffer allocation via local stack

Small buffers easily fit on stack, so their allocation can be simplified.
No need to go through heap and have extra free() calls on each return point.

Signed-off-by: Enrico Weigelt, metux IT consult <info@metux.net>
This commit is contained in:
Enrico Weigelt, metux IT consult 2024-07-16 19:43:01 +02:00
parent f1c658e581
commit 3f37b07864

View File

@ -442,7 +442,6 @@ ProcDbeSwapBuffers(ClientPtr client)
REQUEST(xDbeSwapBuffersReq); REQUEST(xDbeSwapBuffersReq);
WindowPtr pWin; WindowPtr pWin;
DbeScreenPrivPtr pDbeScreenPriv; DbeScreenPrivPtr pDbeScreenPriv;
DbeSwapInfoPtr swapInfo;
xDbeSwapInfo *dbeSwapInfo; xDbeSwapInfo *dbeSwapInfo;
int error; int error;
unsigned int i, j; unsigned int i, j;
@ -465,10 +464,7 @@ ProcDbeSwapBuffers(ClientPtr client)
dbeSwapInfo = (xDbeSwapInfo *) &stuff[1]; dbeSwapInfo = (xDbeSwapInfo *) &stuff[1];
/* Allocate array to record swap information. */ /* Allocate array to record swap information. */
swapInfo = xallocarray(nStuff, sizeof(DbeSwapInfoRec)); DbeSwapInfoRec swapInfo[nStuff];
if (swapInfo == NULL) {
return BadAlloc;
}
for (i = 0; i < nStuff; i++) { for (i = 0; i < nStuff; i++) {
/* Check all windows to swap. */ /* Check all windows to swap. */
@ -476,38 +472,29 @@ ProcDbeSwapBuffers(ClientPtr client)
/* Each window must be a valid window - BadWindow. */ /* Each window must be a valid window - BadWindow. */
error = dixLookupWindow(&pWin, dbeSwapInfo[i].window, client, error = dixLookupWindow(&pWin, dbeSwapInfo[i].window, client,
DixWriteAccess); DixWriteAccess);
if (error != Success) { if (error != Success)
free(swapInfo);
return error; return error;
}
/* Each window must be double-buffered - BadMatch. */ /* Each window must be double-buffered - BadMatch. */
if (DBE_WINDOW_PRIV(pWin) == NULL) { if (DBE_WINDOW_PRIV(pWin) == NULL)
free(swapInfo);
return BadMatch; return BadMatch;
}
/* Each window must only be specified once - BadMatch. */ /* Each window must only be specified once - BadMatch. */
for (j = i + 1; j < nStuff; j++) { for (j = i + 1; j < nStuff; j++) {
if (dbeSwapInfo[i].window == dbeSwapInfo[j].window) { if (dbeSwapInfo[i].window == dbeSwapInfo[j].window)
free(swapInfo);
return BadMatch; return BadMatch;
}
} }
/* Each swap action must be valid - BadValue. */ /* Each swap action must be valid - BadValue. */
if ((dbeSwapInfo[i].swapAction != XdbeUndefined) && if ((dbeSwapInfo[i].swapAction != XdbeUndefined) &&
(dbeSwapInfo[i].swapAction != XdbeBackground) && (dbeSwapInfo[i].swapAction != XdbeBackground) &&
(dbeSwapInfo[i].swapAction != XdbeUntouched) && (dbeSwapInfo[i].swapAction != XdbeUntouched) &&
(dbeSwapInfo[i].swapAction != XdbeCopied)) { (dbeSwapInfo[i].swapAction != XdbeCopied))
free(swapInfo);
return BadValue; return BadValue;
}
/* Everything checks out OK. Fill in the swap info array. */ /* Everything checks out OK. Fill in the swap info array. */
swapInfo[i].pWindow = pWin; swapInfo[i].pWindow = pWin;
swapInfo[i].swapAction = dbeSwapInfo[i].swapAction; swapInfo[i].swapAction = dbeSwapInfo[i].swapAction;
} /* for (i = 0; i < nStuff; i++) */ } /* for (i = 0; i < nStuff; i++) */
/* Call the DDX routine to perform the swap(s). The DDX routine should /* Call the DDX routine to perform the swap(s). The DDX routine should
@ -527,17 +514,13 @@ ProcDbeSwapBuffers(ClientPtr client)
while (nStuff_i > 0) { while (nStuff_i > 0) {
pDbeScreenPriv = DBE_SCREEN_PRIV_FROM_WINDOW(swapInfo[0].pWindow); pDbeScreenPriv = DBE_SCREEN_PRIV_FROM_WINDOW(swapInfo[0].pWindow);
error = (*pDbeScreenPriv->SwapBuffers) (client, &nStuff_i, swapInfo); error = (*pDbeScreenPriv->SwapBuffers) (client, &nStuff_i, swapInfo);
if (error != Success) { if (error != Success)
free(swapInfo);
return error; return error;
}
} }
free(swapInfo);
return Success; return Success;
}
} /* ProcDbeSwapBuffers() */
/****************************************************************************** /******************************************************************************
* *
* DBE DIX Procedure: ProcDbeGetVisualInfo * DBE DIX Procedure: ProcDbeGetVisualInfo
@ -561,13 +544,10 @@ ProcDbeGetVisualInfo(ClientPtr client)
REQUEST(xDbeGetVisualInfoReq); REQUEST(xDbeGetVisualInfoReq);
DbeScreenPrivPtr pDbeScreenPriv; DbeScreenPrivPtr pDbeScreenPriv;
xDbeGetVisualInfoReply rep; xDbeGetVisualInfoReply rep;
Drawable *drawables;
DrawablePtr *pDrawables = NULL;
register int i, j, rc; register int i, j, rc;
register int count; /* number of visual infos in reply */ register int count; /* number of visual infos in reply */
register int length; /* length of reply */ register int length; /* length of reply */
ScreenPtr pScreen; ScreenPtr pScreen;
XdbeScreenVisualInfo *pScrVisInfo;
REQUEST_AT_LEAST_SIZE(xDbeGetVisualInfoReq); REQUEST_AT_LEAST_SIZE(xDbeGetVisualInfoReq);
if (stuff->n > UINT32_MAX / sizeof(CARD32)) if (stuff->n > UINT32_MAX / sizeof(CARD32))
@ -576,30 +556,25 @@ ProcDbeGetVisualInfo(ClientPtr client)
if (stuff->n > UINT32_MAX / sizeof(DrawablePtr)) if (stuff->n > UINT32_MAX / sizeof(DrawablePtr))
return BadAlloc; return BadAlloc;
DrawablePtr pDrawables[stuff->n * sizeof(DrawablePtr)];
/* Make sure any specified drawables are valid. */ /* Make sure any specified drawables are valid. */
if (stuff->n != 0) { if (stuff->n != 0) {
if (!(pDrawables = xallocarray(stuff->n, sizeof(DrawablePtr)))) { Drawable *drawables = (Drawable *) &stuff[1];
return BadAlloc;
}
drawables = (Drawable *) &stuff[1];
for (i = 0; i < stuff->n; i++) { for (i = 0; i < stuff->n; i++) {
rc = dixLookupDrawable(pDrawables + i, drawables[i], client, 0, rc = dixLookupDrawable(pDrawables + i, drawables[i], client, 0,
DixGetAttrAccess); DixGetAttrAccess);
if (rc != Success) { if (rc != Success) {
free(pDrawables);
return rc; return rc;
} }
} }
} }
count = (stuff->n == 0) ? screenInfo.numScreens : stuff->n; count = (stuff->n == 0) ? screenInfo.numScreens : stuff->n;
if (!(pScrVisInfo = calloc(count, sizeof(XdbeScreenVisualInfo)))) { XdbeScreenVisualInfo pScrVisInfo[count];
free(pDrawables); memset(pScrVisInfo, 0, sizeof(pScrVisInfo));
return BadAlloc;
}
length = 0; length = 0;
@ -690,9 +665,6 @@ ProcDbeGetVisualInfo(ClientPtr client)
for (i = 0; i < count; i++) { for (i = 0; i < count; i++) {
free(pScrVisInfo[i].visinfo); free(pScrVisInfo[i].visinfo);
} }
free(pScrVisInfo);
free(pDrawables);
return rc; return rc;