Skip to content

Feature Request: Inherit allocation callbacks from parent engine, also use them for allocating the engine itself #7

@AidanWelch

Description

@AidanWelch

This would make using zaudio as a submodule much easier for me as I wouldn't have to wrap zaudio.init with my own global init. I could instead have my data structures maintain their own zaudio.Engine that they allocate with whatever arbitrary allocator they're passed. I think this could be as simple as something like(though for a lot of s_mem calls):

pub const Engine = opaque {
    // ...
    pub fn create(config: ?Config) Error!*Engine {
        var handle: ?*Engine = null;

        // Replaces: `try maybeError(zaudioEngineCreate(if (config) |conf| &conf else null, &handle));`
        var conf = if (config) |conf| conf else Config.init();
        try maybeError(zaudioEngineCreate(&conf, &handle));
        
        return handle.?;
    }
    extern fn zaudioEngineCreate(config: ?*const Config, out_handle: ?*?*Engine) Result;
    // ...
}
ma_result zaudioEngineCreate(const ma_engine_config* config, ma_engine** out_handle) {
    assert(out_handle != NULL);
    
    // Replaces: `*out_handle = s_mem.onMalloc(sizeof(ma_engine), s_mem.pUserData);`
    // `zaudioEngineConfigInit` already sets `out_config->allocationCallbacks = s_mem;` by default
    *out_handle = config.allocation_callbacks.onMalloc(sizeof(ma_engine),  config.allocation_callbacks.user_data);
    
    ma_result res = ma_engine_init(config, *out_handle);
    if (res != MA_SUCCESS) {
        // Replaces: `s_mem.onFree(*out_handle, s_mem.pUserData);`
        config.allocation_callbacks.onFree(*out_handle, config.allocation_callbacks.user_data);
        *out_handle = NULL;
    }
    return res;
}

I can start a PR if you'd be open to this. I understand it would be more burdensome with certain functions like zaudioWaveformCreate that don't directly take an allocator already.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions