1 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
2 <link rel="stylesheet" href="style.css" type="text/css"/>
3 <script src="code.js" type="text/javascript"></script>
5 <h1>Writing plugins in C?</h1>
15 /* The size of disk in bytes (initialized by
16 size=<SIZE> parameter). */
17 static int64_t size = 0;
19 /* Debug directory operations
23 static struct sparse_array *sa;
28 sa = alloc_sparse_array (memory_debug_dir);
38 free_sparse_array (sa);
42 memory_config (const char *key,
45 if (strcmp (key, "size") == 0) {
46 size = nbdkit_parse_size (value);
52 ("unknown parameter '%s'", key);
60 memory_config_complete (void)
63 nbdkit_error ("you must specify "
64 "size=<SIZE> on the "
71 #define memory_config_help \
72 "size=<SIZE> (required)"
73 " Size of the backing disk"
75 /* Create the per-connection handle. */
77 memory_open (int readonly)
79 /* Used only as a handle pointer. */
85 #define THREAD_MODEL \
86 NBDKIT_THREAD_MODEL_SERIALIZE_ALL_REQUESTS
88 /* Get the disk size. */
90 memory_get_size (void *handle)
92 return (int64_t) size;
97 memory_pread (void *handle, void *buf,
98 uint32_t count, uint64_t offset)
100 sparse_array_read (sa, buf, count, offset);
106 memory_pwrite (void *handle,
108 uint32_t count, uint64_t offset)
110 return sparse_array_write (sa,
116 memory_zero (void *handle,
118 uint64_t offset, int may_trim)
120 sparse_array_zero (sa, count, offset);
124 /* Trim (same as zero). */
126 memory_trim (void *handle,
127 uint32_t count, uint64_t offset)
129 sparse_array_zero (sa, count, offset);
133 static struct nbdkit_plugin plugin = {
135 .version = PACKAGE_VERSION,
137 .unload = memory_unload,
138 .config = memory_config,
139 .config_complete = memory_config_complete,
140 .config_help = memory_config_help,
142 .get_size = memory_get_size,
143 .pread = memory_pread,
144 .pwrite = memory_pwrite,
147 /* In this plugin, errno is preserved
148 * properly along error return
149 * paths from failed system calls.
151 .errno_is_preserved = 1,
154 NBDKIT_REGISTER_PLUGIN(plugin)