Code Monkey home page Code Monkey logo

blobs.jl's Introduction

Blobs

Blobs makes it easy to lay out complex data-structures within a single memory region. Data-structures built using this library:

  • are relocatable - internal pointers are converted to offsets, so the entire memory region can be written to / read from disk or sent over the network without pointer patching
  • require no deserialization - they can be directly read/written without first copying the data into a Julia-native data-structure
  • require no additional heap allocation - field access is just pointer arithmetic and every field read/write returns an isbitstype type which can stored on the stack

This makes them ideal for implementing out-of-core data-structures or for DMA to co-processors.

Safety

This library does not protect against:

  • giving an incorrect length when creating a Blob
  • using a Blob after freeing the underlying allocation

Apart from that, all other operations are safe. User error or invalid data can cause AssertionError or BoundsError but cannot segfault the program or modify memory outside the blob.

Usage

Acquire a Ptr{Nothing} from somewhere:

julia> struct Foo
       x::Int64
       y::Bool
       end

julia> p = Libc.malloc(sizeof(Foo))
Ptr{Nothing} @0x0000000006416020

We can interpret this pointer as any isbitstype Julia struct:

julia> foo = Blob{Foo}(p, 0, sizeof(Foo))
Blobs.Blob{Foo}(Ptr{Nothing} @0x0000000004de87c0, 0, 16)

(See Blobs.malloc_and_init for a safer way to create a fresh blob).

We can access references to fields of Foo using the fieldnames directly:

julia> foo.x
Blobs.Blob{Int64}(Ptr{Nothing} @0x0000000004de87c0, 0, 16)

julia> foo.y
Blobs.Blob{Bool}(Ptr{Nothing} @0x0000000004de87c0, 8, 16)

And use [] to derefence Blobs:

julia> foo[]
Foo(114974496, true)

julia> foo.x[]
114974496

julia> foo.y[]
true

julia> y = foo.y
Blobs.Blob{Bool}(Ptr{Nothing} @0x0000000004de87c0, 8, 16)

julia> y[]
true

Similarly for setting values:

julia> foo[] = Foo(12, true)
Foo(12, true)

julia> foo[]
Foo(12, true)

julia> foo.y[] = false
false

julia> foo.y[]
false

julia> x = foo.x
Blobs.Blob{Int64}(Ptr{Nothing} @0x0000000004de87c0, 0, 16)

julia> x[] = 42
42

julia> x[]
42

julia> foo.x[]
42

The various data-structures provided can be nested arbitrarily. See the tests for examples.

Compatibility with Previous Versions

In the previous versions of this library, two macros @v and @ were used and we keep them for compatibility reasons. This macros bypass some of the bound-checkings and safety measures that are in-place in the normal usage of Blobs. In this section, we will introduce their usage.

Assume that we have the following Foo struct:

julia> struct Foo
       x::Int64
       y::Bool
       end

julia> m = Blobs.malloc_and_init(Foo)
Blob{Foo}(Ptr{Nothing} @0x00007fa0b84234e0, 0, 9)

Use the @a (for address) macro to obtain pointers to the fields of this struct:

julia> @a m.x
Blob{Int64}(Ptr{Nothing} @0x00007fa0b84234e0, 0, 9)

julia> @a m.y
Blob{Bool}(Ptr{Nothing} @0x00007fa0b84234e0, 8, 9)

Or the @v (for value) macro to dereference those pointers:

julia> @v m.x
44307392

julia> @v m.y
false

julia> y = @a m.y
Blob{Bool}(Ptr{Nothing} @0x00007fa0b84234e0, 8, 9)

julia> @v y
false

The @v macro also allows setting the value of a pointer:

julia> @v m.y = true
true

julia> @v m.y
true

julia> x = @a m.x
Blob{Int64}(Ptr{Nothing} @0x00007fa0b84234e0, 0, 9)

julia> @v x = 42
42

julia> @v x
42

julia> @v m.x
42

Also in this case, data-structures can be nested arbitrarily. See the compat-tests for examples.

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.