6 #include "system/nth_alloc.h"
7 #include "system/stacktrace.h"
8 #include "undo_history.h"
14 size_t context_data_size;
17 UndoHistory *create_undo_history(Memory *memory)
19 UndoHistory *result = memory_alloc(memory, sizeof(UndoHistory));
20 result->actions = create_ring_buffer_from_buffer(
23 UNDO_HISTORY_CAPACITY);
24 result->memory = memory;
28 void undo_history_push(UndoHistory *undo_history,
31 size_t context_data_size)
33 trace_assert(undo_history);
35 // TODO(#1244): undo_history_push kinda leaks the memory
38 .context_data = memory_alloc(undo_history->memory, context_data_size),
39 .context_data_size = context_data_size
41 memcpy(item.context_data, context_data, context_data_size);
42 ring_buffer_push(&undo_history->actions, &item);
45 void undo_history_pop(UndoHistory *undo_history)
47 trace_assert(undo_history);
49 if (undo_history->actions.count > 0) {
50 HistoryItem *item = ring_buffer_top(&undo_history->actions);
51 item->revert(item->context_data, item->context_data_size);
52 ring_buffer_pop(&undo_history->actions);
56 void undo_history_clean(UndoHistory *undo_history)
58 trace_assert(undo_history);
60 while (undo_history->actions.count) {
61 ring_buffer_pop(&undo_history->actions);