atom.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /*
  2. Copyright (c) 2003-2006 by Juliusz Chroboczek
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #include "polipo.h"
  20. /* Atoms are interned, read-only reference-counted strings.
  21. Interned means that equality of atoms is equivalent to structural
  22. equality -- you don't need to strcmp, you just compare the AtomPtrs.
  23. This property is used throughout Polipo, e.g. to speed up the HTTP
  24. parser.
  25. Polipo's atoms may contain NUL bytes -- you can use internAtomN to
  26. store any random binary data within an atom. However, Polipo always
  27. terminates your data, so if you store textual data in an atom, you
  28. may use the result of atomString as though it were a (read-only)
  29. C string.
  30. */
  31. static AtomPtr *atomHashTable;
  32. int used_atoms;
  33. void
  34. initAtoms()
  35. {
  36. atomHashTable = calloc((1 << LOG2_ATOM_HASH_TABLE_SIZE),
  37. sizeof(AtomPtr));
  38. if(atomHashTable == NULL) {
  39. do_log(L_ERROR, "Couldn't allocate atom hash table.\n");
  40. exit(1);
  41. }
  42. used_atoms = 0;
  43. }
  44. AtomPtr
  45. internAtomN(const char *string, int n)
  46. {
  47. AtomPtr atom;
  48. int h;
  49. if(n < 0 || n >= (1 << (8 * sizeof(unsigned short))))
  50. return NULL;
  51. h = hash(0, string, n, LOG2_ATOM_HASH_TABLE_SIZE);
  52. atom = atomHashTable[h];
  53. while(atom) {
  54. if(atom->length == n &&
  55. (n == 0 || memcmp(atom->string, string, n) == 0))
  56. break;
  57. atom = atom->next;
  58. }
  59. if(!atom) {
  60. atom = malloc(sizeof(AtomRec) - 1 + n + 1);
  61. if(atom == NULL) {
  62. return NULL;
  63. }
  64. atom->refcount = 0;
  65. atom->length = n;
  66. /* Atoms are used both for binary data and strings. To make
  67. their use as strings more convenient, atoms are always
  68. NUL-terminated. */
  69. memcpy(atom->string, string, n);
  70. atom->string[n] = '\0';
  71. atom->next = atomHashTable[h];
  72. atomHashTable[h] = atom;
  73. used_atoms++;
  74. }
  75. do_log(D_ATOM_REFCOUNT, "A 0x%lx %d++\n",
  76. (unsigned long)atom, atom->refcount);
  77. atom->refcount++;
  78. return atom;
  79. }
  80. AtomPtr
  81. internAtom(const char *string)
  82. {
  83. return internAtomN(string, strlen(string));
  84. }
  85. AtomPtr
  86. atomCat(AtomPtr atom, const char *string)
  87. {
  88. char buf[128];
  89. char *s = buf;
  90. AtomPtr newAtom;
  91. int n = strlen(string);
  92. if(atom->length + n > 128) {
  93. s = malloc(atom->length + n + 1);
  94. if(s == NULL)
  95. return NULL;
  96. }
  97. memcpy(s, atom->string, atom->length);
  98. memcpy(s + atom->length, string, n);
  99. newAtom = internAtomN(s, atom->length + n);
  100. if(s != buf) free(s);
  101. return newAtom;
  102. }
  103. int
  104. atomSplit(AtomPtr atom, char c, AtomPtr *return1, AtomPtr *return2)
  105. {
  106. char *p;
  107. AtomPtr atom1, atom2;
  108. p = memchr(atom->string, c, atom->length);
  109. if(p == NULL)
  110. return 0;
  111. atom1 = internAtomN(atom->string, p - atom->string);
  112. if(atom1 == NULL)
  113. return -ENOMEM;
  114. atom2 = internAtomN(p + 1, atom->length - (p + 1 - atom->string));
  115. if(atom2 == NULL) {
  116. releaseAtom(atom1);
  117. return -ENOMEM;
  118. }
  119. *return1 = atom1;
  120. *return2 = atom2;
  121. return 1;
  122. }
  123. AtomPtr
  124. internAtomLowerN(const char *string, int n)
  125. {
  126. char *s;
  127. char buf[100];
  128. AtomPtr atom;
  129. if(n < 0 || n >= 50000)
  130. return NULL;
  131. if(n < 100) {
  132. s = buf;
  133. } else {
  134. s = malloc(n);
  135. if(s == NULL)
  136. return NULL;
  137. }
  138. lwrcpy(s, string, n);
  139. atom = internAtomN(s, n);
  140. if(s != buf) free(s);
  141. return atom;
  142. }
  143. AtomPtr
  144. retainAtom(AtomPtr atom)
  145. {
  146. if(atom == NULL)
  147. return NULL;
  148. do_log(D_ATOM_REFCOUNT, "A 0x%lx %d++\n",
  149. (unsigned long)atom, atom->refcount);
  150. assert(atom->refcount >= 1 && atom->refcount < LARGE_ATOM_REFCOUNT);
  151. atom->refcount++;
  152. return atom;
  153. }
  154. void
  155. releaseAtom(AtomPtr atom)
  156. {
  157. if(atom == NULL)
  158. return;
  159. do_log(D_ATOM_REFCOUNT, "A 0x%lx %d--\n",
  160. (unsigned long)atom, atom->refcount);
  161. assert(atom->refcount >= 1 && atom->refcount < LARGE_ATOM_REFCOUNT);
  162. atom->refcount--;
  163. if(atom->refcount == 0) {
  164. int h = hash(0, atom->string, atom->length, LOG2_ATOM_HASH_TABLE_SIZE);
  165. assert(atomHashTable[h] != NULL);
  166. if(atom == atomHashTable[h]) {
  167. atomHashTable[h] = atom->next;
  168. free(atom);
  169. } else {
  170. AtomPtr previous = atomHashTable[h];
  171. while(previous->next) {
  172. if(previous->next == atom)
  173. break;
  174. previous = previous->next;
  175. }
  176. assert(previous->next != NULL);
  177. previous->next = atom->next;
  178. free(atom);
  179. }
  180. used_atoms--;
  181. }
  182. }
  183. AtomPtr
  184. internAtomF(const char *format, ...)
  185. {
  186. char *s;
  187. char buf[150];
  188. int n;
  189. va_list args;
  190. AtomPtr atom = NULL;
  191. va_start(args, format);
  192. n = vsnprintf(buf, 150, format, args);
  193. va_end(args);
  194. if(n >= 0 && n < 150) {
  195. atom = internAtomN(buf, n);
  196. } else {
  197. va_start(args, format);
  198. s = vsprintf_a(format, args);
  199. va_end(args);
  200. if(s != NULL) {
  201. atom = internAtom(s);
  202. free(s);
  203. }
  204. }
  205. return atom;
  206. }
  207. static AtomPtr
  208. internAtomErrorV(int e, const char *f, va_list args)
  209. {
  210. char *es = pstrerror(e);
  211. AtomPtr atom;
  212. char *s1, *s2;
  213. int n, rc;
  214. va_list args_copy;
  215. if(f) {
  216. va_copy(args_copy, args);
  217. s1 = vsprintf_a(f, args_copy);
  218. va_end(args_copy);
  219. if(s1 == NULL)
  220. return NULL;
  221. n = strlen(s1);
  222. } else {
  223. s1 = NULL;
  224. n = 0;
  225. }
  226. s2 = malloc(n + 70);
  227. if(s2 == NULL) {
  228. free(s1);
  229. return NULL;
  230. }
  231. if(s1) {
  232. strcpy(s2, s1);
  233. free(s1);
  234. }
  235. rc = snprintf(s2 + n, 69, f ? ": %s" : "%s", es);
  236. if(rc < 0 || rc >= 69) {
  237. free(s2);
  238. return NULL;
  239. }
  240. atom = internAtomN(s2, n + rc);
  241. free(s2);
  242. return atom;
  243. }
  244. AtomPtr
  245. internAtomError(int e, const char *f, ...)
  246. {
  247. AtomPtr atom;
  248. va_list args;
  249. va_start(args, f);
  250. atom = internAtomErrorV(e, f, args);
  251. va_end(args);
  252. return atom;
  253. }
  254. char *
  255. atomString(AtomPtr atom)
  256. {
  257. if(atom)
  258. return atom->string;
  259. else
  260. return "(null)";
  261. }
  262. AtomListPtr
  263. makeAtomList(AtomPtr *atoms, int n)
  264. {
  265. AtomListPtr list;
  266. list = malloc(sizeof(AtomListRec));
  267. if(list == NULL) return NULL;
  268. list->length = 0;
  269. list->size = 0;
  270. list->list = NULL;
  271. if(n > 0) {
  272. int i;
  273. list->list = malloc(n * sizeof(AtomPtr));
  274. if(list->list == NULL) {
  275. free(list);
  276. return NULL;
  277. }
  278. list->size = n;
  279. for(i = 0; i < n; i++)
  280. list->list[i] = atoms[i];
  281. list->length = n;
  282. }
  283. return list;
  284. }
  285. void
  286. destroyAtomList(AtomListPtr list)
  287. {
  288. int i;
  289. if(list->list) {
  290. for(i = 0; i < list->length; i++)
  291. releaseAtom(list->list[i]);
  292. list->length = 0;
  293. free(list->list);
  294. list->list = NULL;
  295. list->size = 0;
  296. }
  297. assert(list->size == 0);
  298. free(list);
  299. }
  300. int
  301. atomListMember(AtomPtr atom, AtomListPtr list)
  302. {
  303. int i;
  304. for(i = 0; i < list->length; i++) {
  305. if(atom == list->list[i])
  306. return 1;
  307. }
  308. return 0;
  309. }
  310. void
  311. atomListCons(AtomPtr atom, AtomListPtr list)
  312. {
  313. if(list->list == NULL) {
  314. assert(list->size == 0);
  315. list->list = malloc(5 * sizeof(AtomPtr));
  316. if(list->list == NULL) {
  317. do_log(L_ERROR, "Couldn't allocate AtomList\n");
  318. return;
  319. }
  320. list->size = 5;
  321. }
  322. if(list->size <= list->length) {
  323. AtomPtr *new_list;
  324. int n = (2 * list->length + 1);
  325. new_list = realloc(list->list, n * sizeof(AtomPtr));
  326. if(new_list == NULL) {
  327. do_log(L_ERROR, "Couldn't realloc AtomList\n");
  328. return;
  329. }
  330. list->list = new_list;
  331. list->size = n;
  332. }
  333. list->list[list->length] = atom;
  334. list->length++;
  335. }