Advertisement
Guest User

Untitled

a guest
Aug 17th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. size_t cs_dynamic_data_nnbuf_get_required_alloc_size(size_t max_payload_size, size_t records_count);
  2. void* cs_dynamic_data_nnbuf_add_record(void* buf, size_t record_size);
  3. void cs_dynamic_data_nnbuf_finalize(void* buf, Source queue_type);
  4. size_t cs_dynamic_data_get_current_content_size(const void* buf);
  5.  
  6. payload_size = content_size = alloc_size - header_size
  7. // comment that payload_size contains dynamic record headers (sizes)
  8.  
  9. // client is required to keep track of written data to prevent overflow
  10. // cs_nnbuf_get_required_alloc_size is interchangeable with cs_dynamic_data_nnbuf_get_required_alloc_size -
  11. // just use the second one when number of dynamic records is known
  12.  
  13. size_t s = cs_nnbuf_get_required_alloc_size(1000);
  14. void* buf = nn_alloc(s);
  15.  
  16. if (cs_dynamic_data_get_current_content_size(buf) + 500 < 1000) { // 0 -> true
  17.     void* rec = cs_dynamic_data_nnbuf_add_record(buf, 500);
  18.     memcpy(rec);
  19. } else {
  20.     cs_dynamic_data_nnbuf_finalize(buf, 0x8);
  21.     nn_send(&buf);
  22. }
  23.  
  24. if (cs_dynamic_data_get_current_content_size(buf) + 500 < 1000) { // 504 -> false
  25.     void* rec = cs_dynamic_data_nnbuf_add_record(buf, 500);
  26.     memcpy(rec);
  27. } else {
  28.     cs_dynamic_data_nnbuf_finalize(buf, 0x8);
  29.     nn_send(&buf);
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement