wwstring.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /*
  2. ** Command & Conquer Generals(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program 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 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** 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 program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /***********************************************************************************************
  19. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : WWSaveLoad *
  23. * *
  24. * $Archive:: /Commando/Code/wwlib/wwstring.cpp $*
  25. * *
  26. * Author:: Patrick Smith *
  27. * *
  28. * $Modtime:: 8/27/01 1:42p $*
  29. * *
  30. * $Revision:: 10 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #include "wwstring.h"
  36. #include "win.h"
  37. #include "wwmemlog.h"
  38. #include "mutex.h"
  39. #include <stdio.h>
  40. ///////////////////////////////////////////////////////////////////
  41. // Static member initialzation
  42. ///////////////////////////////////////////////////////////////////
  43. CriticalSectionClass StringClass::m_Mutex;
  44. TCHAR StringClass::m_NullChar = 0;
  45. TCHAR * StringClass::m_EmptyString = &m_NullChar;
  46. //
  47. // A trick to optimize strings that are allocated from the stack and used only temporarily
  48. //
  49. // For alignment reasons we need twice as large block...
  50. char StringClass::m_TempStrings[(StringClass::MAX_TEMP_STRING*2)*StringClass::MAX_TEMP_BYTES];
  51. unsigned StringClass::ReservedMask=0;
  52. ///////////////////////////////////////////////////////////////////
  53. //
  54. // Get_String
  55. //
  56. ///////////////////////////////////////////////////////////////////
  57. void
  58. StringClass::Get_String (int length, bool is_temp)
  59. {
  60. WWMEMLOG(MEM_STRINGS);
  61. if (!is_temp && length == 0) {
  62. m_Buffer = m_EmptyString;
  63. return;
  64. }
  65. TCHAR *string = NULL;
  66. //
  67. // Should we attempt to use a temp buffer for this string?
  68. //
  69. if (is_temp && length <= MAX_TEMP_LEN && ReservedMask!=ALL_TEMP_STRINGS_USED_MASK) {
  70. //
  71. // Make sure no one else is requesting a temp pointer
  72. // at the same time we are. There is a slight possibility that another
  73. // thread stole the last available buffer in between the if sentence and
  74. // the mutex lock, but that is a feature by design and doesn't cause
  75. // anything bad to happen.
  76. //
  77. CriticalSectionClass::LockClass m(m_Mutex);
  78. //
  79. // Try to find an available temporary buffer
  80. //
  81. // TODO: Don't loop, there are better ways
  82. unsigned mask=1;
  83. for (int index = 0; index < MAX_TEMP_STRING; index ++, mask<<=1) {
  84. unsigned mask=1<<index;
  85. if (!(ReservedMask&mask)) {
  86. ReservedMask|=mask;
  87. //
  88. // Grab this unused buffer for our string
  89. //
  90. unsigned temp_string=reinterpret_cast<unsigned>(m_TempStrings);
  91. temp_string+=MAX_TEMP_BYTES*MAX_TEMP_STRING;
  92. temp_string&=~(MAX_TEMP_BYTES*MAX_TEMP_STRING-1);
  93. temp_string+=index*MAX_TEMP_BYTES;
  94. temp_string+=sizeof(_HEADER); // The buffer contains header as well, and it needs to be at the start
  95. string=reinterpret_cast<char*>(temp_string);
  96. Set_Buffer_And_Allocated_Length (string, MAX_TEMP_LEN);
  97. break;
  98. }
  99. }
  100. }
  101. if (string == NULL) {
  102. Set_Buffer_And_Allocated_Length (Allocate_Buffer (length), length);
  103. }
  104. }
  105. ///////////////////////////////////////////////////////////////////
  106. //
  107. // Resize
  108. //
  109. ///////////////////////////////////////////////////////////////////
  110. void
  111. StringClass::Resize (int new_len)
  112. {
  113. WWMEMLOG(MEM_STRINGS);
  114. int allocated_len = Get_Allocated_Length ();
  115. if (new_len > allocated_len) {
  116. //
  117. // Allocate the new buffer and copy the contents of our current
  118. // string.
  119. //
  120. TCHAR *new_buffer = Allocate_Buffer (new_len);
  121. _tcscpy (new_buffer, m_Buffer);
  122. //
  123. // Switch to the new buffer
  124. //
  125. Set_Buffer_And_Allocated_Length (new_buffer, new_len);
  126. }
  127. return ;
  128. }
  129. ///////////////////////////////////////////////////////////////////
  130. //
  131. // Uninitialised_Grow
  132. //
  133. ///////////////////////////////////////////////////////////////////
  134. void
  135. StringClass::Uninitialised_Grow (int new_len)
  136. {
  137. WWMEMLOG(MEM_STRINGS);
  138. int allocated_len = Get_Allocated_Length ();
  139. if (new_len > allocated_len) {
  140. //
  141. // Switch to a newly allocated buffer
  142. //
  143. TCHAR *new_buffer = Allocate_Buffer (new_len);
  144. Set_Buffer_And_Allocated_Length (new_buffer, new_len);
  145. }
  146. return ;
  147. }
  148. ///////////////////////////////////////////////////////////////////
  149. //
  150. // Uninitialised_Grow
  151. //
  152. ///////////////////////////////////////////////////////////////////
  153. void
  154. StringClass::Free_String (void)
  155. {
  156. if (m_Buffer != m_EmptyString) {
  157. unsigned buffer_base=reinterpret_cast<unsigned>(m_Buffer-sizeof (StringClass::_HEADER));
  158. unsigned temp_base=reinterpret_cast<unsigned>(m_TempStrings+MAX_TEMP_BYTES*MAX_TEMP_STRING);
  159. if ((buffer_base>>11)==(temp_base>>11)) {
  160. //
  161. // Make sure no one else is changing the reserved mask
  162. // at the same time we are.
  163. //
  164. CriticalSectionClass::LockClass m(m_Mutex);
  165. unsigned index=(buffer_base/MAX_TEMP_BYTES)&(MAX_TEMP_STRING-1);
  166. unsigned mask=1<<index;
  167. ReservedMask&=~mask;
  168. }
  169. else {
  170. //
  171. // String wasn't temporary, so free the memory
  172. //
  173. char *buffer = ((char *)m_Buffer) - sizeof (StringClass::_HEADER);
  174. delete [] buffer;
  175. }
  176. //
  177. // Reset the buffer
  178. //
  179. m_Buffer = m_EmptyString;
  180. }
  181. return ;
  182. }
  183. ///////////////////////////////////////////////////////////////////
  184. //
  185. // Format
  186. //
  187. ///////////////////////////////////////////////////////////////////
  188. int _cdecl
  189. StringClass::Format_Args (const TCHAR *format, const va_list & arg_list )
  190. {
  191. //
  192. // Make a guess at the maximum length of the resulting string
  193. //
  194. TCHAR temp_buffer[512] = { 0 };
  195. int retval = 0;
  196. //
  197. // Format the string
  198. //
  199. #ifdef _UNICODE
  200. retval = _vsnwprintf (temp_buffer, 512, format, arg_list);
  201. #else
  202. retval = _vsnprintf (temp_buffer, 512, format, arg_list);
  203. #endif
  204. //
  205. // Copy the string into our buffer
  206. //
  207. (*this) = temp_buffer;
  208. return retval;
  209. }
  210. ///////////////////////////////////////////////////////////////////
  211. //
  212. // Format
  213. //
  214. ///////////////////////////////////////////////////////////////////
  215. int _cdecl
  216. StringClass::Format (const TCHAR *format, ...)
  217. {
  218. va_list arg_list;
  219. va_start (arg_list, format);
  220. //
  221. // Make a guess at the maximum length of the resulting string
  222. //
  223. TCHAR temp_buffer[512] = { 0 };
  224. int retval = 0;
  225. //
  226. // Format the string
  227. //
  228. #ifdef _UNICODE
  229. retval = _vsnwprintf (temp_buffer, 512, format, arg_list);
  230. #else
  231. retval = _vsnprintf (temp_buffer, 512, format, arg_list);
  232. #endif
  233. //
  234. // Copy the string into our buffer
  235. //
  236. (*this) = temp_buffer;
  237. va_end (arg_list);
  238. return retval;
  239. }
  240. ///////////////////////////////////////////////////////////////////
  241. //
  242. // Release_Resources
  243. //
  244. ///////////////////////////////////////////////////////////////////
  245. void
  246. StringClass::Release_Resources (void)
  247. {
  248. Free_String();
  249. }