Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
AidanWelch opened this issue May 20, 2025 · 0 comments

Comments

@AidanWelch
Copy link

AidanWelch commented May 20, 2025

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant