4 #include "bufferable.h"
11 Bufferable::Bufferable():
19 Bufferable::~Bufferable()
24 void Bufferable::use_buffer(Buffer *buf, Bufferable *prev)
26 if(prev && buf!=prev->buffer)
27 throw invalid_argument("Bufferable::use_buffer");
35 prev_in_buffer = prev;
38 next_in_buffer = prev_in_buffer->next_in_buffer;
39 prev_in_buffer->next_in_buffer = this;
46 void Bufferable::unlink_from_buffer()
49 prev_in_buffer->next_in_buffer = next_in_buffer;
52 next_in_buffer->prev_in_buffer = prev_in_buffer;
53 next_in_buffer->update_offset();
60 void Bufferable::update_offset()
62 unsigned new_offset = 0;
64 new_offset = prev_in_buffer->offset+prev_in_buffer->get_data_size();
66 unsigned align = get_alignment();
67 new_offset += align-1;
68 new_offset -= new_offset%align;
69 if(new_offset!=offset)
77 next_in_buffer->update_offset();
79 /* Do not resize the buffer here, as more bufferables may be added before
80 the buffer is actually used. */
83 void Bufferable::update_buffer() const
85 BindRestore bind(buffer, buffer->get_type());
86 if(offset+get_data_size()>=buffer->get_size())
88 const Bufferable *last = this;
89 for(; last->next_in_buffer; last=last->next_in_buffer) ;
91 unsigned total_size = last->offset+last->get_data_size();
93 if(total_size>buffer->get_size())
95 buffer->data(total_size, 0);
96 /* Resizing the buffer invalidates its contents. Non-dirty data may
97 be in use, so reupload it. */
98 for(const Bufferable *b=last; b; b=b->prev_in_buffer)