Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:CARLA simulator Carla BufferPool

From Leeroopedia
Revision as of 12:12, 16 February 2026 by Admin (talk | contribs) (Auto-imported from implementations/CARLA_simulator_Carla_BufferPool.md)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Knowledge Sources
Domains Memory, Performance
Last Updated 2026-02-15 05:00 GMT

Overview

BufferPool is a lock-free object pool for Buffer instances that recycles allocated memory to reduce allocation overhead in high-throughput data pipelines.

Description

The BufferPool class in the carla namespace provides a thread-safe, lock-free pool of reusable Buffer objects. It inherits from std::enable_shared_from_this<BufferPool> to allow buffers to hold a weak reference back to their parent pool.

The pool uses a moodycamel::ConcurrentQueue<Buffer> as its backing store, which provides lock-free multi-producer multi-consumer queue semantics. An optional estimated_size parameter in the constructor pre-allocates queue capacity.

Pop() attempts to dequeue a Buffer from the pool. If the queue is empty, an empty Buffer is returned instead (the try_dequeue failure is intentionally ignored). The returned buffer's _parent_pool member is set to a weak_from_this() (C++17) or shared_from_this() (C++14) reference, enabling automatic return to the pool.

Push(Buffer&&) is a private method called by the Buffer class (which is a friend) during buffer destruction. When a Buffer with a valid _parent_pool is destroyed, it returns itself to the pool instead of freeing the underlying memory, allowing the allocation to be reused by subsequent Pop() calls.

Important behavior: Buffers only grow in size and never shrink unless explicitly cleared. The allocated memory is only freed when the BufferPool itself is destroyed.

Usage

BufferPool is used throughout the CARLA networking and sensor subsystems to efficiently recycle buffers in hot paths such as TCP message reading (in Primary and Secondary classes) and streaming data pipelines.

Code Reference

Source Location

  • Repository: CARLA
  • File: LibCarla/source/carla/BufferPool.h

Signature

class BufferPool : public std::enable_shared_from_this<BufferPool> {
public:
    BufferPool() = default;
    explicit BufferPool(size_t estimated_size);

    Buffer Pop();

private:
    friend class Buffer;
    void Push(Buffer &&buffer);

    moodycamel::ConcurrentQueue<Buffer> _queue;
};

Import

#include "carla/BufferPool.h"

I/O Contract

Inputs

Name Type Required Description
estimated_size size_t No Initial capacity hint for the concurrent queue

Outputs

Name Type Description
Pop() Buffer A recycled or newly-created buffer with a back-reference to this pool

Usage Examples

// Create a shared buffer pool
auto pool = std::make_shared<carla::BufferPool>();

// Pop a buffer from the pool (may be recycled or new)
carla::Buffer buf = pool->Pop();

// Use the buffer...
buf.reset(1024);  // grows if needed

// When buf goes out of scope, it automatically returns to the pool
// instead of freeing memory (if the pool is still alive)

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment