File: undo.c

package info (click to toggle)
chemtool 1.6.9-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 2,592 kB
  • ctags: 2,185
  • sloc: ansic: 39,342; pascal: 825; sh: 153; makefile: 66
file content (1289 lines) | stat: -rw-r--r-- 28,081 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
/*
    libundo, an easy to use undo/redo management library
    Copyright 1999 Matt Kimball

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/mman.h>


#define BLOCK_FLAG_LARGE	0x01

#define MEMORY_RAW_SIZE(mem)	(*(size_t *)(mem))
#define MEMORY_USED(mem)		(MEMORY_RAW_SIZE(mem) & 1)
#define MEMORY_SIZE(mem)		(MEMORY_RAW_SIZE(mem) & ~1)

#define MEMORY_SET_SIZE_USED(mem, size, used) \
			(MEMORY_RAW_SIZE(mem) = ((size) & ~1) | ((used) ? 1 : 0))

#define MEMORY_NEXT(mem) (MEMORY_OFFSET((mem), MEMORY_SIZE(mem)))
#define MEMORY_BODY(mem) (MEMORY_OFFSET((mem), UNDO_MEMORY_OVERHEAD))

#define FOREACH_SIZED_BLOCK(mem, size, block_ix, block) \
			for((block_ix) = 0, (block) = &(mem)->size##_alloc_list[0]; \
				(block_ix) < (mem)->size##_alloc_list_count; \
				(block_ix)++, (block) = &(mem)->size##_alloc_list[(block_ix)])
#define FOREACH_SMALL_BLOCK(mem, block_ix, block) \
			FOREACH_SIZED_BLOCK(mem, small, block_ix, block)
#define FOREACH_LARGE_BLOCK(mem, block_ix, block) \
			FOREACH_SIZED_BLOCK(mem, large, block_ix, block)

#define MEMORY_BACKUP_OVERHEAD(mem) \
			((mem) = MEMORY_OFFSET((mem), -UNDO_MEMORY_OVERHEAD))

#define BLOCK_END(block) MEMORY_OFFSET((block).mem, (block).size)
#define IN_BLOCK(block, m) ((m) >= (block).mem && (m) < BLOCK_END(block))
#define BLOCK_PAGES(block) ((block).size / getpagesize())
#define FOREACH_IN_BLOCK(block, mem) \
			for((mem) = (block).mem; \
				(mem) != BLOCK_END(block); \
				(mem) = MEMORY_NEXT(mem))

/* Some systems define MAP_ANON instead of MAP_ANONYMOUS. */
/* Some (e.g. Solaris <8) have neither, but  map /dev/zero instead */
#ifndef MAP_ANONYMOUS
#ifdef MAP_ANON
#define MAP_ANONYMOUS MAP_ANON
#else
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
static int dev_zero_fd;
#endif /* MAP_ANON */
#endif /* MAP_ANONYMOUS */

#ifdef MAP_ANONYMOUS
#define MAP_NEW_ANON_AT_FLAGS(pos, size, flags) \
            (mmap((pos), (size), \
			      PROT_READ | PROT_WRITE | PROT_EXEC, \
				  MAP_PRIVATE | MAP_ANONYMOUS | (flags),-1, 0))
#else
#define MAP_NEW_ANON_AT_FLAGS(pos, size, flags) \
            (mmap((pos), (size), \
			      PROT_READ | PROT_WRITE | PROT_EXEC, \
				  MAP_PRIVATE | (flags),dev_zero_fd, 0))
#endif				  
#define MAP_NEW_ANON(size) MAP_NEW_ANON_AT_FLAGS(0, (size), 0)
#define MAP_NEW_ANON_AT(pos, size) \
            MAP_NEW_ANON_AT_FLAGS((pos), (size), MAP_FIXED)
#define PAGE_REMAINDER(size) (getpagesize() - (size))

#define IF_IN_BLOCK_VAR \
			unsigned _if_in_block_ix; \
			UNDO_BLOCK *_if_in_block
#define IF_IN_SMALL_BLOCK(memory, ptr) \
			FOREACH_SMALL_BLOCK((memory), _if_in_block_ix, _if_in_block) \
				if(IN_BLOCK(*_if_in_block, (ptr)))
#define IF_IN_LARGE_BLOCK(memory, ptr) \
			FOREACH_LARGE_BLOCK((memory), _if_in_block_ix, _if_in_block) \
				if((ptr) == _if_in_block->mem)

#define NEW(type) ((type *)calloc(1, sizeof(type)))

#define UNDO_SUCCESS            return UNDO_NOERROR;
#define UNDO_ERROR(err)         return (undo_set_error(err), (err));
#define UNDO_ERROR_NULL(err)    return (undo_set_error(err), (void*)NULL);

#ifdef LIBUNDO
void undo_set_error (int err);

typedef struct _UNDO_HISTORY UNDO_HISTORY;
typedef struct _UNDO_HISTORY_ITEM UNDO_HISTORY_ITEM;



struct _UNDO_HISTORY
{
  UNDO_HISTORY_ITEM *item;
  unsigned length;
  unsigned ix;
  size_t memory_limit;
};

struct _UNDO_HISTORY_ITEM
{
  void *mem;
  size_t size;
};

typedef struct _UNDO_MEMORY_STREAM UNDO_MEMORY_STREAM;
typedef struct _UNDO_MEMORY_STREAM_BLOCK_HEADER
  UNDO_MEMORY_STREAM_BLOCK_HEADER;

#define MEMORY_OFFSET(mem, offset) ((void *)((char *)(mem) + (offset)))

struct _UNDO_MEMORY_STREAM
{
  void (*destroy) (UNDO_MEMORY_STREAM * stream);
    size_t (*read) (UNDO_MEMORY_STREAM * stream, size_t offset,
		    void *mem, size_t count);
  void *implementation;
};

UNDO_HISTORY *undo_history_new (void);
int undo_history_destroy (UNDO_HISTORY * history);

int undo_history_record (UNDO_HISTORY * history, UNDO_MEMORY_STREAM * stream);

UNDO_MEMORY_STREAM *undo_history_undo (UNDO_HISTORY * history);
UNDO_MEMORY_STREAM *undo_history_redo (UNDO_HISTORY * history);

unsigned undo_history_undo_count (UNDO_HISTORY * history);
unsigned undo_history_redo_count (UNDO_HISTORY * history);

void undo_history_set_memory_limit (UNDO_HISTORY * history, size_t limit);
size_t undo_history_memory_usage (UNDO_HISTORY * history);

#include "undo.h"



size_t undo_memory_stream_write (size_t offset, void *mem, size_t size,
				 size_t * pos, void *data, size_t length);
size_t undo_memory_stream_length (UNDO_MEMORY_STREAM * stream);

/*  This might not be the same as sizeof(UNDO_MEMORY_STREAM_BLOCK_HEADER)
	if the compiler pads the structure in some way.  This value represents
	the size of the header when it is serialized in a stream.  */
#define STREAM_SERIALIZED_BLOCK_HEADER_SIZE (sizeof(void *) + \
                                    sizeof(size_t) + sizeof(unsigned))
struct _UNDO_MEMORY_STREAM_BLOCK_HEADER
{
  void *addr;
  size_t size;
  unsigned flags;
};

size_t undo_memory_stream_read_header (UNDO_MEMORY_STREAM * stream,
				       size_t offset,
				       UNDO_MEMORY_STREAM_BLOCK_HEADER *
				       header);

typedef struct _UNDO_BLOCK UNDO_BLOCK;
typedef struct _UNDO_MEMORY UNDO_MEMORY;

struct _UNDO_BLOCK
{
  void *mem;
  size_t size;
};

struct _UNDO_MEMORY
{
  UNDO_BLOCK *small_alloc_list;
  unsigned small_alloc_list_count;

  UNDO_BLOCK *large_alloc_list;
  unsigned large_alloc_list_count;
};

struct _UNDO
{
  char *name;

  UNDO_MEMORY *memory;
  UNDO_HISTORY *history;
};


UNDO_MEMORY *undo_memory_new (void);
int undo_memory_destroy (UNDO_MEMORY * memory);

size_t undo_memory_used (UNDO_MEMORY * memory);
unsigned undo_memory_pages_used (UNDO_MEMORY * memory);

void *undo_memory_alloc (UNDO_MEMORY * memory, size_t size);
int undo_memory_free (UNDO_MEMORY * memory, void *alloc);
size_t undo_memory_size (UNDO_MEMORY * memory, void *alloc);

UNDO_MEMORY_STREAM *undo_memory_stream (UNDO_MEMORY * memory);
int undo_memory_set (UNDO_MEMORY * memory, UNDO_MEMORY_STREAM * stream);

#define UNDO_MEMORY_OVERHEAD (sizeof(size_t))


static UNDO *undo_session = NULL;


int
undo_new (char *session_name)
{
  if (session_name == NULL)
    UNDO_ERROR (UNDO_BADPARAM);

  undo_session = NEW (UNDO);
  if (undo_session == NULL)
    UNDO_ERROR (UNDO_NOMEM);

#ifndef MAP_ANONYMOUS
dev_zero_fd = open("/dev/zero",O_RDONLY);
#endif

  undo_session->name = (char *) malloc (strlen (session_name) + 1);
  if (undo_session->name == NULL)
    {
      undo_destroy ();
      UNDO_ERROR (UNDO_NOMEM);
    }
  strcpy (undo_session->name, session_name);

  undo_session->memory = undo_memory_new ();
  if (undo_session->memory == NULL)
    {
      undo_destroy ();
      UNDO_ERROR (UNDO_NOMEM);
    }

  undo_session->history = undo_history_new ();
  if (undo_session->history == NULL)
    {
      undo_destroy ();
      UNDO_ERROR (UNDO_NOMEM);
    }

  UNDO_SUCCESS;
}

int
undo_destroy (void)
{
  if (undo_session == NULL)
    UNDO_ERROR (UNDO_NOSESSION);

  if (undo_session->name)
    free (undo_session->name);

  if (undo_session->memory)
    undo_memory_destroy (undo_session->memory);

  if (undo_session->history)
    undo_history_destroy (undo_session->history);

  undo_session = NULL;
#ifndef MAP_ANONYMOUS
	close(dev_zero_fd);
#endif
	
  UNDO_SUCCESS;
}

UNDO *
undo_get_session (void)
{
  return undo_session;
}

int
undo_set_session (UNDO * undo)
{
  undo_session = undo;

  UNDO_SUCCESS;
}

int
undo_set_memory_limit (size_t max_memory)
{
  if (undo_session == NULL)
    UNDO_ERROR (UNDO_NOSESSION);

  undo_history_set_memory_limit (undo_session->history, max_memory);

  UNDO_SUCCESS;
}

unsigned
undo_get_undo_count (void)
{
  if (undo_session == NULL)
    return 0;

  return undo_history_undo_count (undo_session->history);
}

unsigned
undo_get_redo_count (void)
{
  if (undo_session == NULL)
    return 0;

  return undo_history_redo_count (undo_session->history);
}

int
undo_undo (void)
{
  UNDO_MEMORY_STREAM *stream;
  int ret;

  if (undo_session == NULL)
    UNDO_ERROR (UNDO_NOSESSION);

  if (undo_history_undo_count (undo_session->history) == 0)
    UNDO_ERROR (UNDO_NODO);

  stream = undo_history_undo (undo_session->history);
  if (stream == NULL)
    UNDO_ERROR (UNDO_NOMEM);
  ret = undo_memory_set (undo_session->memory, stream);
  stream->destroy (stream);

  return ret;
}

int
undo_redo (void)
{
  UNDO_MEMORY_STREAM *stream;
  int ret;

  if (undo_session == NULL)
    UNDO_ERROR (UNDO_NOSESSION);

  if (undo_history_redo_count (undo_session->history) == 0)
    UNDO_ERROR (UNDO_NODO);

  stream = undo_history_redo (undo_session->history);
  if (stream == NULL)
    UNDO_ERROR (UNDO_NOMEM);
  ret = undo_memory_set (undo_session->memory, stream);
  stream->destroy (stream);

  return ret;
}

int
undo_snapshot (void)
{
  UNDO_MEMORY_STREAM *stream;
  int ret;

  if (undo_session == NULL)
    UNDO_ERROR (UNDO_NOSESSION);

  stream = undo_memory_stream (undo_session->memory);
  if (stream == NULL)
    UNDO_ERROR (UNDO_NOMEM);
  ret = undo_history_record (undo_session->history, stream);
  stream->destroy (stream);

  return ret;
}

void *
undo_malloc (size_t size)
{
  if (undo_session == NULL)
    UNDO_ERROR_NULL (UNDO_NOSESSION);

  return undo_memory_alloc (undo_session->memory, size);
}

void *
undo_realloc (void *mem, size_t size)
{
  size_t min_size;
  void *new_mem;

  if (undo_session == NULL)
    UNDO_ERROR_NULL (UNDO_NOSESSION);

  if (mem == NULL)
    return undo_memory_alloc (undo_session->memory, size);

  min_size = undo_memory_size (undo_session->memory, mem);
  if (size < min_size)
    min_size = size;

  if (size == min_size)
    return mem;

  new_mem = undo_memory_alloc (undo_session->memory, size);
  if (new_mem == NULL)
    UNDO_ERROR_NULL (UNDO_NOMEM);

  memcpy (new_mem, mem, min_size);
  undo_memory_free (undo_session->memory, mem);

  return new_mem;
}

void
undo_free (void *mem)
{
  if (undo_session == NULL)
    return;

  undo_memory_free (undo_session->memory, mem);
}

static void *undo_memory_alloc_small (UNDO_MEMORY * memory, size_t size);
static void *undo_memory_alloc_large (UNDO_MEMORY * memory, size_t size);
static void *undo_memory_alloc_small_block (UNDO_BLOCK * block, size_t size);
static void undo_memory_block_coalesce_free (UNDO_BLOCK * block);
static void *undo_memory_alloc_new_small_block (UNDO_MEMORY * mem,
						size_t size);
static void *undo_memory_new_block (UNDO_BLOCK ** block_list,
				    unsigned *length, size_t size);
static int undo_memory_new_block_record (UNDO_BLOCK ** block_list,
					 unsigned *len, void *mem,
					 size_t size);
static int undo_memory_delete_block (UNDO_BLOCK ** block_list,
				     unsigned *length, unsigned index);

static void undo_stream_destroy (UNDO_MEMORY_STREAM * stream);
static size_t undo_stream_read (UNDO_MEMORY_STREAM * stream, size_t offset,
				void *mem, size_t size);
static size_t undo_stream_block_write (size_t offset, void *mem, size_t size,
				       size_t * pos,
				       UNDO_BLOCK * block, unsigned flags);
static void undo_memory_clear (UNDO_MEMORY * memory);
static int undo_memory_add_blocks_from_stream (UNDO_MEMORY * memory,
					       UNDO_MEMORY_STREAM * stream);
static void undo_memory_block_header_record (UNDO_MEMORY * memory,
					     UNDO_MEMORY_STREAM_BLOCK_HEADER *
					     header);

UNDO_MEMORY *
undo_memory_new (void)
{
  UNDO_MEMORY *mem;

  mem = NEW (UNDO_MEMORY);
  if (mem == NULL)
    UNDO_ERROR_NULL (UNDO_NOMEM);

  return mem;
}

int
undo_memory_destroy (UNDO_MEMORY * memory)
{
  if (memory == NULL)
    UNDO_ERROR (UNDO_BADPARAM);

  undo_memory_clear (memory);
  free (memory);

  UNDO_SUCCESS;
}

size_t
undo_memory_used (UNDO_MEMORY * memory)
{
  size_t used;
  unsigned block_ix;
  UNDO_BLOCK *block;

  used = 0;
  FOREACH_SMALL_BLOCK (memory, block_ix, block)
  {
    void *mem;

    FOREACH_IN_BLOCK (*block, mem)
    {
      if (MEMORY_USED (mem))
	{
	  used += MEMORY_SIZE (mem);
	}
    }
  }

  FOREACH_LARGE_BLOCK (memory, block_ix, block)
  {
    used += block->size;
  }

  return used;
}

void *
undo_memory_alloc (UNDO_MEMORY * memory, size_t size)
{
  while (size & (sizeof (size_t) - 1))
    size++;

  if (size < getpagesize () - UNDO_MEMORY_OVERHEAD * 2)
    {
      return undo_memory_alloc_small (memory, size);
    }
  else
    {
      return undo_memory_alloc_large (memory, size);
    }
}

int
undo_memory_free (UNDO_MEMORY * memory, void *alloc)
{
  IF_IN_BLOCK_VAR;

  IF_IN_SMALL_BLOCK (memory, alloc)
  {
    MEMORY_BACKUP_OVERHEAD (alloc);
    MEMORY_SET_SIZE_USED (alloc, MEMORY_SIZE (alloc), 0);
    UNDO_SUCCESS;
  }

  IF_IN_LARGE_BLOCK (memory, alloc)
  {
    return undo_memory_delete_block (&memory->large_alloc_list,
				     &memory->large_alloc_list_count,
				     _if_in_block_ix);
  }

  UNDO_ERROR (UNDO_BADPARAM);
}

size_t
undo_memory_size (UNDO_MEMORY * memory, void *alloc)
{
  IF_IN_BLOCK_VAR;

  IF_IN_SMALL_BLOCK (memory, alloc)
  {
    MEMORY_BACKUP_OVERHEAD (alloc);
    return MEMORY_SIZE (alloc) - UNDO_MEMORY_OVERHEAD;
  }

  IF_IN_LARGE_BLOCK (memory, alloc)
  {
    return _if_in_block->size;
  }

  return 0;
}

unsigned
undo_memory_pages_used (UNDO_MEMORY * memory)
{
  unsigned block_ix;
  UNDO_BLOCK *block;
  unsigned pages;

  pages = 0;

  FOREACH_SMALL_BLOCK (memory, block_ix, block)
  {
    pages += BLOCK_PAGES (*block);
  }

  FOREACH_LARGE_BLOCK (memory, block_ix, block)
  {
    pages += BLOCK_PAGES (*block);
  }

  return pages;
}

UNDO_MEMORY_STREAM *
undo_memory_stream (UNDO_MEMORY * memory)
{
  UNDO_MEMORY_STREAM *stream;

  stream = NEW (UNDO_MEMORY_STREAM);
  if (stream == NULL)
    UNDO_ERROR_NULL (UNDO_NOMEM);

  stream->implementation = memory;
  stream->destroy = (void *) undo_stream_destroy;
  stream->read = (void *) undo_stream_read;

  return (UNDO_MEMORY_STREAM *) stream;
}

int
undo_memory_set (UNDO_MEMORY * memory, UNDO_MEMORY_STREAM * stream)
{
  undo_memory_clear (memory);
  return undo_memory_add_blocks_from_stream (memory, stream);
}

static void *
undo_memory_alloc_small (UNDO_MEMORY * memory, size_t size)
{
  unsigned block_ix;
  UNDO_BLOCK *block;

  FOREACH_SMALL_BLOCK (memory, block_ix, block)
  {
    void *mem;

    mem = undo_memory_alloc_small_block (block, size);
    if (mem != NULL)
      {
	return mem;
      }
  }

  return undo_memory_alloc_new_small_block (memory, size);
}

static void *
undo_memory_alloc_large (UNDO_MEMORY * memory, size_t size)
{
  size_t page_fraction;

  page_fraction = size & (getpagesize () - 1);
  if (page_fraction)
    {
      size += PAGE_REMAINDER (page_fraction);
    }

  return undo_memory_new_block (&memory->large_alloc_list,
				&memory->large_alloc_list_count, size);
}

static void *
undo_memory_alloc_small_block (UNDO_BLOCK * block, size_t size)
{
  void *mem;
  size_t new_size;

  undo_memory_block_coalesce_free (block);

  new_size = size + UNDO_MEMORY_OVERHEAD;
  FOREACH_IN_BLOCK (*block, mem)
  {
    if (!MEMORY_USED (mem) && MEMORY_SIZE (mem) >= new_size)
      {
	if (MEMORY_SIZE (mem) == new_size)
	  {
	    MEMORY_SET_SIZE_USED (mem, new_size, 1);
	  }
	else
	  {
	    size_t old_size;
	    void *next;

	    old_size = MEMORY_SIZE (mem);
	    MEMORY_SET_SIZE_USED (mem, new_size, 1);
	    next = MEMORY_NEXT (mem);
	    MEMORY_SET_SIZE_USED (next, old_size - new_size, 0);
	  }
	return MEMORY_BODY (mem);
      }
  }

  return NULL;
}

static void
undo_memory_block_coalesce_free (UNDO_BLOCK * block)
{
  void *last, *mem;

  last = NULL;
  FOREACH_IN_BLOCK (*block, mem)
  {
    if (last == NULL)
      {
	last = mem;
	continue;
      }

    if (!MEMORY_USED (last) && !MEMORY_USED (mem))
      {
	MEMORY_SET_SIZE_USED (last,
			      MEMORY_SIZE (last) + MEMORY_SIZE (mem), 0);
	continue;
      }

    last = mem;
  }
}

static void *
undo_memory_alloc_new_small_block (UNDO_MEMORY * mem, size_t size)
{
  void *page, *next;

  page = undo_memory_new_block (&mem->small_alloc_list,
				&mem->small_alloc_list_count, getpagesize ());
  if (page == NULL)
    {
      UNDO_ERROR_NULL (UNDO_NOMEM);
    }

  MEMORY_SET_SIZE_USED (page, size + UNDO_MEMORY_OVERHEAD, 1);
  next = MEMORY_NEXT (page);
  MEMORY_SET_SIZE_USED (next, PAGE_REMAINDER (size + UNDO_MEMORY_OVERHEAD),
			0);

  return MEMORY_BODY (page);
}

static void *
undo_memory_new_block (UNDO_BLOCK ** block_list, unsigned *len, size_t size)
{
  void *mem;

  mem = MAP_NEW_ANON (size);
  if (mem == NULL)
    UNDO_ERROR_NULL (UNDO_NOMEM);

  if (undo_memory_new_block_record (block_list, len, mem, size))
    {
      munmap (mem, size);
      return NULL;
    }

  return mem;
}

static int
undo_memory_new_block_record (UNDO_BLOCK ** block_list, unsigned *len,
			      void *mem, size_t size)
{
  UNDO_BLOCK *new_block_list;


  new_block_list = (UNDO_BLOCK *) realloc (*block_list,
					   (*len + 1) * sizeof (UNDO_BLOCK));
  if (new_block_list == NULL)
    {
      UNDO_ERROR (UNDO_NOMEM);
    }
  *block_list = new_block_list;
  (*block_list)[*len].mem = mem;
  (*block_list)[*len].size = size;
  (*len)++;

  UNDO_SUCCESS;
}

static int
undo_memory_delete_block (UNDO_BLOCK ** block_list, unsigned *length,
			  unsigned index)
{
  UNDO_BLOCK *block;
  UNDO_BLOCK *new_block_list;

  block = &(*block_list)[index];
  munmap (block->mem, block->size);

  if (*length == 1)
    {
      free (*block_list);
      new_block_list = NULL;
    }
  else
    {
      memcpy (&(*block_list)[index], &(*block_list)[index + 1],
	      sizeof (UNDO_BLOCK) * (*length - 1 - index));
      new_block_list = realloc (*block_list,
				sizeof (UNDO_BLOCK) * (*length - 1));
      if (new_block_list == NULL)
	{
	  (*length)--;
	  UNDO_SUCCESS;
	}
    }
  *block_list = new_block_list;
  (*length)--;

  UNDO_SUCCESS;
}


static void
undo_stream_destroy (UNDO_MEMORY_STREAM * stream)
{
  free (stream);
}

static size_t
undo_stream_read (UNDO_MEMORY_STREAM * stream, size_t offset,
		  void *mem, size_t size)
{
  unsigned block_ix;
  UNDO_BLOCK *block;
  UNDO_MEMORY *memory;
  size_t ret;
  size_t pos;
  unsigned flags;

  memory = (UNDO_MEMORY *) stream->implementation;

  ret = 0;
  pos = 0;
  flags = 0;
  FOREACH_SMALL_BLOCK (memory, block_ix, block)
  {
    ret += undo_stream_block_write (offset, mem, size, &pos, block, flags);
  }

  flags = BLOCK_FLAG_LARGE;
  FOREACH_LARGE_BLOCK (memory, block_ix, block)
  {
    ret += undo_stream_block_write (offset, mem, size, &pos, block, flags);
  }

  return ret;
}

static size_t
undo_stream_block_write (size_t offset, void *mem, size_t size,
			 size_t * pos, UNDO_BLOCK * block, unsigned flags)
{
  size_t ret;

  ret = 0;
  ret += undo_memory_stream_write (offset, mem, size,
				   pos, &block->mem, sizeof (void *));
  ret += undo_memory_stream_write (offset, mem, size,
				   pos, &block->size, sizeof (size_t));
  ret += undo_memory_stream_write (offset, mem, size,
				   pos, &flags, sizeof (unsigned));
  ret += undo_memory_stream_write (offset, mem, size,
				   pos, block->mem, block->size);

  return ret;
}

static void
undo_memory_clear (UNDO_MEMORY * memory)
{
  unsigned block_ix;
  UNDO_BLOCK *block;

  FOREACH_SMALL_BLOCK (memory, block_ix, block)
  {
    munmap (block->mem, block->size);
  }
  FOREACH_LARGE_BLOCK (memory, block_ix, block)
  {
    munmap (block->mem, block->size);
  }

  memset (memory, 0, sizeof (UNDO_MEMORY));
}

static int
undo_memory_add_blocks_from_stream (UNDO_MEMORY * memory,
				    UNDO_MEMORY_STREAM * stream)
{
  size_t pos, num_read;
  UNDO_MEMORY_STREAM_BLOCK_HEADER header;

  pos = 0;
  do
    {
      num_read = undo_memory_stream_read_header (stream, pos, &header);
      if (num_read != STREAM_SERIALIZED_BLOCK_HEADER_SIZE)
	{
	  UNDO_SUCCESS;
	}
      pos += num_read;

      if (MAP_NEW_ANON_AT (header.addr, header.size) != header.addr)
	{
	  UNDO_ERROR (UNDO_NOMEM);
	}
      undo_memory_block_header_record (memory, &header);

      num_read = stream->read (stream, pos, header.addr, header.size);
      pos += num_read;
    }
  while (1);
}

static void
undo_memory_block_header_record (UNDO_MEMORY * memory,
				 UNDO_MEMORY_STREAM_BLOCK_HEADER * header)
{
  UNDO_BLOCK **undo_block_list;
  unsigned *undo_block_list_count;

  if (header->flags & BLOCK_FLAG_LARGE)
    {
      undo_block_list = &memory->large_alloc_list;
      undo_block_list_count = &memory->large_alloc_list_count;
    }
  else
    {
      undo_block_list = &memory->small_alloc_list;
      undo_block_list_count = &memory->small_alloc_list_count;
    }

  undo_memory_new_block_record (undo_block_list, undo_block_list_count,
				header->addr, header->size);
}

size_t
undo_memory_stream_write (size_t offset, void *mem, size_t size,
			  size_t * pos, void *data, size_t length)
{
  size_t begin_mem, end_mem;
  size_t begin_data;

  if (offset < *pos)
    {
      begin_mem = *pos - offset;
      begin_data = 0;
    }
  else
    {
      begin_mem = 0;
      begin_data = offset - *pos;
    }

  if (offset + size < *pos + length)
    {
      end_mem = size;
    }
  else
    {
      end_mem = size - (offset + size - *pos - length);
    }

  *pos += length;

  if ((signed) end_mem < 0)
    return 0;
  if (end_mem <= begin_mem)
    return 0;

  memcpy (MEMORY_OFFSET (mem, begin_mem),
	  MEMORY_OFFSET (data, begin_data), end_mem - begin_mem);

  return end_mem - begin_mem;
}

size_t
undo_memory_stream_length (UNDO_MEMORY_STREAM * stream)
{
  size_t len, diff;
  char buff[4096];

  len = 0;
  do
    {
      diff = stream->read (stream, len, buff, 4096);
      len += diff;
    }
  while (diff != 0);

  return len;
}


size_t
undo_memory_stream_read_header (UNDO_MEMORY_STREAM * stream,
				size_t offset,
				UNDO_MEMORY_STREAM_BLOCK_HEADER * header)
{
  size_t num_read;

  num_read = 0;
  num_read += stream->read (stream, offset + num_read,
			    &header->addr, sizeof (void *));
  num_read += stream->read (stream, offset + num_read,
			    &header->size, sizeof (size_t));
  num_read += stream->read (stream, offset + num_read,
			    &header->flags, sizeof (unsigned));

  return num_read;
}




static void undo_history_stream_destroy (UNDO_MEMORY_STREAM * stream);
static size_t undo_history_stream_read (UNDO_MEMORY_STREAM * stream,
					size_t offset,
					void *mem, size_t size);
static int undo_history_add_item (UNDO_HISTORY * history,
				  void *mem, size_t size);
static UNDO_MEMORY_STREAM *undo_history_stream (UNDO_HISTORY * history);
static void undo_history_shrink (UNDO_HISTORY * history, size_t size);
static void undo_history_strip (UNDO_HISTORY * history, int count);

UNDO_HISTORY *
undo_history_new (void)
{
  UNDO_HISTORY *history;

  history = NEW (UNDO_HISTORY);
  if (history == NULL)
    UNDO_ERROR_NULL (UNDO_NOMEM);

  return history;
}

int
undo_history_destroy (UNDO_HISTORY * history)
{
  if (history == NULL)
    UNDO_ERROR (UNDO_BADPARAM);

  free (history);

  UNDO_SUCCESS;
}

int
undo_history_record (UNDO_HISTORY * history, UNDO_MEMORY_STREAM * stream)
{
  void *mem;
  size_t size;
  int ret;

  if (history->memory_limit == 0)
    UNDO_ERROR (UNDO_NOLIMIT);

  size = undo_memory_stream_length (stream);
  if (size > history->memory_limit)
    {
      undo_history_shrink (history, 0);
      UNDO_SUCCESS;
    }
  undo_history_shrink (history, history->memory_limit - size);

  mem = (void *) malloc (size);
  if (mem == NULL)
    UNDO_ERROR (UNDO_NOMEM);
  stream->read (stream, 0, mem, size);

  ret = undo_history_add_item (history, mem, size);
  if (ret)
    {
      free (mem);
    }

  return ret;
}

UNDO_MEMORY_STREAM *
undo_history_undo (UNDO_HISTORY * history)
{
  UNDO_MEMORY_STREAM *stream;

  if (history->ix == 0)
    UNDO_ERROR_NULL (UNDO_NODO);

  stream = undo_history_stream (history);

  if (stream)
    history->ix--;

  return stream;
}

UNDO_MEMORY_STREAM *
undo_history_redo (UNDO_HISTORY * history)
{
  UNDO_MEMORY_STREAM *stream;

  if (history->ix >= history->length - 1)
    UNDO_ERROR_NULL (UNDO_NODO);

  stream = undo_history_stream (history);

  if (stream)
    history->ix++;

  return stream;
}

unsigned
undo_history_undo_count (UNDO_HISTORY * history)
{
  if (history->length)
    return history->ix;

  return 0;
}

unsigned
undo_history_redo_count (UNDO_HISTORY * history)
{
  if (history->length)
    return history->length - 1 - history->ix;

  return 0;
}

void
undo_history_set_memory_limit (UNDO_HISTORY * history, size_t limit)
{
  history->memory_limit = limit;
}

size_t
undo_history_memory_usage (UNDO_HISTORY * history)
{
  int ix;
  size_t total;

  total = 0;
  for (ix = 0; ix < history->length; ix++)
    {
      total += history->item[ix].size;
    }

  return total;
}

static void
undo_history_stream_destroy (UNDO_MEMORY_STREAM * stream)
{
  free (stream);
}

static size_t
undo_history_stream_read (UNDO_MEMORY_STREAM * stream,
			  size_t offset, void *mem, size_t size)
{
  UNDO_HISTORY *history;
  size_t pos;
  void *history_mem;
  size_t history_size;

  history = (UNDO_HISTORY *) stream->implementation;

  pos = 0;
  history_mem = history->item[history->ix].mem;
  history_size = history->item[history->ix].size;
  return undo_memory_stream_write (offset, mem, size,
				   &pos, history_mem, history_size);
}

static int
undo_history_add_item (UNDO_HISTORY * history, void *mem, size_t size)
{
  UNDO_HISTORY_ITEM *new_item;

  new_item = realloc (history->item,
		      sizeof (UNDO_HISTORY_ITEM) * (history->length + 1));
  if (new_item == NULL)
    {
      UNDO_ERROR (UNDO_NOMEM);
    }
  history->item = new_item;

  history->length++;

  history->ix = history->length - 1;
  history->item[history->ix].mem = mem;
  history->item[history->ix].size = size;

  UNDO_SUCCESS;
}

static UNDO_MEMORY_STREAM *
undo_history_stream (UNDO_HISTORY * history)
{
  UNDO_MEMORY_STREAM *stream;

  stream = NEW (UNDO_MEMORY_STREAM);
  if (stream == NULL)
    UNDO_ERROR_NULL (UNDO_NOMEM);

  stream->implementation = history;
  stream->destroy = undo_history_stream_destroy;
  stream->read = undo_history_stream_read;

  return stream;
}

static void
undo_history_shrink (UNDO_HISTORY * history, size_t size)
{
  int first_saved;
  int ix;
  size_t total;

  total = 0;
  first_saved = 0;
  for (ix = 0; ix < history->length; ix++)
    {
      total += history->item[ix].size;
      while (total > size)
	{
	  total -= history->item[first_saved].size;
	  first_saved++;
	}
    }

  undo_history_strip (history, first_saved);
}

static void
undo_history_strip (UNDO_HISTORY * history, int count)
{
  int ix;

  if (count == 0)
    return;

  for (ix = 0; ix < count; ix++)
    {
      free (history->item[ix].mem);
    }

  memmove (history->item, history->item + count,
	   sizeof (UNDO_HISTORY_ITEM) * (history->length - count));
  history->length -= count;
  history->ix = history->length - 1;
}


static int undo_last_error = UNDO_NOERROR;

void
undo_set_error (int err)
{
  undo_last_error = err;
}

int
undo_get_errcode (void)
{
  int ret;

  ret = undo_last_error;
  undo_last_error = UNDO_NOERROR;
  return ret;
}

char *
undo_strerror (int errcode)
{
  char *err_string[] = {
    "No error",
    "Bad parameter passed to undo function",
    "Out of memory",
    "No active undo session",
    "Nothing to undo/redo",
    "No undoable memory limit set"
  };

  if (errcode < 0)
    return NULL;

  if (errcode >= sizeof (err_string) / sizeof (char *))
    return NULL;

  return err_string[errcode];
}
#endif