packages feed

nova-nix-0.11.0.0 / cbits / nn_list.h

/* * nn_list.h - Contiguous array of thunk pointers for nova-nix lists. * * A flat C array of nn_thunk_t* pointers, 8 bytes per element in one * contiguous run, against the ~48 bytes per element of the cons cells a * Haskell [Thunk] takes on the GHC heap. * * The items array is allocated via nn_env_alloc_items (page-based bump * allocator).  The nn_list_t header is malloc'd and tracked for bulk * cleanup via nn_list_free_all().  Both are freed at arena teardown, * unless the region collector frees them first: a header through * nn_list_sweep, an items array with the lines of the page that hold it * (nn_region.h). * * Lists are immutable after construction: allocate with nn_list_new, * fill via nn_list_set, then read with nn_list_get/nn_list_count. * nn_list_drop builds a suffix over its source's items array, which * that immutability makes safe to share. * * NULL is the empty list, so a constructor cannot also use it to report * failure: each returns 0 or -1 and hands the list back through `out`. * * Lifecycle: constructed during evaluation, freed via nn_list_free_all() * at arena teardown or by nn_list_sweep.  Not thread-safe - * single-threaded evaluation only. */#ifndef NN_LIST_H#define NN_LIST_H#include "nn_tracker.h"#include <stdint.h>/* Forward declaration - nn_thunk_t is defined in nn_thunk.h */struct nn_thunk;/* --- Types --- *//* A list: contiguous array of thunk pointers with a count. */typedef struct nn_list {    struct nn_thunk **items;   /* contiguous array (page-allocated) */    uint32_t          count;   /* number of elements */} nn_list_t;/* --- Lifecycle --- *//* Allocate a new list with space for `count` thunk pointers. * The items array is allocated via the env page allocator (O(1)). * The header is malloc'd and tracked for bulk cleanup. * Returns 0 with *out the new list, or NULL when count is 0; returns -1 * with *out NULL when an allocation fails. */int nn_list_new(nn_list_t **out, uint32_t count);/* The elements of `list` from index `n` on, as a new tracked header over * the same items array: O(1) whatever the length, where a copy would * add count - n slots to the env pages. * Returns 0 with *out the suffix, or NULL when no element remains * (`list` empty or n >= count); returns -1 with *out NULL when the * header cannot be allocated. */int nn_list_drop(nn_list_t **out, const nn_list_t *list, uint32_t n);/* Free all tracked list headers at once (arena-style cleanup). * Items arrays are freed by nn_env_destroy (page allocator). * Call this once at evaluation end. */void nn_list_free_all(void);/* --- Access --- *//* Number of elements in the list. */uint32_t nn_list_count(const nn_list_t *list);/* Get the thunk pointer at the given index. * Index must be < nn_list_count(list). */struct nn_thunk *nn_list_get(const nn_list_t *list, uint32_t index);/* Set the thunk pointer at the given index (for construction). * Index must be < nn_list_count(list). */void nn_list_set(nn_list_t *list, uint32_t index, struct nn_thunk *thunk);/* --- Census (nn_stats.h) --- *//* Every header allocated and not yet freed: the module's own tracker, * whose array moves when the next header is created and is freed by * nn_list_free_all.  A header nn_list_drop made shares its source's * items array and ends where the source ends, so headers that end at one * address share one array, which the longest of them spans. */const nn_tracker_t *nn_list_tracked(void);/* Free the headers from place `from` of the tracker on that `keep` * refuses, and close up the rest (nn_tracker_sweep).  An items array is * in an env page, which a header's free leaves alone. */void nn_list_sweep(uint32_t from, nn_tracker_keep_t keep, void *ctx);#endif /* NN_LIST_H */