forked from ldmud/ldmud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmstrings.c
2190 lines (1783 loc) · 60.9 KB
/
mstrings.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*---------------------------------------------------------------------------
* String Management
*
*---------------------------------------------------------------------------
* TODO:: Optimize for short strings to reduce overhead?
* To reduce the memory used for string storage, the driver implements
* string sharing: for every string the driver keeps track in a refcount
* how many users it has. If the refcount falls back to 0, the string can
* be safely deallocated again. On the other hand, if a refcount overflows
* to 0, the string is considered a constant.
*
* To reduce memory usage even further, strings can be entered into a table.
* On the creation of a new string the driver can lookup the table for
* an already existing copy and return a reference to a string held therein.
* This is used mainly for function names in programs, but also for
* mapping keys. The table is organized as a hash table
* with HTABLE_SIZE entries.
*
* Strings are sequences of chars, stored in an array of known size. The
* size itself is stored separately, allowing the string to contain every
* possible character. Internally the module appends a '\0' character
* to the string data to make it somewhat compatible with C system
* functions; however, this character itself is not counted in the size
* of the string, and the module itself doesn't rely on it.
*
* Strings are managed using a single structure: string_t.
*
* struct string_s
* {
* struct {
* Bool tabled : 1;
* unsigned int ref : 31;
* } info;
* string_t * next; String table pointer.
* size_t size; Length of the string
* hash32_t hash; 0, or the hash of the string
* char txt[1.. .size];
* char null Gratuituous terminator
* }
*
* The hash of the string is computed on-demand. Should the string hash
* to value 0, the value 0x8000 is used instead - this way the usual
* calculation (hash % tablesize) won't be affected.
*
* This string_t value is the one referenced by svalues and the like.
* It allows the following string types:
*
* Untabled, freely allocated, constant strings:
* .type is STRING_UNTABLED
*
* Untabled, freely allocated, mutable (constant-length) strings:
* .type is STRING_MUTABLE
*
* Tabled (shared) strings:
* .type is STRING_TABLED
* .next is the hash chain pointer, the reference from the
* table is not counted.
*
* TODO: Make functions mstr_add() resilient to receiving NULL
* TODO:: pointers as args. This way stuff like rc =
* TODO:: mstr_add(rc,...) will always work and we need to check
* TODO:: for rc != NULL only at the end.
* TODO: Distinguish between the allocated size of a string and the
* TODO:: used size. To use this efficiently, functions like mstr_insert()...
* TODO:: might become necessary.
*---------------------------------------------------------------------------
*/
#include "driver.h"
#include <assert.h>
#include <stdio.h>
#include "mstrings.h"
#include "gcollect.h"
#include "hash.h"
#include "main.h"
#include "simulate.h"
#include "stdstrings.h"
#include "strfuns.h"
#include "svalue.h"
#include "xalloc.h"
#include "../mudlib/sys/driver_info.h"
/*-------------------------------------------------------------------------*/
#if HTABLE_SIZE > MAX_HASH32
#error The hash table size must not be larger then MAX_HASH32.
The end.
#endif
static INLINE hash32_t
HashToIndex(hash32_t hash)
/* Adapt a hash value to our table size.
*/
{
#if !( (HTABLE_SIZE) & (HTABLE_SIZE)-1 )
// use faster masking in case of HTABLE_SIZE being a power of 2.
return hash & ((HTABLE_SIZE)-1);
#else
// fallback to slow modulo.
return hash % HTABLE_SIZE;
#endif
}
/*-------------------------------------------------------------------------*/
static string_t ** stringtable = NULL;
/* The hashed string table: an array of pointers to the heads of
* the string chains.
*/
string_t *empty_byte_string;
/* Empty byte sequence.
*/
/* Statistics */
mp_uint mstr_used = 0;
/* Number of virtually allocated strings - every reference counts
* as separate copy.
*/
mp_uint mstr_used_size = 0;
/* Total virtual size of allocated strings counted
* - every reference counts as separate copy.
* This does include the memory by the string management structures.
*/
static mp_uint mstr_tabled_count = 0;
/* Number of distinct strings in the string table.
*/
static mp_uint mstr_tabled_size = 0;
/* Total memory held in the string table.
*/
static mp_uint mstr_chains = 0;
/* Number of hash chains in the string table.
*/
static statcounter_t mstr_added = 0;
/* Number of distinct strings added to the string table.
*/
static statcounter_t mstr_deleted = 0;
/* Number of distinct strings deleted from the string table.
*/
static statcounter_t mstr_collisions = 0;
/* Number of collisions when adding a new distinct string.
*/
static mp_uint mstr_untabled_count = 0;
/* Number of distinct untabled strings.
*/
static mp_uint mstr_untabled_size = 0;
/* Total memory held in untabled strings.
*/
static mp_uint mstr_searchlen_byvalue = 0;
/* Number of search steps along hash chains with content comparisons.
*/
static statcounter_t mstr_searches_byvalue = 0;
/* Number of searches in the string table with content comparison.
*/
static statcounter_t mstr_found_byvalue = 0;
/* Number of successful searches in the string table with content comparison.
*/
static mp_uint mstr_searchlen = 0;
/* Number of search steps along hash chains without content comparisons.
*/
static statcounter_t mstr_searches = 0;
/* Number of searches in the string table without content comparisons.
*/
static statcounter_t mstr_found = 0;
/* Number of successful searches in the string table without content comparison.
*/
#ifdef EXT_STRING_STATS
statcounter_t stNumEqual = 0;
statcounter_t stNumHashEqual = 0;
statcounter_t stNumTabledEqual = 0;
statcounter_t stNumComp = 0;
statcounter_t stNumTabledComp = 0;
statcounter_t stNumTabledChecked = 0;
statcounter_t stNumTabledCheckedTable = 0;
statcounter_t stNumTabledCheckedSearch = 0;
#endif /* EXT_STRING_STATS */
/*-------------------------------------------------------------------------*/
static INLINE hash32_t
hash_string_inl (const char * const s, size_t size)
/* Compute the hash for string <s> of length <size> and return it.
* The result will always be non-zero.
*/
{
if (size > MSTRING_HASH_LENGTH)
size = MSTRING_HASH_LENGTH;
hash32_t hash = hashmem32(s, size);
if (!hash)
hash = 1 << (sizeof (hash) * CHAR_BIT - 1);
return hash;
} /* hash_string_inl() */
hash32_t
hash_string (const char * const s, size_t size)
{
return hash_string_inl(s, size);
}
hash32_t
hash_string_chained (const char * const s, size_t size, hash32_t chainhash)
/* Compute the hash for string <s> of length <size> and use <chainhash> as
* initial value. Used to hash several strings into one hash value.
* The result will always be non-zero.
*/
{
if (size > MSTRING_HASH_LENGTH)
size = MSTRING_HASH_LENGTH;
hash32_t hash = hashmem32_chained(s, size, chainhash);
if (!hash)
hash = 1 << (sizeof (hash) * CHAR_BIT - 1);
return hash;
} // hash_string_chained
/*-------------------------------------------------------------------------*/
static INLINE hash32_t
get_hash (string_t * pStr)
/* Return the hash of string <pStr>, computing it if necessary.
*/
{
/* We store no hashes for mutable strings. */
if (pStr->info.type == STRING_MUTABLE)
return hash_string_inl(pStr->txt, pStr->size);
if (!pStr->u.tabled.hash)
pStr->u.tabled.hash = hash_string_inl(pStr->txt, pStr->size);
return pStr->u.tabled.hash;
} /* get_hash() */
/*-------------------------------------------------------------------------*/
hash32_t
mstring_get_hash (string_t * pStr)
/* Aliased to: mstr_get_hash()
*
* Return the hash value of <pStr>, computing it if necessary.
*/
{
return get_hash(pStr);
} /* mstring_get_hash() */
/*-------------------------------------------------------------------------*/
static INLINE string_t *
find_and_move (const char * const s, size_t size, bool is_byte, hash32_t hash)
/* If <s> is a tabled string of length <size> and <hash> in the related
* stringtable chain: find it, move it to the head of the chain and return its
* string_t*.
*
* If <s> is not tabled, return NULL.
*/
{
string_t *prev, *rover;
hash32_t idx = HashToIndex(hash);
mstr_searches_byvalue++;
/* Find the string in the table */
mstr_searchlen_byvalue++;
for ( prev = NULL, rover = stringtable[idx]
; rover != NULL
&& get_txt(rover) != s
&& !( size == mstrsize(rover)
&& hash == get_hash(rover)
&& is_byte == (rover->info.unicode == STRING_BYTES)
&& 0 == memcmp(get_txt(rover), s, size)
)
; prev = rover, rover = rover->u.tabled.next
)
mstr_searchlen_byvalue++;
/* If the string is in the table (rover != NULL), but not at the beginning
* of the chain, move it there.
*/
if (rover && prev)
{
prev->u.tabled.next = rover->u.tabled.next;
rover->u.tabled.next = stringtable[idx];
stringtable[idx] = rover;
}
if (rover)
mstr_found_byvalue++;
return rover;
} /* find_and_move() */
/*-------------------------------------------------------------------------*/
static INLINE string_t *
move_to_head (string_t *s, int idx)
/* If <s> is a tabled string in the stringtable[<index>] chain: move it to
* the head of the chain and return its pointer.
* If <s> is not found in that chain, return NULL.
*/
{
string_t *prev, *rover;
mstr_searches++;
/* Find the string in the table */
mstr_searchlen++;
for ( prev = NULL, rover = stringtable[idx]
; rover != NULL && rover != s
; prev = rover, rover = rover->u.tabled.next
)
{
mstr_searchlen++;
}
/* If s is found (rover != NULL), but not at the beginning of the chain,
* move it there
*/
if (rover && prev)
{
prev->u.tabled.next = rover->u.tabled.next;
rover->u.tabled.next = stringtable[idx];
stringtable[idx] = rover;
}
if (rover)
mstr_found++;
return rover;
} /* move_to_head() */
/*-------------------------------------------------------------------------*/
static INLINE string_t *
make_new_tabled (const char * const pTxt, size_t size, enum unicode_type unicode, hash32_t hash MTRACE_DECL)
/* Helper function for mstring_new_tabled() and mstring_new_n_tabled().
*
* Create a new tabled string by copying the data string <pTxt> of length
* <size> and <hash> and return it counting the result as one reference. The
* string MUST NOT yet exist in the table.
*
* If memory runs out, NULL is returned.
*/
{
string_t * string;
hash32_t idx = HashToIndex(hash);
/* Get the memory for a new one */
string = xalloc_pass(size + 1 + sizeof(*string));
/* We add one null byte. */
if (!string)
return NULL;
/* Set up the structures and table the string */
string->size = size;
string->u.tabled.hash = hash;
memcpy(string->txt, pTxt, size);
string->txt[size] = '\0';
string->info.type = STRING_TABLED;
string->info.unicode = unicode;
string->info.ref = 1;
/* An uninitialized memory read at this point is ok: it's because
* the bitfield is initialized in parts.
*/
mstr_added++;
if (NULL == stringtable[idx])
mstr_chains++;
else
mstr_collisions++;
string->u.tabled.next = stringtable[idx];
stringtable[idx] = string;
{
size_t msize;
msize = mstr_mem_size(string);
mstr_used++;
mstr_used_size += msize;
mstr_tabled_count++;
mstr_tabled_size += msize;
}
return string;
} /* make_new_tabled() */
/*-------------------------------------------------------------------------*/
string_t *
mstring_alloc_string (size_t iSize MTRACE_DECL)
/* Aliased to: alloc_mstring(iSize)
* Also called by mstring_new_string().
*
* Create a new untabled string with space for <iSize> characters and
* return it, counting the result as one reference.
*
* If memory runs out, NULL is returned.
*/
{
string_t *string;
/* Get the memory */
string = xalloc_pass(iSize + 1 + sizeof(*string));
if (!string)
return NULL;
/* Set up the structures */
string->size = iSize;
string->u.tabled.next = NULL;
string->u.tabled.hash = 0;
string->txt[iSize] = '\0';
string->info.type = STRING_UNTABLED;
string->info.unicode = STRING_ASCII;
string->info.ref = 1;
/* An uninitialized memory read at this point is ok: it's because
* the bitfield is initialized in parts.
*/
{
size_t msize;
msize = mstr_mem_size(string);
mstr_used++;
mstr_used_size += msize;
mstr_untabled_count++;
mstr_untabled_size += msize;
}
return string;
} /* mstring_alloc_string() */
/*-------------------------------------------------------------------------*/
string_t *
mstring_new_string (const char * const pTxt, enum unicode_type unicode MTRACE_DECL)
/* Aliased to: new_mstring(pTxt, unicode)
*
* Create a new untabled string by copying the C string <pTxt> and
* return it, counting the result as one reference.
*
* If memory runs out, NULL is returned.
*/
{
string_t *string;
size_t size;
size = strlen(pTxt);
string = mstring_alloc_string(size MTRACE_PASS);
if (string)
{
string->info.unicode = unicode;
if (size)
memcpy(string->txt, pTxt, size);
}
return string;
} /* mstring_new_string() */
/*-------------------------------------------------------------------------*/
string_t *
mstring_new_n_string (const char * const pTxt, size_t len, enum unicode_type unicode MTRACE_DECL)
/* Aliased to: new_n_mstring(pTxt, len, unicode)
*
* Create a new untabled string by copying the <len> characters at <pTxt>
* and return it, counting the result as one reference.
*
* If memory runs out, NULL is returned.
*/
{
string_t *string;
string = mstring_alloc_string(len MTRACE_PASS);
if (string)
{
string->info.unicode = unicode;
if (len)
memcpy(string->txt, pTxt, len);
}
return string;
} /* mstring_new_n_string() */
/*-------------------------------------------------------------------------*/
string_t *
mstring_new_tabled (const char * const pTxt, enum unicode_type unicode MTRACE_DECL)
/* Aliased to: new_tabled(pTxt, unicode)
*
* Create a new tabled string by copying the C string <pTxt> and
* return it counting the result as one reference. If a tabled string
* for the same <pTxt> already exists, a reference to that one is returned.
*
* If memory runs out, NULL is returned.
*/
{
hash32_t hash;
size_t size;
string_t * string;
size = strlen(pTxt);
hash = hash_string_inl(pTxt, size);
/* Check if the string has already been tabled */
string = find_and_move(pTxt, size, unicode == STRING_BYTES, hash);
if (string)
{
return ref_mstring(string);
}
/* No: create a new one */
return make_new_tabled(pTxt, size, unicode, hash MTRACE_PASS);
} /* mstring_new_tabled() */
/*-------------------------------------------------------------------------*/
string_t *
mstring_new_n_tabled (const char * const pTxt, size_t size, enum unicode_type unicode MTRACE_DECL)
/* Aliased to: new_n_tabled(pTxt, len, unicode)
*
* Create a new tabled string by copying the C string <pTxt> of length <size>
* and return it counting the result as one reference. If a tabled string
* for the same <pTxt> already exists, a reference to that one is returned.
*
* If memory runs out, NULL is returned.
*/
{
hash32_t hash;
string_t * string;
hash = hash_string_inl(pTxt, size);
/* Check if the string has already been tabled */
string = find_and_move(pTxt, size, unicode == STRING_BYTES, hash);
if (string)
{
return ref_mstring(string);
}
/* No: create a new one */
return make_new_tabled(pTxt, size, unicode, hash MTRACE_PASS);
} /* mstring_new_n_tabled() */
/*-------------------------------------------------------------------------*/
string_t *
mstring_new_unicode_string (const char * const pTxt MTRACE_DECL)
/* Aliased to: new_unicode_mstring(pTxt)
*
* Create a new untabled unicode string by copying the C string <pTxt>
* and return it, counting the result as one reference.
*
* If memory runs out, NULL is returned.
*/
{
size_t len = strlen(pTxt);
return mstring_new_n_string(pTxt, len, is_ascii(pTxt, len) ? STRING_ASCII : STRING_UTF8 MTRACE_PASS);
} /* mstring_new_unicode_string() */
/*-------------------------------------------------------------------------*/
string_t *
mstring_new_n_unicode_string (const char * const pTxt, size_t len MTRACE_DECL)
/* Aliased to: new_n_unicode_mstring(pTxt, len)
*
* Create a new untabled unicode string by copying the <len> characters
* at <pTxt> and return it, counting the result as one reference.
*
* If memory runs out, NULL is returned.
*/
{
return mstring_new_n_string(pTxt, len, is_ascii(pTxt, len) ? STRING_ASCII : STRING_UTF8 MTRACE_PASS);
} /* mstring_new_n_unicode_string() */
/*-------------------------------------------------------------------------*/
string_t *
mstring_new_unicode_tabled (const char * const pTxt MTRACE_DECL)
/* Aliased to: new_unicode_mstring(pTxt)
*
* Create a new tabled unicode string by copying the C string <pTxt> and
* return it counting the result as one reference. If a tabled string
* for the same <pTxt> already exists, a reference to that one is returned.
*
* If memory runs out, NULL is returned.
*/
{
size_t len = strlen(pTxt);
return mstring_new_n_tabled(pTxt, len, is_ascii(pTxt, len) ? STRING_ASCII : STRING_UTF8 MTRACE_PASS);
} /* mstring_new_unicode_tabled() */
/*-------------------------------------------------------------------------*/
string_t *
mstring_new_n_unicode_tabled (const char * const pTxt, size_t len MTRACE_DECL)
/* Aliased to: new_unicode_mstring(pTxt)
*
* Create a new tabled unicode string by copying the C string <pTxt> of
* length <size> and return it counting the result as one reference.
* If a tabled string for the same <pTxt> already exists, a reference
* to that one is returned.
*
* If memory runs out, NULL is returned.
*/
{
return mstring_new_n_tabled(pTxt, len, is_ascii(pTxt, len) ? STRING_ASCII : STRING_UTF8 MTRACE_PASS);
} /* mstring_new_n_unicode_tabled() */
/*-------------------------------------------------------------------------*/
static string_t *
table_string (string_t * pStr MTRACE_DECL)
/* Called by: mstring_make_tabled()
*
* Table the string <pStr> and return a pointer to the tabled string.
* If <pStr> is already tabled, it will also be the result.
* If <pStr> is not tabled, but a string of this content already exist,
* the reference to the tabled string will be the result.
* Otherwise, <pStr> is added to the table and returned.
*
* Return NULL when out of memory.
*/
{
string_t *string;
hash32_t hash;
hash32_t idx;
size_t size;
size_t msize;
/* If the string is already tabled, our work is done */
if (pStr->info.type == STRING_TABLED)
return pStr;
msize = mstr_mem_size(pStr);
/* Get or create the tabled string for this untabled one */
size = pStr->size;
hash = get_hash(pStr);
idx = HashToIndex(hash);
/* Check if the string has already been tabled */
string = find_and_move(pStr->txt, size, pStr->info.unicode == STRING_BYTES, hash);
if (!string)
{
/* No: add the string into the table.
*/
string = mstring_make_constant(pStr, false MTRACE_PASS);
if (!string)
return string;
string->info.type = STRING_TABLED;
mstr_added++;
if (NULL == stringtable[idx])
mstr_chains++;
else
mstr_collisions++;
string->u.tabled.next = stringtable[idx];
stringtable[idx] = pStr;
mstr_tabled_count++;
mstr_tabled_size += msize;
mstr_untabled_count--;
mstr_untabled_size -= msize;
}
/* That's all */
return string;
} /* table_string() */
/*-------------------------------------------------------------------------*/
string_t *
mstring_make_tabled (string_t * pStr, Bool deref_arg MTRACE_DECL)
/* Aliased to: make_tabled(pStr) : deref_arg = MY_TRUE
* make_tabled_from(pStr) : deref_arg = MY_FALSE
*
* Take the string <pStr> and convert it into an tabled string if not already
* tabled.
* Return the counted reference to the tabled instance, and, if <deref_arg> is
* TRUE, dereference the <pStr> once.
*
* Return NULL when out of memory.
*/
{
string_t *string;
/* Table the string one way or the other (always succeeds) */
string = table_string(pStr MTRACE_PASS);
if (!string)
return NULL;
(void)ref_mstring(string);
if (deref_arg)
free_mstring(pStr);
return string;
} /* mstring_make_tabled() */
/*-------------------------------------------------------------------------*/
string_t *
mstring_make_constant (string_t * pStr, bool in_place_only MTRACE_DECL)
/* Aliased to: make_constant(pStr) : in_place_only = false
* try_make_constant(pStr) : in_place_only = true
*
* Take the string <pStr> and convert it into a constant string if it is
* a mutable string. This is done in-place if there is only one reference
* left. Therefore the caller must make sure, that this reference comes
* from an rvalue, not an lvalue.
*
* If the conversion can't be done in-place, do it by copying. Then the
* new string is returned and <pStr> is dereference once.
* If <in_place_only> is true, then no copying is done. Instead NULL is
* returned.
*
* Return NULL when out of memory.
*/
{
string_t *string;
switch (pStr->info.type)
{
case STRING_UNTABLED:
case STRING_TABLED:
return pStr;
case STRING_MUTABLE:
/* If there is only one reference, we can convert it in-place. */
if (pStr->info.ref == 1)
{
pStr->info.type = STRING_UNTABLED;
pStr->u.tabled.hash = 0;
pStr->u.tabled.next = NULL;
return pStr;
}
break;
}
if (in_place_only)
return NULL;
/* Table the string one way or the other (always succeeds) */
string = mstring_dup(pStr MTRACE_PASS);
if (!string)
return NULL;
free_mstring(pStr);
return string;
} /* mstring_make_constant() */
/*-------------------------------------------------------------------------*/
string_t *
mstring_make_mutable (string_t * pStr MTRACE_DECL)
/* Aliased to: make_mutable(pStr)
*
* Take the string <pStr> and convert it into a mutable string if it is
* not already one of the type. The conversion is usually done by copying
* if there is more than one reference to the string (or if it is tabled).
*
* Return the counted reference to the tabled instance, and dereference
* the <pStr> once.
*
* Return NULL when out of memory.
*/
{
string_t *string;
switch (pStr->info.type)
{
case STRING_UNTABLED:
/* If there is only one reference, we can convert it in-place. */
if (pStr->info.ref == 1)
{
pStr->info.type = STRING_MUTABLE;
pStr->u.mutable.char_lvalues = NULL;
pStr->u.mutable.range_lvalues = NULL;
return pStr;
}
break;
case STRING_TABLED:
/* For tabled strings we always have to make a copy. */
break;
case STRING_MUTABLE:
return pStr;
}
/* Table the string one way or the other (always succeeds) */
string = mstring_dup(pStr MTRACE_PASS);
if (!string)
return NULL;
string->info.type = STRING_MUTABLE;
string->u.mutable.char_lvalues = NULL;
string->u.mutable.range_lvalues = NULL;
free_mstring(pStr);
return string;
} /* mstring_make_mutable() */
/*-------------------------------------------------------------------------*/
string_t *
mstring_dup (string_t * pStr MTRACE_DECL)
/* Aliased to: dup_mstring(pStr)
*
* Create and return a new untabled string with the same text as <pStr> but
* just one reference.
* If memory runs out, NULL is returned.
*
* Purpose is to create an instance of a string which an be freely modified
* (which is why .hash is cleared).
*
* See also: mstring_unshare().
*/
{
string_t *string;
/* Create a new untabled string from the tabled one */
string = mstring_alloc_string(pStr->size MTRACE_PASS);
if (string)
{
memcpy(string->txt, pStr->txt, pStr->size);
string->info.unicode = pStr->info.unicode;
}
return string;
} /* mstring_dup() */
/*-------------------------------------------------------------------------*/
string_t *
mstring_unshare (string_t * pStr MTRACE_DECL)
/* Aliased to: unshare_mstring(pStr)
*
* Like mstring_dup(), this function creates and returns an untabled string
* with the same text as <pStr>, and with just one reference. In contrast
* to mstring_dup(), this function also dereferences <pStr> on success (which
* allows it to optimize certain cases).
* If memory runs out, NULL is returned.
*
* Purpose is to create an instance of a string which an be freely modified
* (which is why .hash is cleared).
*/
{
string_t *string;
/* Check for the easy cases where the argument string can be
* the result: untabled and just one reference.
*/
if (pStr->info.type != STRING_TABLED && pStr->info.ref == 1)
{
pStr->info.type = STRING_UNTABLED;
pStr->u.tabled.hash = 0;
pStr->u.tabled.next = NULL;
return pStr;
}
/* Otherwise create a new untabled string from the tabled one */
string = mstring_alloc_string(pStr->size MTRACE_PASS);
if (string)
{
memcpy(string->txt, pStr->txt, pStr->size);
string->info.unicode = pStr->info.unicode;
free_mstring(pStr);
}
return string;
} /* mstring_unshare() */
/*-------------------------------------------------------------------------*/
string_t *
mstring_resize (string_t * pStr, size_t newlen MTRACE_DECL)
/* Aliased to: resize_mstring(pStr,newlen)
*
* Create an untabled copy of <pStr> with just one reference and space
* for <newlen> bytes, remove one reference from <pStr>, and then return
* the new string.
* If memory runs out, NULL is returned, but the original string is still
* dereferenced.
*/
{
string_t *string;
/* Check for the easy case */
if (pStr->info.type != STRING_TABLED && pStr->info.ref == 1
&& pStr->size == newlen)
{
pStr->info.type = STRING_UNTABLED;
pStr->u.tabled.hash = 0;
pStr->u.tabled.next = NULL;
return pStr;
}
/* Otherwise create a new untabled string from the tabled one */
string = mstring_alloc_string(newlen MTRACE_PASS);
if (string)
{
if (newlen > pStr->size)
memcpy(string->txt, pStr->txt, pStr->size);
else
memcpy(string->txt, pStr->txt, newlen);
string->info.unicode = pStr->info.unicode;
}
free_mstring(pStr);
return string;
} /* mstring_resize() */
/*-------------------------------------------------------------------------*/
string_t *
mstring_find_tabled (string_t * pStr)
/* Aliased to: find_tabled(pStr)
*
* Find the tabled string with the same content as <pStr> and return it.
* If <pStr> is a tabled string, it will be the result itself.
* If there is no such tabled string, NULL is returned.
*
* The function does not change refcounts.
*/
{
hash32_t hash;
size_t size;
#ifdef EXT_STRING_STATS
stNumTabledChecked++;
#endif /* EXT_STRING_STATS */
/* If pStr is tabled, our work is done */
if (pStr->info.type == STRING_TABLED)
{
#ifdef EXT_STRING_STATS
stNumTabledCheckedTable++;