scratch_buffer.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /* Variable-sized buffer with on-stack default allocation.
  2. Copyright (C) 2017-2023 Free Software Foundation, Inc.
  3. This file is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Lesser General Public License as
  5. published by the Free Software Foundation; either version 2.1 of the
  6. License, or (at your option) any later version.
  7. This file is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program. If not, see <https://www.gnu.org/licenses/>. */
  13. /* Written by Paul Eggert, 2017. */
  14. #ifndef _GL_SCRATCH_BUFFER_H
  15. #define _GL_SCRATCH_BUFFER_H
  16. /* Scratch buffers with a default stack allocation and fallback to
  17. heap allocation. It is expected that this function is used in this
  18. way:
  19. struct scratch_buffer tmpbuf;
  20. scratch_buffer_init (&tmpbuf);
  21. while (!function_that_uses_buffer (tmpbuf.data, tmpbuf.length))
  22. if (!scratch_buffer_grow (&tmpbuf))
  23. return -1;
  24. scratch_buffer_free (&tmpbuf);
  25. return 0;
  26. The allocation functions (scratch_buffer_grow,
  27. scratch_buffer_grow_preserve, scratch_buffer_set_array_size) make
  28. sure that the heap allocation, if any, is freed, so that the code
  29. above does not have a memory leak. The buffer still remains in a
  30. state that can be deallocated using scratch_buffer_free, so a loop
  31. like this is valid as well:
  32. struct scratch_buffer tmpbuf;
  33. scratch_buffer_init (&tmpbuf);
  34. while (!function_that_uses_buffer (tmpbuf.data, tmpbuf.length))
  35. if (!scratch_buffer_grow (&tmpbuf))
  36. break;
  37. scratch_buffer_free (&tmpbuf);
  38. scratch_buffer_grow and scratch_buffer_grow_preserve are guaranteed
  39. to grow the buffer by at least 512 bytes. This means that when
  40. using the scratch buffer as a backing store for a non-character
  41. array whose element size, in bytes, is 512 or smaller, the scratch
  42. buffer only has to grow once to make room for at least one more
  43. element.
  44. */
  45. /* Scratch buffer. Must be initialized with scratch_buffer_init
  46. before its use. */
  47. struct scratch_buffer;
  48. /* Initializes *BUFFER so that BUFFER->data points to BUFFER->__space
  49. and BUFFER->length reflects the available space. */
  50. #if 0
  51. extern void scratch_buffer_init (struct scratch_buffer *buffer);
  52. #endif
  53. /* Deallocates *BUFFER (if it was heap-allocated). */
  54. #if 0
  55. extern void scratch_buffer_free (struct scratch_buffer *buffer);
  56. #endif
  57. /* Grow *BUFFER by some arbitrary amount. The buffer contents is NOT
  58. preserved. Return true on success, false on allocation failure (in
  59. which case the old buffer is freed). On success, the new buffer is
  60. larger than the previous size. On failure, *BUFFER is deallocated,
  61. but remains in a free-able state, and errno is set. */
  62. #if 0
  63. extern bool scratch_buffer_grow (struct scratch_buffer *buffer);
  64. #endif
  65. /* Like scratch_buffer_grow, but preserve the old buffer
  66. contents on success, as a prefix of the new buffer. */
  67. #if 0
  68. extern bool scratch_buffer_grow_preserve (struct scratch_buffer *buffer);
  69. #endif
  70. /* Grow *BUFFER so that it can store at least NELEM elements of SIZE
  71. bytes. The buffer contents are NOT preserved. Both NELEM and SIZE
  72. can be zero. Return true on success, false on allocation failure
  73. (in which case the old buffer is freed, but *BUFFER remains in a
  74. free-able state, and errno is set). It is unspecified whether this
  75. function can reduce the array size. */
  76. #if 0
  77. extern bool scratch_buffer_set_array_size (struct scratch_buffer *buffer,
  78. size_t nelem, size_t size);
  79. #endif
  80. /* The implementation is imported from glibc. */
  81. /* Avoid possible conflicts with symbols exported by the GNU libc. */
  82. #define __libc_scratch_buffer_grow gl_scratch_buffer_grow
  83. #define __libc_scratch_buffer_grow_preserve gl_scratch_buffer_grow_preserve
  84. #define __libc_scratch_buffer_set_array_size gl_scratch_buffer_set_array_size
  85. #ifndef _GL_LIKELY
  86. /* Rely on __builtin_expect, as provided by the module 'builtin-expect'. */
  87. # define _GL_LIKELY(cond) __builtin_expect ((cond), 1)
  88. # define _GL_UNLIKELY(cond) __builtin_expect ((cond), 0)
  89. #endif
  90. #include <malloc/scratch_buffer.gl.h>
  91. #endif /* _GL_SCRATCH_BUFFER_H */