ctstring.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /* Copyright 2019 The TensorFlow Authors. All Rights Reserved.
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.
  11. ==============================================================================*/
  12. #ifndef TENSORFLOW_CORE_PLATFORM_CTSTRING_H_
  13. #define TENSORFLOW_CORE_PLATFORM_CTSTRING_H_
  14. #include <stdint.h>
  15. #include <stdlib.h>
  16. #include "tensorflow/core/platform/ctstring_internal.h"
  17. // Initialize a new tstring. This must be called before using any function
  18. // below.
  19. inline void TF_TString_Init(TF_TString *str);
  20. // Deallocate a tstring.
  21. inline void TF_TString_Dealloc(TF_TString *str);
  22. // Resizes `str' to `new_size'. This function will appropriately grow or shrink
  23. // the string buffer to fit a `new_size' string. Grown regions of the string
  24. // will be initialized with `c'.
  25. inline char *TF_TString_Resize(TF_TString *str, size_t new_size, char c);
  26. // Similar to TF_TString_Resize, except the newly allocated regions will remain
  27. // uninitialized. This is useful if you plan on overwriting the newly grown
  28. // regions immediately after allocation; doing so will elide a superfluous
  29. // initialization of the new buffer.
  30. inline char *TF_TString_ResizeUninitialized(TF_TString *str, size_t new_size);
  31. // Reserves a string buffer with a capacity of at least `new_cap'.
  32. // Reserve will not change the size, or the contents of the existing
  33. // string. This is useful if you have a rough idea of `str's upperbound in
  34. // size, and want to avoid allocations as you append to `str'. It should not be
  35. // considered safe to write in the region between size and capacity; explicitly
  36. // resize before doing so.
  37. inline void TF_TString_Reserve(TF_TString *str, size_t new_cap);
  38. // Similar to TF_TString_Reserve, except that we ensure amortized growth, i.e.
  39. // that we grow the capacity by at least a constant factor >1.
  40. inline void TF_TString_ReserveAmortized(TF_TString *str, size_t new_cap);
  41. // Returns the size of the string.
  42. inline size_t TF_TString_GetSize(const TF_TString *str);
  43. // Returns the capacity of the string buffer. It should not be considered safe
  44. // to write in the region between size and capacity---call Resize or
  45. // ResizeUninitialized before doing so.
  46. inline size_t TF_TString_GetCapacity(const TF_TString *str);
  47. // Returns the underlying type of the tstring:
  48. // TF_TSTR_SMALL:
  49. // Small string optimization; the contents of strings
  50. // less than 22-bytes are stored in the TF_TString struct. This avoids any
  51. // heap allocations.
  52. // TF_TSTR_LARGE:
  53. // Heap allocated string.
  54. // TF_TSTR_OFFSET: (currently unused)
  55. // An offset defined string. The string buffer begins at an internally
  56. // defined little-endian offset from `str'; i.e. GetDataPointer() = str +
  57. // offset. This type is useful for memory mapping or reading string tensors
  58. // directly from file, without the need to deserialize the data. For
  59. // security reasons, it is imperative that OFFSET based string tensors are
  60. // validated before use, or are from a trusted source.
  61. // TF_TSTR_VIEW:
  62. // A view into an unowned character string.
  63. //
  64. // NOTE:
  65. // VIEW and OFFSET types are immutable, so any modifcation via Append,
  66. // AppendN, or GetMutableDataPointer of a VIEW/OFFSET based tstring will
  67. // result in a conversion to an owned type (SMALL/LARGE).
  68. inline TF_TString_Type TF_TString_GetType(const TF_TString *str);
  69. // Returns a const char pointer to the start of the underlying string. The
  70. // underlying character buffer may not be null-terminated.
  71. inline const char *TF_TString_GetDataPointer(const TF_TString *str);
  72. // Returns a char pointer to a mutable representation of the underlying string.
  73. // In the case of VIEW and OFFSET types, `src' is converted to an owned type
  74. // (SMALL/LARGE). The underlying character buffer may not be null-terminated.
  75. inline char *TF_TString_GetMutableDataPointer(TF_TString *str);
  76. // Sets `dst' as a VIEW type to `src'. `dst' will not take ownership of `src'.
  77. // It is the user's responsibility to ensure that the lifetime of `src' exceeds
  78. // `dst'. Any mutations to `dst' via Append, AppendN, or GetMutableDataPointer,
  79. // will result in a copy into an owned SMALL or LARGE type, and will not modify
  80. // `src'.
  81. inline void TF_TString_AssignView(TF_TString *dst, const char *src,
  82. size_t size);
  83. // Appends `src' onto `dst'. If `dst' is a VIEW or OFFSET type, it will first
  84. // be converted to an owned LARGE or SMALL type. `dst' should not point to
  85. // memory owned by `src'.
  86. inline void TF_TString_Append(TF_TString *dst, const TF_TString *src);
  87. inline void TF_TString_AppendN(TF_TString *dst, const char *src, size_t size);
  88. // Copy/Move/Assign semantics
  89. //
  90. // | src | dst | complexity
  91. // Copy | * | SMALL/LARGE | fixed/O(size)
  92. // Assign | SMALL | SMALL | fixed
  93. // Assign | OFFSET | VIEW | fixed
  94. // Assign | VIEW | VIEW | fixed
  95. // Assign | LARGE | LARGE | O(size)
  96. // Move | * | same as src | fixed
  97. // Copies `src' to `dst'. `dst' will be an owned type (SMALL/LARGE). `src'
  98. // should not point to memory owned by `dst'.
  99. inline void TF_TString_Copy(TF_TString *dst, const char *src, size_t size);
  100. // Assigns a `src' tstring to `dst'. An OFFSET `src' type will yield a `VIEW'
  101. // `dst'. LARGE `src' types will be copied to a new buffer; all other `src'
  102. // types will incur a fixed cost.
  103. inline void TF_TString_Assign(TF_TString *dst, const TF_TString *src);
  104. // Moves a `src' tstring to `dst'. Moving a LARGE `src' to `dst' will result in
  105. // a valid but unspecified `src'. This function incurs a fixed cost for all
  106. // inputs.
  107. inline void TF_TString_Move(TF_TString *dst, TF_TString *src);
  108. #endif // TENSORFLOW_CORE_PLATFORM_CTSTRING_H_
粤ICP备19079148号