% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/buffer.R
\name{buffer}
\alias{buffer}
\title{Shared Memory Buffers}
\usage{
buffer(
  type = c("double", "integer", "logical", "raw"),
  dim,
  init = NULL,
  backing = c("auto", "mmap", "shm")
)
}
\arguments{
\item{type}{Character. Data type: "double" (default), "integer", "logical",
or "raw".}

\item{dim}{Integer vector. Dimensions of the buffer. For a vector, specify
the length. For a matrix, specify \code{c(nrow, ncol)}. For arrays,
specify all dimensions.}

\item{init}{Initial value to fill the buffer. Default is type-appropriate
zero (\code{0}, \code{0L}, \code{FALSE}, or \code{raw(0)}).}

\item{backing}{Backing type for shared memory: "auto" (default), "mmap",
or "shm".}
}
\value{
An S3 object of class "shard_buffer" that supports:
\itemize{
\item Slice assignment: \code{buf[idx] <- values}
\item Slice reading: \code{buf[idx]}
\item Full extraction: \code{buf[]}
\item Conversion to R vector: \code{as.vector(buf)}, \code{as.double(buf)}, etc.
}
}
\description{
Create typed writable output buffers backed by shared memory
for cross-process writes during parallel execution.

Creates a typed output buffer backed by shared memory that can be written
to by parallel workers using slice assignment.
}
\details{
Buffers provide an explicit output mechanism for \code{\link{shard_map}}.
Instead of returning results from workers (which requires serialization
and memory copying), workers write directly to shared buffers.

Supported types:
\itemize{
\item \code{"double"}: 8-byte floating point (default)
\item \code{"integer"}: 4-byte signed integer
\item \code{"logical"}: 4-byte logical (stored as integer)
\item \code{"raw"}: 1-byte raw data
}

Buffers support slice assignment using standard R indexing:
\code{buf[1:100] <- values}
}
\examples{
\donttest{
out <- buffer("double", dim = 100)
out[1:10] <- rnorm(10)
result <- out[]
}
}
\seealso{
\code{\link{segment_create}} for low-level segment operations,
\code{\link{share}} for read-only shared inputs
}
