Giter VIP home page Giter VIP logo

zcirc's Introduction

zcirc

A dynamic circular buffer allocator for zig

Purpose

One use case for circular buffers is handling allocations that depend on previous allocations, after which the previous data is no longer necessary. This library handles that use case when you don't know ahead of time how much memory you might need and can't afford the worst-case latency of a resize operation.

Installation

Choose your favorite method for vendoring this code into your repository. I think git-subrepo strikes a nicer balance than git-submodules or most other alternatives.

When Zig gets is own builtin package manager we'll be available there as well.

git subrepo clone git+https://github.com/hmusgrave/zcirc.git [optional-subdir]

Examples

const std = @import("std");
const CircularAllocator= @import("zcirc.zig").CircularAllocator;

test "something" {
    // Much like working with an ArenaAllocator
    var circle = CircularAllocator.init(std.testing.allocator);
    defer circle.deinit();
    var allocator = circle.allocator();

    // Allocations work like any other allocator
    var a = try allocator.create(u32);
    a.* = 12;
    var b = try allocator.alloc(u8, a.*);

    // The total "busy" bytes in our internal buffers include the
    // space for a and b, plus some slop for alignment and bookkeeping
    const overhead = @alignOf(CircularAllocator.Meta) + @sizeOf(CircularAllocator.Meta) - 2;
    try std.testing.expectEqual(
        (b.len + @sizeOf(u32)) + (@alignOf(u8) + @alignOf(u32)) + 2*overhead,
        circle.count()
    );

    // Deletes all data up through (including) the pointer `a`
    circle.free_left(a);

    // The total "busy" bytes only track space used for `b` now
    try std.testing.expectEqual(b.len + @alignOf(u8) + overhead, circle.count());

    // Deletes all data from (including) the slice `b`
    circle.free_right(b);

    // Our internal buffers are completely empty. New allocations will
    // attempt to reuse that space.
    try std.testing.expectEqual(@as(usize, 0), circle.count());
}

Status

Contributions welcome. I'll check back on this repo at least once per month. Currently targets Zig 0.10.*-dev.

The test suite isn't very complete yet, but any bugs should be superficial at this point.

zcirc's People

Contributors

hmusgrave avatar

Stargazers

Márk Bartos avatar Alex Kwiatkowski avatar Nikita avatar  avatar Christoph Kröppl avatar  avatar Andrew Chou avatar Abner Kaizer avatar Colton Franklin avatar  avatar Aksel Hjerpbakk avatar Hoang Phan avatar Loris Cro avatar  avatar Keith Chambers avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.