|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include "../../plugin.h" |
| 4 | + |
| 5 | +// This extension lets the plugin request "scratch" memory |
| 6 | +// from the host. Scratch memory can be accessed during the |
| 7 | +// `process()` callback, but its content not persistent |
| 8 | +// between callbacks. |
| 9 | +// |
| 10 | +// The motivation for this extension is to allow the plugin host |
| 11 | +// to "share" a single scratch buffer across multiple plugin |
| 12 | +// instances. |
| 13 | +// |
| 14 | +// For example, imagine the host needs to process three plugins |
| 15 | +// in sequence, and each plugin requires 10K of scratch memory. |
| 16 | +// If each plugin pre-allocates its own scratch memory, then 30K |
| 17 | +// of memory is being allocated in total. However, if each plugin |
| 18 | +// requests 10K of scratch memory from the host, then the host can |
| 19 | +// allocate a single 10K scratch buffer, and make it available to all |
| 20 | +// three plugins. |
| 21 | +// |
| 22 | +// This optimization may allow for reduced memory usage and improved |
| 23 | +// CPU cache usage. |
| 24 | + |
| 25 | +static CLAP_CONSTEXPR const char CLAP_EXT_SCRATCH_MEMORY[] = "clap.scratch-memory/1"; |
| 26 | + |
| 27 | +#ifdef __cplusplus |
| 28 | +extern "C" { |
| 29 | +#endif |
| 30 | + |
| 31 | +typedef struct clap_host_scratch_memory { |
| 32 | + // Asks the host for certain amount of scratch memory. |
| 33 | + // If the host is unable to provide the memory, it should |
| 34 | + // return "false". |
| 35 | + // |
| 36 | + // The plugin may call this method multiple times (for |
| 37 | + // example, gradually decreasing the amount of scratch |
| 38 | + // being asked for until the host returns true), however, |
| 39 | + // the plugin should avoid calling this method un-neccesarily |
| 40 | + // since the host implementation may be relatively expensive. |
| 41 | + // If the plugin calls `reserve()` multiple times, then the |
| 42 | + // final call determines the actual amount of scratch memory |
| 43 | + // that will be available to the plugin. If the final call |
| 44 | + // returns false then no scratch memory will be provided, |
| 45 | + // regardless of any previous calls to `reserve()`. |
| 46 | + // |
| 47 | + // When the plugin is de-activated, the scratch memory |
| 48 | + // is invalidated, and the host may free the memory if |
| 49 | + // appropriate. The plugin will need to reserve scratch |
| 50 | + // memory again the next time it is activated. |
| 51 | + // |
| 52 | + // In the context of plugins and hosts that implement |
| 53 | + // the "thread-pool" extension, scratch memory is assumed |
| 54 | + // to be "thread-local". The plugin should request the maximum |
| 55 | + // amount of scratch memory that it will need on a single |
| 56 | + // thread. Accordingly, the host must ensure that each |
| 57 | + // thread can independently provide the requested amount |
| 58 | + // of scratch memory. |
| 59 | + // |
| 60 | + // [main-thread & being-activated] |
| 61 | + bool(CLAP_ABI *reserve)(const clap_host_t *host, uint32_t scratch_size_bytes); |
| 62 | + |
| 63 | + // Asks the host for the previously reserved scratch memory. |
| 64 | + // If the host returned "true" when scratch memory was requested, |
| 65 | + // then this method must return a pointer to a memory block at least |
| 66 | + // as large as the reserved size. If the host returned "false" |
| 67 | + // when scratch memory was requested, then this method must not |
| 68 | + // be called, and will return NULL. |
| 69 | + // |
| 70 | + // This method may only be called by the plugin from the audio thread, |
| 71 | + // (i.e. during the process() or thread_pool.exec() callback), and |
| 72 | + // the provided memory is only valid until the plugin returns from |
| 73 | + // that callback. The plugin must not hold any references to data |
| 74 | + // that lives in the scratch memory after returning from the callback, |
| 75 | + // as that data will likely be over-written by another plugin using |
| 76 | + // the same scratch memory. |
| 77 | + // |
| 78 | + // The provided memory is not initialized, and may have been used |
| 79 | + // by other plugin instances, so the plugin must correctly initialize |
| 80 | + // the memory when using it. |
| 81 | + // |
| 82 | + // The provided memory is owned by the host, so the plugin should not |
| 83 | + // attempt to free the memory. |
| 84 | + // |
| 85 | + // [audio-thread] |
| 86 | + void*(CLAP_ABI *access)(const clap_host_t *host); |
| 87 | +} clap_host_scratch_memory_t; |
| 88 | + |
| 89 | +#ifdef __cplusplus |
| 90 | +} |
| 91 | +#endif |
0 commit comments