Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

91 lines
2.3 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #ifndef STRINGPOOL_H
  8. #define STRINGPOOL_H
  9. #if defined( _WIN32 )
  10. #pragma once
  11. #endif
  12. #include "utlrbtree.h"
  13. #include "utlvector.h"
  14. //-----------------------------------------------------------------------------
  15. // Purpose: Allocates memory for strings, checking for duplicates first,
  16. // reusing exising strings if duplicate found.
  17. //-----------------------------------------------------------------------------
  18. class CStringPool
  19. {
  20. public:
  21. CStringPool();
  22. ~CStringPool();
  23. unsigned int Count() const;
  24. const char * Allocate( const char *pszValue );
  25. void FreeAll();
  26. // searches for a string already in the pool
  27. const char * Find( const char *pszValue );
  28. protected:
  29. typedef CUtlRBTree<const char *, unsigned short> CStrSet;
  30. CStrSet m_Strings;
  31. };
  32. //-----------------------------------------------------------------------------
  33. // Purpose: A reference counted string pool.
  34. //
  35. // Elements are stored more efficiently than in the conventional string pool,
  36. // quicker to look up, and storage is tracked via reference counts.
  37. //
  38. // At some point this should replace CStringPool
  39. //-----------------------------------------------------------------------------
  40. class CCountedStringPool
  41. {
  42. public: // HACK, hash_item_t structure should not be public.
  43. struct hash_item_t
  44. {
  45. char* pString;
  46. unsigned short nNextElement;
  47. unsigned char nReferenceCount;
  48. unsigned char pad;
  49. };
  50. enum
  51. {
  52. INVALID_ELEMENT = 0,
  53. MAX_REFERENCE = 0xFF,
  54. HASH_TABLE_SIZE = 1024
  55. };
  56. CUtlVector<unsigned short> m_HashTable; // Points to each element
  57. CUtlVector<hash_item_t> m_Elements;
  58. unsigned short m_FreeListStart;
  59. public:
  60. CCountedStringPool();
  61. virtual ~CCountedStringPool();
  62. void FreeAll();
  63. char *FindString( const char* pIntrinsic );
  64. char *ReferenceString( const char* pIntrinsic );
  65. void DereferenceString( const char* pIntrinsic );
  66. // These are only reliable if there are less than 64k strings in your string pool
  67. unsigned short FindStringHandle( const char* pIntrinsic );
  68. unsigned short ReferenceStringHandle( const char* pIntrinsic );
  69. char *HandleToString( unsigned short handle );
  70. void SpewStrings();
  71. };
  72. #endif // STRINGPOOL_H