paper.h 2.2 KB

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