melder_strings.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /* melder_strings.cpp
  2. *
  3. * Copyright (C) 2006-2012,2014-2018 Paul Boersma
  4. *
  5. * This code is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * This code is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. * See the GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this work. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "melder.h"
  19. #include "../kar/UnicodeData.h"
  20. #define FREE_THRESHOLD_BYTES 10000LL
  21. static int64 totalNumberOfAllocations = 0, totalNumberOfDeallocations = 0, totalAllocationSize = 0, totalDeallocationSize = 0;
  22. void MelderString16_free (MelderString16 *me) {
  23. if (! my string) return;
  24. Melder_free (my string);
  25. if (Melder_debug == 34)
  26. Melder_casual (U"from MelderString16_free\t", Melder_pointer (my string), U"\t", my bufferSize, U"\t", sizeof (char16));
  27. totalNumberOfDeallocations += 1;
  28. totalDeallocationSize += my bufferSize * (int64) sizeof (char16_t);
  29. my bufferSize = 0;
  30. my length = 0;
  31. }
  32. void MelderString_free (MelderString *me) {
  33. if (! my string) return;
  34. Melder_free (my string);
  35. if (Melder_debug == 34)
  36. Melder_casual (U"from MelderString32_free\t", Melder_pointer (my string), U"\t", my bufferSize, U"\t", sizeof (char32));
  37. totalNumberOfDeallocations += 1;
  38. totalDeallocationSize += my bufferSize * (int64) sizeof (char32);
  39. my bufferSize = 0;
  40. my length = 0;
  41. }
  42. void MelderString_expand (MelderString *me, int64 sizeNeeded) {
  43. Melder_assert (my bufferSize >= 0);
  44. Melder_assert (sizeNeeded >= 0);
  45. sizeNeeded = (int64) (1.618034 * sizeNeeded) + 100;
  46. Melder_assert (sizeNeeded > 0);
  47. if (my string) {
  48. totalNumberOfDeallocations += 1;
  49. totalDeallocationSize += my bufferSize * (int64) sizeof (char32);
  50. }
  51. int64 bytesNeeded = sizeNeeded * (int64) sizeof (char32);
  52. Melder_assert (bytesNeeded > 0);
  53. try {
  54. if (Melder_debug == 34)
  55. Melder_casual (U"from MelderString:expandIfNecessary\t", Melder_pointer (my string), U"\t", sizeNeeded, U"\t", sizeof (char32));
  56. my string = (char32 *) Melder_realloc (my string, bytesNeeded);
  57. } catch (MelderError) {
  58. my bufferSize = 0;
  59. my length = 0;
  60. throw;
  61. }
  62. totalNumberOfAllocations += 1;
  63. totalAllocationSize += bytesNeeded;
  64. my bufferSize = sizeNeeded;
  65. }
  66. #define expandIfNecessary(type) \
  67. if (sizeNeeded > my bufferSize) { \
  68. Melder_assert (my bufferSize >= 0); \
  69. Melder_assert (sizeNeeded >= 0); \
  70. sizeNeeded = (int64) (1.618034 * sizeNeeded) + 100; \
  71. Melder_assert (sizeNeeded > 0); \
  72. if (my string) { \
  73. totalNumberOfDeallocations += 1; \
  74. totalDeallocationSize += my bufferSize * (int64) sizeof (type); \
  75. } \
  76. int64 bytesNeeded = sizeNeeded * (int64) sizeof (type); \
  77. Melder_assert (bytesNeeded > 0); \
  78. try { \
  79. if (Melder_debug == 34) \
  80. Melder_casual (U"from MelderString:expandIfNecessary\t", Melder_pointer (my string), U"\t", sizeNeeded, U"\t", sizeof (type)); \
  81. my string = (type *) Melder_realloc (my string, bytesNeeded); \
  82. } catch (MelderError) { \
  83. my bufferSize = 0; \
  84. my length = 0; \
  85. throw; \
  86. } \
  87. totalNumberOfAllocations += 1; \
  88. totalAllocationSize += bytesNeeded; \
  89. my bufferSize = sizeNeeded; \
  90. }
  91. void MelderString16_empty (MelderString16 *me) {
  92. if (my bufferSize * (int64) sizeof (char16) >= FREE_THRESHOLD_BYTES) {
  93. MelderString16_free (me);
  94. }
  95. int64 sizeNeeded = 1;
  96. expandIfNecessary (char16)
  97. my string [0] = '\0';
  98. my length = 0;
  99. }
  100. void MelderString_empty (MelderString *me) {
  101. if (my bufferSize * (int64) sizeof (char32) >= FREE_THRESHOLD_BYTES) {
  102. MelderString_free (me);
  103. }
  104. int64 sizeNeeded = 1;
  105. if (sizeNeeded > my bufferSize) MelderString_expand (me, sizeNeeded);
  106. my string [0] = '\0';
  107. my length = 0;
  108. }
  109. void MelderString_ncopy (MelderString *me, conststring32 source, int64 n) {
  110. if (my bufferSize * (int64) sizeof (char32) >= FREE_THRESHOLD_BYTES)
  111. MelderString_free (me);
  112. if (! source) source = U"";
  113. int64 length = str32len (source);
  114. if (length > n) length = n;
  115. int64 sizeNeeded = length + 1;
  116. if (sizeNeeded > my bufferSize) MelderString_expand (me, sizeNeeded);
  117. str32ncpy (my string, source, length);
  118. my string [length] = '\0';
  119. my length = length;
  120. }
  121. void MelderString16_appendCharacter (MelderString16 *me, char32 kar) {
  122. int64 sizeNeeded = my length + 3; // make room for character, potential surrogate character, and null character
  123. expandIfNecessary (char16)
  124. if (kar <= 0x00'FFFF) {
  125. my string [my length] = (char16) kar; // guarded cast
  126. my length ++;
  127. } else if (kar <= 0x10'FFFF) {
  128. kar -= 0x01'0000;
  129. my string [my length] = (char16) (0x00'D800 | (kar >> 10));
  130. my length ++;
  131. my string [my length] = (char16) (0x00'DC00 | (kar & 0x00'03FF));
  132. my length ++;
  133. } else {
  134. my string [my length] = UNICODE_REPLACEMENT_CHARACTER;
  135. my length ++;
  136. }
  137. my string [my length] = '\0';
  138. }
  139. void MelderString_appendCharacter (MelderString *me, char32 character) {
  140. int64 sizeNeeded = my length + 2; // make room for character and null character
  141. if (sizeNeeded > my bufferSize) MelderString_expand (me, sizeNeeded);
  142. my string [my length] = character;
  143. my length ++;
  144. my string [my length] = U'\0';
  145. }
  146. void MelderString_get (MelderString *me, char32 *destination) {
  147. if (my string) {
  148. str32cpy (destination, my string);
  149. } else {
  150. destination [0] = U'\0';
  151. }
  152. }
  153. int64 MelderString_allocationCount () {
  154. return totalNumberOfAllocations;
  155. }
  156. int64 MelderString_deallocationCount () {
  157. return totalNumberOfDeallocations;
  158. }
  159. int64 MelderString_allocationSize () {
  160. return totalAllocationSize;
  161. }
  162. int64 MelderString_deallocationSize () {
  163. return totalDeallocationSize;
  164. }
  165. MelderString MelderCat::_buffers [MelderCat::_k_NUMBER_OF_BUFFERS] { };
  166. int MelderCat::_bufferNumber = 0;
  167. /* End of file melder_strings.cpp */