You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
92 lines
2.8 KiB
92 lines
2.8 KiB
// timeout_heap.h |
|
|
|
#ifndef TIMEOUT_HEAP_H |
|
#define TIMEOUT_HEAP_H |
|
|
|
#include <stdint.h> // For uint64_t |
|
#include <stddef.h> // For size_t |
|
|
|
typedef uint64_t TimeoutTime; // e.g., milliseconds since epoch or from now |
|
|
|
typedef struct { |
|
TimeoutTime expiration; // Sort key (smaller = earlier) |
|
void *data; // User data (e.g., callback or ID) |
|
int deleted; // 0 = active, 1 = deleted |
|
} TimeoutEntry; |
|
|
|
typedef struct TimeoutHeap TimeoutHeap; |
|
|
|
struct TimeoutHeap { |
|
TimeoutEntry *heap; // Dynamic array |
|
size_t size; // Current number of elements |
|
size_t capacity; // Allocated size |
|
size_t freed_count; // Number of freed timer nodes |
|
void* user_data; // User data for free callback |
|
void (*free_callback)(void* user_data, void* data); // Callback to free data |
|
}; |
|
|
|
/** |
|
* Create a new timeout heap with initial capacity. |
|
* @param initial_capacity Starting capacity (will grow as needed). |
|
* @return Pointer to the heap, or NULL on failure. |
|
*/ |
|
TimeoutHeap *timeout_heap_create(size_t initial_capacity); |
|
|
|
/** |
|
* Destroy the timeout heap and free resources. |
|
* @param h The heap to destroy. |
|
*/ |
|
void timeout_heap_destroy(TimeoutHeap *h); |
|
|
|
/** |
|
* Set a callback function to free data when deleted nodes are removed. |
|
* @param h The heap. |
|
* @param user_data User data passed to callback. |
|
* @param callback Callback function (if NULL, data is freed with free()). |
|
*/ |
|
void timeout_heap_set_free_callback(TimeoutHeap *h, void* user_data, void (*callback)(void* user_data, void* data)); |
|
|
|
/** |
|
* Insert a new timeout into the heap. |
|
* @param h The heap. |
|
* @param expiration The expiration time. |
|
* @param data User data associated with the timeout. |
|
* @return 0 on success, -1 on allocation failure. |
|
*/ |
|
int timeout_heap_push(TimeoutHeap *h, TimeoutTime expiration, void *data); |
|
|
|
/** |
|
* Peek at the earliest non-deleted timeout without removing it. |
|
* @param h The heap. |
|
* @param out Where to store the entry. |
|
* @return 0 on success, -1 if empty. |
|
*/ |
|
int timeout_heap_peek(TimeoutHeap *h, TimeoutEntry *out); |
|
|
|
/** |
|
* Pop the earliest non-deleted timeout from the heap. |
|
* @param h The heap. |
|
* @param out Where to store the entry. |
|
* @return 0 on success, -1 if empty. |
|
*/ |
|
int timeout_heap_pop(TimeoutHeap *h, TimeoutEntry *out); |
|
|
|
/** |
|
* Cancel a timeout by matching expiration and data. |
|
* Scans the heap linearly, so O(n) time. |
|
* Assumes combinations are unique; cancels the first match. |
|
* @param h The heap. |
|
* @param expiration The expiration time to match. |
|
* @param data The data to match. |
|
* @return 0 if found and canceled, -1 if not found. |
|
*/ |
|
int timeout_heap_cancel(TimeoutHeap *h, TimeoutTime expiration, void *data); |
|
|
|
/** |
|
* Get the number of freed timer nodes. |
|
* @param h The heap. |
|
* @return Count of freed timer nodes. |
|
*/ |
|
size_t timeout_heap_get_freed_count(TimeoutHeap *h); |
|
|
|
#endif // TIMEOUT_HEAP_H
|
|
|