PageRenderTime 55ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/dix/resource.c

https://github.com/cubanismo/xserver
C | 1247 lines | 800 code | 133 blank | 314 comment | 143 complexity | 79d08a6aa796bbf5ca0395c2c36be11a MD5 | raw file
Possible License(s): MIT
  1. /************************************************************
  2. Copyright 1987, 1998 The Open Group
  3. Permission to use, copy, modify, distribute, and sell this software and its
  4. documentation for any purpose is hereby granted without fee, provided that
  5. the above copyright notice appear in all copies and that both that
  6. copyright notice and this permission notice appear in supporting
  7. documentation.
  8. The above copyright notice and this permission notice shall be included in
  9. all copies or substantial portions of the Software.
  10. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  11. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  12. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  13. OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  14. AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  15. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. Except as contained in this notice, the name of The Open Group shall not be
  17. used in advertising or otherwise to promote the sale, use or other dealings
  18. in this Software without prior written authorization from The Open Group.
  19. Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts.
  20. All Rights Reserved
  21. Permission to use, copy, modify, and distribute this software and its
  22. documentation for any purpose and without fee is hereby granted,
  23. provided that the above copyright notice appear in all copies and that
  24. both that copyright notice and this permission notice appear in
  25. supporting documentation, and that the name of Digital not be
  26. used in advertising or publicity pertaining to distribution of the
  27. software without specific, written prior permission.
  28. DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  29. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  30. DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  31. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  32. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  33. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  34. SOFTWARE.
  35. ********************************************************/
  36. /* The panoramix components contained the following notice */
  37. /*****************************************************************
  38. Copyright (c) 1991, 1997 Digital Equipment Corporation, Maynard, Massachusetts.
  39. Permission is hereby granted, free of charge, to any person obtaining a copy
  40. of this software and associated documentation files (the "Software"), to deal
  41. in the Software without restriction, including without limitation the rights
  42. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  43. copies of the Software.
  44. The above copyright notice and this permission notice shall be included in
  45. all copies or substantial portions of the Software.
  46. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  47. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  48. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  49. DIGITAL EQUIPMENT CORPORATION BE LIABLE FOR ANY CLAIM, DAMAGES, INCLUDING,
  50. BUT NOT LIMITED TO CONSEQUENTIAL OR INCIDENTAL DAMAGES, OR OTHER LIABILITY,
  51. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
  52. IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  53. Except as contained in this notice, the name of Digital Equipment Corporation
  54. shall not be used in advertising or otherwise to promote the sale, use or other
  55. dealings in this Software without prior written authorization from Digital
  56. Equipment Corporation.
  57. ******************************************************************/
  58. /* XSERVER_DTRACE additions:
  59. * Copyright (c) 2005-2006, Oracle and/or its affiliates. All rights reserved.
  60. *
  61. * Permission is hereby granted, free of charge, to any person obtaining a
  62. * copy of this software and associated documentation files (the "Software"),
  63. * to deal in the Software without restriction, including without limitation
  64. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  65. * and/or sell copies of the Software, and to permit persons to whom the
  66. * Software is furnished to do so, subject to the following conditions:
  67. *
  68. * The above copyright notice and this permission notice (including the next
  69. * paragraph) shall be included in all copies or substantial portions of the
  70. * Software.
  71. *
  72. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  73. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  74. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  75. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  76. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  77. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  78. * DEALINGS IN THE SOFTWARE.
  79. */
  80. /* Routines to manage various kinds of resources:
  81. *
  82. * CreateNewResourceType, CreateNewResourceClass, InitClientResources,
  83. * FakeClientID, AddResource, FreeResource, FreeClientResources,
  84. * FreeAllResources, LookupIDByType, LookupIDByClass, GetXIDRange
  85. */
  86. /*
  87. * A resource ID is a 32 bit quantity, the upper 2 bits of which are
  88. * off-limits for client-visible resources. The next 8 bits are
  89. * used as client ID, and the low 22 bits come from the client.
  90. * A resource ID is "hashed" by extracting and xoring subfields
  91. * (varying with the size of the hash table).
  92. *
  93. * It is sometimes necessary for the server to create an ID that looks
  94. * like it belongs to a client. This ID, however, must not be one
  95. * the client actually can create, or we have the potential for conflict.
  96. * The 31st bit of the ID is reserved for the server's use for this
  97. * purpose. By setting CLIENT_ID(id) to the client, the SERVER_BIT to
  98. * 1, and an otherwise arbitrary ID in the low 22 bits, we can create a
  99. * resource "owned" by the client.
  100. */
  101. #ifdef HAVE_DIX_CONFIG_H
  102. #include <dix-config.h>
  103. #endif
  104. #include <X11/X.h>
  105. #include "misc.h"
  106. #include "os.h"
  107. #include "resource.h"
  108. #include "dixstruct.h"
  109. #include "opaque.h"
  110. #include "windowstr.h"
  111. #include "dixfont.h"
  112. #include "colormap.h"
  113. #include "inputstr.h"
  114. #include "dixevents.h"
  115. #include "dixgrabs.h"
  116. #include "cursor.h"
  117. #ifdef PANORAMIX
  118. #include "panoramiX.h"
  119. #include "panoramiXsrv.h"
  120. #endif
  121. #include "xace.h"
  122. #include <assert.h>
  123. #include "registry.h"
  124. #include "gcstruct.h"
  125. #ifdef XSERVER_DTRACE
  126. #include <sys/types.h>
  127. typedef const char *string;
  128. #include "Xserver-dtrace.h"
  129. #define TypeNameString(t) LookupResourceName(t)
  130. #endif
  131. static void RebuildTable(int /*client */
  132. );
  133. #define SERVER_MINID 32
  134. #define INITBUCKETS 64
  135. #define INITHASHSIZE 6
  136. #define MAXHASHSIZE 11
  137. typedef struct _Resource {
  138. struct _Resource *next;
  139. XID id;
  140. RESTYPE type;
  141. void *value;
  142. } ResourceRec, *ResourcePtr;
  143. typedef struct _ClientResource {
  144. ResourcePtr *resources;
  145. int elements;
  146. int buckets;
  147. int hashsize; /* log(2)(buckets) */
  148. XID fakeID;
  149. XID endFakeID;
  150. } ClientResourceRec;
  151. RESTYPE lastResourceType;
  152. static RESTYPE lastResourceClass;
  153. RESTYPE TypeMask;
  154. struct ResourceType {
  155. DeleteType deleteFunc;
  156. SizeType sizeFunc;
  157. FindTypeSubResources findSubResFunc;
  158. int errorValue;
  159. };
  160. /**
  161. * Used by all resources that don't specify a function to calculate
  162. * resource size. Currently this is used for all resources with
  163. * insignificant memory usage.
  164. *
  165. * @see GetResourceTypeSizeFunc, SetResourceTypeSizeFunc
  166. *
  167. * @param[in] value Pointer to resource object.
  168. *
  169. * @param[in] id Resource ID for the object.
  170. *
  171. * @param[out] size Fill all fields to zero to indicate that size of
  172. * resource can't be determined.
  173. */
  174. static void
  175. GetDefaultBytes(void *value, XID id, ResourceSizePtr size)
  176. {
  177. size->resourceSize = 0;
  178. size->pixmapRefSize = 0;
  179. size->refCnt = 1;
  180. }
  181. /**
  182. * Used by all resources that don't specify a function to iterate
  183. * through subresources. Currently this is used for all resources with
  184. * insignificant memory usage.
  185. *
  186. * @see FindSubResources, SetResourceTypeFindSubResFunc
  187. *
  188. * @param[in] value Pointer to resource object.
  189. *
  190. * @param[in] func Function to call for each subresource.
  191. * @param[out] cdata Pointer to opaque data.
  192. */
  193. static void
  194. DefaultFindSubRes(void *value, FindAllRes func, void *cdata)
  195. {
  196. /* do nothing */
  197. }
  198. /**
  199. * Calculate drawable size in bytes. Reference counting is not taken
  200. * into account.
  201. *
  202. * @param[in] drawable Pointer to a drawable.
  203. *
  204. * @return Estimate of total memory usage for the drawable.
  205. */
  206. static unsigned long
  207. GetDrawableBytes(DrawablePtr drawable)
  208. {
  209. int bytes = 0;
  210. if (drawable)
  211. {
  212. int bytesPerPixel = drawable->bitsPerPixel >> 3;
  213. int numberOfPixels = drawable->width * drawable->height;
  214. bytes = numberOfPixels * bytesPerPixel;
  215. }
  216. return bytes;
  217. }
  218. /**
  219. * Calculate pixmap size in bytes. Reference counting is taken into
  220. * account. Any extra data attached by extensions and drivers is not
  221. * taken into account. The purpose of this function is to estimate
  222. * memory usage that can be attributed to single reference of the
  223. * pixmap.
  224. *
  225. * @param[in] value Pointer to a pixmap.
  226. *
  227. * @param[in] id Resource ID of pixmap. If the pixmap hasn't been
  228. * added as resource, just pass value->drawable.id.
  229. *
  230. * @param[out] size Estimate of memory usage attributed to a single
  231. * pixmap reference.
  232. */
  233. static void
  234. GetPixmapBytes(void *value, XID id, ResourceSizePtr size)
  235. {
  236. PixmapPtr pixmap = value;
  237. size->resourceSize = 0;
  238. size->pixmapRefSize = 0;
  239. size->refCnt = pixmap->refcnt;
  240. if (pixmap && pixmap->refcnt)
  241. {
  242. DrawablePtr drawable = &pixmap->drawable;
  243. size->resourceSize = GetDrawableBytes(drawable);
  244. size->pixmapRefSize = size->resourceSize / pixmap->refcnt;
  245. }
  246. }
  247. /**
  248. * Calculate window size in bytes. The purpose of this function is to
  249. * estimate memory usage that can be attributed to all pixmap
  250. * references of the window.
  251. *
  252. * @param[in] value Pointer to a window.
  253. *
  254. * @param[in] id Resource ID of window.
  255. *
  256. * @param[out] size Estimate of memory usage attributed to a all
  257. * pixmap references of a window.
  258. */
  259. static void
  260. GetWindowBytes(void *value, XID id, ResourceSizePtr size)
  261. {
  262. SizeType pixmapSizeFunc = GetResourceTypeSizeFunc(RT_PIXMAP);
  263. ResourceSizeRec pixmapSize = { 0, 0, 0 };
  264. WindowPtr window = value;
  265. /* Currently only pixmap bytes are reported to clients. */
  266. size->resourceSize = 0;
  267. /* Calculate pixmap reference sizes. */
  268. size->pixmapRefSize = 0;
  269. size->refCnt = 1;
  270. if (window->backgroundState == BackgroundPixmap)
  271. {
  272. PixmapPtr pixmap = window->background.pixmap;
  273. pixmapSizeFunc(pixmap, pixmap->drawable.id, &pixmapSize);
  274. size->pixmapRefSize += pixmapSize.pixmapRefSize;
  275. }
  276. if (window->border.pixmap && !window->borderIsPixel)
  277. {
  278. PixmapPtr pixmap = window->border.pixmap;
  279. pixmapSizeFunc(pixmap, pixmap->drawable.id, &pixmapSize);
  280. size->pixmapRefSize += pixmapSize.pixmapRefSize;
  281. }
  282. }
  283. /**
  284. * Iterate through subresources of a window. The purpose of this
  285. * function is to gather accurate information on what resources
  286. * a resource uses.
  287. *
  288. * @note Currently only sub-pixmaps are iterated
  289. *
  290. * @param[in] value Pointer to a window
  291. *
  292. * @param[in] func Function to call with each subresource
  293. *
  294. * @param[out] cdata Pointer to opaque data
  295. */
  296. static void
  297. FindWindowSubRes(void *value, FindAllRes func, void *cdata)
  298. {
  299. WindowPtr window = value;
  300. /* Currently only pixmap subresources are reported to clients. */
  301. if (window->backgroundState == BackgroundPixmap)
  302. {
  303. PixmapPtr pixmap = window->background.pixmap;
  304. func(window->background.pixmap, pixmap->drawable.id, RT_PIXMAP, cdata);
  305. }
  306. if (window->border.pixmap && !window->borderIsPixel)
  307. {
  308. PixmapPtr pixmap = window->border.pixmap;
  309. func(window->background.pixmap, pixmap->drawable.id, RT_PIXMAP, cdata);
  310. }
  311. }
  312. /**
  313. * Calculate graphics context size in bytes. The purpose of this
  314. * function is to estimate memory usage that can be attributed to all
  315. * pixmap references of the graphics context.
  316. *
  317. * @param[in] value Pointer to a graphics context.
  318. *
  319. * @param[in] id Resource ID of graphics context.
  320. *
  321. * @param[out] size Estimate of memory usage attributed to a all
  322. * pixmap references of a graphics context.
  323. */
  324. static void
  325. GetGcBytes(void *value, XID id, ResourceSizePtr size)
  326. {
  327. SizeType pixmapSizeFunc = GetResourceTypeSizeFunc(RT_PIXMAP);
  328. ResourceSizeRec pixmapSize = { 0, 0, 0 };
  329. GCPtr gc = value;
  330. /* Currently only pixmap bytes are reported to clients. */
  331. size->resourceSize = 0;
  332. /* Calculate pixmap reference sizes. */
  333. size->pixmapRefSize = 0;
  334. size->refCnt = 1;
  335. if (gc->stipple)
  336. {
  337. PixmapPtr pixmap = gc->stipple;
  338. pixmapSizeFunc(pixmap, pixmap->drawable.id, &pixmapSize);
  339. size->pixmapRefSize += pixmapSize.pixmapRefSize;
  340. }
  341. if (gc->tile.pixmap && !gc->tileIsPixel)
  342. {
  343. PixmapPtr pixmap = gc->tile.pixmap;
  344. pixmapSizeFunc(pixmap, pixmap->drawable.id, &pixmapSize);
  345. size->pixmapRefSize += pixmapSize.pixmapRefSize;
  346. }
  347. }
  348. /**
  349. * Iterate through subresources of a graphics context. The purpose of
  350. * this function is to gather accurate information on what resources a
  351. * resource uses.
  352. *
  353. * @note Currently only sub-pixmaps are iterated
  354. *
  355. * @param[in] value Pointer to a window
  356. *
  357. * @param[in] func Function to call with each subresource
  358. *
  359. * @param[out] cdata Pointer to opaque data
  360. */
  361. static void
  362. FindGCSubRes(void *value, FindAllRes func, void *cdata)
  363. {
  364. GCPtr gc = value;
  365. /* Currently only pixmap subresources are reported to clients. */
  366. if (gc->stipple)
  367. {
  368. PixmapPtr pixmap = gc->stipple;
  369. func(pixmap, pixmap->drawable.id, RT_PIXMAP, cdata);
  370. }
  371. if (gc->tile.pixmap && !gc->tileIsPixel)
  372. {
  373. PixmapPtr pixmap = gc->tile.pixmap;
  374. func(pixmap, pixmap->drawable.id, RT_PIXMAP, cdata);
  375. }
  376. }
  377. static struct ResourceType *resourceTypes;
  378. static const struct ResourceType predefTypes[] = {
  379. [RT_NONE & (RC_LASTPREDEF - 1)] = {
  380. .deleteFunc = (DeleteType) NoopDDA,
  381. .sizeFunc = GetDefaultBytes,
  382. .findSubResFunc = DefaultFindSubRes,
  383. .errorValue = BadValue,
  384. },
  385. [RT_WINDOW & (RC_LASTPREDEF - 1)] = {
  386. .deleteFunc = DeleteWindow,
  387. .sizeFunc = GetWindowBytes,
  388. .findSubResFunc = FindWindowSubRes,
  389. .errorValue = BadWindow,
  390. },
  391. [RT_PIXMAP & (RC_LASTPREDEF - 1)] = {
  392. .deleteFunc = dixDestroyPixmap,
  393. .sizeFunc = GetPixmapBytes,
  394. .findSubResFunc = DefaultFindSubRes,
  395. .errorValue = BadPixmap,
  396. },
  397. [RT_GC & (RC_LASTPREDEF - 1)] = {
  398. .deleteFunc = FreeGC,
  399. .sizeFunc = GetGcBytes,
  400. .findSubResFunc = FindGCSubRes,
  401. .errorValue = BadGC,
  402. },
  403. [RT_FONT & (RC_LASTPREDEF - 1)] = {
  404. .deleteFunc = CloseFont,
  405. .sizeFunc = GetDefaultBytes,
  406. .findSubResFunc = DefaultFindSubRes,
  407. .errorValue = BadFont,
  408. },
  409. [RT_CURSOR & (RC_LASTPREDEF - 1)] = {
  410. .deleteFunc = FreeCursor,
  411. .sizeFunc = GetDefaultBytes,
  412. .findSubResFunc = DefaultFindSubRes,
  413. .errorValue = BadCursor,
  414. },
  415. [RT_COLORMAP & (RC_LASTPREDEF - 1)] = {
  416. .deleteFunc = FreeColormap,
  417. .sizeFunc = GetDefaultBytes,
  418. .findSubResFunc = DefaultFindSubRes,
  419. .errorValue = BadColor,
  420. },
  421. [RT_CMAPENTRY & (RC_LASTPREDEF - 1)] = {
  422. .deleteFunc = FreeClientPixels,
  423. .sizeFunc = GetDefaultBytes,
  424. .findSubResFunc = DefaultFindSubRes,
  425. .errorValue = BadColor,
  426. },
  427. [RT_OTHERCLIENT & (RC_LASTPREDEF - 1)] = {
  428. .deleteFunc = OtherClientGone,
  429. .sizeFunc = GetDefaultBytes,
  430. .findSubResFunc = DefaultFindSubRes,
  431. .errorValue = BadValue,
  432. },
  433. [RT_PASSIVEGRAB & (RC_LASTPREDEF - 1)] = {
  434. .deleteFunc = DeletePassiveGrab,
  435. .sizeFunc = GetDefaultBytes,
  436. .findSubResFunc = DefaultFindSubRes,
  437. .errorValue = BadValue,
  438. },
  439. };
  440. CallbackListPtr ResourceStateCallback;
  441. static _X_INLINE void
  442. CallResourceStateCallback(ResourceState state, ResourceRec * res)
  443. {
  444. if (ResourceStateCallback) {
  445. ResourceStateInfoRec rsi = { state, res->id, res->type, res->value };
  446. CallCallbacks(&ResourceStateCallback, &rsi);
  447. }
  448. }
  449. RESTYPE
  450. CreateNewResourceType(DeleteType deleteFunc, const char *name)
  451. {
  452. RESTYPE next = lastResourceType + 1;
  453. struct ResourceType *types;
  454. if (next & lastResourceClass)
  455. return 0;
  456. types = realloc(resourceTypes, (next + 1) * sizeof(*resourceTypes));
  457. if (!types)
  458. return 0;
  459. lastResourceType = next;
  460. resourceTypes = types;
  461. resourceTypes[next].deleteFunc = deleteFunc;
  462. resourceTypes[next].sizeFunc = GetDefaultBytes;
  463. resourceTypes[next].findSubResFunc = DefaultFindSubRes;
  464. resourceTypes[next].errorValue = BadValue;
  465. /* Called even if name is NULL, to remove any previous entry */
  466. RegisterResourceName(next, name);
  467. return next;
  468. }
  469. /**
  470. * Get the function used to calculate resource size. Extensions and
  471. * drivers need to be able to determine the current size calculation
  472. * function if they want to wrap or override it.
  473. *
  474. * @param[in] type Resource type used in size calculations.
  475. *
  476. * @return Function to calculate the size of a single
  477. * resource.
  478. */
  479. SizeType
  480. GetResourceTypeSizeFunc(RESTYPE type)
  481. {
  482. return resourceTypes[type & TypeMask].sizeFunc;
  483. }
  484. /**
  485. * Override the default function that calculates resource size. For
  486. * example, video driver knows better how to calculate pixmap memory
  487. * usage and can therefore wrap or override size calculation for
  488. * RT_PIXMAP.
  489. *
  490. * @param[in] type Resource type used in size calculations.
  491. *
  492. * @param[in] sizeFunc Function to calculate the size of a single
  493. * resource.
  494. */
  495. void
  496. SetResourceTypeSizeFunc(RESTYPE type, SizeType sizeFunc)
  497. {
  498. resourceTypes[type & TypeMask].sizeFunc = sizeFunc;
  499. }
  500. /**
  501. * Provide a function for iterating the subresources of a resource.
  502. * This allows for example more accurate accounting of the (memory)
  503. * resources consumed by a resource.
  504. *
  505. * @see FindSubResources
  506. *
  507. * @param[in] type Resource type used in size calculations.
  508. *
  509. * @param[in] sizeFunc Function to calculate the size of a single
  510. * resource.
  511. */
  512. void
  513. SetResourceTypeFindSubResFunc(RESTYPE type, FindTypeSubResources findFunc)
  514. {
  515. resourceTypes[type & TypeMask].findSubResFunc = findFunc;
  516. }
  517. void
  518. SetResourceTypeErrorValue(RESTYPE type, int errorValue)
  519. {
  520. resourceTypes[type & TypeMask].errorValue = errorValue;
  521. }
  522. RESTYPE
  523. CreateNewResourceClass(void)
  524. {
  525. RESTYPE next = lastResourceClass >> 1;
  526. if (next & lastResourceType)
  527. return 0;
  528. lastResourceClass = next;
  529. TypeMask = next - 1;
  530. return next;
  531. }
  532. static ClientResourceRec clientTable[MAXCLIENTS];
  533. /*****************
  534. * InitClientResources
  535. * When a new client is created, call this to allocate space
  536. * in resource table
  537. *****************/
  538. Bool
  539. InitClientResources(ClientPtr client)
  540. {
  541. int i, j;
  542. if (client == serverClient) {
  543. lastResourceType = RT_LASTPREDEF;
  544. lastResourceClass = RC_LASTPREDEF;
  545. TypeMask = RC_LASTPREDEF - 1;
  546. free(resourceTypes);
  547. resourceTypes = malloc(sizeof(predefTypes));
  548. if (!resourceTypes)
  549. return FALSE;
  550. memcpy(resourceTypes, predefTypes, sizeof(predefTypes));
  551. }
  552. clientTable[i = client->index].resources =
  553. malloc(INITBUCKETS * sizeof(ResourcePtr));
  554. if (!clientTable[i].resources)
  555. return FALSE;
  556. clientTable[i].buckets = INITBUCKETS;
  557. clientTable[i].elements = 0;
  558. clientTable[i].hashsize = INITHASHSIZE;
  559. /* Many IDs allocated from the server client are visible to clients,
  560. * so we don't use the SERVER_BIT for them, but we have to start
  561. * past the magic value constants used in the protocol. For normal
  562. * clients, we can start from zero, with SERVER_BIT set.
  563. */
  564. clientTable[i].fakeID = client->clientAsMask |
  565. (client->index ? SERVER_BIT : SERVER_MINID);
  566. clientTable[i].endFakeID = (clientTable[i].fakeID | RESOURCE_ID_MASK) + 1;
  567. for (j = 0; j < INITBUCKETS; j++) {
  568. clientTable[i].resources[j] = NULL;
  569. }
  570. return TRUE;
  571. }
  572. int
  573. HashResourceID(XID id, int numBits)
  574. {
  575. id &= RESOURCE_ID_MASK;
  576. switch (numBits)
  577. {
  578. case 6:
  579. return ((int)(0x03F & (id ^ (id>>6) ^ (id>>12))));
  580. case 7:
  581. return ((int)(0x07F & (id ^ (id>>7) ^ (id>>13))));
  582. case 8:
  583. return ((int)(0x0FF & (id ^ (id>>8) ^ (id>>16))));
  584. case 9:
  585. return ((int)(0x1FF & (id ^ (id>>9))));
  586. case 10:
  587. return ((int)(0x3FF & (id ^ (id>>10))));
  588. case 11:
  589. return ((int)(0x7FF & (id ^ (id>>11))));
  590. }
  591. if (numBits >= 11)
  592. return ((int)(0x7FF & (id ^ (id>>11))));
  593. else
  594. {
  595. assert(numBits >= 0);
  596. return id & ~((~0) << numBits);
  597. }
  598. }
  599. static XID
  600. AvailableID(int client, XID id, XID maxid, XID goodid)
  601. {
  602. ResourcePtr res;
  603. if ((goodid >= id) && (goodid <= maxid))
  604. return goodid;
  605. for (; id <= maxid; id++) {
  606. res = clientTable[client].resources[HashResourceID(id, clientTable[client].hashsize)];
  607. while (res && (res->id != id))
  608. res = res->next;
  609. if (!res)
  610. return id;
  611. }
  612. return 0;
  613. }
  614. void
  615. GetXIDRange(int client, Bool server, XID *minp, XID *maxp)
  616. {
  617. XID id, maxid;
  618. ResourcePtr *resp;
  619. ResourcePtr res;
  620. int i;
  621. XID goodid;
  622. id = (Mask) client << CLIENTOFFSET;
  623. if (server)
  624. id |= client ? SERVER_BIT : SERVER_MINID;
  625. maxid = id | RESOURCE_ID_MASK;
  626. goodid = 0;
  627. for (resp = clientTable[client].resources, i = clientTable[client].buckets;
  628. --i >= 0;) {
  629. for (res = *resp++; res; res = res->next) {
  630. if ((res->id < id) || (res->id > maxid))
  631. continue;
  632. if (((res->id - id) >= (maxid - res->id)) ?
  633. (goodid = AvailableID(client, id, res->id - 1, goodid)) :
  634. !(goodid = AvailableID(client, res->id + 1, maxid, goodid)))
  635. maxid = res->id - 1;
  636. else
  637. id = res->id + 1;
  638. }
  639. }
  640. if (id > maxid)
  641. id = maxid = 0;
  642. *minp = id;
  643. *maxp = maxid;
  644. }
  645. /**
  646. * GetXIDList is called by the XC-MISC extension's MiscGetXIDList function.
  647. * This function tries to find count unused XIDs for the given client. It
  648. * puts the IDs in the array pids and returns the number found, which should
  649. * almost always be the number requested.
  650. *
  651. * The circumstances that lead to a call to this function are very rare.
  652. * Xlib must run out of IDs while trying to generate a request that wants
  653. * multiple ID's, like the Multi-buffering CreateImageBuffers request.
  654. *
  655. * No rocket science in the implementation; just iterate over all
  656. * possible IDs for the given client and pick the first count IDs
  657. * that aren't in use. A more efficient algorithm could probably be
  658. * invented, but this will be used so rarely that this should suffice.
  659. */
  660. unsigned int
  661. GetXIDList(ClientPtr pClient, unsigned count, XID *pids)
  662. {
  663. unsigned int found = 0;
  664. XID rc, id = pClient->clientAsMask;
  665. XID maxid;
  666. void *val;
  667. maxid = id | RESOURCE_ID_MASK;
  668. while ((found < count) && (id <= maxid)) {
  669. rc = dixLookupResourceByClass(&val, id, RC_ANY, serverClient,
  670. DixGetAttrAccess);
  671. if (rc == BadValue) {
  672. pids[found++] = id;
  673. }
  674. id++;
  675. }
  676. return found;
  677. }
  678. /*
  679. * Return the next usable fake client ID.
  680. *
  681. * Normally this is just the next one in line, but if we've used the last
  682. * in the range, we need to find a new range of safe IDs to avoid
  683. * over-running another client.
  684. */
  685. XID
  686. FakeClientID(int client)
  687. {
  688. XID id, maxid;
  689. id = clientTable[client].fakeID++;
  690. if (id != clientTable[client].endFakeID)
  691. return id;
  692. GetXIDRange(client, TRUE, &id, &maxid);
  693. if (!id) {
  694. if (!client)
  695. FatalError("FakeClientID: server internal ids exhausted\n");
  696. MarkClientException(clients[client]);
  697. id = ((Mask) client << CLIENTOFFSET) | (SERVER_BIT * 3);
  698. maxid = id | RESOURCE_ID_MASK;
  699. }
  700. clientTable[client].fakeID = id + 1;
  701. clientTable[client].endFakeID = maxid + 1;
  702. return id;
  703. }
  704. Bool
  705. AddResource(XID id, RESTYPE type, void *value)
  706. {
  707. int client;
  708. ClientResourceRec *rrec;
  709. ResourcePtr res, *head;
  710. #ifdef XSERVER_DTRACE
  711. XSERVER_RESOURCE_ALLOC(id, type, value, TypeNameString(type));
  712. #endif
  713. client = CLIENT_ID(id);
  714. rrec = &clientTable[client];
  715. if (!rrec->buckets) {
  716. ErrorF("[dix] AddResource(%lx, %x, %lx), client=%d \n",
  717. (unsigned long) id, type, (unsigned long) value, client);
  718. FatalError("client not in use\n");
  719. }
  720. if ((rrec->elements >= 4 * rrec->buckets) && (rrec->hashsize < MAXHASHSIZE))
  721. RebuildTable(client);
  722. head = &rrec->resources[HashResourceID(id, clientTable[client].hashsize)];
  723. res = malloc(sizeof(ResourceRec));
  724. if (!res) {
  725. (*resourceTypes[type & TypeMask].deleteFunc) (value, id);
  726. return FALSE;
  727. }
  728. res->next = *head;
  729. res->id = id;
  730. res->type = type;
  731. res->value = value;
  732. *head = res;
  733. rrec->elements++;
  734. CallResourceStateCallback(ResourceStateAdding, res);
  735. return TRUE;
  736. }
  737. static void
  738. RebuildTable(int client)
  739. {
  740. int j;
  741. ResourcePtr res, next;
  742. ResourcePtr **tails, *resources;
  743. ResourcePtr **tptr, *rptr;
  744. /*
  745. * For now, preserve insertion order, since some ddx layers depend
  746. * on resources being free in the opposite order they are added.
  747. */
  748. j = 2 * clientTable[client].buckets;
  749. tails = malloc(j * sizeof(ResourcePtr *));
  750. if (!tails)
  751. return;
  752. resources = malloc(j * sizeof(ResourcePtr));
  753. if (!resources) {
  754. free(tails);
  755. return;
  756. }
  757. for (rptr = resources, tptr = tails; --j >= 0; rptr++, tptr++) {
  758. *rptr = NULL;
  759. *tptr = rptr;
  760. }
  761. clientTable[client].hashsize++;
  762. for (j = clientTable[client].buckets,
  763. rptr = clientTable[client].resources; --j >= 0; rptr++) {
  764. for (res = *rptr; res; res = next) {
  765. next = res->next;
  766. res->next = NULL;
  767. tptr = &tails[HashResourceID(res->id, clientTable[client].hashsize)];
  768. **tptr = res;
  769. *tptr = &res->next;
  770. }
  771. }
  772. free(tails);
  773. clientTable[client].buckets *= 2;
  774. free(clientTable[client].resources);
  775. clientTable[client].resources = resources;
  776. }
  777. static void
  778. doFreeResource(ResourcePtr res, Bool skip)
  779. {
  780. CallResourceStateCallback(ResourceStateFreeing, res);
  781. if (!skip)
  782. resourceTypes[res->type & TypeMask].deleteFunc(res->value, res->id);
  783. free(res);
  784. }
  785. void
  786. FreeResource(XID id, RESTYPE skipDeleteFuncType)
  787. {
  788. int cid;
  789. ResourcePtr res;
  790. ResourcePtr *prev, *head;
  791. int *eltptr;
  792. int elements;
  793. if (((cid = CLIENT_ID(id)) < MAXCLIENTS) && clientTable[cid].buckets) {
  794. head = &clientTable[cid].resources[HashResourceID(id, clientTable[cid].hashsize)];
  795. eltptr = &clientTable[cid].elements;
  796. prev = head;
  797. while ((res = *prev)) {
  798. if (res->id == id) {
  799. RESTYPE rtype = res->type;
  800. #ifdef XSERVER_DTRACE
  801. XSERVER_RESOURCE_FREE(res->id, res->type,
  802. res->value, TypeNameString(res->type));
  803. #endif
  804. *prev = res->next;
  805. elements = --*eltptr;
  806. doFreeResource(res, rtype == skipDeleteFuncType);
  807. if (*eltptr != elements)
  808. prev = head; /* prev may no longer be valid */
  809. }
  810. else
  811. prev = &res->next;
  812. }
  813. }
  814. }
  815. void
  816. FreeResourceByType(XID id, RESTYPE type, Bool skipFree)
  817. {
  818. int cid;
  819. ResourcePtr res;
  820. ResourcePtr *prev, *head;
  821. if (((cid = CLIENT_ID(id)) < MAXCLIENTS) && clientTable[cid].buckets) {
  822. head = &clientTable[cid].resources[HashResourceID(id, clientTable[cid].hashsize)];
  823. prev = head;
  824. while ((res = *prev)) {
  825. if (res->id == id && res->type == type) {
  826. #ifdef XSERVER_DTRACE
  827. XSERVER_RESOURCE_FREE(res->id, res->type,
  828. res->value, TypeNameString(res->type));
  829. #endif
  830. *prev = res->next;
  831. clientTable[cid].elements--;
  832. doFreeResource(res, skipFree);
  833. break;
  834. }
  835. else
  836. prev = &res->next;
  837. }
  838. }
  839. }
  840. /*
  841. * Change the value associated with a resource id. Caller
  842. * is responsible for "doing the right thing" with the old
  843. * data
  844. */
  845. Bool
  846. ChangeResourceValue(XID id, RESTYPE rtype, void *value)
  847. {
  848. int cid;
  849. ResourcePtr res;
  850. if (((cid = CLIENT_ID(id)) < MAXCLIENTS) && clientTable[cid].buckets) {
  851. res = clientTable[cid].resources[HashResourceID(id, clientTable[cid].hashsize)];
  852. for (; res; res = res->next)
  853. if ((res->id == id) && (res->type == rtype)) {
  854. res->value = value;
  855. return TRUE;
  856. }
  857. }
  858. return FALSE;
  859. }
  860. /* Note: if func adds or deletes resources, then func can get called
  861. * more than once for some resources. If func adds new resources,
  862. * func might or might not get called for them. func cannot both
  863. * add and delete an equal number of resources!
  864. */
  865. void
  866. FindClientResourcesByType(ClientPtr client,
  867. RESTYPE type, FindResType func, void *cdata)
  868. {
  869. ResourcePtr *resources;
  870. ResourcePtr this, next;
  871. int i, elements;
  872. int *eltptr;
  873. if (!client)
  874. client = serverClient;
  875. resources = clientTable[client->index].resources;
  876. eltptr = &clientTable[client->index].elements;
  877. for (i = 0; i < clientTable[client->index].buckets; i++) {
  878. for (this = resources[i]; this; this = next) {
  879. next = this->next;
  880. if (!type || this->type == type) {
  881. elements = *eltptr;
  882. (*func) (this->value, this->id, cdata);
  883. if (*eltptr != elements)
  884. next = resources[i]; /* start over */
  885. }
  886. }
  887. }
  888. }
  889. void FindSubResources(void *resource,
  890. RESTYPE type,
  891. FindAllRes func,
  892. void *cdata)
  893. {
  894. struct ResourceType rtype = resourceTypes[type & TypeMask];
  895. rtype.findSubResFunc(resource, func, cdata);
  896. }
  897. void
  898. FindAllClientResources(ClientPtr client, FindAllRes func, void *cdata)
  899. {
  900. ResourcePtr *resources;
  901. ResourcePtr this, next;
  902. int i, elements;
  903. int *eltptr;
  904. if (!client)
  905. client = serverClient;
  906. resources = clientTable[client->index].resources;
  907. eltptr = &clientTable[client->index].elements;
  908. for (i = 0; i < clientTable[client->index].buckets; i++) {
  909. for (this = resources[i]; this; this = next) {
  910. next = this->next;
  911. elements = *eltptr;
  912. (*func) (this->value, this->id, this->type, cdata);
  913. if (*eltptr != elements)
  914. next = resources[i]; /* start over */
  915. }
  916. }
  917. }
  918. void *
  919. LookupClientResourceComplex(ClientPtr client,
  920. RESTYPE type,
  921. FindComplexResType func, void *cdata)
  922. {
  923. ResourcePtr *resources;
  924. ResourcePtr this, next;
  925. void *value;
  926. int i;
  927. if (!client)
  928. client = serverClient;
  929. resources = clientTable[client->index].resources;
  930. for (i = 0; i < clientTable[client->index].buckets; i++) {
  931. for (this = resources[i]; this; this = next) {
  932. next = this->next;
  933. if (!type || this->type == type) {
  934. /* workaround func freeing the type as DRI1 does */
  935. value = this->value;
  936. if ((*func) (value, this->id, cdata))
  937. return value;
  938. }
  939. }
  940. }
  941. return NULL;
  942. }
  943. void
  944. FreeClientNeverRetainResources(ClientPtr client)
  945. {
  946. ResourcePtr *resources;
  947. ResourcePtr this;
  948. ResourcePtr *prev;
  949. int j, elements;
  950. int *eltptr;
  951. if (!client)
  952. return;
  953. resources = clientTable[client->index].resources;
  954. eltptr = &clientTable[client->index].elements;
  955. for (j = 0; j < clientTable[client->index].buckets; j++) {
  956. prev = &resources[j];
  957. while ((this = *prev)) {
  958. RESTYPE rtype = this->type;
  959. if (rtype & RC_NEVERRETAIN) {
  960. #ifdef XSERVER_DTRACE
  961. XSERVER_RESOURCE_FREE(this->id, this->type,
  962. this->value, TypeNameString(this->type));
  963. #endif
  964. *prev = this->next;
  965. clientTable[client->index].elements--;
  966. elements = *eltptr;
  967. doFreeResource(this, FALSE);
  968. if (*eltptr != elements)
  969. prev = &resources[j]; /* prev may no longer be valid */
  970. }
  971. else
  972. prev = &this->next;
  973. }
  974. }
  975. }
  976. void
  977. FreeClientResources(ClientPtr client)
  978. {
  979. ResourcePtr *resources;
  980. ResourcePtr this;
  981. int j;
  982. /* This routine shouldn't be called with a null client, but just in
  983. case ... */
  984. if (!client)
  985. return;
  986. HandleSaveSet(client);
  987. resources = clientTable[client->index].resources;
  988. for (j = 0; j < clientTable[client->index].buckets; j++) {
  989. /* It may seem silly to update the head of this resource list as
  990. we delete the members, since the entire list will be deleted any way,
  991. but there are some resource deletion functions "FreeClientPixels" for
  992. one which do a LookupID on another resource id (a Colormap id in this
  993. case), so the resource list must be kept valid up to the point that
  994. it is deleted, so every time we delete a resource, we must update the
  995. head, just like in FreeResource. I hope that this doesn't slow down
  996. mass deletion appreciably. PRH */
  997. ResourcePtr *head;
  998. head = &resources[j];
  999. for (this = *head; this; this = *head) {
  1000. #ifdef XSERVER_DTRACE
  1001. XSERVER_RESOURCE_FREE(this->id, this->type,
  1002. this->value, TypeNameString(this->type));
  1003. #endif
  1004. *head = this->next;
  1005. clientTable[client->index].elements--;
  1006. doFreeResource(this, FALSE);
  1007. }
  1008. }
  1009. free(clientTable[client->index].resources);
  1010. clientTable[client->index].resources = NULL;
  1011. clientTable[client->index].buckets = 0;
  1012. }
  1013. void
  1014. FreeAllResources(void)
  1015. {
  1016. int i;
  1017. for (i = currentMaxClients; --i >= 0;) {
  1018. if (clientTable[i].buckets)
  1019. FreeClientResources(clients[i]);
  1020. }
  1021. }
  1022. Bool
  1023. LegalNewID(XID id, ClientPtr client)
  1024. {
  1025. void *val;
  1026. int rc;
  1027. #ifdef PANORAMIX
  1028. XID minid, maxid;
  1029. if (!noPanoramiXExtension) {
  1030. minid = client->clientAsMask | (client->index ?
  1031. SERVER_BIT : SERVER_MINID);
  1032. maxid = (clientTable[client->index].fakeID | RESOURCE_ID_MASK) + 1;
  1033. if ((id >= minid) && (id <= maxid))
  1034. return TRUE;
  1035. }
  1036. #endif /* PANORAMIX */
  1037. if (client->clientAsMask == (id & ~RESOURCE_ID_MASK)) {
  1038. rc = dixLookupResourceByClass(&val, id, RC_ANY, serverClient,
  1039. DixGetAttrAccess);
  1040. return rc == BadValue;
  1041. }
  1042. return FALSE;
  1043. }
  1044. int
  1045. dixLookupResourceByType(void **result, XID id, RESTYPE rtype,
  1046. ClientPtr client, Mask mode)
  1047. {
  1048. int cid = CLIENT_ID(id);
  1049. ResourcePtr res = NULL;
  1050. *result = NULL;
  1051. if ((rtype & TypeMask) > lastResourceType)
  1052. return BadImplementation;
  1053. if ((cid < MAXCLIENTS) && clientTable[cid].buckets) {
  1054. res = clientTable[cid].resources[HashResourceID(id, clientTable[cid].hashsize)];
  1055. for (; res; res = res->next)
  1056. if (res->id == id && res->type == rtype)
  1057. break;
  1058. }
  1059. if (!res)
  1060. return resourceTypes[rtype & TypeMask].errorValue;
  1061. if (client) {
  1062. client->errorValue = id;
  1063. cid = XaceHook(XACE_RESOURCE_ACCESS, client, id, res->type,
  1064. res->value, RT_NONE, NULL, mode);
  1065. if (cid == BadValue)
  1066. return resourceTypes[rtype & TypeMask].errorValue;
  1067. if (cid != Success)
  1068. return cid;
  1069. }
  1070. *result = res->value;
  1071. return Success;
  1072. }
  1073. int
  1074. dixLookupResourceByClass(void **result, XID id, RESTYPE rclass,
  1075. ClientPtr client, Mask mode)
  1076. {
  1077. int cid = CLIENT_ID(id);
  1078. ResourcePtr res = NULL;
  1079. *result = NULL;
  1080. if ((cid < MAXCLIENTS) && clientTable[cid].buckets) {
  1081. res = clientTable[cid].resources[HashResourceID(id, clientTable[cid].hashsize)];
  1082. for (; res; res = res->next)
  1083. if (res->id == id && (res->type & rclass))
  1084. break;
  1085. }
  1086. if (!res)
  1087. return BadValue;
  1088. if (client) {
  1089. client->errorValue = id;
  1090. cid = XaceHook(XACE_RESOURCE_ACCESS, client, id, res->type,
  1091. res->value, RT_NONE, NULL, mode);
  1092. if (cid != Success)
  1093. return cid;
  1094. }
  1095. *result = res->value;
  1096. return Success;
  1097. }