Message.c 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  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. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. #include "wire/Message.h"
  16. #include "util/UniqueName.h"
  17. struct Message* Message_new(uint32_t messageLength,
  18. uint32_t amountOfPadding,
  19. struct Allocator* alloc)
  20. {
  21. uint8_t* buff = Allocator_malloc(alloc, messageLength + amountOfPadding);
  22. struct Message* out = Allocator_calloc(alloc, sizeof(struct Message), 1);
  23. out->bytes = &buff[amountOfPadding];
  24. out->length = out->capacity = messageLength;
  25. out->padding = amountOfPadding;
  26. out->alloc = alloc;
  27. return out;
  28. }
  29. void Message_setAssociatedFd(struct Message* msg, int fd)
  30. {
  31. if (fd == -1) {
  32. msg->associatedFd = 0;
  33. } else if (fd == 0) {
  34. msg->associatedFd = -1;
  35. } else {
  36. msg->associatedFd = fd;
  37. }
  38. }
  39. int Message_getAssociatedFd(struct Message* msg)
  40. {
  41. if (msg->associatedFd == -1) {
  42. return 0;
  43. } else if (msg->associatedFd == 0) {
  44. return -1;
  45. } else {
  46. return msg->associatedFd;
  47. }
  48. }
  49. struct Message* Message_clone(struct Message* toClone, struct Allocator* alloc)
  50. {
  51. Assert_true(toClone->capacity >= toClone->length);
  52. int32_t len = toClone->capacity + toClone->padding;
  53. uint8_t* allocation = Allocator_malloc(alloc, len + 8);
  54. while (((uintptr_t)allocation % 8) != (((uintptr_t)toClone->bytes - toClone->padding) % 8)) {
  55. allocation++;
  56. }
  57. Bits_memcpy(allocation, toClone->bytes - toClone->padding, len);
  58. return Allocator_clone(alloc, (&(struct Message) {
  59. .length = toClone->length,
  60. .padding = toClone->padding,
  61. .bytes = allocation + toClone->padding,
  62. .capacity = toClone->capacity,
  63. .alloc = alloc
  64. }));
  65. }
  66. void Message_copyOver(struct Message* output,
  67. struct Message* input,
  68. struct Allocator* allocator)
  69. {
  70. size_t inTotalLength = input->length + input->padding;
  71. size_t outTotalLength = output->length + output->padding;
  72. uint8_t* allocation = output->bytes - output->padding;
  73. if (inTotalLength > outTotalLength) {
  74. allocation = Allocator_realloc(allocator, allocation, inTotalLength);
  75. }
  76. Bits_memcpy(allocation, input->bytes - input->padding, inTotalLength);
  77. output->bytes = allocation + input->padding;
  78. output->length = input->length;
  79. output->padding = input->padding;
  80. }