resource.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /* shttpd - allocating memory of various types
  2. Copyright (C) 2018 Ariadne Devos
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. This program 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 General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. #ifndef _sHT_RESOURCE_H
  14. #define _sHT_RESOURCE_H
  15. #include <stddef.h>
  16. /* See doc/memory-allocation.md. */
  17. struct sHT_object_cache;
  18. /** Try to allocate a fresh object cache.
  19. @var{capacity}: the number of objects that will be available
  20. @var{elem_size}: the size of each object.
  21. @var{errno} may be mutated. This procedure does not know about signals or
  22. POSIX asynchronuous cancellation.
  23. If the kernel doesn't like our use of mmap(2), the thread or process might be
  24. killed.
  25. This may fail by returning @code{NULL} if memory is tight, if virtual memory
  26. is fragmented, or the mmap(2) syscall is disallowed. Else, return a fresh
  27. object cache. */
  28. __attribute__((warn_unused_result))
  29. struct sHT_object_cache *
  30. sHT_alloc_cache(size_t capacity, size_t elem_size);
  31. /** Free an object cache, making all allocated object pointers dangling.
  32. @var{cache}: the object cache to get rid of, not NULL
  33. @var{errno} may be mutated, @var{cache} will be dangling, and so will
  34. all allocated object pointers be.
  35. This procedure does not know about signals or POSIX asynchronuous
  36. cancellation.
  37. If the kernel doen't like our use of munmap(2), the thread or process
  38. might be killed.
  39. */
  40. __attribute__((nonnull (1)))
  41. void
  42. sHT_free_cache(struct sHT_object_cache *cache);
  43. /** Try to allocate a fresh object from an object cache.
  44. @var{cache}: the object cache to utilise
  45. The object will have the size specified by @var{sHT_alloc_cache},
  46. and at least standard alignment (i.e. @code{_Alignof(max_align_t)}}.
  47. This procedure does not know about signals or POSIX asynchronuous
  48. cancellation.
  49. This may fail by returning @code{NULL} if the object cache is exhausted.
  50. Else, return a fresh object. On a speculative execution, the object may
  51. not be fresh: it may share memory with an already-allocated object of the
  52. cache. If this is a problem in your use cache, you can use
  53. @var{sHT_despeculate}. */
  54. __attribute__((warn_unused_result))
  55. __attribute__((nonnull (1)))
  56. void *
  57. sHT_alloc(struct sHT_object_cache *cache);
  58. /** Free an object that belongs to a certain cache.
  59. @var{cache}: the object cache @var{object} was allocated from
  60. @var{object}: the object to free
  61. The object must have been allocated by @var{sHT_alloc} with the same
  62. cache argument. The object will become dangling.
  63. @var{object} speculatively being NULL is also allowed, although that
  64. speculatively messes up statistics.
  65. This procedure does not know about signals or POSIX asynchronuous
  66. cancellation.
  67. This cannot fail in any way. */
  68. __attribute__((nonnull (1)))
  69. void
  70. sHT_free(struct sHT_object_cache *cache, void *object);
  71. /** Test if a cache is exhausted.
  72. @var{cache}: the object cache to test
  73. This does not mutate anything.
  74. This procedure does not know about signals or POSIX asynchronuous
  75. cancellation.
  76. This cannot fail in any way. Return 1 if exhausted, 0 otherwise.
  77. An incorrect boolean may be returned on a speculative execution. */
  78. __attribute__((nonnull (1)))
  79. __attribute__((pure))
  80. _Bool
  81. sHT_cache_exhausted_p(struct sHT_object_cache *cache);
  82. #endif