1
0

Message.c 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. Message_t* Message_new(uint32_t messageLength,
  17. uint32_t amountOfPadding,
  18. struct Allocator* alloc)
  19. {
  20. uint8_t* buff = Allocator_malloc(alloc, messageLength + amountOfPadding);
  21. Message_t* out = Allocator_calloc(alloc, sizeof(struct Message), 1);
  22. out->_ad = buff;
  23. out->_adLen = 0;
  24. out->_msgbytes = &buff[amountOfPadding];
  25. out->_length = out->_capacity = messageLength;
  26. out->_padding = amountOfPadding;
  27. out->_alloc = alloc;
  28. return out;
  29. }
  30. struct Message* Message_new_fromRust(uint32_t messageLength,
  31. uint32_t amountOfPadding,
  32. struct Allocator* alloc)
  33. {
  34. return Message_new(messageLength, amountOfPadding, alloc);
  35. }
  36. void Message_setAssociatedFd(Message_t* msg, int fd)
  37. {
  38. if (fd == -1) {
  39. msg->_associatedFd = 0;
  40. } else if (fd == 0) {
  41. msg->_associatedFd = -1;
  42. } else {
  43. msg->_associatedFd = fd;
  44. }
  45. }
  46. int Message_getAssociatedFd(Message_t* msg)
  47. {
  48. if (msg->_associatedFd == -1) {
  49. return 0;
  50. } else if (msg->_associatedFd == 0) {
  51. return -1;
  52. } else {
  53. return msg->_associatedFd;
  54. }
  55. }
  56. Message_t* Message_clone(Message_t* toClone, struct Allocator* alloc)
  57. {
  58. Assert_true(toClone->_capacity >= toClone->_length);
  59. int32_t len = toClone->_capacity + toClone->_padding + toClone->_adLen;
  60. uint8_t* allocation = Allocator_malloc(alloc, len + 8);
  61. while (((uintptr_t)allocation % 8) != (((uintptr_t)toClone->_msgbytes - toClone->_padding - toClone->_adLen) % 8)) {
  62. allocation++;
  63. }
  64. Bits_memcpy(allocation, toClone->_msgbytes - toClone->_padding - toClone->_adLen, len);
  65. return Allocator_clone(alloc, (&(struct Message) {
  66. ._length = toClone->_length,
  67. ._padding = toClone->_padding,
  68. ._msgbytes = allocation + toClone->_adLen + toClone->_padding,
  69. ._ad = allocation + toClone->_adLen,
  70. ._adLen = toClone->_adLen,
  71. ._capacity = toClone->_capacity,
  72. ._alloc = alloc
  73. }));
  74. }