paper.h 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. // Copyright © 2018 Ariadne Devos
  3. /* shttpd -- circular character buffers */
  4. #ifndef _sHT_PAPER_H
  5. #define _sHT_PAPER_H
  6. #include <stdint.h>
  7. /* Text that is passed around and scribbled is called 'paper'. */
  8. /* According to https://stackoverflow.com/questions/42258274/
  9. should-i-send-data-in-chunks-or-send-it-all-at-once, the exact buffer size
  10. doesn't really matter. So choose a convenient, round number that allows for
  11. many concurrent connections. (Increase this value if performance tests tell
  12. you to do so.)
  13. This is guaranteed to be a power of 2, and a multiple of 1024. */
  14. #define sHT_PAPER_SIZE 4096
  15. #define sHT_PAPER_SIZE_MAX 4095
  16. /** A single unsigned octet, which may or may not represent a character. */
  17. typedef unsigned char sHT_octet;
  18. /** A single unsigned octet, which should not be interpreted as a character.
  19. (E.g. from most image formats). */
  20. typedef sHT_octet sHT_byte;
  21. /** A character (or possibly, part of a UTF-8 sequence), represented by
  22. an unsigned octet. */
  23. typedef sHT_octet sHT_char;
  24. typedef uint_least16_t sHT_size;
  25. typedef sHT_octet *sHT_paper;
  26. /* Stack */
  27. struct sHT_paper_mill
  28. {
  29. unsigned int capacity;
  30. unsigned int length;
  31. sHT_octet **first;
  32. };
  33. /* May return NULL (allocation failure). */
  34. __attribute__((warn_unused_result))
  35. __attribute__((nonnull (1)))
  36. sHT_paper
  37. sHT_alloc_paper(struct sHT_paper_mill *mill);
  38. /* Assumes mill->length != mill->capacity. Cannot fail. */
  39. __attribute__((nonnull (1, 2)))
  40. void
  41. sHT_free_paper(struct sHT_paper_mill *mill, sHT_paper p);
  42. struct sHT_buffer
  43. {
  44. sHT_paper first;
  45. sHT_size offset;
  46. sHT_size length;
  47. };
  48. #endif