BufferConsumer: rework SetOutputBuffersFor

* Use MemoryDeleter.
* Avoid use of dynamic allocation.
This commit is contained in:
Dario Casalinuovo
2015-07-11 15:40:15 +02:00
parent f50d7408b5
commit 0341eac71b
+23 -28
View File
@@ -33,6 +33,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <AutoDeleter.h>
#include <BufferProducer.h> #include <BufferProducer.h>
#include <BufferGroup.h> #include <BufferGroup.h>
#include <Buffer.h> #include <Buffer.h>
@@ -266,53 +267,47 @@ BBufferConsumer::SetOutputBuffersFor(const media_source &source,
if (IS_INVALID_DESTINATION(destination)) if (IS_INVALID_DESTINATION(destination))
return B_MEDIA_BAD_DESTINATION; return B_MEDIA_BAD_DESTINATION;
producer_set_buffer_group_command *command; int32 buffer_count = 0;
BBuffer **buffers;
int32 buffer_count;
size_t size;
status_t rv;
if (group == 0) { if (group > 0) {
buffer_count = 0; if (group->CountBuffers(&buffer_count) != B_OK)
} else {
if (B_OK != group->CountBuffers(&buffer_count))
return B_ERROR; return B_ERROR;
} }
if (buffer_count != 0) { size_t size = sizeof(producer_set_buffer_group_command)
buffers = new BBuffer * [buffer_count]; + buffer_count * sizeof(media_buffer_id);
if (B_OK != group->GetBufferList(buffer_count, buffers)) {
delete [] buffers; producer_set_buffer_group_command *command
return B_ERROR; = static_cast<producer_set_buffer_group_command *>(malloc(size));
} MemoryDeleter deleter(command);
} else {
buffers = NULL;
}
size = sizeof(producer_set_buffer_group_command) + buffer_count * sizeof(media_buffer_id);
command = static_cast<producer_set_buffer_group_command *>(malloc(size));
command->source = source; command->source = source;
command->destination = destination; command->destination = destination;
command->user_data = user_data; command->user_data = user_data;
command->change_tag = NewChangeTag(); command->change_tag = NewChangeTag();
command->buffer_count = buffer_count;
for (int32 i = 0; i < buffer_count; i++)
command->buffers[i] = buffers[i]->ID();
delete [] buffers; BBuffer *buffers[buffer_count];
if (buffer_count != 0) {
if (group->GetBufferList(buffer_count, buffers) != B_OK)
return B_ERROR;
for (int32 i = 0; i < buffer_count; i++)
command->buffers[i] = buffers[i]->ID();
}
command->buffer_count = buffer_count;
if (change_tag != NULL) if (change_tag != NULL)
*change_tag = command->change_tag; *change_tag = command->change_tag;
rv = SendToPort(source.port, PRODUCER_SET_BUFFER_GROUP, command, size); status_t status = SendToPort(source.port, PRODUCER_SET_BUFFER_GROUP,
free(command); command, size);
if (rv == B_OK) { if (status == B_OK) {
// XXX will leak memory if port write failed // XXX will leak memory if port write failed
delete fDeleteBufferGroup; delete fDeleteBufferGroup;
fDeleteBufferGroup = will_reclaim ? NULL : group; fDeleteBufferGroup = will_reclaim ? NULL : group;
} }
return rv; return status;
} }