Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(54)

Side by Side Diff: third_party/libxml/src/dict.c

Issue 1977213002: Roll libxml to 8effcb578e0590cc01bbcab0f9dccefc6bdbcdbd (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Update README.chromium. Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * dict.c: dictionary of reusable strings, just used to avoid allocation 2 * dict.c: dictionary of reusable strings, just used to avoid allocation
3 * and freeing operations. 3 * and freeing operations.
4 * 4 *
5 * Copyright (C) 2003-2012 Daniel Veillard. 5 * Copyright (C) 2003-2012 Daniel Veillard.
6 * 6 *
7 * Permission to use, copy, modify, and distribute this software for any 7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above 8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies. 9 * copyright notice and this permission notice appear in all copies.
10 * 10 *
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
80 xmlDictComputeBigQKey(prefix, plen, name, len, (dict)->seed))) 80 xmlDictComputeBigQKey(prefix, plen, name, len, (dict)->seed)))
81 81
82 #else /* !WITH_BIG_KEY */ 82 #else /* !WITH_BIG_KEY */
83 #define xmlDictComputeKey(dict, name, len) \ 83 #define xmlDictComputeKey(dict, name, len) \
84 xmlDictComputeFastKey(name, len, (dict)->seed) 84 xmlDictComputeFastKey(name, len, (dict)->seed)
85 #define xmlDictComputeQKey(dict, prefix, plen, name, len) \ 85 #define xmlDictComputeQKey(dict, prefix, plen, name, len) \
86 xmlDictComputeFastQKey(prefix, plen, name, len, (dict)->seed) 86 xmlDictComputeFastQKey(prefix, plen, name, len, (dict)->seed)
87 #endif /* WITH_BIG_KEY */ 87 #endif /* WITH_BIG_KEY */
88 88
89 /* 89 /*
90 * An entry in the dictionnary 90 * An entry in the dictionary
91 */ 91 */
92 typedef struct _xmlDictEntry xmlDictEntry; 92 typedef struct _xmlDictEntry xmlDictEntry;
93 typedef xmlDictEntry *xmlDictEntryPtr; 93 typedef xmlDictEntry *xmlDictEntryPtr;
94 struct _xmlDictEntry { 94 struct _xmlDictEntry {
95 struct _xmlDictEntry *next; 95 struct _xmlDictEntry *next;
96 const xmlChar *name; 96 const xmlChar *name;
97 unsigned int len; 97 unsigned int len;
98 int valid; 98 int valid;
99 unsigned long okey; 99 unsigned long okey;
100 }; 100 };
101 101
102 typedef struct _xmlDictStrings xmlDictStrings; 102 typedef struct _xmlDictStrings xmlDictStrings;
103 typedef xmlDictStrings *xmlDictStringsPtr; 103 typedef xmlDictStrings *xmlDictStringsPtr;
104 struct _xmlDictStrings { 104 struct _xmlDictStrings {
105 xmlDictStringsPtr next; 105 xmlDictStringsPtr next;
106 xmlChar *free; 106 xmlChar *free;
107 xmlChar *end; 107 xmlChar *end;
108 size_t size; 108 size_t size;
109 size_t nbStrings; 109 size_t nbStrings;
110 xmlChar array[1]; 110 xmlChar array[1];
111 }; 111 };
112 /* 112 /*
113 * The entire dictionnary 113 * The entire dictionary
114 */ 114 */
115 struct _xmlDict { 115 struct _xmlDict {
116 int ref_counter; 116 int ref_counter;
117 117
118 struct _xmlDictEntry *dict; 118 struct _xmlDictEntry *dict;
119 size_t size; 119 size_t size;
120 unsigned int nbElems; 120 unsigned int nbElems;
121 xmlDictStringsPtr strings; 121 xmlDictStringsPtr strings;
122 122
123 struct _xmlDict *subdict; 123 struct _xmlDict *subdict;
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
222 if (!xmlDictInitialized) 222 if (!xmlDictInitialized)
223 return; 223 return;
224 224
225 xmlFreeRMutex(xmlDictMutex); 225 xmlFreeRMutex(xmlDictMutex);
226 226
227 xmlDictInitialized = 0; 227 xmlDictInitialized = 0;
228 } 228 }
229 229
230 /* 230 /*
231 * xmlDictAddString: 231 * xmlDictAddString:
232 * @dict: the dictionnary 232 * @dict: the dictionary
233 * @name: the name of the userdata 233 * @name: the name of the userdata
234 * @len: the length of the name 234 * @len: the length of the name
235 * 235 *
236 * Add the string to the array[s] 236 * Add the string to the array[s]
237 * 237 *
238 * Returns the pointer of the local string, or NULL in case of error. 238 * Returns the pointer of the local string, or NULL in case of error.
239 */ 239 */
240 static const xmlChar * 240 static const xmlChar *
241 xmlDictAddString(xmlDictPtr dict, const xmlChar *name, unsigned int namelen) { 241 xmlDictAddString(xmlDictPtr dict, const xmlChar *name, unsigned int namelen) {
242 xmlDictStringsPtr pool; 242 xmlDictStringsPtr pool;
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
284 ret = pool->free; 284 ret = pool->free;
285 memcpy(pool->free, name, namelen); 285 memcpy(pool->free, name, namelen);
286 pool->free += namelen; 286 pool->free += namelen;
287 *(pool->free++) = 0; 287 *(pool->free++) = 0;
288 pool->nbStrings++; 288 pool->nbStrings++;
289 return(ret); 289 return(ret);
290 } 290 }
291 291
292 /* 292 /*
293 * xmlDictAddQString: 293 * xmlDictAddQString:
294 * @dict: the dictionnary 294 * @dict: the dictionary
295 * @prefix: the prefix of the userdata 295 * @prefix: the prefix of the userdata
296 * @plen: the prefix length 296 * @plen: the prefix length
297 * @name: the name of the userdata 297 * @name: the name of the userdata
298 * @len: the length of the name 298 * @len: the length of the name
299 * 299 *
300 * Add the QName to the array[s] 300 * Add the QName to the array[s]
301 * 301 *
302 * Returns the pointer of the local string, or NULL in case of error. 302 * Returns the pointer of the local string, or NULL in case of error.
303 */ 303 */
304 static const xmlChar * 304 static const xmlChar *
(...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after
526 default: break; 526 default: break;
527 } 527 }
528 return(value); 528 return(value);
529 } 529 }
530 530
531 /** 531 /**
532 * xmlDictCreate: 532 * xmlDictCreate:
533 * 533 *
534 * Create a new dictionary 534 * Create a new dictionary
535 * 535 *
536 * Returns the newly created dictionnary, or NULL if an error occured. 536 * Returns the newly created dictionary, or NULL if an error occured.
537 */ 537 */
538 xmlDictPtr 538 xmlDictPtr
539 xmlDictCreate(void) { 539 xmlDictCreate(void) {
540 xmlDictPtr dict; 540 xmlDictPtr dict;
541 541
542 if (!xmlDictInitialized) 542 if (!xmlDictInitialized)
543 if (!__xmlInitializeDict()) 543 if (!__xmlInitializeDict())
544 return(NULL); 544 return(NULL);
545 545
546 #ifdef DICT_DEBUG_PATTERNS 546 #ifdef DICT_DEBUG_PATTERNS
(...skipping 19 matching lines...) Expand all
566 #endif 566 #endif
567 return(dict); 567 return(dict);
568 } 568 }
569 xmlFree(dict); 569 xmlFree(dict);
570 } 570 }
571 return(NULL); 571 return(NULL);
572 } 572 }
573 573
574 /** 574 /**
575 * xmlDictCreateSub: 575 * xmlDictCreateSub:
576 * @sub: an existing dictionnary 576 * @sub: an existing dictionary
577 * 577 *
578 * Create a new dictionary, inheriting strings from the read-only 578 * Create a new dictionary, inheriting strings from the read-only
579 * dictionnary @sub. On lookup, strings are first searched in the 579 * dictionary @sub. On lookup, strings are first searched in the
580 * new dictionnary, then in @sub, and if not found are created in the 580 * new dictionary, then in @sub, and if not found are created in the
581 * new dictionnary. 581 * new dictionary.
582 * 582 *
583 * Returns the newly created dictionnary, or NULL if an error occured. 583 * Returns the newly created dictionary, or NULL if an error occured.
584 */ 584 */
585 xmlDictPtr 585 xmlDictPtr
586 xmlDictCreateSub(xmlDictPtr sub) { 586 xmlDictCreateSub(xmlDictPtr sub) {
587 xmlDictPtr dict = xmlDictCreate(); 587 xmlDictPtr dict = xmlDictCreate();
588 588
589 if ((dict != NULL) && (sub != NULL)) { 589 if ((dict != NULL) && (sub != NULL)) {
590 #ifdef DICT_DEBUG_PATTERNS 590 #ifdef DICT_DEBUG_PATTERNS
591 fprintf(stderr, "R"); 591 fprintf(stderr, "R");
592 #endif 592 #endif
593 dict->seed = sub->seed; 593 dict->seed = sub->seed;
594 dict->subdict = sub; 594 dict->subdict = sub;
595 xmlDictReference(dict->subdict); 595 xmlDictReference(dict->subdict);
596 } 596 }
597 return(dict); 597 return(dict);
598 } 598 }
599 599
600 /** 600 /**
601 * xmlDictReference: 601 * xmlDictReference:
602 * @dict: the dictionnary 602 * @dict: the dictionary
603 * 603 *
604 * Increment the reference counter of a dictionary 604 * Increment the reference counter of a dictionary
605 * 605 *
606 * Returns 0 in case of success and -1 in case of error 606 * Returns 0 in case of success and -1 in case of error
607 */ 607 */
608 int 608 int
609 xmlDictReference(xmlDictPtr dict) { 609 xmlDictReference(xmlDictPtr dict) {
610 if (!xmlDictInitialized) 610 if (!xmlDictInitialized)
611 if (!__xmlInitializeDict()) 611 if (!__xmlInitializeDict())
612 return(-1); 612 return(-1);
613 613
614 if (dict == NULL) return -1; 614 if (dict == NULL) return -1;
615 xmlRMutexLock(xmlDictMutex); 615 xmlRMutexLock(xmlDictMutex);
616 dict->ref_counter++; 616 dict->ref_counter++;
617 xmlRMutexUnlock(xmlDictMutex); 617 xmlRMutexUnlock(xmlDictMutex);
618 return(0); 618 return(0);
619 } 619 }
620 620
621 /** 621 /**
622 * xmlDictGrow: 622 * xmlDictGrow:
623 * @dict: the dictionnary 623 * @dict: the dictionary
624 * @size: the new size of the dictionnary 624 * @size: the new size of the dictionary
625 * 625 *
626 * resize the dictionnary 626 * resize the dictionary
627 * 627 *
628 * Returns 0 in case of success, -1 in case of failure 628 * Returns 0 in case of success, -1 in case of failure
629 */ 629 */
630 static int 630 static int
631 xmlDictGrow(xmlDictPtr dict, size_t size) { 631 xmlDictGrow(xmlDictPtr dict, size_t size) {
632 unsigned long key, okey; 632 unsigned long key, okey;
633 size_t oldsize, i; 633 size_t oldsize, i;
634 xmlDictEntryPtr iter, next; 634 xmlDictEntryPtr iter, next;
635 struct _xmlDictEntry *olddict; 635 struct _xmlDictEntry *olddict;
636 #ifdef DEBUG_GROW 636 #ifdef DEBUG_GROW
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after
748 #ifdef DEBUG_GROW 748 #ifdef DEBUG_GROW
749 xmlGenericError(xmlGenericErrorContext, 749 xmlGenericError(xmlGenericErrorContext,
750 "xmlDictGrow : from %lu to %lu, %u elems\n", oldsize, size, nbElem); 750 "xmlDictGrow : from %lu to %lu, %u elems\n", oldsize, size, nbElem);
751 #endif 751 #endif
752 752
753 return(ret); 753 return(ret);
754 } 754 }
755 755
756 /** 756 /**
757 * xmlDictFree: 757 * xmlDictFree:
758 * @dict: the dictionnary 758 * @dict: the dictionary
759 * 759 *
760 * Free the hash @dict and its contents. The userdata is 760 * Free the hash @dict and its contents. The userdata is
761 * deallocated with @f if provided. 761 * deallocated with @f if provided.
762 */ 762 */
763 void 763 void
764 xmlDictFree(xmlDictPtr dict) { 764 xmlDictFree(xmlDictPtr dict) {
765 size_t i; 765 size_t i;
766 xmlDictEntryPtr iter; 766 xmlDictEntryPtr iter;
767 xmlDictEntryPtr next; 767 xmlDictEntryPtr next;
768 int inside_dict = 0; 768 int inside_dict = 0;
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
810 while (pool != NULL) { 810 while (pool != NULL) {
811 nextp = pool->next; 811 nextp = pool->next;
812 xmlFree(pool); 812 xmlFree(pool);
813 pool = nextp; 813 pool = nextp;
814 } 814 }
815 xmlFree(dict); 815 xmlFree(dict);
816 } 816 }
817 817
818 /** 818 /**
819 * xmlDictLookup: 819 * xmlDictLookup:
820 * @dict: the dictionnary 820 * @dict: the dictionary
821 * @name: the name of the userdata 821 * @name: the name of the userdata
822 * @len: the length of the name, if -1 it is recomputed 822 * @len: the length of the name, if -1 it is recomputed
823 * 823 *
824 * Add the @name to the dictionnary @dict if not present. 824 * Add the @name to the dictionary @dict if not present.
825 * 825 *
826 * Returns the internal copy of the name or NULL in case of internal error 826 * Returns the internal copy of the name or NULL in case of internal error
827 */ 827 */
828 const xmlChar * 828 const xmlChar *
829 xmlDictLookup(xmlDictPtr dict, const xmlChar *name, int len) { 829 xmlDictLookup(xmlDictPtr dict, const xmlChar *name, int len) {
830 unsigned long key, okey, nbi = 0; 830 unsigned long key, okey, nbi = 0;
831 xmlDictEntryPtr entry; 831 xmlDictEntryPtr entry;
832 xmlDictEntryPtr insert; 832 xmlDictEntryPtr insert;
833 const xmlChar *ret; 833 const xmlChar *ret;
834 unsigned int l; 834 unsigned int l;
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
950 if (xmlDictGrow(dict, MAX_HASH_LEN * 2 * dict->size) != 0) 950 if (xmlDictGrow(dict, MAX_HASH_LEN * 2 * dict->size) != 0)
951 return(NULL); 951 return(NULL);
952 } 952 }
953 /* Note that entry may have been freed at this point by xmlDictGrow */ 953 /* Note that entry may have been freed at this point by xmlDictGrow */
954 954
955 return(ret); 955 return(ret);
956 } 956 }
957 957
958 /** 958 /**
959 * xmlDictExists: 959 * xmlDictExists:
960 * @dict: the dictionnary 960 * @dict: the dictionary
961 * @name: the name of the userdata 961 * @name: the name of the userdata
962 * @len: the length of the name, if -1 it is recomputed 962 * @len: the length of the name, if -1 it is recomputed
963 * 963 *
964 * Check if the @name exists in the dictionnary @dict. 964 * Check if the @name exists in the dictionary @dict.
965 * 965 *
966 * Returns the internal copy of the name or NULL if not found. 966 * Returns the internal copy of the name or NULL if not found.
967 */ 967 */
968 const xmlChar * 968 const xmlChar *
969 xmlDictExists(xmlDictPtr dict, const xmlChar *name, int len) { 969 xmlDictExists(xmlDictPtr dict, const xmlChar *name, int len) {
970 unsigned long key, okey, nbi = 0; 970 unsigned long key, okey, nbi = 0;
971 xmlDictEntryPtr insert; 971 xmlDictEntryPtr insert;
972 unsigned int l; 972 unsigned int l;
973 973
974 if ((dict == NULL) || (name == NULL)) 974 if ((dict == NULL) || (name == NULL))
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
1058 #endif 1058 #endif
1059 } 1059 }
1060 } 1060 }
1061 1061
1062 /* not found */ 1062 /* not found */
1063 return(NULL); 1063 return(NULL);
1064 } 1064 }
1065 1065
1066 /** 1066 /**
1067 * xmlDictQLookup: 1067 * xmlDictQLookup:
1068 * @dict: the dictionnary 1068 * @dict: the dictionary
1069 * @prefix: the prefix 1069 * @prefix: the prefix
1070 * @name: the name 1070 * @name: the name
1071 * 1071 *
1072 * Add the QName @prefix:@name to the hash @dict if not present. 1072 * Add the QName @prefix:@name to the hash @dict if not present.
1073 * 1073 *
1074 * Returns the internal copy of the QName or NULL in case of internal error 1074 * Returns the internal copy of the QName or NULL in case of internal error
1075 */ 1075 */
1076 const xmlChar * 1076 const xmlChar *
1077 xmlDictQLookup(xmlDictPtr dict, const xmlChar *prefix, const xmlChar *name) { 1077 xmlDictQLookup(xmlDictPtr dict, const xmlChar *prefix, const xmlChar *name) {
1078 unsigned long okey, key, nbi = 0; 1078 unsigned long okey, key, nbi = 0;
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
1163 if ((nbi > MAX_HASH_LEN) && 1163 if ((nbi > MAX_HASH_LEN) &&
1164 (dict->size <= ((MAX_DICT_HASH / 2) / MAX_HASH_LEN))) 1164 (dict->size <= ((MAX_DICT_HASH / 2) / MAX_HASH_LEN)))
1165 xmlDictGrow(dict, MAX_HASH_LEN * 2 * dict->size); 1165 xmlDictGrow(dict, MAX_HASH_LEN * 2 * dict->size);
1166 /* Note that entry may have been freed at this point by xmlDictGrow */ 1166 /* Note that entry may have been freed at this point by xmlDictGrow */
1167 1167
1168 return(ret); 1168 return(ret);
1169 } 1169 }
1170 1170
1171 /** 1171 /**
1172 * xmlDictOwns: 1172 * xmlDictOwns:
1173 * @dict: the dictionnary 1173 * @dict: the dictionary
1174 * @str: the string 1174 * @str: the string
1175 * 1175 *
1176 * check if a string is owned by the disctionary 1176 * check if a string is owned by the disctionary
1177 * 1177 *
1178 * Returns 1 if true, 0 if false and -1 in case of error 1178 * Returns 1 if true, 0 if false and -1 in case of error
1179 * -1 in case of error 1179 * -1 in case of error
1180 */ 1180 */
1181 int 1181 int
1182 xmlDictOwns(xmlDictPtr dict, const xmlChar *str) { 1182 xmlDictOwns(xmlDictPtr dict, const xmlChar *str) {
1183 xmlDictStringsPtr pool; 1183 xmlDictStringsPtr pool;
1184 1184
1185 if ((dict == NULL) || (str == NULL)) 1185 if ((dict == NULL) || (str == NULL))
1186 return(-1); 1186 return(-1);
1187 pool = dict->strings; 1187 pool = dict->strings;
1188 while (pool != NULL) { 1188 while (pool != NULL) {
1189 if ((str >= &pool->array[0]) && (str <= pool->free)) 1189 if ((str >= &pool->array[0]) && (str <= pool->free))
1190 return(1); 1190 return(1);
1191 pool = pool->next; 1191 pool = pool->next;
1192 } 1192 }
1193 if (dict->subdict) 1193 if (dict->subdict)
1194 return(xmlDictOwns(dict->subdict, str)); 1194 return(xmlDictOwns(dict->subdict, str));
1195 return(0); 1195 return(0);
1196 } 1196 }
1197 1197
1198 /** 1198 /**
1199 * xmlDictSize: 1199 * xmlDictSize:
1200 * @dict: the dictionnary 1200 * @dict: the dictionary
1201 * 1201 *
1202 * Query the number of elements installed in the hash @dict. 1202 * Query the number of elements installed in the hash @dict.
1203 * 1203 *
1204 * Returns the number of elements in the dictionnary or 1204 * Returns the number of elements in the dictionary or
1205 * -1 in case of error 1205 * -1 in case of error
1206 */ 1206 */
1207 int 1207 int
1208 xmlDictSize(xmlDictPtr dict) { 1208 xmlDictSize(xmlDictPtr dict) {
1209 if (dict == NULL) 1209 if (dict == NULL)
1210 return(-1); 1210 return(-1);
1211 if (dict->subdict) 1211 if (dict->subdict)
1212 return(dict->nbElems + dict->subdict->nbElems); 1212 return(dict->nbElems + dict->subdict->nbElems);
1213 return(dict->nbElems); 1213 return(dict->nbElems);
1214 } 1214 }
1215 1215
1216 /** 1216 /**
1217 * xmlDictSetLimit: 1217 * xmlDictSetLimit:
1218 * @dict: the dictionnary 1218 * @dict: the dictionary
1219 * @limit: the limit in bytes 1219 * @limit: the limit in bytes
1220 * 1220 *
1221 * Set a size limit for the dictionary 1221 * Set a size limit for the dictionary
1222 * Added in 2.9.0 1222 * Added in 2.9.0
1223 * 1223 *
1224 * Returns the previous limit of the dictionary or 0 1224 * Returns the previous limit of the dictionary or 0
1225 */ 1225 */
1226 size_t 1226 size_t
1227 xmlDictSetLimit(xmlDictPtr dict, size_t limit) { 1227 xmlDictSetLimit(xmlDictPtr dict, size_t limit) {
1228 size_t ret; 1228 size_t ret;
1229 1229
1230 if (dict == NULL) 1230 if (dict == NULL)
1231 return(0); 1231 return(0);
1232 ret = dict->limit; 1232 ret = dict->limit;
1233 dict->limit = limit; 1233 dict->limit = limit;
1234 return(ret); 1234 return(ret);
1235 } 1235 }
1236 1236
1237 /** 1237 /**
1238 * xmlDictGetUsage: 1238 * xmlDictGetUsage:
1239 * @dict: the dictionnary 1239 * @dict: the dictionary
1240 * 1240 *
1241 * Get how much memory is used by a dictionary for strings 1241 * Get how much memory is used by a dictionary for strings
1242 * Added in 2.9.0 1242 * Added in 2.9.0
1243 * 1243 *
1244 * Returns the amount of strings allocated 1244 * Returns the amount of strings allocated
1245 */ 1245 */
1246 size_t 1246 size_t
1247 xmlDictGetUsage(xmlDictPtr dict) { 1247 xmlDictGetUsage(xmlDictPtr dict) {
1248 xmlDictStringsPtr pool; 1248 xmlDictStringsPtr pool;
1249 size_t limit = 0; 1249 size_t limit = 0;
1250 1250
1251 if (dict == NULL) 1251 if (dict == NULL)
1252 return(0); 1252 return(0);
1253 pool = dict->strings; 1253 pool = dict->strings;
1254 while (pool != NULL) { 1254 while (pool != NULL) {
1255 limit += pool->size; 1255 limit += pool->size;
1256 pool = pool->next; 1256 pool = pool->next;
1257 } 1257 }
1258 return(limit); 1258 return(limit);
1259 } 1259 }
1260 1260
1261 #define bottom_dict 1261 #define bottom_dict
1262 #include "elfgcchack.h" 1262 #include "elfgcchack.h"
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698