6 #include "system/nth_alloc.h"
8 #include "system/stacktrace.h"
9 #include "undo_history.h"
15 size_t context_data_size;
19 void undo_history_destroy_item(void *item)
21 free(((HistoryItem*)item)->context_data);
24 UndoHistory create_undo_history(void)
27 result.actions = create_ring_buffer(
29 UNDO_HISTORY_CAPACITY,
30 undo_history_destroy_item);
34 void undo_history_push(UndoHistory *undo_history,
37 size_t context_data_size)
39 trace_assert(undo_history);
43 .context_data = malloc(context_data_size),
44 .context_data_size = context_data_size
46 trace_assert(item.context_data);
47 memcpy(item.context_data, context_data, context_data_size);
49 ring_buffer_push(&undo_history->actions, &item);
52 void undo_history_pop(UndoHistory *undo_history)
54 trace_assert(undo_history);
56 if (undo_history->actions.count > 0) {
57 HistoryItem *item = ring_buffer_top(&undo_history->actions);
58 item->revert(item->context_data, item->context_data_size);
59 ring_buffer_pop(&undo_history->actions);