aaudio: use unique_ptr in FixedBlockAdapter

Code cleanup.

Bug: 151650670
Test: atest CtsNativeMediaAAudioTestCases
Change-Id: I980862379ade9b6cc62f1743c916208e3911a00a
Merged-In: I980862379ade9b6cc62f1743c916208e3911a00a
(cherry picked from commit 2f39c0b0fcbc5b857df2823247d163f2ba1c14a8)
gugelfrei
Phil Burk 4 years ago
parent bc8a9381f5
commit 38ef26a93f

@ -18,22 +18,17 @@
#include "FixedBlockAdapter.h"
FixedBlockAdapter::~FixedBlockAdapter() {
close();
}
int32_t FixedBlockAdapter::open(int32_t bytesPerFixedBlock)
{
mSize = bytesPerFixedBlock;
mStorage = new uint8_t[bytesPerFixedBlock];
mStorage = std::make_unique<uint8_t[]>(bytesPerFixedBlock);
mPosition = 0;
return 0;
}
int32_t FixedBlockAdapter::close()
{
delete[] mStorage;
mStorage = nullptr;
mStorage.reset();
mSize = 0;
mPosition = 0;
return 0;

@ -17,6 +17,7 @@
#ifndef AAUDIO_FIXED_BLOCK_ADAPTER_H
#define AAUDIO_FIXED_BLOCK_ADAPTER_H
#include <memory>
#include <stdio.h>
/**
@ -37,7 +38,7 @@ public:
FixedBlockAdapter(FixedBlockProcessor &fixedBlockProcessor)
: mFixedBlockProcessor(fixedBlockProcessor) {}
virtual ~FixedBlockAdapter();
virtual ~FixedBlockAdapter() = default;
/**
* Allocate internal resources needed for buffering data.
@ -63,7 +64,7 @@ public:
protected:
FixedBlockProcessor &mFixedBlockProcessor;
uint8_t *mStorage = nullptr; // Store data here while assembling buffers.
std::unique_ptr<uint8_t[]> mStorage; // Store data here while assembling buffers.
int32_t mSize = 0; // Size in bytes of the fixed size buffer.
int32_t mPosition = 0; // Offset of the last byte read or written.
};

@ -39,7 +39,7 @@ int32_t FixedBlockReader::readFromStorage(uint8_t *buffer, int32_t numBytes) {
if (bytesToRead > dataAvailable) {
bytesToRead = dataAvailable;
}
memcpy(buffer, mStorage + mPosition, bytesToRead);
memcpy(buffer, &mStorage[mPosition], bytesToRead);
mPosition += bytesToRead;
return bytesToRead;
}
@ -60,7 +60,7 @@ int32_t FixedBlockReader::processVariableBlock(uint8_t *buffer, int32_t numBytes
bytesLeft -= mSize;
} else {
// Just need a partial block so we have to use storage.
result = mFixedBlockProcessor.onProcessFixedBlock(mStorage, mSize);
result = mFixedBlockProcessor.onProcessFixedBlock(mStorage.get(), mSize);
mPosition = 0;
}
}

@ -30,7 +30,7 @@ int32_t FixedBlockWriter::writeToStorage(uint8_t *buffer, int32_t numBytes) {
if (bytesToStore > roomAvailable) {
bytesToStore = roomAvailable;
}
memcpy(mStorage + mPosition, buffer, bytesToStore);
memcpy(&mStorage[mPosition], buffer, bytesToStore);
mPosition += bytesToStore;
return bytesToStore;
}
@ -46,7 +46,7 @@ int32_t FixedBlockWriter::processVariableBlock(uint8_t *buffer, int32_t numBytes
bytesLeft -= bytesWritten;
// If storage full then flush it out
if (mPosition == mSize) {
result = mFixedBlockProcessor.onProcessFixedBlock(mStorage, mSize);
result = mFixedBlockProcessor.onProcessFixedBlock(mStorage.get(), mSize);
mPosition = 0;
}
}

Loading…
Cancel
Save