Skip to content

⏪ Access variable arguments in struct layout fashion in C

License

Notifications You must be signed in to change notification settings

mulle-c/mulle-vararg

Repository files navigation

mulle-vararg

⏪ Access variable arguments in struct layout fashion in C

A variable argument passing scheme written in C (C11). It's an alternative to stdarg or varargs, but not compatible with either.

Release Version Release Notes
Mulle kybernetiK tag Build Status RELEASENOTES

API

There is an API to access mulle-vararg arguments. You can also build up a mulle-vararg manually with the builder functions.

How it works

mulle-vararg assumes that the arguments are not layed out in stack alignment fashion but like in a struct. The C promotion rules are still observed though.

Remember the C argument promotion rules are

  1. char and short to int/unsigned int
  2. float to double

Let's assume there is a compiler that uses mulle-vararg for variable arguments. It collects all function parameters and packs them into a struct, then passes this struct to the function.

A printf function being being called like this:

printf( "%d %f %lld\n", (char) 'x', (float) 0.2, 1848LL;

would get its arguments embedded in a struct like this

struct
{
   char    *format;
   struct
   {
      int         value1;     // standard char -> int promotion
      double      value2;     // standard float -> double promotion
      long long   value3;
   } varargs;
} _param;

mulle-vararg provides the necessary functions to read such a struct. It has some experimental code to write to it.

Advantages

  • Easy to write in C, does not need compiler ABI internals to construct or read. You don't need libffi or some such.
  • Cheap forwarding to other functions.

Disadvantages

  • Not compatible with <stdarg.h>

You are here

Overview

Add

This project is a component of the mulle-core library. As such you usually will not add or install it individually, unless you specifically do not want to link against mulle-core.

Add as an individual component

Use mulle-sde to add mulle-vararg to your project:

mulle-sde add github:mulle-c/mulle-vararg

To only add the sources of mulle-vararg with dependency sources use clib:

clib install --out src/mulle-c mulle-c/mulle-vararg

Add -isystem src/mulle-c to your CFLAGS and compile all the sources that were downloaded with your project.

Install

Use mulle-sde to build and install mulle-vararg and all dependencies:

mulle-sde install --prefix /usr/local \
   https://github.com/mulle-c/mulle-vararg/archive/latest.tar.gz

Legacy Installation

Install the requirements:

Requirements Description
mulle-c11 🔀 Cross-platform C compiler glue (and some cpp conveniences)

Download the latest tar or zip archive and unpack it.

Install mulle-vararg into /usr/local with cmake:

cmake -B build \
      -DCMAKE_INSTALL_PREFIX=/usr/local \
      -DCMAKE_PREFIX_PATH=/usr/local \
      -DCMAKE_BUILD_TYPE=Release &&
cmake --build build --config Release &&
cmake --install build --config Release

Author

Nat! for Mulle kybernetiK

About

⏪ Access variable arguments in struct layout fashion in C

Topics

Resources

License

Stars

Watchers

Forks