Giter VIP home page Giter VIP logo

librts's Introduction

What is libRTS?

From The Lost Art of C Structure Packing:

The first thing to understand is that, on modern processors, the way your C compiler lays out basic C datatypes in memory is constrained in order to make memory accesses faster.

Put simply, the way a struct is defined in your C code is not necessarily the way it will be defined in memory. The compiler may insert extra unused bytes into your struct to make struct member accesses more efficient.

struct s {
    char *p; /* 4 bytes */
    char c;  /* 1 byte */
    int x;   /* 4 bytes */
}

sizeof(s) == 12 ?

For example, on an x86 system the size of this struct is not 9 bytes, even though the sum size of each member is 9 bytes. Besides having a size, all types have an intrinsic alignment requirement. In this case, the member c has a alignment requirement of only 1 byte, but x must be aligned with a 4 byte address. The C compiler will actually generate an internal representation like this:

struct s {
    char *p;
    char c;
    char unused[3];
    int x;
}

High level languages may not know at compile time how to manipulate structs passed to and from C functions. This can make foreign function interfaces cumbersome and non-portable. libRTS allows you to define C structs (and unions!) at runtime and gives you the ability to manipulate them as part your foreign fuction interface. In fact, libRTS is designed to complement systems that already use libFFI!

The Basics

The first thing you need to do is create an RtsType object and set a few of its members. This will represent the "shape" of your struct and allow you to perform introspection on its members. You then complete the initialization of your struct by calling the function rts_type_init.

RtsStatus rts_type_init(RtsType *type)

This initalizes type. rts_type_init returns a status code of type RtsStatus. This will either be RTS_STATUS_OK if the initialization was successful, or RTS_STATUS_BAD_TYPEDEF if the type or any of its members is incorrect.

Simple Example

#include <assert.h>
#include <rts/rts.h>

int main(int argc, char **argv) {

    struct s {
        void *p;
        char c;
        int x;
    };
    
    RtsType *elements[] = {&RTS_TYPE_POINTER, &RTS_TYPE_CHAR, &RTS_TYPE_SINT, NULL};
    size_t offsets[3];
    RtsType s_type;
    s_type.tag = RTS_TYPE_TAG_STRUCT;
    s_type.elements = elements;
    s_type.offsets = offsets;

    assert(rts_type_init(&s_type) == RTS_STATUS_OK);

    assert(s_type.size == sizeof(struct s));

    assert(offsets[0] == offsetof(struct s, p));
    assert(offsets[1] == offsetof(struct s, c));
    assert(offsets[2] == offsetof(struct s, x));
    
    return 0;
}

Installing

libRTS uses CMake to build and install.

  • From the librts directory, create a build folder and navigate inside of it:
$ mkdir build
$ cd build
  • Next use CMake to configure the project for building on your system. This causes CMake to read the CMakeLists.txt file in the project root and generate a build enviorment in the current directory:
$ cmake ..
  • Finally, build and install the library:
$ make
$ sudo make install
  • Additionally, you may uninstall using the uninstall target:
$ sudo make uninstall

Supported Platforms

libRTS has been tested on x86/64 and ARM. It probably works OOTB with many others.

librts's People

Contributors

lavignes avatar oulrich1 avatar

Watchers

James Cloos avatar  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.