Skip to content

Commit

Permalink
Switch from scoped_ptr to std::unique_ptr in shared code
Browse files Browse the repository at this point in the history
b/291356560
  • Loading branch information
alexanderbobrovnik committed Feb 15, 2024
1 parent 4657405 commit 39a47ca
Show file tree
Hide file tree
Showing 38 changed files with 182 additions and 150 deletions.
2 changes: 1 addition & 1 deletion starboard/common/murmurhash2.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ uint32_t MurmurHash2_32(const void* src, uint32_t size, uint32_t prev_hash) {
memcpy(aligned_src, src, size);
return MurmurHash2_32_Aligned(aligned_src, size, prev_hash);
} else {
scoped_array<char> aligned_src(new char[size]);
std::unique_ptr<char[]> aligned_src(new char[size]);
memcpy(aligned_src.get(), src, size);
return MurmurHash2_32_Aligned(aligned_src.get(), size, prev_hash);
}
Expand Down
2 changes: 1 addition & 1 deletion starboard/common/murmurhash2.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#ifndef STARBOARD_COMMON_MURMURHASH2_H_
#define STARBOARD_COMMON_MURMURHASH2_H_

#include "starboard/common/scoped_ptr.h"
#include <memory>
#include "starboard/types.h"

namespace starboard {
Expand Down
12 changes: 12 additions & 0 deletions starboard/common/scoped_ptr.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
// scoped_array, scoped_ptr_malloc.

#include <algorithm>
#include <memory>
#include <utility>

#include "starboard/common/log.h"
Expand Down Expand Up @@ -134,6 +135,9 @@ class scoped_ptr {
: ptr_(other.release()) {}
#endif

scoped_ptr(scoped_ptr&& other)
: ptr_(other.release()) {}

// Constructor. Move constructor for C++03 move emulation of this type.
scoped_ptr(RValue rvalue) // NOLINT(runtime/explicit)
: ptr_(rvalue.object->release()) {}
Expand Down Expand Up @@ -497,6 +501,14 @@ scoped_ptr<T> make_scoped_ptr(T* ptr) {
return scoped_ptr<T>(ptr);
}

#ifdef DEPRECATED_SCOPED_PTR
template<class C>
using unique_ptr_alias = std::unique_ptr<C>;
#else
template<class C>
using unique_ptr_alias = scoped_ptr<C>;
#endif

} // namespace starboard

#endif // STARBOARD_COMMON_SCOPED_PTR_H_
3 changes: 2 additions & 1 deletion starboard/common/thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#define STARBOARD_COMMON_THREAD_H_

#include <functional>
#include <memory>
#include <string>

#include "starboard/common/scoped_ptr.h"
Expand Down Expand Up @@ -74,7 +75,7 @@ class Thread {
atomic_bool* joined_bool();

struct Data;
scoped_ptr<Data> d_;
std::unique_ptr<Data> d_;

Thread(const Thread&) = delete;
void operator=(const Thread&) = delete;
Expand Down
5 changes: 2 additions & 3 deletions starboard/shared/starboard/application.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#include "starboard/atomic.h"
#include "starboard/common/condition_variable.h"
#include "starboard/common/log.h"
#include "starboard/common/scoped_ptr.h"
#include "starboard/common/string.h"
#include "starboard/configuration.h"
#include "starboard/event.h"
Expand Down Expand Up @@ -249,7 +248,7 @@ bool Application::DispatchAndDelete(Application::Event* event) {
}

// Ensure the event is deleted unless it is released.
scoped_ptr<Event> scoped_event(event);
std::unique_ptr<Event> scoped_event(event);

// Ensure that we go through the the appropriate lifecycle events based on
// the current state. If intermediate events need to be processed, use
Expand Down Expand Up @@ -409,7 +408,7 @@ bool Application::DispatchAndDelete(Application::Event* event) {

bool Application::HandleEventAndUpdateState(Application::Event* event) {
// Ensure the event is deleted unless it is released.
scoped_ptr<Event> scoped_event(event);
std::unique_ptr<Event> scoped_event(event);

// Call OnSuspend() and OnResume() before the event as needed.
if (scoped_event->event->type == kSbEventTypeUnfreeze &&
Expand Down
3 changes: 2 additions & 1 deletion starboard/shared/starboard/application.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#ifndef STARBOARD_SHARED_STARBOARD_APPLICATION_H_
#define STARBOARD_SHARED_STARBOARD_APPLICATION_H_

#include <memory>
#include <vector>

#include "starboard/atomic.h"
Expand Down Expand Up @@ -441,7 +442,7 @@ class Application {
SbThread thread_;

// CommandLine instance initialized in |Run|.
scoped_ptr<CommandLine> command_line_;
std::unique_ptr<CommandLine> command_line_;

// The deep link included in the Start event sent to Cobalt. Initially NULL,
// derived classes may set it during initialization using |SetStartLink|.
Expand Down
21 changes: 11 additions & 10 deletions starboard/shared/starboard/net_args.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@

#include "starboard/shared/starboard/net_args.h"

#include <memory>
#include <string>
#include <vector>

#include "starboard/common/scoped_ptr.h"
#include "starboard/common/log.h"
#include "starboard/common/socket.h"
#include "starboard/common/time.h"
#include "starboard/socket_waiter.h"
Expand All @@ -37,8 +38,8 @@ namespace shared {
namespace starboard {
namespace {

scoped_ptr<Socket> CreateListenSocket() {
scoped_ptr<Socket> socket(
std::unique_ptr<Socket> CreateListenSocket() {
std::unique_ptr<Socket> socket(
new Socket(NET_ARGS_IP_VERSION, kSbSocketProtocolTcp));
socket->SetReuseAddress(true);
SbSocketAddress sock_addr;
Expand All @@ -56,7 +57,7 @@ scoped_ptr<Socket> CreateListenSocket() {
if (sock_err != kSbSocketOk) {
SbLogRawFormatF(kErrFmt, sock_err);
}
return socket.Pass();
return socket;
}

void WaitUntilReadableOrConnectionReset(SbSocket sock) {
Expand All @@ -76,18 +77,18 @@ void WaitUntilReadableOrConnectionReset(SbSocket sock) {
SbSocketWaiterDestroy(waiter);
}

scoped_ptr<Socket> WaitForClientConnection(Socket* listen_sock,
std::unique_ptr<Socket> WaitForClientConnection(Socket* listen_sock,
int64_t timeout) {
int64_t expire_time = (timeout >= 0) && (timeout < kSbInt64Max)
? CurrentMonotonicTime() + timeout
: kSbInt64Max;
while (true) {
scoped_ptr<Socket> client_connection(listen_sock->Accept());
std::unique_ptr<Socket> client_connection(listen_sock->Accept());
if (client_connection) {
return client_connection.Pass();
return client_connection;
}
if (CurrentMonotonicTime() > expire_time) {
return scoped_ptr<Socket>();
return std::unique_ptr<Socket>();
}
SbThreadSleep(1000);
}
Expand All @@ -112,8 +113,8 @@ std::vector<std::string> SplitStringByLines(const std::string& string_buff) {
const char kNetArgsCommandSwitchWait[] = "net_args_wait_for_connection";

std::vector<std::string> NetArgsWaitForPayload(int64_t timeout) {
scoped_ptr<Socket> listen = CreateListenSocket();
scoped_ptr<Socket> client_connection =
std::unique_ptr<Socket> listen = CreateListenSocket();
std::unique_ptr<Socket> client_connection =
WaitForClientConnection(listen.get(), timeout);
if (!client_connection) {
SB_LOG(ERROR) << "Timed out waiting for net args.";
Expand Down
35 changes: 18 additions & 17 deletions starboard/shared/starboard/net_log.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <functional>
#include <map>
#include <string>
#include <utility>

#include "starboard/common/atomic.h"
#include "starboard/common/log.h"
Expand Down Expand Up @@ -67,10 +68,10 @@ using RunFunction = std::function<void(Semaphore*)>;

class FunctionThread : public Thread {
public:
static scoped_ptr<Thread> Create(const std::string& thread_name,
static std::unique_ptr<Thread> Create(const std::string& thread_name,
RunFunction run_function) {
scoped_ptr<Thread> out(new FunctionThread(thread_name, run_function));
return out.Pass();
std::unique_ptr<Thread> out(new FunctionThread(thread_name, run_function));
return out;
}

FunctionThread(const std::string& name, RunFunction run_function)
Expand Down Expand Up @@ -104,8 +105,8 @@ std::string ToString(SbSocketError error) {
return ss.str();
}

scoped_ptr<Socket> CreateListenSocket() {
scoped_ptr<Socket> socket(
std::unique_ptr<Socket> CreateListenSocket() {
std::unique_ptr<Socket> socket(
new Socket(NET_LOG_IP_VERSION, kSbSocketProtocolTcp));
socket->SetReuseAddress(true);
SbSocketAddress sock_addr;
Expand All @@ -127,7 +128,7 @@ scoped_ptr<Socket> CreateListenSocket() {
if (sock_err != kSbSocketOk) {
SbLogRawFormatF(kErrFmt, sock_err);
}
return socket.Pass();
return socket;
}

class BufferedSocketWriter {
Expand Down Expand Up @@ -255,7 +256,7 @@ class BufferedSocketWriter {
// callback will be invoked.
class SocketListener {
public:
typedef std::function<void(scoped_ptr<Socket>)> Callback;
typedef std::function<void(std::unique_ptr<Socket>)> Callback;

SocketListener(Socket* listen_socket, Callback cb)
: listen_socket_(listen_socket), callback_(cb) {
Expand All @@ -269,18 +270,18 @@ class SocketListener {
private:
void Run(Semaphore* joined_sema) {
while (!joined_sema->TakeWait(100'000)) {
scoped_ptr<Socket> client_connection(listen_socket_->Accept());
std::unique_ptr<Socket> client_connection(listen_socket_->Accept());

if (client_connection) {
callback_(client_connection.Pass());
callback_(std::move(client_connection));
break;
}
}
}

Socket* listen_socket_;
Callback callback_;
scoped_ptr<Thread> thread_;
std::unique_ptr<Thread> thread_;
};

class NetLogServer {
Expand Down Expand Up @@ -311,17 +312,17 @@ class NetLogServer {
socket_listener_.reset(new SocketListener(listen_socket_.get(), cb));
}

void OnClientConnect(scoped_ptr<Socket> client_socket) {
void OnClientConnect(std::unique_ptr<Socket> client_socket) {
ScopedLock lock(socket_mutex_);
client_socket_ = client_socket.Pass();
client_socket_ = std::move(client_socket);
client_socket_->SetSendBufferSize(NET_LOG_SOCKET_BUFFER_SIZE);
client_socket_->SetTcpKeepAlive(true, 1'000'000);
}

// Has a client ever connected?
bool HasClientConnected() {
ScopedLock lock(socket_mutex_);
return client_socket_;
return client_socket_.get() != nullptr;
}

void OnLog(const char* msg) {
Expand Down Expand Up @@ -372,12 +373,12 @@ class NetLogServer {
}
}

scoped_ptr<Socket> listen_socket_;
scoped_ptr<Socket> client_socket_;
std::unique_ptr<Socket> listen_socket_;
std::unique_ptr<Socket> client_socket_;
Mutex socket_mutex_;

scoped_ptr<SocketListener> socket_listener_;
scoped_ptr<Thread> writer_thread_;
std::unique_ptr<SocketListener> socket_listener_;
std::unique_ptr<Thread> writer_thread_;
Semaphore writer_thread_sema_;
atomic_bool is_joined_;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ namespace filter {

class AdaptiveAudioDecoder : public AudioDecoder, private JobQueue::JobOwner {
public:
typedef std::function<scoped_ptr<filter::AudioDecoder>(
typedef std::function<unique_ptr_alias<filter::AudioDecoder>(
const media::AudioStreamInfo& audio_stream_info,
SbDrmSystem drm_system)>
AudioDecoderCreator;
Expand Down Expand Up @@ -79,9 +79,9 @@ class AdaptiveAudioDecoder : public AudioDecoder, private JobQueue::JobOwner {
OutputCB output_cb_ = nullptr;
ErrorCB error_cb_ = nullptr;

scoped_ptr<filter::AudioDecoder> audio_decoder_;
scoped_ptr<filter::AudioResampler> resampler_;
scoped_ptr<filter::AudioChannelLayoutMixer> channel_mixer_;
unique_ptr_alias<filter::AudioDecoder> audio_decoder_;
std::unique_ptr<filter::AudioResampler> resampler_;
std::unique_ptr<filter::AudioChannelLayoutMixer> channel_mixer_;
InputBuffers pending_input_buffers_;
ConsumedCB pending_consumed_cb_;
std::queue<scoped_refptr<DecodedAudio>> decoded_audios_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#ifndef STARBOARD_SHARED_STARBOARD_PLAYER_FILTER_AUDIO_CHANNEL_LAYOUT_MIXER_H_
#define STARBOARD_SHARED_STARBOARD_PLAYER_FILTER_AUDIO_CHANNEL_LAYOUT_MIXER_H_

#include <memory>
#include <vector>

#include "starboard/common/ref_counted.h"
Expand All @@ -38,7 +39,7 @@ class AudioChannelLayoutMixer {
virtual scoped_refptr<DecodedAudio> Mix(
const scoped_refptr<DecodedAudio>& audio_data) = 0;

static scoped_ptr<AudioChannelLayoutMixer> Create(
static std::unique_ptr<AudioChannelLayoutMixer> Create(
SbMediaAudioSampleType sample_type,
SbMediaAudioFrameStorageType storage_type,
int output_channels);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -388,11 +388,11 @@ AudioChannelLayoutMixerImpl::MixMonoToStereoOptimized(
} // namespace

// static
scoped_ptr<AudioChannelLayoutMixer> AudioChannelLayoutMixer::Create(
std::unique_ptr<AudioChannelLayoutMixer> AudioChannelLayoutMixer::Create(
SbMediaAudioSampleType sample_type,
SbMediaAudioFrameStorageType storage_type,
int output_channels) {
return scoped_ptr<AudioChannelLayoutMixer>(new AudioChannelLayoutMixerImpl(
return std::unique_ptr<AudioChannelLayoutMixer>(new AudioChannelLayoutMixerImpl(
sample_type, storage_type, output_channels));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

#include "starboard/common/log.h"
#include "starboard/common/mutex.h"
#include "starboard/common/scoped_ptr.h"
#include "starboard/media.h"
#include "starboard/shared/starboard/thread_checker.h"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include <algorithm>
#include <string>
#include <utility>

#include "starboard/common/time.h"
#include "starboard/memory.h"
Expand Down Expand Up @@ -67,8 +68,8 @@ SbMediaAudioSampleType GetSinkAudioSampleType(
} // namespace

AudioRendererPcm::AudioRendererPcm(
scoped_ptr<AudioDecoder> decoder,
scoped_ptr<AudioRendererSink> audio_renderer_sink,
unique_ptr_alias<AudioDecoder> decoder,
unique_ptr_alias<AudioRendererSink> audio_renderer_sink,
const media::AudioStreamInfo& audio_stream_info,
int max_cached_frames,
int min_frames_per_append)
Expand All @@ -79,10 +80,10 @@ AudioRendererPcm::AudioRendererPcm(
bytes_per_frame_(media::GetBytesPerSample(sink_sample_type_) * channels_),
frame_buffer_(max_cached_frames_ * bytes_per_frame_),
frames_consumed_set_at_(CurrentMonotonicTime()),
decoder_(decoder.Pass()),
decoder_(std::move(decoder)),
process_audio_data_job_(
std::bind(&AudioRendererPcm::ProcessAudioData, this)),
audio_renderer_sink_(audio_renderer_sink.Pass()) {
audio_renderer_sink_(std::move(audio_renderer_sink)) {
SB_DLOG(INFO) << "Creating AudioRendererPcm with " << channels_
<< " channels, " << bytes_per_frame_ << " bytes per frame, "
<< max_cached_frames_ << " max cached frames, and "
Expand Down
Loading

0 comments on commit 39a47ca

Please sign in to comment.