trafilo 0.1.0
Streaming event-handler framework in C
Loading...
Searching...
No Matches
window.c File Reference
#include "headers/window.h"
#include <stdlib.h>
#include <string.h>
#include <time.h>

Functions

static long timespec_diff_ms (struct timespec start, struct timespec end)
 Helper to calculate difference in milliseconds between two timespecs. Returns long (end - start).
void sliding_window_init (sliding_window_t *w, long window_size_ms, long slide_ms)
 Initialize a sliding window.
void sliding_window_destroy (sliding_window_t *w)
 Graceful cleanup of a window. Frees all ts_node_t in the window, not the window itself.
int sliding_window_add (sliding_window_t *w, struct timespec event_ts)
 Append the event timestamp as ts_node_t to tail. Evicts head while head->ts < (event_ts - window_size_ms).
int sliding_window_should_emit (const sliding_window_t *w, struct timespec now)
 Checks if it's time to emit (slide_ms elapsed since last emit).
void sliding_window_mark_emitted (sliding_window_t *w, struct timespec now)
 Called on emit, sets the last emit time.
size_t sliding_window_count (const sliding_window_t *w)
 Get the count of events currently in the window.
struct timespec sliding_window_oldest (const sliding_window_t *w)
 Get the timestamp of the oldest event in the window.
struct timespec sliding_window_newest (const sliding_window_t *w)
 Get the timestamp of the newest event in the window.

Function Documentation

◆ sliding_window_add()

int sliding_window_add ( sliding_window_t * w,
struct timespec event_ts )

Append the event timestamp as ts_node_t to tail. Evicts head while head->ts < (event_ts - window_size_ms).

Parameters
wPointer to the window.
event_tsTimestamp of the event.
Returns
0 on success, -1 on alloc error, -2 on empty sliding window.

◆ sliding_window_count()

size_t sliding_window_count ( const sliding_window_t * w)

Get the count of events currently in the window.

Parameters
wPointer to the window.
Returns
Number of events in the window.

◆ sliding_window_destroy()

void sliding_window_destroy ( sliding_window_t * w)

Graceful cleanup of a window. Frees all ts_node_t in the window, not the window itself.

Parameters
wPointer to the window.

◆ sliding_window_init()

void sliding_window_init ( sliding_window_t * w,
long window_size_ms,
long slide_ms )

Initialize a sliding window.

Parameters
wPointer to the window.
window_size_msWindow span.
slide_msSliding interval.

◆ sliding_window_mark_emitted()

void sliding_window_mark_emitted ( sliding_window_t * w,
struct timespec now )

Called on emit, sets the last emit time.

Parameters
wPointer to the window.
nowCurrent timestamp.

◆ sliding_window_newest()

struct timespec sliding_window_newest ( const sliding_window_t * w)

Get the timestamp of the newest event in the window.

Parameters
wPointer to the window.
Returns
Timespec of the newest event.

◆ sliding_window_oldest()

struct timespec sliding_window_oldest ( const sliding_window_t * w)

Get the timestamp of the oldest event in the window.

Parameters
wPointer to the window.
Returns
Timespec of the oldest event.

◆ sliding_window_should_emit()

int sliding_window_should_emit ( const sliding_window_t * w,
struct timespec now )

Checks if it's time to emit (slide_ms elapsed since last emit).

Parameters
wPointer to the window.
nowCurrent timestamp.
Returns
1 if should emit, 0 otherwise.

◆ timespec_diff_ms()

long timespec_diff_ms ( struct timespec start,
struct timespec end )
static

Helper to calculate difference in milliseconds between two timespecs. Returns long (end - start).