Search code examples
csyntaxsystemd

About the syntax used in systemd sd-device source


So, I was reading the source code of libsystemd and found this line that I think can only be some kind of obscure trickery.

Please can someone explain.

Link: https://github.com/systemd/systemd/blob/32243272fff325fbdeb158817fe65d5337f0528d/src/libsystemd/sd-device/sd-device.c#L42

This is the line, so you don't have to open the link to read it. device = new(sd_device, 1); Where device is declared right before, as sd_device *device; And sd_device is a type.


Solution

  • new is a macro where the type sd_device in device = new(sd_device, 1); is used as sizeof(sd_device):

    #define new(t, n) ((t*) malloc_multiply(n, sizeof(t)))
    

    After preprocessing, the line in question therefore becomes a call to malloc_multiply:

    device = ((sd_device*) malloc_multiply(1, sizeof(sd_device)));