haiku library

User-defined mechanism requirements

  • haiku requires user to provide his own allocations, assertions and logging mechanisms:

    • hk_user_assert_t: a user-defined assertions (assert and an optional user context)

    • hk_user_logger_t: a user-defined logging (log and an optional user context)

    • hk_user_memory_allocator_t: a user-defined memory allocations (alloc/free/realloc and an optional user context)

  • The current C99 implementation does not provides a default/fallback mechanism but it can be trivially implemented using the standard library functions. A simple implementation is provided inside the online sample repository.

  • To properly initialize the haiku module, the user call haiku_module_create and specify function pointer to appropriate struct members:

haiku_module_create(&(hk_module_desc){
    .assertion = {.assert_fn = my_sample_assert},
    .logger    = {.log_fn    = my_sample_logger},
    .allocator  = {
        .realloc_fn = my_sample_realloc,
        .alloc_fn   = my_sample_alloc,
        .free_fn    = my_sample_free,
    }
});
// [...]
haiku_module_destroy();

For the graphics library, the function haiku_module_create is wrapped and replaced by a mandatory call to function hkgfx_module_create (and equivalent destroy function).

struct hk_user_assert_s

User-defined assertion.

Public Members

void *context

Pointer to an user-defined assertion context

void (*assert_fn)(bool invariant, const char *message, void *context)

User-defined assertion callback.

Param invariant:

Boolean expression to check against.

Param message:

String message describing the failure case.

Param context:

A pointer to the user context for assertions.

struct hk_user_logger_s

User-defined logger

Public Members

void *context

Pointer to an user-defined log/error context

void (*log_fn)(void *context, int level, const char *message, ...)

User-defined assertion callback.

Param context:

A pointer to the user context for logging.

Param level:

How important the message is.

Param message:

String message to format and log.

Param …:

Format argument (variable size list).

struct hk_user_memory_allocator_s

User-defined allocator.

Public Members

void *context

Pointer to user-defined allocation context.

void *(*alloc_fn)(size_t bytesize, void *context)

User-defined allocation function pointer.

Param bytesize:

The bytesize of the allocation

Param context:

A pointer to the user-defined context

Return:

void* Returns a pointer to the allocation

void (*free_fn)(void *pointer, size_t bytesize, void *context)

User-defined deallocation function pointer.

Param pointer:

The pointer to deallocate

Param bytesize:

The bytesize of the allocation

Param context:

A pointer to the user-defined context

void *(*realloc_fn)(size_t newsize, void *old_pointer, size_t oldsize, void *context)

User-defined reallocation function pointer.

Param pointer:

The pointer to reallocate

Param oldsize:

The bytesize of the previous allocation

Param newsize:

The bytesize of the new allocation

Param context:

A pointer to the user-defined context

void haiku_module_create(const hk_module_desc *desc)

Initialize the haiku graphics module. This call is mandatory at the beginning of the program.

void haiku_module_destroy(void)

Deinitialize the haiku graphics module. This call is mandatory at the end of the program.