1984not-GmbH / molch

An implementation of the axolotl ratchet based on libsodium.
Other
23 stars 3 forks source link

"check_allocations" macro that automatically checks if any allocations have failed #83

Closed FSMaxB closed 8 years ago

FSMaxB commented 8 years ago

Just a crazy idea on how the C preprocessor could be abused. Right now molch has the throw_on_failed_alloc macro, see #78. Let's take a look at some example:

buffer_t  *header_key = buffer_create_on_heap(HEADER_KEY_SIZE, 0);
throw_on_failed_alloc(header_key);
buffer_t *message_key = buffer_create_on_heap(MESSAGE_KEY_SIZE, 0);
throw_on_failed_alloc(message_key);

But that could be better, right?

check_allocations(header_key, message_key,
    buffer_t  *header_key = buffer_create_on_heap(HEADER_KEY_SIZE, 0);
    buffer_t *message_key = buffer_create_on_heap(MESSAGE_KEY_SIZE, 0);
)

This macro would then automatically produce code like this:

buffer_t *header_key = buffer_create_on_heap(HEADER_KEY_SIZE, 0);
buffer_t *message_key = buffer_create_on_heap(MESSAGE_KEY_SIZE, 0);
throw_on_failed_alloc(header_key);
throw_on_failed_alloc(message_key);

This could be achieved by some crazy macro trick like this

FSMaxB commented 8 years ago

Although this seems nice, it would make debugging a lot harder. I don't think this is worth it.