Skip to content

Commit a5aca00

Browse files
jatinchowdhury18abique
authored andcommitted
draft scratch-memory extension
1 parent 1d35ca1 commit a5aca00

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed
+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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

include/clap/plugin.h

+1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ typedef struct clap_plugin {
6161
// In this call the plugin may allocate memory and prepare everything needed for the process
6262
// call. The process's sample rate will be constant and process's frame count will included in
6363
// the [min, max] range, which is bounded by [1, INT32_MAX].
64+
// In this call the plugin may call host-provided methods marked [being-activated].
6465
// Once activated the latency and port configuration must remain constant, until deactivation.
6566
// Returns true on success.
6667
// [main-thread & !active]

0 commit comments

Comments
 (0)