_falQuarks.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. /*
  2. * CDE - Common Desktop Environment
  3. *
  4. * Copyright (c) 1993-2012, The Open Group. All rights reserved.
  5. *
  6. * These libraries and programs are free software; you can
  7. * redistribute them and/or modify them under the terms of the GNU
  8. * Lesser General Public License as published by the Free Software
  9. * Foundation; either version 2 of the License, or (at your option)
  10. * any later version.
  11. *
  12. * These libraries and programs are distributed in the hope that
  13. * they will be useful, but WITHOUT ANY WARRANTY; without even the
  14. * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  15. * PURPOSE. See the GNU Lesser General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with these libraries and programs; if not, write
  20. * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
  21. * Floor, Boston, MA 02110-1301 USA
  22. */
  23. /* Quarks.c 1.1 - Fujitsu source for CDEnext 95/11/06 20:31:17 */
  24. /* $XConsortium: _falQuarks.c /main/2 1996/09/09 13:20:21 rswiston $ */
  25. /***********************************************************
  26. Copyright 1987, 1988, 1990 by Digital Equipment Corporation, Maynard,
  27. All Rights Reserved
  28. Permission to use, copy, modify, and distribute this software and its
  29. documentation for any purpose and without fee is hereby granted,
  30. provided that the above copyright notice appear in all copies and that
  31. both that copyright notice and this permission notice appear in
  32. supporting documentation, and that the name Digital not be
  33. used in advertising or publicity pertaining to distribution of the
  34. software without specific, written prior permission.
  35. DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  36. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  37. DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  38. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  39. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  40. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  41. SOFTWARE.
  42. ******************************************************************/
  43. /*
  44. Copyright (c) 1987, 1988, 1990 X Consortium
  45. Permission is hereby granted, free of charge, to any person obtaining
  46. a copy of this software and associated documentation files (the
  47. "Software"), to deal in the Software without restriction, including
  48. without limitation the rights to use, copy, modify, merge, publish,
  49. distribute, sublicense, and/or sell copies of the Software, and to
  50. permit persons to whom the Software is furnished to do so, subject to
  51. the following conditions:
  52. The above copyright notice and this permission notice shall be included
  53. in all copies or substantial portions of the Software.
  54. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  55. OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  56. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  57. IN NO EVENT SHALL THE X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR
  58. OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  59. ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  60. OTHER DEALINGS IN THE SOFTWARE.
  61. Except as contained in this notice, the name of the X Consortium shall
  62. not be used in advertising or otherwise to promote the sale, use or
  63. other dealings in this Software without prior written authorization
  64. from the X Consortium.
  65. */
  66. #include "_fallibint.h"
  67. #include <X11/Xresource.h>
  68. /* Not cost effective, at least for vanilla MIT clients */
  69. /* #define PERMQ */
  70. typedef unsigned long Signature;
  71. typedef unsigned long Entry;
  72. #ifdef PERMQ
  73. typedef unsigned char Bits;
  74. #endif
  75. static XrmQuark nextQuark = 1; /* next available quark number */
  76. static unsigned long quarkMask = 0;
  77. static Entry zero = 0;
  78. static Entry *quarkTable = &zero; /* crock */
  79. static unsigned long quarkRehash;
  80. static XrmString **stringTable = NULL;
  81. #ifdef PERMQ
  82. static Bits **permTable = NULL;
  83. #endif
  84. static XrmQuark nextUniq = -1; /* next quark from falrmUniqueQuark */
  85. #define QUANTUMSHIFT 8
  86. #define QUANTUMMASK ((1 << QUANTUMSHIFT) - 1)
  87. #define CHUNKPER 8
  88. #define CHUNKMASK ((CHUNKPER << QUANTUMSHIFT) - 1)
  89. #define LARGEQUARK ((Entry)0x80000000L)
  90. #define QUARKSHIFT 18
  91. #define QUARKMASK ((LARGEQUARK - 1) >> QUARKSHIFT)
  92. #define XSIGMASK ((1L << QUARKSHIFT) - 1)
  93. #define STRQUANTSIZE (sizeof(XrmString) * (QUANTUMMASK + 1))
  94. #ifdef PERMQ
  95. #define QUANTSIZE (STRQUANTSIZE + \
  96. (sizeof(Bits) * ((QUANTUMMASK + 1) >> 3))
  97. #else
  98. #define QUANTSIZE STRQUANTSIZE
  99. #endif
  100. #define HASH(sig) ((sig) & quarkMask)
  101. #define REHASHVAL(sig) ((((sig) % quarkRehash) + 2) | 1)
  102. #define REHASH(idx,rehash) ((idx + rehash) & quarkMask)
  103. #define NAME(q) stringTable[(q) >> QUANTUMSHIFT][(q) & QUANTUMMASK]
  104. #ifdef PERMQ
  105. #define BYTEREF(q) permTable[(q) >> QUANTUMSHIFT][((q) & QUANTUMMASK) >> 3]
  106. #define ISPERM(q) (BYTEREF(q) & (1 << ((q) & 7)))
  107. #define SETPERM(q) BYTEREF(q) |= (1 << ((q) & 7))
  108. #define CLEARPERM(q) BYTEREF(q) &= ~(1 << ((q) & 7))
  109. #endif
  110. /* Permanent memory allocation */
  111. #define WALIGN sizeof(unsigned long)
  112. #define DALIGN sizeof(double)
  113. #define NEVERFREETABLESIZE ((8192-12) & ~(DALIGN-1))
  114. static char *neverFreeTable = NULL;
  115. static int neverFreeTableSize = 0;
  116. static char *permalloc(unsigned int length)
  117. {
  118. char *ret;
  119. if (neverFreeTableSize < length) {
  120. if (length >= NEVERFREETABLESIZE)
  121. return Xmalloc(length);
  122. if (! (ret = Xmalloc(NEVERFREETABLESIZE)))
  123. return (char *) NULL;
  124. neverFreeTableSize = NEVERFREETABLESIZE;
  125. neverFreeTable = ret;
  126. }
  127. ret = neverFreeTable;
  128. neverFreeTable += length;
  129. neverFreeTableSize -= length;
  130. return(ret);
  131. }
  132. typedef struct {char a; double b;} TestType1;
  133. typedef struct {char a; unsigned long b;} TestType2;
  134. #ifdef XTHREADS
  135. static char *_falpermalloc();
  136. char *falpermalloc(unsigned int length)
  137. {
  138. char *p;
  139. _XLockMutex(_Xglobal_lock);
  140. p = _falpermalloc(length);
  141. _XUnlockMutex(_Xglobal_lock);
  142. return p;
  143. }
  144. #define falpermalloc _falpermalloc
  145. static
  146. #endif /* XTHREADS */
  147. char *falpermalloc(unsigned int length)
  148. {
  149. int i;
  150. if (neverFreeTableSize && length < NEVERFREETABLESIZE) {
  151. if ((sizeof(TestType1) !=
  152. (sizeof(TestType2) - sizeof(unsigned long) + sizeof(double))) &&
  153. !(length & (DALIGN-1)) &&
  154. (i = (NEVERFREETABLESIZE - neverFreeTableSize) & (DALIGN-1))) {
  155. neverFreeTableSize -= DALIGN - i;
  156. neverFreeTable += DALIGN - i;
  157. } else
  158. if (i = (NEVERFREETABLESIZE - neverFreeTableSize) & (WALIGN-1)) {
  159. neverFreeTableSize -= WALIGN - i;
  160. neverFreeTable += WALIGN - i;
  161. }
  162. }
  163. return permalloc(length);
  164. }
  165. static Bool
  166. ExpandQuarkTable(void)
  167. {
  168. unsigned long oldmask, newmask;
  169. char c, *s;
  170. Entry *oldentries, *entries;
  171. Entry entry;
  172. int oldidx, newidx, rehash;
  173. Signature sig;
  174. XrmQuark q;
  175. oldentries = quarkTable;
  176. if (oldmask = quarkMask)
  177. newmask = (oldmask << 1) + 1;
  178. else {
  179. if (!stringTable) {
  180. stringTable = (XrmString **)Xmalloc(sizeof(XrmString *) *
  181. CHUNKPER);
  182. if (!stringTable)
  183. return False;
  184. stringTable[0] = (XrmString *)NULL;
  185. }
  186. #ifdef PERMQ
  187. if (!permTable)
  188. permTable = (Bits **)Xmalloc(sizeof(Bits *) * CHUNKPER);
  189. if (!permTable)
  190. return False;
  191. #endif
  192. stringTable[0] = (XrmString *)falpermalloc(QUANTSIZE);
  193. if (!stringTable[0])
  194. return False;
  195. #ifdef PERMQ
  196. permTable[0] = (Bits *)((char *)stringTable[0] + STRQUANTSIZE);
  197. #endif
  198. newmask = 0x1ff;
  199. }
  200. entries = (Entry *)Xmalloc(sizeof(Entry) * (newmask + 1));
  201. if (!entries)
  202. return False;
  203. bzero((char *)entries, sizeof(Entry) * (newmask + 1));
  204. quarkTable = entries;
  205. quarkMask = newmask;
  206. quarkRehash = quarkMask - 2;
  207. for (oldidx = 0; oldidx <= oldmask; oldidx++) {
  208. if (entry = oldentries[oldidx]) {
  209. if (entry & LARGEQUARK)
  210. q = entry & (LARGEQUARK-1);
  211. else
  212. q = (entry >> QUARKSHIFT) & QUARKMASK;
  213. for (sig = 0, s = NAME(q); c = *s++; )
  214. sig = (sig << 1) + c;
  215. newidx = HASH(sig);
  216. if (entries[newidx]) {
  217. rehash = REHASHVAL(sig);
  218. do {
  219. newidx = REHASH(newidx, rehash);
  220. } while (entries[newidx]);
  221. }
  222. entries[newidx] = entry;
  223. }
  224. }
  225. if (oldmask)
  226. Xfree((char *)oldentries);
  227. return True;
  228. }
  229. XrmQuark _falrmInternalStringToQuark(
  230. const char *name, int len, Signature sig, Bool permstring)
  231. {
  232. XrmQuark q;
  233. Entry entry;
  234. int idx;
  235. int rehash = 0;
  236. int i;
  237. char *s1, *s2;
  238. char *new;
  239. idx = HASH(sig);
  240. _XLockMutex(_Xglobal_lock);
  241. while (entry = quarkTable[idx]) {
  242. if (entry & LARGEQUARK)
  243. q = entry & (LARGEQUARK-1);
  244. else {
  245. if ((entry - sig) & XSIGMASK){
  246. if (!rehash)
  247. rehash = REHASHVAL(sig);
  248. idx = REHASH(idx, rehash);
  249. continue;
  250. }
  251. q = (entry >> QUARKSHIFT) & QUARKMASK;
  252. }
  253. for (i = len, s1 = (char *)name, s2 = NAME(q); --i >= 0; ) {
  254. if (*s1++ != *s2++){
  255. if (!rehash)
  256. rehash = REHASHVAL(sig);
  257. idx = REHASH(idx, rehash);
  258. continue;
  259. }
  260. }
  261. if (*s2) {
  262. if (!rehash)
  263. rehash = REHASHVAL(sig);
  264. idx = REHASH(idx, rehash);
  265. continue;
  266. }
  267. #ifdef PERMQ
  268. if (permstring && !ISPERM(q)) {
  269. Xfree(NAME(q));
  270. NAME(q) = (char *)name;
  271. SETPERM(q);
  272. }
  273. #endif
  274. _XUnlockMutex(_Xglobal_lock);
  275. return q;
  276. }
  277. if (nextUniq == nextQuark){
  278. _XUnlockMutex(_Xglobal_lock);
  279. return NULLQUARK;
  280. }
  281. if ((nextQuark + (nextQuark >> 2)) > quarkMask) {
  282. if (!ExpandQuarkTable()){
  283. _XUnlockMutex(_Xglobal_lock);
  284. return NULLQUARK;
  285. }
  286. _XUnlockMutex(_Xglobal_lock);
  287. return _falrmInternalStringToQuark(name, len, sig, permstring);
  288. }
  289. q = nextQuark;
  290. if (!(q & QUANTUMMASK)) {
  291. if (!(q & CHUNKMASK)) {
  292. if (!(new = Xrealloc((char *)stringTable,
  293. sizeof(XrmString *) *
  294. ((q >> QUANTUMSHIFT) + CHUNKPER)))){
  295. _XUnlockMutex(_Xglobal_lock);
  296. return NULLQUARK;
  297. }
  298. stringTable = (XrmString **)new;
  299. #ifdef PERMQ
  300. if (!(new = Xrealloc((char *)permTable,
  301. sizeof(Bits *) *
  302. ((q >> QUANTUMSHIFT) + CHUNKPER)))){
  303. _XUnlockMutex(_Xglobal_lock);
  304. return NULLQUARK;
  305. }
  306. permTable = (Bits **)new;
  307. #endif
  308. }
  309. new = falpermalloc(QUANTSIZE);
  310. if (!new){
  311. _XUnlockMutex(_Xglobal_lock);
  312. return NULLQUARK;
  313. }
  314. stringTable[q >> QUANTUMSHIFT] = (XrmString *)new;
  315. #ifdef PERMQ
  316. permTable[q >> QUANTUMSHIFT] = (Bits *)(new + STRQUANTSIZE);
  317. #endif
  318. }
  319. if (!permstring) {
  320. s2 = (char *)name;
  321. #ifdef PERMQ
  322. name = Xmalloc(len+1);
  323. #else
  324. name = permalloc(len+1);
  325. #endif
  326. if (!name){
  327. _XUnlockMutex(_Xglobal_lock);
  328. return NULLQUARK;
  329. }
  330. for (i = len, s1 = (char *)name; --i >= 0; )
  331. *s1++ = *s2++;
  332. *s1++ = '\0';
  333. #ifdef PERMQ
  334. CLEARPERM(q);
  335. }
  336. else {
  337. SETPERM(q);
  338. #endif
  339. }
  340. NAME(q) = (char *)name;
  341. if (q <= QUARKMASK)
  342. entry = (q << QUARKSHIFT) | (sig & XSIGMASK);
  343. else
  344. entry = q | LARGEQUARK;
  345. quarkTable[idx] = entry;
  346. nextQuark++;
  347. _XUnlockMutex(_Xglobal_lock);
  348. return q;
  349. }
  350. XrmQuark falrmStringToQuark(const char *name)
  351. {
  352. char c, *tname;
  353. Signature sig = 0;
  354. if (!name)
  355. return (NULLQUARK);
  356. for (tname = (char *)name; c = *tname++; )
  357. sig = (sig << 1) + c;
  358. return _falrmInternalStringToQuark(name, tname-(char *)name-1, sig, False);
  359. }
  360. XrmQuark falrmPermStringToQuark(
  361. const char *name)
  362. {
  363. char c, *tname;
  364. Signature sig = 0;
  365. if (!name)
  366. return (NULLQUARK);
  367. for (tname = (char *)name; c = *tname++; )
  368. sig = (sig << 1) + c;
  369. return _falrmInternalStringToQuark(name, tname-(char *)name-1, sig, True);
  370. }
  371. XrmQuark falrmUniqueQuark(void)
  372. {
  373. XrmQuark q;
  374. _XLockMutex(_Xglobal_lock);
  375. if (nextUniq == nextQuark)
  376. q = NULLQUARK;
  377. else
  378. q = nextUniq--;
  379. _XUnlockMutex(_Xglobal_lock);
  380. return q;
  381. }
  382. XrmString falrmQuarkToString(XrmQuark quark)
  383. {
  384. XrmString s;
  385. _XLockMutex(_Xglobal_lock);
  386. if (quark <= 0 || quark >= nextQuark)
  387. s = NULLSTRING;
  388. else {
  389. #ifdef PERMQ
  390. /* We have to mark the quark as permanent, since the caller might hold
  391. * onto the string pointer forver.
  392. */
  393. SETPERM(quark);
  394. #endif
  395. s = NAME(quark);
  396. }
  397. _XUnlockMutex(_Xglobal_lock);
  398. return s;
  399. }