2828#include < time.h>
2929#endif
3030
31- #define K2_PLATFORM_HEADER_H_VERSION 17
31+ #define K2_PLATFORM_HEADER_H_VERSION 18
3232
3333// Always check that enum value is a valid value!
3434
@@ -124,6 +124,18 @@ struct ImageInfo {
124124 const struct KeyValuePair * extra_info;
125125};
126126
127+ // `stored_at` (the moment of the last publish/republish) is always treated as time origin 0,
128+ // so both `now` and `expires_at` are nanosecond offsets from it, not wall-clock timestamps.
129+ struct SharedMemoryInfo {
130+ // Nanoseconds elapsed since `stored_at`.
131+ uint64_t now;
132+ // Nanoseconds from `stored_at` to the expiration moment. `0` means the memory is immortal
133+ // (it was published/republished with a zero TTL) and never expires.
134+ uint64_t expires_at;
135+ // `true` if the memory was last published by this instance.
136+ bool is_own;
137+ };
138+
127139/* *
128140 * Symbols provided by component image
129141 * Every image should provide these symbols
@@ -199,9 +211,9 @@ void k2_free_checked(void* ptr, size_t size, size_t align);
199211
200212/* *
201213 * Shared memory provides a mechanism for instances to share data.
202- * To use it, first allocate memory with `k2_alloc_shared_memory `, then publish
203- * it with a unique name using `k2_publish_shared_memory `. Other instances can
204- * then retrieve the memory by name with `k2_get_shared_memory `.
214+ * To use it, first allocate memory with `k2_shared_memory_alloc `, then publish
215+ * it with a unique name using `k2_shared_memory_publish `. Other instances can
216+ * then retrieve the memory by name with `k2_shared_memory_get `.
205217 *
206218 * Lifecycle:
207219 * - A finite TTL removes the name from shared-memory storage after the TTL.
@@ -211,21 +223,21 @@ void k2_free_checked(void* ptr, size_t size, size_t align);
211223 * storage when the last user reference is released.
212224 * - UINT64_MAX publishes a strong entry with no initial expiration. Storage
213225 * keeps it alive until it is replaced, assigned a finite expiration by
214- * `k2_republish_shared_memory ` or `k2_seek_ttl_to_shared_memory `, or the
226+ * `k2_shared_memory_republish ` or `k2_shared_memory_seek_ttl_to `, or the
215227 * shared-memory subsystem stops.
216228 *
217229 * Reference counting:
218- * - A successful `k2_publish_shared_memory ` gives the calling instance one
230+ * - A successful `k2_shared_memory_publish ` gives the calling instance one
219231 * user reference. For a zero TTL, storage keeps only a weak reference and
220232 * does not add a reference of its own.
221233 * - For a finite TTL or UINT64_MAX, storage keeps a strong reference until
222234 * the entry is removed from storage.
223- * - Calling `k2_get_shared_memory ` acquires a user reference if this instance
235+ * - Calling `k2_shared_memory_get ` acquires a user reference if this instance
224236 * does not already hold one; repeated calls for the same pointer while it is
225237 * already held do not add another reference.
226- * - Calling `k2_release_shared_memory ` clears the user reference held by this
238+ * - Calling `k2_shared_memory_release ` clears the user reference held by this
227239 * instance, if `pointer` is currently held. Unpublished memory from
228- * `k2_alloc_shared_memory ` is simply freed.
240+ * `k2_shared_memory_alloc ` is simply freed.
229241 */
230242
231243/* *
@@ -246,34 +258,34 @@ void k2_free_checked(void* ptr, size_t size, size_t align);
246258 * `ENOMEM` => Not enough memory to allocate the requested region.
247259 * `ENOSYS` => Shared memory subsystem is unavailable on this host.
248260 */
249- int32_t k2_alloc_shared_memory (size_t size, size_t align, void ** pointer);
261+ int32_t k2_shared_memory_alloc (size_t size, size_t align, void ** pointer);
250262
251263/* *
252264 * Publishes shared memory with a name and TTL, making it discoverable by other instances.
253265 *
254266 * @param `name` Name to associate with the memory region. Must be unique.
255267 * Should be valid UTF-8 and not contain null bytes.
256268 * @param `name_len` Length of the name in bytes. Must be greater than 0.
257- * @param `memory` Pointer to memory previously allocated via `k2_alloc_shared_memory `.
269+ * @param `memory` Pointer to memory previously allocated via `k2_shared_memory_alloc `.
258270 * @param `ttl` Lifetime selector in milliseconds:
259271 * `0` publishes a weak entry with no TTL;
260272 * `1..=UINT64_MAX-1` uses a finite TTL;
261273 * `UINT64_MAX` means an initially infinite strong lifetime.
262274 * @param `as_mut` Controls whether write protection is disabled. If true, the memory is made writable.
263275 * @param `ignore_if_exist` If true, allows re-publishing memory previously allocated via
264- * `k2_alloc_shared_memory ` even if a live published memory with the same name already exists.
276+ * `k2_shared_memory_alloc ` even if a live published memory with the same name already exists.
265277 *
266278 * @return `0` on success. libc-like `errno` on error.
267279 *
268280 * Possible `errno`:
269281 * `EINVAL` => `name` is NULL, `name_len` is 0, `memory` is NULL, `name` is
270282 * not valid UTF-8, or `ttl` is too small (non-zero and less than
271283 * 100 ms).
272- * `ENOENT` => `memory` was not allocated by `k2_alloc_shared_memory `.
284+ * `ENOENT` => `memory` was not allocated by `k2_shared_memory_alloc `.
273285 * `EEXIST` => Memory with this name already exists.
274286 * `ENOSYS` => Shared memory subsystem is unavailable on this host.
275287 */
276- int32_t k2_publish_shared_memory (const char * name, size_t name_len, const void * memory, uint64_t ttl, bool as_mut, bool ignore_if_exist);
288+ int32_t k2_shared_memory_publish (const char * name, size_t name_len, const void * memory, uint64_t ttl, bool as_mut, bool ignore_if_exist);
277289
278290/* *
279291 * Retrieves shared memory by name and, unless already held by this
@@ -290,11 +302,11 @@ int32_t k2_publish_shared_memory(const char* name, size_t name_len, const void*
290302 * Possible `errno`:
291303 * `EINVAL` => `name` is NULL, `name_len` is 0, `pointer` is NULL, or `name`
292304 * is not valid UTF-8.
293- * `ENOENT` => No memory found with the given name, the weak entry has no live
294- * user references, or its finite TTL has removed it from storage .
305+ * `ENOENT` => No memory found with the given name (memory was freed).
306+ * `EKEYEXPIRED` => TTL for this name has already expired .
295307 * `ENOSYS` => Shared memory subsystem is unavailable on this host.
296308 */
297- int32_t k2_get_shared_memory (const char * name, size_t name_len, const void ** pointer, size_t * size);
309+ int32_t k2_shared_memory_get (const char * name, size_t name_len, const void ** pointer, size_t * size);
298310
299311/* *
300312 * Republishes shared memory, selecting the lifetime mode and restarting the
@@ -316,7 +328,7 @@ int32_t k2_get_shared_memory(const char* name, size_t name_len, const void** poi
316328 * user references, or its finite TTL has removed it from storage.
317329 * `ENOSYS` => Shared memory subsystem is unavailable on this host.
318330 */
319- int32_t k2_republish_shared_memory (const char * name, size_t name_len, uint64_t ttl);
331+ int32_t k2_shared_memory_republish (const char * name, size_t name_len, uint64_t ttl);
320332
321333/* *
322334 * Fast-forwards the expiration of published shared memory ahead of its TTL, by name.
@@ -352,13 +364,13 @@ int32_t k2_republish_shared_memory(const char* name, size_t name_len, uint64_t t
352364 * user references, or its finite TTL has removed it from storage.
353365 * `ENOSYS` => Shared memory subsystem is unavailable on this host.
354366 */
355- int32_t k2_seek_ttl_to_shared_memory (const char * name, size_t name_len, uint8_t percentile, uint64_t remaining_lifetime_limit);
367+ int32_t k2_shared_memory_seek_ttl_to (const char * name, size_t name_len, uint8_t percentile, uint64_t remaining_lifetime_limit);
356368
357369/* *
358- * Frees shared memory allocated via `k2_alloc_shared_memory ` that was never
370+ * Frees shared memory allocated via `k2_shared_memory_alloc ` that was never
359371 * published, or releases the reference to shared memory held by this
360- * instance, acquired via `k2_publish_shared_memory ` or
361- * `k2_get_shared_memory ` (see the reference counting notes above).
372+ * instance, acquired via `k2_shared_memory_publish ` or
373+ * `k2_shared_memory_get ` (see the reference counting notes above).
362374 *
363375 * Releasing a reference may remove a weak entry when it was the last user
364376 * reference. For a finite-TTL entry, the allocation is physically reclaimed
@@ -367,8 +379,8 @@ int32_t k2_seek_ttl_to_shared_memory(const char* name, size_t name_len, uint8_t
367379 * released.
368380 *
369381 * @param `pointer` Pointer to shared memory, as returned by
370- * `k2_alloc_shared_memory ` or `k2_get_shared_memory `, or
371- * passed as the `memory` argument to `k2_publish_shared_memory `.
382+ * `k2_shared_memory_alloc ` or `k2_shared_memory_get `, or
383+ * passed as the `memory` argument to `k2_shared_memory_publish `.
372384 *
373385 * @return `0` on success: either the unpublished allocation was freed, or the
374386 * reference held by this instance was released. libc-like `errno`
@@ -377,10 +389,31 @@ int32_t k2_seek_ttl_to_shared_memory(const char* name, size_t name_len, uint8_t
377389 * Possible `errno`:
378390 * `EINVAL` => `pointer` is NULL.
379391 * `ENOENT` => `pointer` does not correspond to memory allocated by this
380- * instance via `k2_alloc_shared_memory `, nor to a reference
392+ * instance via `k2_shared_memory_alloc `, nor to a reference
381393 * currently held by this instance.
382394 */
383- int32_t k2_release_shared_memory (const void * pointer);
395+ int32_t k2_shared_memory_release (const void * pointer);
396+
397+ /* *
398+ * Retrieves timing and ownership info about published shared memory, by name, without
399+ * taking a reference to it.
400+ *
401+ * @param `name` Name of the shared memory to query.
402+ * @param `name_len` Length of the name in bytes. Must be greater than 0.
403+ * @param `info` Out parameter, filled with the current info on success. `stored_at` (the
404+ * moment of the last publish/republish) is always treated as time origin 0, so
405+ * `info->now` and `info->expires_at` are nanosecond offsets from it, not
406+ * wall-clock timestamps. `info->expires_at == 0` means the memory is immortal
407+ * (published/republished with a zero TTL or weak lifetime) and never expires.
408+ *
409+ * @return `0` on success. libc-like `errno` on error.
410+ *
411+ * Possible `errno`:
412+ * `EINVAL` => `name` is NULL, `name_len` is 0, `info` is NULL, or `name` is not valid UTF-8.
413+ * `ENOENT` => No memory found with the given name (memory was freed).
414+ * `EKEYEXPIRED` => TTL for this name has already expired.
415+ */
416+ int32_t k2_shared_memory_info (const char * name, size_t name_len, struct SharedMemoryInfo * info);
384417
385418/* *
386419 * Immediately abort component execution.
0 commit comments