6 #include "system/nth_alloc.h"
7 #include "system/stacktrace.h"
8 #include "undo_history.h"
14 size_t context_data_size;
18 void undo_history_destroy_item(void *item)
20 free(((HistoryItem*)item)->context_data);
23 UndoHistory create_undo_history(void)
26 result.actions = create_ring_buffer(
28 UNDO_HISTORY_CAPACITY,
29 undo_history_destroy_item);
33 void undo_history_push(UndoHistory *undo_history,
36 size_t context_data_size)
38 trace_assert(undo_history);
42 .context_data = malloc(context_data_size),
43 .context_data_size = context_data_size
45 trace_assert(item.context_data);
46 memcpy(item.context_data, context_data, context_data_size);
48 ring_buffer_push(&undo_history->actions, &item);
51 void undo_history_pop(UndoHistory *undo_history)
53 trace_assert(undo_history);
55 if (undo_history->actions.count > 0) {
56 HistoryItem *item = ring_buffer_top(&undo_history->actions);
57 item->revert(item->context_data, item->context_data_size);
58 ring_buffer_pop(&undo_history->actions);
62 void undo_history_clean(UndoHistory *undo_history)
64 trace_assert(undo_history);
66 while (undo_history->actions.count) {
67 ring_buffer_pop(&undo_history->actions);