forked from carla-simulator/carla
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add buffer pool to reuse allocated memory
- Loading branch information
Showing
19 changed files
with
3,921 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#include "carla/Buffer.h" | ||
|
||
#include "carla/BufferPool.h" | ||
|
||
namespace carla { | ||
|
||
void Buffer::ReuseThisBuffer() { | ||
auto pool = _parent_pool.lock(); | ||
if (pool != nullptr) { | ||
pool->Push(std::move(*this)); | ||
} | ||
} | ||
|
||
} // namespace carla |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// Copyright (c) 2017 Computer Vision Center (CVC) at the Universitat Autonoma | ||
// de Barcelona (UAB). | ||
// | ||
// This work is licensed under the terms of the MIT license. | ||
// For a copy, see <https://opensource.org/licenses/MIT>. | ||
|
||
#pragma once | ||
|
||
#include "carla/Buffer.h" | ||
|
||
#include "moodycamel/ConcurrentQueue.h" | ||
|
||
#include <memory> | ||
|
||
namespace carla { | ||
|
||
/// A pool of Buffer. Buffers popped from this pool automatically return to | ||
/// the pool on destruction so the allocated memory can be reused. | ||
/// | ||
/// @warning Buffers adjust their size only by growing, they never shrink | ||
/// unless explicitly cleared. The allocated memory is only deleted when this | ||
/// pool is destroyed. | ||
class BufferPool : public std::enable_shared_from_this<BufferPool> { | ||
public: | ||
|
||
BufferPool() = default; | ||
|
||
explicit BufferPool(size_t estimated_size) : _queue(estimated_size) {} | ||
|
||
/// Pop a Buffer from the queue, creates a new one if the queue is empty. | ||
Buffer Pop() { | ||
Buffer item; | ||
_queue.try_dequeue(item); // we don't care if it fails. | ||
#if __cplusplus >= 201703L // C++17 | ||
item._parent_pool = weak_from_this(); | ||
#else | ||
item._parent_pool = shared_from_this(); | ||
#endif | ||
return item; | ||
} | ||
|
||
private: | ||
|
||
friend class Buffer; | ||
|
||
void Push(Buffer buffer) { | ||
_queue.enqueue(std::move(buffer)); | ||
} | ||
|
||
moodycamel::ConcurrentQueue<Buffer> _queue; | ||
}; | ||
|
||
} // namespace carla |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Copyright (c) 2017 Computer Vision Center (CVC) at the Universitat Autonoma | ||
// de Barcelona (UAB). | ||
// | ||
// This work is licensed under the terms of the MIT license. | ||
// For a copy, see <https://opensource.org/licenses/MIT>. | ||
|
||
#include "carla/streaming/detail/StreamState.h" | ||
|
||
#include "carla/BufferPool.h" | ||
|
||
namespace carla { | ||
namespace streaming { | ||
namespace detail { | ||
|
||
StreamState::StreamState(const token_type &token) | ||
: _token(token), | ||
_buffer_pool(std::make_shared<BufferPool>()) {} | ||
|
||
StreamState::~StreamState() = default; | ||
|
||
Buffer StreamState::MakeBuffer() { | ||
return _buffer_pool->Pop(); | ||
} | ||
|
||
} // namespace detail | ||
} // namespace streaming | ||
} // namespace carla |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.