resource.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845
  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. /* Routines to manage various kinds of resources:
  59. *
  60. * CreateNewResourceType, InitClientResources,
  61. * FakeClientID, AddResource, FreeResource, FreeClientResources,
  62. * FreeAllResources, LookupIDByType, LookupIDByClass, GetXIDRange
  63. */
  64. /*
  65. * A resource ID is a 32 bit quantity, the upper 2 bits of which are
  66. * off-limits for client-visible resources. The next 8 bits are
  67. * used as client ID, and the low 22 bits come from the client.
  68. * A resource ID is "hashed" by extracting and xoring subfields
  69. * (varying with the size of the hash table).
  70. *
  71. * It is sometimes necessary for the server to create an ID that looks
  72. * like it belongs to a client. This ID, however, must not be one
  73. * the client actually can create, or we have the potential for conflict.
  74. * The 31st bit of the ID is reserved for the server's use for this
  75. * purpose. By setting CLIENT_ID(id) to the client, the SERVER_BIT to
  76. * 1, and an otherwise arbitrary ID in the low 22 bits, we can create a
  77. * resource "owned" by the client.
  78. */
  79. #ifdef HAVE_DIX_CONFIG_H
  80. #include <dix-config.h>
  81. #endif
  82. #include <X11/X.h>
  83. #include "misc.h"
  84. #include "os.h"
  85. #include "resource.h"
  86. #include "dixstruct.h"
  87. #include "opaque.h"
  88. #include "windowstr.h"
  89. #include "dixfont.h"
  90. #include "colormap.h"
  91. #include "inputstr.h"
  92. #include "dixevents.h"
  93. #include "dixgrabs.h"
  94. #include "cursor.h"
  95. #include <assert.h>
  96. static void RebuildTable(int /*client */
  97. );
  98. #define SERVER_MINID 32
  99. #define INITBUCKETS 64
  100. #define INITHASHSIZE 6
  101. #define MAXHASHSIZE 11
  102. typedef struct _Resource {
  103. struct _Resource *next;
  104. XID id;
  105. RESTYPE type;
  106. pointer value;
  107. } ResourceRec, *ResourcePtr;
  108. #define NullResource ((ResourcePtr)NULL)
  109. typedef struct _ClientResource {
  110. ResourcePtr *resources;
  111. int elements;
  112. int buckets;
  113. int hashsize; /* log(2)(buckets) */
  114. XID fakeID;
  115. XID endFakeID;
  116. XID expectID;
  117. } ClientResourceRec;
  118. _X_EXPORT RESTYPE lastResourceType;
  119. static RESTYPE lastResourceClass;
  120. _X_EXPORT RESTYPE TypeMask;
  121. static DeleteType *DeleteFuncs = (DeleteType *) NULL;
  122. #ifdef XResExtension
  123. _X_EXPORT Atom *ResourceNames = NULL;
  124. _X_EXPORT void
  125. RegisterResourceName(RESTYPE type, char *name)
  126. {
  127. ResourceNames[type & TypeMask] = MakeAtom(name, strlen(name), TRUE);
  128. }
  129. #endif
  130. _X_EXPORT RESTYPE
  131. CreateNewResourceType(DeleteType deleteFunc)
  132. {
  133. RESTYPE next = lastResourceType + 1;
  134. DeleteType *funcs;
  135. if (next & lastResourceClass)
  136. return 0;
  137. funcs = (DeleteType *) realloc(DeleteFuncs,
  138. (next + 1) * sizeof(DeleteType));
  139. if (!funcs)
  140. return 0;
  141. #ifdef XResExtension
  142. {
  143. Atom *newnames;
  144. newnames = realloc(ResourceNames, (next + 1) * sizeof(Atom));
  145. if (!newnames)
  146. return 0;
  147. ResourceNames = newnames;
  148. ResourceNames[next] = 0;
  149. }
  150. #endif
  151. lastResourceType = next;
  152. DeleteFuncs = funcs;
  153. DeleteFuncs[next] = deleteFunc;
  154. return next;
  155. }
  156. ClientResourceRec clientTable[MAXCLIENTS];
  157. /*****************
  158. * InitClientResources
  159. * When a new client is created, call this to allocate space
  160. * in resource table
  161. *****************/
  162. Bool
  163. InitClientResources(ClientPtr client)
  164. {
  165. int i, j;
  166. if (client == serverClient) {
  167. lastResourceType = RT_LASTPREDEF;
  168. lastResourceClass = RC_LASTPREDEF;
  169. TypeMask = RC_LASTPREDEF - 1;
  170. if (DeleteFuncs)
  171. free(DeleteFuncs);
  172. DeleteFuncs = malloc((lastResourceType + 1) *
  173. sizeof(DeleteType));
  174. if (!DeleteFuncs)
  175. return FALSE;
  176. DeleteFuncs[RT_NONE & TypeMask] = (DeleteType) NoopDDA;
  177. DeleteFuncs[RT_WINDOW & TypeMask] = DeleteWindow;
  178. DeleteFuncs[RT_PIXMAP & TypeMask] = dixDestroyPixmap;
  179. DeleteFuncs[RT_GC & TypeMask] = FreeGC;
  180. DeleteFuncs[RT_FONT & TypeMask] = CloseFont;
  181. DeleteFuncs[RT_CURSOR & TypeMask] = FreeCursor;
  182. DeleteFuncs[RT_COLORMAP & TypeMask] = FreeColormap;
  183. DeleteFuncs[RT_CMAPENTRY & TypeMask] = FreeClientPixels;
  184. DeleteFuncs[RT_OTHERCLIENT & TypeMask] = OtherClientGone;
  185. DeleteFuncs[RT_PASSIVEGRAB & TypeMask] = DeletePassiveGrab;
  186. #ifdef XResExtension
  187. if (ResourceNames)
  188. free(ResourceNames);
  189. ResourceNames = malloc((lastResourceType + 1) * sizeof(Atom));
  190. if (!ResourceNames)
  191. return FALSE;
  192. #endif
  193. }
  194. clientTable[i = client->index].resources =
  195. malloc(INITBUCKETS * sizeof(ResourcePtr));
  196. if (!clientTable[i].resources)
  197. return FALSE;
  198. clientTable[i].buckets = INITBUCKETS;
  199. clientTable[i].elements = 0;
  200. clientTable[i].hashsize = INITHASHSIZE;
  201. /* Many IDs allocated from the server client are visible to clients,
  202. * so we don't use the SERVER_BIT for them, but we have to start
  203. * past the magic value constants used in the protocol. For normal
  204. * clients, we can start from zero, with SERVER_BIT set.
  205. */
  206. clientTable[i].fakeID = client->clientAsMask |
  207. (client->index ? SERVER_BIT : SERVER_MINID);
  208. clientTable[i].endFakeID = (clientTable[i].fakeID | RESOURCE_ID_MASK) + 1;
  209. clientTable[i].expectID = client->clientAsMask;
  210. for (j = 0; j < INITBUCKETS; j++) {
  211. clientTable[i].resources[j] = NullResource;
  212. }
  213. return TRUE;
  214. }
  215. static int
  216. Hash(int client, register XID id)
  217. {
  218. id &= RESOURCE_ID_MASK;
  219. switch (clientTable[client].hashsize) {
  220. case 6:
  221. return ((int) (0x03F & (id ^ (id >> 6) ^ (id >> 12))));
  222. case 7:
  223. return ((int) (0x07F & (id ^ (id >> 7) ^ (id >> 13))));
  224. case 8:
  225. return ((int) (0x0FF & (id ^ (id >> 8) ^ (id >> 16))));
  226. case 9:
  227. return ((int) (0x1FF & (id ^ (id >> 9))));
  228. case 10:
  229. return ((int) (0x3FF & (id ^ (id >> 10))));
  230. case 11:
  231. return ((int) (0x7FF & (id ^ (id >> 11))));
  232. }
  233. return -1;
  234. }
  235. static XID
  236. AvailableID(register int client,
  237. register XID id, register XID maxid, register XID goodid)
  238. {
  239. ResourcePtr res;
  240. if ((goodid >= id) && (goodid <= maxid))
  241. return goodid;
  242. for (; id <= maxid; id++) {
  243. res = clientTable[client].resources[Hash(client, id)];
  244. while (res && (res->id != id))
  245. res = res->next;
  246. if (!res)
  247. return id;
  248. }
  249. return 0;
  250. }
  251. _X_EXPORT void
  252. GetXIDRange(int client, Bool server, XID *minp, XID *maxp)
  253. {
  254. XID id, maxid;
  255. ResourcePtr *resp;
  256. ResourcePtr res;
  257. int i;
  258. XID goodid;
  259. id = (Mask) client << CLIENTOFFSET;
  260. if (server)
  261. id |= client ? SERVER_BIT : SERVER_MINID;
  262. maxid = id | RESOURCE_ID_MASK;
  263. goodid = 0;
  264. for (resp = clientTable[client].resources, i = clientTable[client].buckets;
  265. --i >= 0;) {
  266. for (res = *resp++; res; res = res->next) {
  267. if ((res->id < id) || (res->id > maxid))
  268. continue;
  269. if (((res->id - id) >= (maxid - res->id)) ?
  270. (goodid = AvailableID(client, id, res->id - 1, goodid)) :
  271. !(goodid = AvailableID(client, res->id + 1, maxid, goodid)))
  272. maxid = res->id - 1;
  273. else
  274. id = res->id + 1;
  275. }
  276. }
  277. if (id > maxid)
  278. id = maxid = 0;
  279. *minp = id;
  280. *maxp = maxid;
  281. }
  282. /**
  283. * GetXIDList is called by the XC-MISC extension's MiscGetXIDList function.
  284. * This function tries to find count unused XIDs for the given client. It
  285. * puts the IDs in the array pids and returns the number found, which should
  286. * almost always be the number requested.
  287. *
  288. * The circumstances that lead to a call to this function are very rare.
  289. * Xlib must run out of IDs while trying to generate a request that wants
  290. * multiple ID's, like the Multi-buffering CreateImageBuffers request.
  291. *
  292. * No rocket science in the implementation; just iterate over all
  293. * possible IDs for the given client and pick the first count IDs
  294. * that aren't in use. A more efficient algorithm could probably be
  295. * invented, but this will be used so rarely that this should suffice.
  296. */
  297. _X_EXPORT unsigned int
  298. GetXIDList(ClientPtr pClient, unsigned count, XID *pids)
  299. {
  300. unsigned int found = 0;
  301. XID id = pClient->clientAsMask;
  302. XID maxid;
  303. maxid = id | RESOURCE_ID_MASK;
  304. while ((found < count) && (id <= maxid)) {
  305. if (!LookupIDByClass(id, RC_ANY)) {
  306. pids[found++] = id;
  307. }
  308. id++;
  309. }
  310. return found;
  311. }
  312. /*
  313. * Return the next usable fake client ID.
  314. *
  315. * Normally this is just the next one in line, but if we've used the last
  316. * in the range, we need to find a new range of safe IDs to avoid
  317. * over-running another client.
  318. */
  319. _X_EXPORT XID
  320. FakeClientID(register int client)
  321. {
  322. XID id, maxid;
  323. id = clientTable[client].fakeID++;
  324. if (id != clientTable[client].endFakeID)
  325. return id;
  326. GetXIDRange(client, TRUE, &id, &maxid);
  327. if (!id) {
  328. if (!client)
  329. FatalError("FakeClientID: server internal ids exhausted\n");
  330. MarkClientException(clients[client]);
  331. id = ((Mask) client << CLIENTOFFSET) | (SERVER_BIT * 3);
  332. maxid = id | RESOURCE_ID_MASK;
  333. }
  334. clientTable[client].fakeID = id + 1;
  335. clientTable[client].endFakeID = maxid + 1;
  336. return id;
  337. }
  338. _X_EXPORT Bool
  339. AddResource(XID id, RESTYPE type, pointer value)
  340. {
  341. int client;
  342. ClientResourceRec *rrec;
  343. ResourcePtr res, *head;
  344. client = CLIENT_ID(id);
  345. rrec = &clientTable[client];
  346. if (!rrec->buckets) {
  347. ErrorF("AddResource(%lx, %lx, %lx), client=%d \n",
  348. (unsigned long) id, type, (unsigned long) value, client);
  349. FatalError("client not in use\n");
  350. }
  351. if ((rrec->elements >= 4 * rrec->buckets) && (rrec->hashsize < MAXHASHSIZE))
  352. RebuildTable(client);
  353. head = &rrec->resources[Hash(client, id)];
  354. res = malloc(sizeof(ResourceRec));
  355. if (!res) {
  356. (*DeleteFuncs[type & TypeMask]) (value, id);
  357. return FALSE;
  358. }
  359. res->next = *head;
  360. res->id = id;
  361. res->type = type;
  362. res->value = value;
  363. *head = res;
  364. rrec->elements++;
  365. if (!(id & SERVER_BIT) && (id >= rrec->expectID))
  366. rrec->expectID = id + 1;
  367. return TRUE;
  368. }
  369. static void
  370. RebuildTable(int client)
  371. {
  372. int j;
  373. ResourcePtr res, next;
  374. ResourcePtr **tails, *resources;
  375. ResourcePtr **tptr, *rptr;
  376. /*
  377. * For now, preserve insertion order, since some ddx layers depend
  378. * on resources being free in the opposite order they are added.
  379. */
  380. j = 2 * clientTable[client].buckets;
  381. tails = (ResourcePtr **) ALLOCATE_LOCAL(j * sizeof(ResourcePtr *));
  382. if (!tails)
  383. return;
  384. resources = malloc(j * sizeof(ResourcePtr));
  385. if (!resources) {
  386. DEALLOCATE_LOCAL(tails);
  387. return;
  388. }
  389. for (rptr = resources, tptr = tails; --j >= 0; rptr++, tptr++) {
  390. *rptr = NullResource;
  391. *tptr = rptr;
  392. }
  393. clientTable[client].hashsize++;
  394. for (j = clientTable[client].buckets,
  395. rptr = clientTable[client].resources; --j >= 0; rptr++) {
  396. for (res = *rptr; res; res = next) {
  397. next = res->next;
  398. res->next = NullResource;
  399. tptr = &tails[Hash(client, res->id)];
  400. **tptr = res;
  401. *tptr = &res->next;
  402. }
  403. }
  404. DEALLOCATE_LOCAL(tails);
  405. clientTable[client].buckets *= 2;
  406. free(clientTable[client].resources);
  407. clientTable[client].resources = resources;
  408. }
  409. _X_EXPORT void
  410. FreeResource(XID id, RESTYPE skipDeleteFuncType)
  411. {
  412. int cid;
  413. ResourcePtr res;
  414. ResourcePtr *prev, *head;
  415. int *eltptr;
  416. int elements;
  417. Bool gotOne = FALSE;
  418. if (((cid = CLIENT_ID(id)) < MAXCLIENTS) && clientTable[cid].buckets) {
  419. head = &clientTable[cid].resources[Hash(cid, id)];
  420. eltptr = &clientTable[cid].elements;
  421. prev = head;
  422. while ((res = *prev)) {
  423. if (res->id == id) {
  424. RESTYPE rtype = res->type;
  425. *prev = res->next;
  426. elements = --*eltptr;
  427. if (rtype & RC_CACHED)
  428. FlushClientCaches(res->id);
  429. if (rtype != skipDeleteFuncType)
  430. (*DeleteFuncs[rtype & TypeMask]) (res->value, res->id);
  431. free(res);
  432. if (*eltptr != elements)
  433. prev = head; /* prev may no longer be valid */
  434. gotOne = TRUE;
  435. }
  436. else
  437. prev = &res->next;
  438. }
  439. if (clients[cid] && (id == clients[cid]->lastDrawableID)) {
  440. clients[cid]->lastDrawable = (DrawablePtr) WindowTable[0];
  441. clients[cid]->lastDrawableID = WindowTable[0]->drawable.id;
  442. }
  443. }
  444. if (!gotOne)
  445. ErrorF("Freeing resource id=%lX which isn't there.\n",
  446. (unsigned long) id);
  447. }
  448. _X_EXPORT void
  449. FreeResourceByType(XID id, RESTYPE type, Bool skipFree)
  450. {
  451. int cid;
  452. ResourcePtr res;
  453. ResourcePtr *prev, *head;
  454. if (((cid = CLIENT_ID(id)) < MAXCLIENTS) && clientTable[cid].buckets) {
  455. head = &clientTable[cid].resources[Hash(cid, id)];
  456. prev = head;
  457. while ((res = *prev)) {
  458. if (res->id == id && res->type == type) {
  459. *prev = res->next;
  460. if (type & RC_CACHED)
  461. FlushClientCaches(res->id);
  462. if (!skipFree)
  463. (*DeleteFuncs[type & TypeMask]) (res->value, res->id);
  464. free(res);
  465. break;
  466. }
  467. else
  468. prev = &res->next;
  469. }
  470. if (clients[cid] && (id == clients[cid]->lastDrawableID)) {
  471. clients[cid]->lastDrawable = (DrawablePtr) WindowTable[0];
  472. clients[cid]->lastDrawableID = WindowTable[0]->drawable.id;
  473. }
  474. }
  475. }
  476. /*
  477. * Change the value associated with a resource id. Caller
  478. * is responsible for "doing the right thing" with the old
  479. * data
  480. */
  481. _X_EXPORT Bool
  482. ChangeResourceValue(XID id, RESTYPE rtype, pointer value)
  483. {
  484. int cid;
  485. ResourcePtr res;
  486. if (((cid = CLIENT_ID(id)) < MAXCLIENTS) && clientTable[cid].buckets) {
  487. res = clientTable[cid].resources[Hash(cid, id)];
  488. for (; res; res = res->next)
  489. if ((res->id == id) && (res->type == rtype)) {
  490. if (rtype & RC_CACHED)
  491. FlushClientCaches(res->id);
  492. res->value = value;
  493. return TRUE;
  494. }
  495. }
  496. return FALSE;
  497. }
  498. /* Note: if func adds or deletes resources, then func can get called
  499. * more than once for some resources. If func adds new resources,
  500. * func might or might not get called for them. func cannot both
  501. * add and delete an equal number of resources!
  502. */
  503. _X_EXPORT void
  504. FindClientResourcesByType(ClientPtr client,
  505. RESTYPE type, FindResType func, pointer cdata)
  506. {
  507. ResourcePtr *resources;
  508. ResourcePtr this, next;
  509. int i, elements;
  510. int *eltptr;
  511. if (!client)
  512. client = serverClient;
  513. resources = clientTable[client->index].resources;
  514. eltptr = &clientTable[client->index].elements;
  515. for (i = 0; i < clientTable[client->index].buckets; i++) {
  516. for (this = resources[i]; this; this = next) {
  517. next = this->next;
  518. if (!type || this->type == type) {
  519. elements = *eltptr;
  520. (*func) (this->value, this->id, cdata);
  521. if (*eltptr != elements)
  522. next = resources[i]; /* start over */
  523. }
  524. }
  525. }
  526. }
  527. _X_EXPORT void
  528. FindAllClientResources(ClientPtr client, FindAllRes func, pointer cdata)
  529. {
  530. ResourcePtr *resources;
  531. ResourcePtr this, next;
  532. int i, elements;
  533. int *eltptr;
  534. if (!client)
  535. client = serverClient;
  536. resources = clientTable[client->index].resources;
  537. eltptr = &clientTable[client->index].elements;
  538. for (i = 0; i < clientTable[client->index].buckets; i++) {
  539. for (this = resources[i]; this; this = next) {
  540. next = this->next;
  541. elements = *eltptr;
  542. (*func) (this->value, this->id, this->type, cdata);
  543. if (*eltptr != elements)
  544. next = resources[i]; /* start over */
  545. }
  546. }
  547. }
  548. pointer
  549. LookupClientResourceComplex(ClientPtr client,
  550. RESTYPE type,
  551. FindComplexResType func, pointer cdata)
  552. {
  553. ResourcePtr *resources;
  554. ResourcePtr this;
  555. int i;
  556. if (!client)
  557. client = serverClient;
  558. resources = clientTable[client->index].resources;
  559. for (i = 0; i < clientTable[client->index].buckets; i++) {
  560. for (this = resources[i]; this; this = this->next) {
  561. if (!type || this->type == type) {
  562. if ((*func) (this->value, this->id, cdata))
  563. return this->value;
  564. }
  565. }
  566. }
  567. return NULL;
  568. }
  569. void
  570. FreeClientNeverRetainResources(ClientPtr client)
  571. {
  572. ResourcePtr *resources;
  573. ResourcePtr this;
  574. ResourcePtr *prev;
  575. int j;
  576. if (!client)
  577. return;
  578. resources = clientTable[client->index].resources;
  579. for (j = 0; j < clientTable[client->index].buckets; j++) {
  580. prev = &resources[j];
  581. while ((this = *prev)) {
  582. RESTYPE rtype = this->type;
  583. if (rtype & RC_NEVERRETAIN) {
  584. *prev = this->next;
  585. if (rtype & RC_CACHED)
  586. FlushClientCaches(this->id);
  587. (*DeleteFuncs[rtype & TypeMask]) (this->value, this->id);
  588. free(this);
  589. }
  590. else
  591. prev = &this->next;
  592. }
  593. }
  594. }
  595. void
  596. FreeClientResources(ClientPtr client)
  597. {
  598. ResourcePtr *resources;
  599. ResourcePtr this;
  600. int j;
  601. /* This routine shouldn't be called with a null client, but just in
  602. case ... */
  603. if (!client)
  604. return;
  605. HandleSaveSet(client);
  606. resources = clientTable[client->index].resources;
  607. for (j = 0; j < clientTable[client->index].buckets; j++) {
  608. /* It may seem silly to update the head of this resource list as
  609. we delete the members, since the entire list will be deleted any way,
  610. but there are some resource deletion functions "FreeClientPixels" for
  611. one which do a LookupID on another resource id (a Colormap id in this
  612. case), so the resource list must be kept valid up to the point that
  613. it is deleted, so every time we delete a resource, we must update the
  614. head, just like in FreeResource. I hope that this doesn't slow down
  615. mass deletion appreciably. PRH */
  616. ResourcePtr *head;
  617. head = &resources[j];
  618. for (this = *head; this; this = *head) {
  619. RESTYPE rtype = this->type;
  620. *head = this->next;
  621. if (rtype & RC_CACHED)
  622. FlushClientCaches(this->id);
  623. (*DeleteFuncs[rtype & TypeMask]) (this->value, this->id);
  624. free(this);
  625. }
  626. }
  627. free(clientTable[client->index].resources);
  628. clientTable[client->index].resources = NULL;
  629. clientTable[client->index].buckets = 0;
  630. }
  631. void
  632. FreeAllResources()
  633. {
  634. int i;
  635. for (i = currentMaxClients; --i >= 0;) {
  636. if (clientTable[i].buckets)
  637. FreeClientResources(clients[i]);
  638. }
  639. }
  640. _X_EXPORT Bool
  641. LegalNewID(XID id, register ClientPtr client)
  642. {
  643. return ((client->clientAsMask == (id & ~RESOURCE_ID_MASK)) &&
  644. ((clientTable[client->index].expectID <= id) ||
  645. !LookupIDByClass(id, RC_ANY)));
  646. }
  647. /* SecurityLookupIDByType and SecurityLookupIDByClass:
  648. * These are the heart of the resource ID security system. They take
  649. * two additional arguments compared to the old LookupID functions:
  650. * the client doing the lookup, and the access mode (see resource.h).
  651. * The resource is returned if it exists and the client is allowed access,
  652. * else NULL is returned.
  653. */
  654. _X_EXPORT pointer
  655. SecurityLookupIDByType(ClientPtr client, XID id, RESTYPE rtype, Mask mode)
  656. {
  657. int cid;
  658. ResourcePtr res;
  659. pointer retval = NULL;
  660. if (((cid = CLIENT_ID(id)) < MAXCLIENTS) && clientTable[cid].buckets) {
  661. res = clientTable[cid].resources[Hash(cid, id)];
  662. for (; res; res = res->next)
  663. if ((res->id == id) && (res->type == rtype)) {
  664. retval = res->value;
  665. break;
  666. }
  667. }
  668. return retval;
  669. }
  670. _X_EXPORT pointer
  671. SecurityLookupIDByClass(ClientPtr client, XID id, RESTYPE classes, Mask mode)
  672. {
  673. int cid;
  674. ResourcePtr res = NULL;
  675. pointer retval = NULL;
  676. if (((cid = CLIENT_ID(id)) < MAXCLIENTS) && clientTable[cid].buckets) {
  677. res = clientTable[cid].resources[Hash(cid, id)];
  678. for (; res; res = res->next)
  679. if ((res->id == id) && (res->type & classes)) {
  680. retval = res->value;
  681. break;
  682. }
  683. }
  684. return retval;
  685. }
  686. /* We can't replace the LookupIDByType and LookupIDByClass functions with
  687. * macros because of compatibility with loadable servers.
  688. */
  689. _X_EXPORT pointer
  690. LookupIDByType(XID id, RESTYPE rtype)
  691. {
  692. return SecurityLookupIDByType(NullClient, id, rtype, SecurityUnknownAccess);
  693. }
  694. _X_EXPORT pointer
  695. LookupIDByClass(XID id, RESTYPE classes)
  696. {
  697. return SecurityLookupIDByClass(NullClient, id, classes,
  698. SecurityUnknownAccess);
  699. }