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:Ggml org Llama cpp RAII Pointers

From Leeroopedia
Knowledge Sources
Domains API, Memory_Management
Last Updated 2026-02-15 00:00 GMT

Overview

Provides C++ RAII smart pointer typedefs for the core llama.cpp opaque types, enabling automatic resource cleanup.

Description

This header defines custom deleter structs (`llama_model_deleter`, `llama_context_deleter`, `llama_sampler_deleter`, `llama_adapter_lora_deleter`) that call the corresponding `llama_*_free` functions. It then typedefs `std::unique_ptr` specializations as `llama_model_ptr`, `llama_context_ptr`, `llama_sampler_ptr`, and `llama_adapter_lora_ptr`. The header is C++-only, guarded by a `__cplusplus` check.

Usage

Use these smart pointer types in C++ code to manage llama.cpp resources with automatic cleanup. They are widely used in examples, tools, and the server implementation to prevent resource leaks.

Code Reference

Source Location

Signature

struct llama_model_deleter {
    void operator()(llama_model * model);
};
struct llama_context_deleter {
    void operator()(llama_context * context);
};
struct llama_sampler_deleter {
    void operator()(llama_sampler * sampler);
};
struct llama_adapter_lora_deleter {
    void operator()(llama_adapter_lora *);
};

typedef std::unique_ptr<llama_model, llama_model_deleter> llama_model_ptr;
typedef std::unique_ptr<llama_context, llama_context_deleter> llama_context_ptr;
typedef std::unique_ptr<llama_sampler, llama_sampler_deleter> llama_sampler_ptr;
typedef std::unique_ptr<llama_adapter_lora, llama_adapter_lora_deleter> llama_adapter_lora_ptr;

Import

#include "llama-cpp.h"
// or equivalently:
#include <memory>
#include "llama.h"

I/O Contract

Inputs

Name Type Required Description
model llama_model * Yes Raw pointer to a llama_model for llama_model_ptr
context llama_context * Yes Raw pointer to a llama_context for llama_context_ptr
sampler llama_sampler * Yes Raw pointer to a llama_sampler for llama_sampler_ptr
adapter llama_adapter_lora * Yes Raw pointer to a llama_adapter_lora for llama_adapter_lora_ptr

Outputs

Name Type Description
llama_model_ptr std::unique_ptr<llama_model, llama_model_deleter> RAII wrapper that calls llama_model_free on destruction
llama_context_ptr std::unique_ptr<llama_context, llama_context_deleter> RAII wrapper that calls llama_free on destruction
llama_sampler_ptr std::unique_ptr<llama_sampler, llama_sampler_deleter> RAII wrapper that calls llama_sampler_free on destruction
llama_adapter_lora_ptr std::unique_ptr<llama_adapter_lora, llama_adapter_lora_deleter> RAII wrapper for LoRA adapter (deleter is currently a no-op)

Usage Examples

#include "llama-cpp.h"

// Model is automatically freed when ptr goes out of scope
llama_model_ptr model(llama_model_load_from_file("model.gguf", params));

// Context is automatically freed when ptr goes out of scope
llama_context_ptr ctx(llama_init_from_model(model.get(), ctx_params));

Related Pages

Page Connections

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