Skip to content

include/tenzr/frontend/memory_pool.h

Namespaces

Name
tenzr

Classes

Name
class tenzr::PinnedBuffer
class tenzr::MemoryPool
Process-wide cache of reusable PinnedBuffer allocations, keyed by size.

Source code

// Copyright 2026 The Tenzr Project Authors
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef TENZR_FRONTEND_MEMORY_POOL_H_
#define TENZR_FRONTEND_MEMORY_POOL_H_

#include <cstddef>
#include <functional>
#include <memory>
#include <mutex>
#include <unordered_map>
#include <vector>

namespace tenzr {

  class PinnedBuffer {
   public:
    explicit PinnedBuffer(size_t size_bytes, bool page_locked = true);
    ~PinnedBuffer();

    static std::shared_ptr<PinnedBuffer> Wrap(void* data, size_t size_bytes, std::function<void()> deleter);

    PinnedBuffer(const PinnedBuffer&) = delete;
    PinnedBuffer& operator=(const PinnedBuffer&) = delete;
    PinnedBuffer(PinnedBuffer&&) noexcept;
    PinnedBuffer& operator=(PinnedBuffer&&) noexcept;

    void* Data() { return data_; }
    const void* Data() const { return data_; }

    size_t SizeBytes() const { return size_bytes_; }

    bool IsPageLocked() const { return page_locked_; }

    template <typename T> T* As() { return static_cast<T*>(data_); }

    template <typename T> const T* As() const { return static_cast<const T*>(data_); }

   private:
    PinnedBuffer() = default;
    void* data_{nullptr};
    size_t size_bytes_{0};
    bool page_locked_{false};
    std::function<void()> deleter_{};
  };

  class MemoryPool {
   public:
    static MemoryPool& Instance();

    MemoryPool();
    ~MemoryPool();

    MemoryPool(const MemoryPool&) = delete;
    MemoryPool& operator=(const MemoryPool&) = delete;

    std::shared_ptr<PinnedBuffer> Allocate(size_t size_bytes, bool page_locked = true);

    void Clear();

   private:
    std::mutex mutex_;
    std::unordered_map<size_t, std::vector<std::unique_ptr<PinnedBuffer>>> free_buffers_;
  };

}  // namespace tenzr

#endif  // TENZR_FRONTEND_MEMORY_POOL_H_