scratch_buffer.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /* Variable-sized buffer with on-stack default allocation.
  2. Copyright (C) 2015-2021 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU General Public
  6. License as published by the Free Software Foundation; either
  7. version 3 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #ifndef _SCRATCH_BUFFER_H
  16. #define _SCRATCH_BUFFER_H
  17. /* Scratch buffers with a default stack allocation and fallback to
  18. heap allocation. It is expected that this function is used in this
  19. way:
  20. struct scratch_buffer tmpbuf;
  21. scratch_buffer_init (&tmpbuf);
  22. while (!function_that_uses_buffer (tmpbuf.data, tmpbuf.length))
  23. if (!scratch_buffer_grow (&tmpbuf))
  24. return -1;
  25. scratch_buffer_free (&tmpbuf);
  26. return 0;
  27. The allocation functions (scratch_buffer_grow,
  28. scratch_buffer_grow_preserve, scratch_buffer_set_array_size) make
  29. sure that the heap allocation, if any, is freed, so that the code
  30. above does not have a memory leak. The buffer still remains in a
  31. state that can be deallocated using scratch_buffer_free, so a loop
  32. like this is valid as well:
  33. struct scratch_buffer tmpbuf;
  34. scratch_buffer_init (&tmpbuf);
  35. while (!function_that_uses_buffer (tmpbuf.data, tmpbuf.length))
  36. if (!scratch_buffer_grow (&tmpbuf))
  37. break;
  38. scratch_buffer_free (&tmpbuf);
  39. scratch_buffer_grow and scratch_buffer_grow_preserve are guaranteed
  40. to grow the buffer by at least 512 bytes. This means that when
  41. using the scratch buffer as a backing store for a non-character
  42. array whose element size, in bytes, is 512 or smaller, the scratch
  43. buffer only has to grow once to make room for at least one more
  44. element.
  45. */
  46. #include <stdbool.h>
  47. #include <stddef.h>
  48. #include <stdlib.h>
  49. /* Scratch buffer. Must be initialized with scratch_buffer_init
  50. before its use. */
  51. struct scratch_buffer {
  52. void *data; /* Pointer to the beginning of the scratch area. */
  53. size_t length; /* Allocated space at the data pointer, in bytes. */
  54. union { max_align_t __align; char __c[1024]; } __space;
  55. };
  56. /* Initializes *BUFFER so that BUFFER->data points to BUFFER->__space
  57. and BUFFER->length reflects the available space. */
  58. static inline void
  59. scratch_buffer_init (struct scratch_buffer *buffer)
  60. {
  61. buffer->data = buffer->__space.__c;
  62. buffer->length = sizeof (buffer->__space);
  63. }
  64. /* Deallocates *BUFFER (if it was heap-allocated). */
  65. static inline void
  66. scratch_buffer_free (struct scratch_buffer *buffer)
  67. {
  68. if (buffer->data != buffer->__space.__c)
  69. free (buffer->data);
  70. }
  71. /* Grow *BUFFER by some arbitrary amount. The buffer contents is NOT
  72. preserved. Return true on success, false on allocation failure (in
  73. which case the old buffer is freed). On success, the new buffer is
  74. larger than the previous size. On failure, *BUFFER is deallocated,
  75. but remains in a free-able state, and errno is set. */
  76. bool __libc_scratch_buffer_grow (struct scratch_buffer *buffer);
  77. libc_hidden_proto (__libc_scratch_buffer_grow)
  78. /* Alias for __libc_scratch_buffer_grow. */
  79. static __always_inline bool
  80. scratch_buffer_grow (struct scratch_buffer *buffer)
  81. {
  82. return __glibc_likely (__libc_scratch_buffer_grow (buffer));
  83. }
  84. /* Like __libc_scratch_buffer_grow, but preserve the old buffer
  85. contents on success, as a prefix of the new buffer. */
  86. bool __libc_scratch_buffer_grow_preserve (struct scratch_buffer *buffer);
  87. libc_hidden_proto (__libc_scratch_buffer_grow_preserve)
  88. /* Alias for __libc_scratch_buffer_grow_preserve. */
  89. static __always_inline bool
  90. scratch_buffer_grow_preserve (struct scratch_buffer *buffer)
  91. {
  92. return __glibc_likely (__libc_scratch_buffer_grow_preserve (buffer));
  93. }
  94. /* Grow *BUFFER so that it can store at least NELEM elements of SIZE
  95. bytes. The buffer contents are NOT preserved. Both NELEM and SIZE
  96. can be zero. Return true on success, false on allocation failure
  97. (in which case the old buffer is freed, but *BUFFER remains in a
  98. free-able state, and errno is set). It is unspecified whether this
  99. function can reduce the array size. */
  100. bool __libc_scratch_buffer_set_array_size (struct scratch_buffer *buffer,
  101. size_t nelem, size_t size);
  102. libc_hidden_proto (__libc_scratch_buffer_set_array_size)
  103. /* Alias for __libc_scratch_set_array_size. */
  104. static __always_inline bool
  105. scratch_buffer_set_array_size (struct scratch_buffer *buffer,
  106. size_t nelem, size_t size)
  107. {
  108. return __glibc_likely (__libc_scratch_buffer_set_array_size
  109. (buffer, nelem, size));
  110. }
  111. /* Return a copy of *BUFFER's first SIZE bytes as a heap-allocated block,
  112. deallocating *BUFFER if it was heap-allocated. SIZE must be at
  113. most *BUFFER's size. Return NULL (setting errno) on memory
  114. exhaustion. */
  115. void *__libc_scratch_buffer_dupfree (struct scratch_buffer *buffer,
  116. size_t size);
  117. libc_hidden_proto (__libc_scratch_buffer_dupfree)
  118. /* Alias for __libc_scratch_dupfree. */
  119. static __always_inline void *
  120. scratch_buffer_dupfree (struct scratch_buffer *buffer, size_t size)
  121. {
  122. void *r = __libc_scratch_buffer_dupfree (buffer, size);
  123. return __glibc_likely (r != NULL) ? r : NULL;
  124. }
  125. #endif /* _SCRATCH_BUFFER_H */