(!1614) xfixes: use stack allocation and static init for reply structs
Canonicalize all reply structures onto stack allocation and static initialization, like already done in most other extension. So make the code easier to understand and allow further simplifications by subsequent commits. Also gaining a little bit efficiency by skipping some heap allocations. Dynamically sized buffers (where the upper bound isn't known), are still allocated on heap. Signed-off-by: Enrico Weigelt, metux IT consult <info@metux.net>
This commit is contained in:
		
							parent
							
								
									4426951b20
								
							
						
					
					
						commit
						b29ffcd79c
					
				
							
								
								
									
										145
									
								
								xfixes/cursor.c
								
								
								
								
							
							
						
						
									
										145
									
								
								xfixes/cursor.c
								
								
								
								
							|  | @ -355,9 +355,7 @@ int | ||||||
| ProcXFixesGetCursorImage(ClientPtr client) | ProcXFixesGetCursorImage(ClientPtr client) | ||||||
| { | { | ||||||
| /*    REQUEST(xXFixesGetCursorImageReq); */ | /*    REQUEST(xXFixesGetCursorImageReq); */ | ||||||
|     xXFixesGetCursorImageReply *rep; |  | ||||||
|     CursorPtr pCursor; |     CursorPtr pCursor; | ||||||
|     CARD32 *image; |  | ||||||
|     int npixels, width, height, rc, x, y; |     int npixels, width, height, rc, x, y; | ||||||
| 
 | 
 | ||||||
|     REQUEST_SIZE_MATCH(xXFixesGetCursorImageReq); |     REQUEST_SIZE_MATCH(xXFixesGetCursorImageReq); | ||||||
|  | @ -372,39 +370,41 @@ ProcXFixesGetCursorImage(ClientPtr client) | ||||||
|     width = pCursor->bits->width; |     width = pCursor->bits->width; | ||||||
|     height = pCursor->bits->height; |     height = pCursor->bits->height; | ||||||
|     npixels = width * height; |     npixels = width * height; | ||||||
|     rep = calloc(1, | 
 | ||||||
|                  sizeof(xXFixesGetCursorImageReply) + npixels * sizeof(CARD32)); |     CARD32 *image = calloc(npixels, sizeof(CARD32)); | ||||||
|     if (!rep) |     if (!image) | ||||||
|         return BadAlloc; |         return BadAlloc; | ||||||
| 
 | 
 | ||||||
|     rep->type = X_Reply; |  | ||||||
|     rep->sequenceNumber = client->sequence; |  | ||||||
|     rep->length = npixels; |  | ||||||
|     rep->width = width; |  | ||||||
|     rep->height = height; |  | ||||||
|     rep->x = x; |  | ||||||
|     rep->y = y; |  | ||||||
|     rep->xhot = pCursor->bits->xhot; |  | ||||||
|     rep->yhot = pCursor->bits->yhot; |  | ||||||
|     rep->cursorSerial = pCursor->serialNumber; |  | ||||||
| 
 |  | ||||||
|     image = (CARD32 *) (rep + 1); |  | ||||||
|     CopyCursorToImage(pCursor, image); |     CopyCursorToImage(pCursor, image); | ||||||
|  | 
 | ||||||
|  |     xXFixesGetCursorImageReply rep = { | ||||||
|  |         .type = X_Reply, | ||||||
|  |         .sequenceNumber = client->sequence, | ||||||
|  |         .length = npixels, | ||||||
|  |         .width = width, | ||||||
|  |         .height = height, | ||||||
|  |         .x = x, | ||||||
|  |         .y = y, | ||||||
|  |         .xhot = pCursor->bits->xhot, | ||||||
|  |         .yhot = pCursor->bits->yhot, | ||||||
|  |         .cursorSerial = pCursor->serialNumber, | ||||||
|  |     }; | ||||||
|  | 
 | ||||||
|     if (client->swapped) { |     if (client->swapped) { | ||||||
|         swaps(&rep->sequenceNumber); |         swaps(&rep.sequenceNumber); | ||||||
|         swapl(&rep->length); |         swapl(&rep.length); | ||||||
|         swaps(&rep->x); |         swaps(&rep.x); | ||||||
|         swaps(&rep->y); |         swaps(&rep.y); | ||||||
|         swaps(&rep->width); |         swaps(&rep.width); | ||||||
|         swaps(&rep->height); |         swaps(&rep.height); | ||||||
|         swaps(&rep->xhot); |         swaps(&rep.xhot); | ||||||
|         swaps(&rep->yhot); |         swaps(&rep.yhot); | ||||||
|         swapl(&rep->cursorSerial); |         swapl(&rep.cursorSerial); | ||||||
|         SwapLongs(image, npixels); |         SwapLongs(image, npixels); | ||||||
|     } |     } | ||||||
|     WriteToClient(client, |     WriteToClient(client, sizeof(xXFixesGetCursorImageReply), &rep); | ||||||
|                   sizeof(xXFixesGetCursorImageReply) + (npixels << 2), rep); |     WriteToClient(client, npixels * sizeof(CARD32), image); | ||||||
|     free(rep); |     free(image); | ||||||
|     return Success; |     return Success; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | @ -448,10 +448,9 @@ SProcXFixesSetCursorName(ClientPtr client) | ||||||
| int | int | ||||||
| ProcXFixesGetCursorName(ClientPtr client) | ProcXFixesGetCursorName(ClientPtr client) | ||||||
| { | { | ||||||
|     CursorPtr pCursor; |  | ||||||
|     xXFixesGetCursorNameReply reply; |  | ||||||
| 
 |  | ||||||
|     REQUEST(xXFixesGetCursorNameReq); |     REQUEST(xXFixesGetCursorNameReq); | ||||||
|  | 
 | ||||||
|  |     CursorPtr pCursor; | ||||||
|     const char *str; |     const char *str; | ||||||
|     int len; |     int len; | ||||||
| 
 | 
 | ||||||
|  | @ -463,7 +462,7 @@ ProcXFixesGetCursorName(ClientPtr client) | ||||||
|         str = ""; |         str = ""; | ||||||
|     len = strlen(str); |     len = strlen(str); | ||||||
| 
 | 
 | ||||||
|     reply = (xXFixesGetCursorNameReply) { |     xXFixesGetCursorNameReply rep = { | ||||||
|         .type = X_Reply, |         .type = X_Reply, | ||||||
|         .sequenceNumber = client->sequence, |         .sequenceNumber = client->sequence, | ||||||
|         .length = bytes_to_int32(len), |         .length = bytes_to_int32(len), | ||||||
|  | @ -471,12 +470,12 @@ ProcXFixesGetCursorName(ClientPtr client) | ||||||
|         .nbytes = len |         .nbytes = len | ||||||
|     }; |     }; | ||||||
|     if (client->swapped) { |     if (client->swapped) { | ||||||
|         swaps(&reply.sequenceNumber); |         swaps(&rep.sequenceNumber); | ||||||
|         swapl(&reply.length); |         swapl(&rep.length); | ||||||
|         swapl(&reply.atom); |         swapl(&rep.atom); | ||||||
|         swaps(&reply.nbytes); |         swaps(&rep.nbytes); | ||||||
|     } |     } | ||||||
|     WriteReplyToClient(client, sizeof(xXFixesGetCursorNameReply), &reply); |     WriteReplyToClient(client, sizeof(xXFixesGetCursorNameReply), &rep); | ||||||
|     WriteToClient(client, len, str); |     WriteToClient(client, len, str); | ||||||
| 
 | 
 | ||||||
|     return Success; |     return Success; | ||||||
|  | @ -495,12 +494,10 @@ int | ||||||
| ProcXFixesGetCursorImageAndName(ClientPtr client) | ProcXFixesGetCursorImageAndName(ClientPtr client) | ||||||
| { | { | ||||||
| /*    REQUEST(xXFixesGetCursorImageAndNameReq); */ | /*    REQUEST(xXFixesGetCursorImageAndNameReq); */ | ||||||
|     xXFixesGetCursorImageAndNameReply *rep; |  | ||||||
|     CursorPtr pCursor; |     CursorPtr pCursor; | ||||||
|     CARD32 *image; |  | ||||||
|     int npixels; |     int npixels; | ||||||
|     const char *name; |     const char *name; | ||||||
|     int nbytes, nbytesRound; |     int nbytes; | ||||||
|     int width, height; |     int width, height; | ||||||
|     int rc, x, y; |     int rc, x, y; | ||||||
| 
 | 
 | ||||||
|  | @ -518,45 +515,47 @@ ProcXFixesGetCursorImageAndName(ClientPtr client) | ||||||
|     npixels = width * height; |     npixels = width * height; | ||||||
|     name = pCursor->name ? NameForAtom(pCursor->name) : ""; |     name = pCursor->name ? NameForAtom(pCursor->name) : ""; | ||||||
|     nbytes = strlen(name); |     nbytes = strlen(name); | ||||||
|     nbytesRound = pad_to_int32(nbytes); | 
 | ||||||
|     rep = calloc(1, sizeof(xXFixesGetCursorImageAndNameReply) + |     // pixmap plus name (padded to 4 bytes)
 | ||||||
|                  npixels * sizeof(CARD32) + nbytesRound); |     CARD32 *image = calloc(npixels + bytes_to_int32(nbytes), sizeof(CARD32)); | ||||||
|     if (!rep) |     if (!image) | ||||||
|         return BadAlloc; |         return BadAlloc; | ||||||
| 
 | 
 | ||||||
|     rep->type = X_Reply; |  | ||||||
|     rep->sequenceNumber = client->sequence; |  | ||||||
|     rep->length = npixels + bytes_to_int32(nbytesRound); |  | ||||||
|     rep->width = width; |  | ||||||
|     rep->height = height; |  | ||||||
|     rep->x = x; |  | ||||||
|     rep->y = y; |  | ||||||
|     rep->xhot = pCursor->bits->xhot; |  | ||||||
|     rep->yhot = pCursor->bits->yhot; |  | ||||||
|     rep->cursorSerial = pCursor->serialNumber; |  | ||||||
|     rep->cursorName = pCursor->name; |  | ||||||
|     rep->nbytes = nbytes; |  | ||||||
| 
 |  | ||||||
|     image = (CARD32 *) (rep + 1); |  | ||||||
|     CopyCursorToImage(pCursor, image); |     CopyCursorToImage(pCursor, image); | ||||||
|     memcpy((image + npixels), name, nbytes); |     memcpy((image + npixels), name, nbytes); | ||||||
|  | 
 | ||||||
|  |     xXFixesGetCursorImageAndNameReply rep = { | ||||||
|  |         .type = X_Reply, | ||||||
|  |         .sequenceNumber = client->sequence, | ||||||
|  |         .length = bytes_to_int32(sizeof(image)), | ||||||
|  |         .width = width, | ||||||
|  |         .height = height, | ||||||
|  |         .x = x, | ||||||
|  |         .y = y, | ||||||
|  |         .xhot = pCursor->bits->xhot, | ||||||
|  |         .yhot = pCursor->bits->yhot, | ||||||
|  |         .cursorSerial = pCursor->serialNumber, | ||||||
|  |         .cursorName = pCursor->name, | ||||||
|  |         .nbytes = nbytes, | ||||||
|  |     }; | ||||||
|  | 
 | ||||||
|     if (client->swapped) { |     if (client->swapped) { | ||||||
|         swaps(&rep->sequenceNumber); |         swaps(&rep.sequenceNumber); | ||||||
|         swapl(&rep->length); |         swapl(&rep.length); | ||||||
|         swaps(&rep->x); |         swaps(&rep.x); | ||||||
|         swaps(&rep->y); |         swaps(&rep.y); | ||||||
|         swaps(&rep->width); |         swaps(&rep.width); | ||||||
|         swaps(&rep->height); |         swaps(&rep.height); | ||||||
|         swaps(&rep->xhot); |         swaps(&rep.xhot); | ||||||
|         swaps(&rep->yhot); |         swaps(&rep.yhot); | ||||||
|         swapl(&rep->cursorSerial); |         swapl(&rep.cursorSerial); | ||||||
|         swapl(&rep->cursorName); |         swapl(&rep.cursorName); | ||||||
|         swaps(&rep->nbytes); |         swaps(&rep.nbytes); | ||||||
|         SwapLongs(image, npixels); |         SwapLongs(image, npixels); | ||||||
|     } |     } | ||||||
|     WriteToClient(client, sizeof(xXFixesGetCursorImageAndNameReply) + |     WriteToClient(client, sizeof(xXFixesGetCursorImageAndNameReply), &rep); | ||||||
|                   (npixels << 2) + nbytesRound, rep); |     WriteToClient(client, sizeof(image), image); | ||||||
|     free(rep); |     free(image); | ||||||
|     return Success; |     return Success; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -87,21 +87,20 @@ int | ||||||
| ProcXFixesGetClientDisconnectMode(ClientPtr client) | ProcXFixesGetClientDisconnectMode(ClientPtr client) | ||||||
| { | { | ||||||
|     ClientDisconnectPtr pDisconnect = GetClientDisconnect(client); |     ClientDisconnectPtr pDisconnect = GetClientDisconnect(client); | ||||||
|     xXFixesGetClientDisconnectModeReply reply; |  | ||||||
| 
 | 
 | ||||||
|     REQUEST_SIZE_MATCH(xXFixesGetClientDisconnectModeReq); |     REQUEST_SIZE_MATCH(xXFixesGetClientDisconnectModeReq); | ||||||
| 
 | 
 | ||||||
|     reply = (xXFixesGetClientDisconnectModeReply) { |     xXFixesGetClientDisconnectModeReply rep = { | ||||||
|         .type = X_Reply, |         .type = X_Reply, | ||||||
|         .sequenceNumber = client->sequence, |         .sequenceNumber = client->sequence, | ||||||
|         .length = 0, |         .length = 0, | ||||||
|         .disconnect_mode = pDisconnect->disconnect_mode, |         .disconnect_mode = pDisconnect->disconnect_mode, | ||||||
|     }; |     }; | ||||||
|     if (client->swapped) { |     if (client->swapped) { | ||||||
|         swaps(&reply.sequenceNumber); |         swaps(&rep.sequenceNumber); | ||||||
|         swapl(&reply.disconnect_mode); |         swapl(&rep.disconnect_mode); | ||||||
|     } |     } | ||||||
|     WriteToClient(client, sizeof(xXFixesGetClientDisconnectModeReply), &reply); |     WriteToClient(client, sizeof(xXFixesGetClientDisconnectModeReply), &rep); | ||||||
| 
 | 
 | ||||||
|     return Success; |     return Success; | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -507,8 +507,6 @@ int | ||||||
| ProcXFixesFetchRegion(ClientPtr client) | ProcXFixesFetchRegion(ClientPtr client) | ||||||
| { | { | ||||||
|     RegionPtr pRegion; |     RegionPtr pRegion; | ||||||
|     xXFixesFetchRegionReply *reply; |  | ||||||
|     xRectangle *pRect; |  | ||||||
|     BoxPtr pExtent; |     BoxPtr pExtent; | ||||||
|     BoxPtr pBox; |     BoxPtr pBox; | ||||||
|     int i, nBox; |     int i, nBox; | ||||||
|  | @ -522,37 +520,39 @@ ProcXFixesFetchRegion(ClientPtr client) | ||||||
|     pBox = RegionRects(pRegion); |     pBox = RegionRects(pRegion); | ||||||
|     nBox = RegionNumRects(pRegion); |     nBox = RegionNumRects(pRegion); | ||||||
| 
 | 
 | ||||||
|     reply = calloc(sizeof(xXFixesFetchRegionReply) + nBox * sizeof(xRectangle), |     xRectangle *pRect = calloc(nBox, sizeof(xRectangle)); | ||||||
|                    1); |     if (!pRect) | ||||||
|     if (!reply) |  | ||||||
|         return BadAlloc; |         return BadAlloc; | ||||||
|     reply->type = X_Reply; |  | ||||||
|     reply->sequenceNumber = client->sequence; |  | ||||||
|     reply->length = nBox << 1; |  | ||||||
|     reply->x = pExtent->x1; |  | ||||||
|     reply->y = pExtent->y1; |  | ||||||
|     reply->width = pExtent->x2 - pExtent->x1; |  | ||||||
|     reply->height = pExtent->y2 - pExtent->y1; |  | ||||||
| 
 | 
 | ||||||
|     pRect = (xRectangle *) (reply + 1); |  | ||||||
|     for (i = 0; i < nBox; i++) { |     for (i = 0; i < nBox; i++) { | ||||||
|         pRect[i].x = pBox[i].x1; |         pRect[i].x = pBox[i].x1; | ||||||
|         pRect[i].y = pBox[i].y1; |         pRect[i].y = pBox[i].y1; | ||||||
|         pRect[i].width = pBox[i].x2 - pBox[i].x1; |         pRect[i].width = pBox[i].x2 - pBox[i].x1; | ||||||
|         pRect[i].height = pBox[i].y2 - pBox[i].y1; |         pRect[i].height = pBox[i].y2 - pBox[i].y1; | ||||||
|     } |     } | ||||||
|  | 
 | ||||||
|  |     xXFixesFetchRegionReply rep = { | ||||||
|  |         .type = X_Reply, | ||||||
|  |         .sequenceNumber = client->sequence, | ||||||
|  |         .length = nBox << 1, | ||||||
|  |         .x = pExtent->x1, | ||||||
|  |         .y = pExtent->y1, | ||||||
|  |         .width = pExtent->x2 - pExtent->x1, | ||||||
|  |         .height = pExtent->y2 - pExtent->y1, | ||||||
|  |     }; | ||||||
|  | 
 | ||||||
|     if (client->swapped) { |     if (client->swapped) { | ||||||
|         swaps(&reply->sequenceNumber); |         swaps(&rep.sequenceNumber); | ||||||
|         swapl(&reply->length); |         swapl(&rep.length); | ||||||
|         swaps(&reply->x); |         swaps(&rep.x); | ||||||
|         swaps(&reply->y); |         swaps(&rep.y); | ||||||
|         swaps(&reply->width); |         swaps(&rep.width); | ||||||
|         swaps(&reply->height); |         swaps(&rep.height); | ||||||
|         SwapShorts((INT16 *) pRect, nBox * 4); |         SwapShorts((INT16 *) pRect, nBox * 4); | ||||||
|     } |     } | ||||||
|     WriteToClient(client, sizeof(xXFixesFetchRegionReply) + |     WriteToClient(client, sizeof(xXFixesFetchRegionReply), &rep); | ||||||
|                          nBox * sizeof(xRectangle), (char *) reply); |     WriteToClient(client, sizeof(pRect), pRect); | ||||||
|     free(reply); |     free(pRect); | ||||||
|     return Success; |     return Success; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue