-
Notifications
You must be signed in to change notification settings - Fork 5
/
mapped_file.c
1017 lines (773 loc) · 22.7 KB
/
mapped_file.c
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
/* pyrsistence - A Python extension for External Memory Data Structures (EMDs)
* huku <[email protected]>
*
* mapped_file.c - Simple API for memory mapped files.
*
* My original intention was to expose an API for memory mapped files similar to
* this for `FILE' objects on most modern operating systems. However, the various
* incompatibilities in the representation of `off_t', `fpos_t' and other data
* types between the various operating systems, rendered this task impossible.
* For example, at first, I assumed that `sizeof(off_t) >= sizeof(size_t)' on
* most (all?) POSIX systems, but this was not the case on Microsoft Windows.
* Using `fpos_t' instead of `off_t' is not a solution; the man pages on UNIX
* systems clearly state that `fpos_t' is not necessarily a primitive type.
*
* In the following, I decided use `ssize_t' instead of `off_t' and `size_t'
* instead of `fpos_t'.
*
* Bewarned that this code is still experimental and various integer related
* bugs may be present. Let me know if you happen to hit any!
*/
#ifndef _WIN32
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#endif
#include "util.h"
#include "marshaller.h"
#include "mapped_file.h"
/* Holes in the memory mapped buffer are ordered by size in a red-black tree. */
static int hole_cmp(const void *a, const void *b)
{
hole_t *ha = (hole_t *)a;
hole_t *hb = (hole_t *)b;
int ret = 0;
if(ha->size < hb->size)
ret = -1;
if(ha->size > hb->size)
ret = 1;
return ret;
}
/* Allocate, initialize and return a new mapped file object. */
static mapped_file_t *mapped_file_alloc(const char *filename)
{
mapped_file_t *mf;
if((mf = PyMem_MALLOC(sizeof(mapped_file_t))) == NULL)
goto _err1;
memset(mf, 0, sizeof(mapped_file_t));
if((mf->holes = rbtree_alloc(hole_cmp)) == NULL)
goto _err2;
if((mf->filename = PyMem_MALLOC(strlen(filename) + 1)) == NULL)
goto _err3;
strcpy(mf->filename, filename);
return mf;
_err3:
PyMem_FREE(mf->holes);
_err2:
PyMem_FREE(mf);
_err1:
return NULL;
}
/* Deallocate a mapped file object. */
static void mapped_file_free(mapped_file_t *mf)
{
PyMem_FREE(mf->filename);
rbtree_free(mf->holes);
PyMem_FREE(mf);
}
/* Make sure it's safe to access `size' bytes starting from position `pos'. */
static int mapped_file_check_range(mapped_file_t *mf, size_t pos,
size_t size)
{
return (size <= SSIZE_MAX && pos <= SSIZE_MAX && pos + size <= mf->size);
}
/* Make sure we can write at least `size' bytes starting from position `pos' in
* mapped file `mf'. Mapped file may be resized to fulfil the request.
*/
static int mapped_file_ensure_range(mapped_file_t *mf, size_t pos,
size_t size)
{
size_t mf_size = mf->size;
int ret = -1;
if(size > SSIZE_MAX || pos > SSIZE_MAX)
goto _err;
if(mf_size < pos + size)
{
while(mf_size < pos + size)
{
if(mf_size + (mf_size >> 1) < mf_size)
goto _err;
mf_size += mf_size >> 1;
}
if(mapped_file_truncate(mf, mf_size) != 0)
goto _err;
}
ret = 0;
_err:
return ret;
}
/* Equivalent to `read()' for memory mapped files. */
ssize_t mapped_file_read(mapped_file_t *mf, void *buf, size_t size)
{
size_t mf_pos = mf->pos;
ssize_t ret = -1;
if(mapped_file_check_range(mf, mf_pos, size) == 0)
goto _err;
memcpy(buf, (char *)mf->address + mf_pos, size);
mf->pos += size;
ret = size;
_err:
return ret;
}
/* Equivalent to `write()' for memory mapped files. */
ssize_t mapped_file_write(mapped_file_t *mf, void *buf, size_t size)
{
size_t mf_pos = mf->pos;
ssize_t ret = -1;
if(mapped_file_ensure_range(mf, mf_pos, size) != 0)
goto _err;
memcpy((char *)mf->address + mf_pos, buf, size);
mf_pos += size;
if(mf_pos > mf->eof)
mf->eof = mf_pos;
mf->pos = mf_pos;
ret = size;
_err:
return ret;
}
/* Safe `memset()' of a mapped file's contents. Sets `size' bytes starting from
* position `pos'.
*/
int mapped_file_memset(mapped_file_t *mf, int c, size_t size)
{
size_t mf_pos = mf->pos;
int ret = -1;
if(mapped_file_ensure_range(mf, mf_pos, size) != 0)
goto _err;
memset((char *)mf->address + mf_pos, c, size);
mf_pos += size;
if(mf_pos > mf->eof)
mf->eof = mf_pos;
mf->pos = mf_pos;
ret = 0;
_err:
return ret;
}
/* Equivalent to `fseek()' for memory mapped files. */
int mapped_file_seek(mapped_file_t *mf, ssize_t off, int whence)
{
int ret = -1;
if(whence == SEEK_CUR)
off += mf->pos;
else if(whence == SEEK_END)
off += mf->size;
/* Remember that `mf->size <= SSIZE_MAX' in `map_file()'. */
if(off >= 0 && (size_t)off <= mf->size)
{
mf->pos = (size_t)off;
ret = 0;
}
return ret;
}
/* Equivalent to `ftell()' for memory mapped files. */
size_t mapped_file_tell(mapped_file_t *mf)
{
return mf->pos;
}
/* Get mapped buffer size of mapped file `mf'. */
size_t mapped_file_get_size(mapped_file_t *mf)
{
return mf->size;
}
/* Get current EOF position of mapped file `mf'. */
size_t mapped_file_get_eof(mapped_file_t *mf)
{
return mf->eof;
}
/* Return the position of a chunk of size `size' in memory mapped file `mf'.
* It's safe to seek there and write `size' bytes. On error -1 is returned.
*/
ssize_t mapped_file_allocate_chunk(mapped_file_t *mf, size_t size)
{
hole_t hole;
rbnode_t *node;
size_t pos;
ssize_t ret = -1;
if(size > SSIZE_MAX)
goto _err;
size = HOLE_SIZE(size);
hole.pos = 0;
hole.size = size;
node = rbtree_find_node(mf->holes, &hole);
if(node == NULL)
{
pos = mf->eof;
if(mapped_file_seek(mf, pos, SEEK_SET) != 0)
goto _err;
if(mapped_file_write(mf, &size, sizeof(size_t)) != sizeof(size_t))
goto _err;
size -= sizeof(size_t);
if(mapped_file_memset(mf, 0, size) != 0)
goto _err;
}
else
{
pos = ((hole_t *)node->data)->pos;
PyMem_FREE(node->data);
rbtree_delete_node(mf->holes, node);
}
ret = (ssize_t)(pos + sizeof(size_t));
_err:
return ret;
}
/* Mark the chunk at position `pos' in mapped file `mf' as free. Chunk must have
* been allocated using `mapped_file_allocate_chunk()'.
*/
void mapped_file_free_chunk(mapped_file_t *mf, size_t pos)
{
hole_t *hole;
size_t size;
pos -= sizeof(size_t);
if(mapped_file_seek(mf, pos, SEEK_SET) != 0)
goto _err;
if(mapped_file_read(mf, &size, sizeof(size_t)) != sizeof(size_t))
goto _err;
if(mapped_file_check_range(mf, pos, size) == 0)
goto _err;
if((hole = PyMem_MALLOC(sizeof(hole_t))) == NULL)
goto _err;
hole->pos = pos;
hole->size = size;
rbtree_insert_node(mf->holes, hole);
_err:
return;
}
/* Allocate a chunk of appropriate size from mapped file `mf' and marshal Python
* string object `obj' in it.
*/
static ssize_t mapped_file_marshal_string_object(mapped_file_t *mf,
PyObject *obj)
{
Py_ssize_t size;
char *data;
ssize_t pos, ret = -1;
#if PY_MAJOR_VERSION >= 3
if(PyBytes_AsStringAndSize(obj, &data, &size) == -1)
goto _err;
#else
if(PyString_AsStringAndSize(obj, &data, &size) == -1)
goto _err;
#endif
if((pos = mapped_file_allocate_chunk(mf, (size_t)size)) < 0)
goto _err;
if((size_t)pos != mapped_file_tell(mf) &&
mapped_file_seek(mf, pos, SEEK_SET) != 0)
{
mapped_file_free_chunk(mf, pos);
goto _err;
}
if(mapped_file_write(mf, data, size) != size)
{
mapped_file_free_chunk(mf, pos);
goto _err;
}
ret = pos;
_err:
return ret;
}
/* Allocate a chunk of appropriate size from mapped file `mf' and marshal Python
* object `obj' in it.
*/
ssize_t mapped_file_marshal_object(em_common_t *em_obj, mapped_file_t *mf,
PyObject *obj)
{
PyObject *str;
ssize_t pos = -1;
if((str = marshal(em_obj, obj)) == NULL)
goto _err;
pos = mapped_file_marshal_string_object(mf, str);
Py_DECREF(str);
_err:
return pos;
}
/* Return the size of chunk at position `pos' in mapped file `mf'. Chunk must
* have been allocated using `mapped_file_allocate_chunk()'.
*/
static ssize_t mapped_file_get_chunk_size(mapped_file_t *mf, size_t pos)
{
size_t size;
ssize_t ret = -1;
pos -= sizeof(size_t);
if(pos != mapped_file_tell(mf) &&
mapped_file_seek(mf, (ssize_t)pos, SEEK_SET) != 0)
goto _err;
if(mapped_file_read(mf, &size, sizeof(size_t)) != sizeof(size_t))
goto _err;
ret = (ssize_t)(size - sizeof(size_t));
_err:
return ret;
}
/* Unmarshal a Python object from position `pos' in memory mapped file `mf'. */
PyObject *mapped_file_unmarshal_object(em_common_t *em_obj, mapped_file_t *mf,
size_t pos)
{
ssize_t size;
PyObject *str, *ret = NULL;
/* The call to `mapped_file_get_chunk_size()' will seek to the correct
* position in the memory mapped file.
*/
if((size = mapped_file_get_chunk_size(mf, pos)) < 0)
goto _err;
#if PY_MAJOR_VERSION >= 3
if((str = PyBytes_FromStringAndSize((char *)mf->address + pos, size)) == NULL)
goto _err;
#else
if((str = PyString_FromStringAndSize((char *)mf->address + pos, size)) == NULL)
goto _err;
#endif
ret = unmarshal(em_obj, str);
Py_DECREF(str);
_err:
return ret;
}
#ifdef _WIN32
/* Truncate file `fd' at `size' bytes and map it in memory. */
static void *map_file(HANDLE fd, LARGE_INTEGER size)
{
LARGE_INTEGER disk_size, cur_off;
HANDLE h;
void *address;
if(size.QuadPart < 0 || size.QuadPart > SSIZE_MAX)
goto _err1;
GetFileSizeEx(fd, &disk_size);
if(size.QuadPart == 0)
size = disk_size;
/* Quoting the MSDN page for `SetFilePointerEx()':
*
* "If the file is extended, the contents of the file between the old end of
* the file and the new end of the file are not guaranteed to be zero; the
* behavior is defined by the file system."
*
* Looks like for NTFS, the extended part of the file is filled with zeros.
*/
if(SetFilePointerEx(fd, size, &cur_off, FILE_BEGIN) == FALSE)
{
serror("map_file: SetFilePointerEx");
goto _err1;
}
if(SetEndOfFile(fd) == FALSE)
{
serror("map_file: SetEndOfFile");
goto _err2;
}
if((h = CreateFileMappingA(fd, NULL, PAGE_READWRITE, 0, 0, NULL)) == NULL)
{
serror("map_file: CreateFileMappingA");
goto _err3;
}
if((address = MapViewOfFile(h, FILE_MAP_ALL_ACCESS, 0, 0, 0)) == NULL)
serror("map_file: MapViewOfFile");
/* We need not keep a reference to the file mapping object, we can safely
* close it now. Quoting the MSDN page for `CreateFileMapping()':
*
* "Mapped views of a file mapping object maintain internal references to
* the object, and a file mapping object does not close until all references
* to it are released. Therefore, to fully close a file mapping object, an
* application must unmap all mapped views of the file mapping object by
* calling UnmapViewOfFile and close the file mapping object handle by
* calling CloseHandle. These functions can be called in any order."
*/
CloseHandle(h);
return address;
_err3:
SetFilePointerEx(fd, disk_size, NULL, FILE_BEGIN);
SetEndOfFile(fd);
_err2:
SetFilePointerEx(fd, cur_off, NULL, FILE_BEGIN);
_err1:
return NULL;
}
/* Open existing file `filename' and map it in memory. */
mapped_file_t *mapped_file_open(const char *filename)
{
HANDLE fd;
LARGE_INTEGER disk_size;
void *address;
mapped_file_t *mf;
if((fd = CreateFileA(filename, GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
OPEN_EXISTING, 0, NULL)) == INVALID_HANDLE_VALUE)
{
serror("mapped_file_open: CreateFileA");
goto _err1;
}
GetFileSizeEx(fd, &disk_size);
if((address = map_file(fd, disk_size)) == NULL)
goto _err2;
if((mf = mapped_file_alloc(filename)) == NULL)
goto _err3;
mf->fd = fd;
mf->address = address;
mf->size = (size_t)disk_size.QuadPart;
mf->eof = (size_t)disk_size.QuadPart;
return mf;
_err3:
UnmapViewOfFile(address);
_err2:
CloseHandle(fd);
_err1:
return NULL;
}
/* Create file `filename', truncate it at `size' bytes and map it in memory. */
mapped_file_t *mapped_file_create(const char *filename, size_t size)
{
HANDLE fd;
LARGE_INTEGER lsize;
void *address;
mapped_file_t *mf;
if((fd = CreateFileA(filename, GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
CREATE_ALWAYS, 0, NULL)) == INVALID_HANDLE_VALUE)
{
serror("mapped_file_create: CreateFileA");
goto _err1;
}
lsize.QuadPart = size;
if((address = map_file(fd, lsize)) == NULL)
goto _err2;
if((mf = mapped_file_alloc(filename)) == NULL)
goto _err3;
mf->fd = fd;
mf->address = address;
mf->size = size;
return mf;
_err3:
UnmapViewOfFile(address);
_err2:
CloseHandle(fd);
_err1:
return NULL;
}
/* Synchronize memory contents to disk. */
int mapped_file_sync(mapped_file_t *mf, size_t pos, size_t size)
{
int ret = -1;
if(mapped_file_check_range(mf, pos, size) == 0)
goto _err;
ret = FlushViewOfFile((char *)mf->address + pos, size);
_err:
return ret;
}
/* Give kernel a hint about the type of access we are going to perform. It's a
* no-op for now; may be implemented in the future using `ReOpenFile()'.
*/
int mapped_file_set_access(mapped_file_t *mf, int advice)
{
UNREFERENCED_PARAMETER(mf);
UNREFERENCED_PARAMETER(advice);
return 0;
}
/* Equivalent to `ftruncate()' for memory mapped files. */
int mapped_file_truncate(mapped_file_t *mf, size_t size)
{
LARGE_INTEGER lsize;
void *address;
int ret = -1;
/* We need to unmap before actually re-mapping the file. Unfortunately,
* there's no `mremap()' equivalent on Microsoft Windows.
*/
UnmapViewOfFile(mf->address);
lsize.QuadPart = size;
if((address = map_file(mf->fd, lsize)) == NULL)
goto _err;
mf->address = address;
mf->size = size;
if(mf->pos > size)
mf->pos = size;
if(mf->eof > size)
mf->eof = size;
ret = 0;
_err:
return ret;
}
/* Equivalent to `rename()' for memory mapped files. */
int mapped_file_rename(mapped_file_t *mf, const char *filename)
{
char *filename_p;
int ret = -1;
if((filename_p = PyMem_MALLOC(strlen(filename) + 1)) == NULL)
goto _err;
strcpy(filename_p, filename);
if(MoveFileExA(mf->filename, filename,
MOVEFILE_WRITE_THROUGH | MOVEFILE_REPLACE_EXISTING) == 0)
{
serror("mapped_file_rename: MoveFileExA");
PyMem_FREE(filename_p);
goto _err;
}
PyMem_FREE(mf->filename);
mf->filename = filename_p;
ret = 0;
_err:
return ret;
}
/* Instructs Microsoft Windows to delete the file once it's closed. We can't
* directly delete the file in any other way and the whole process should be
* transparent to the user.
*/
int mapped_file_unlink(mapped_file_t *mf)
{
HANDLE fd;
int ret = -1;
if((fd = ReOpenFile(mf->fd, GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
FILE_FLAG_DELETE_ON_CLOSE)) == INVALID_HANDLE_VALUE)
{
serror("mapped_file_unlink: ReOpenFile");
goto _err;
}
CloseHandle(mf->fd);
mf->fd = fd;
ret = 0;
_err:
return ret;
}
/* Equivalent to `close()' for memory mapped files. It's not safe to access `mf'
* after a call to `mapped_file_close()'.
*/
void mapped_file_close(mapped_file_t *mf)
{
UnmapViewOfFile(mf->address);
CloseHandle(mf->fd);
mapped_file_free(mf);
}
#else
/* Truncate file `fd' at `size' bytes and map it in memory. */
static void *map_file(int fd, size_t size)
{
struct stat st;
void *address = NULL;
if(size > SSIZE_MAX)
goto _err;
/* Use `fstat()' to read file's original size. */
if(fstat(fd, &st) != 0)
{
serror("map_file: fstat");
goto _err;
}
if(ftruncate(fd, size) != 0)
{
serror("map_file: ftruncate");
goto _err;
}
address = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if(address == MAP_FAILED)
{
/* Make sure we return `NULL' instead of `MAP_FAILED'. */
address = NULL;
serror("map_file: mmap");
/* Restore original file size on failure to avoid undefined behaviour
* when accessing a memory mapped region for a file that has, for
* example, been shrunk by `ftruncate()'.
*/
if(ftruncate(fd, st.st_size) != 0)
serror("map_file: ftruncate");
}
_err:
return address;
}
/* Open existing file `filename' and map it in memory. */
mapped_file_t *mapped_file_open(const char *filename)
{
int fd;
struct stat st;
void *address;
mapped_file_t *mf;
if((fd = open(filename, O_RDWR)) < 0)
{
serror("mapped_file_open: fopen");
goto _err1;
}
if(fstat(fd, &st) != 0)
{
serror("mapped_file_open: fstat");
goto _err2;
}
if((address = map_file(fd, st.st_size)) == NULL)
goto _err2;
if((mf = mapped_file_alloc(filename)) == NULL)
goto _err3;
mf->fd = fd;
mf->address = address;
mf->size = st.st_size;
mf->eof = st.st_size;
return mf;
_err3:
munmap(address, st.st_size);
_err2:
close(fd);
_err1:
return NULL;
}
/* Create file `filename', truncate it at `size' bytes and map it in memory. */
mapped_file_t *mapped_file_create(const char *filename, size_t size)
{
int fd;
void *address;
mapped_file_t *mf;
if((fd = open(filename, O_RDWR | O_CREAT | O_TRUNC, 0600)) < 0)
{
serror("mapped_file_create: open");
goto _err1;
}
if((address = map_file(fd, size)) == NULL)
goto _err2;
if((mf = mapped_file_alloc(filename)) == NULL)
goto _err3;
mf->fd = fd;
mf->address = address;
mf->size = size;
return mf;
_err3:
munmap(address, size);
_err2:
close(fd);
unlink(filename);
_err1:
return NULL;
}
/* Synchronize memory contents to disk. */
int mapped_file_sync(mapped_file_t *mf, size_t pos, size_t size)
{
int ret = -1;
if(mapped_file_check_range(mf, pos, size) == 0)
goto _err;
ret = msync((char *)mf->address + pos, size, MS_SYNC);
_err:
return ret;
}
/* Give kernel a hint about the type of access we are going to perform. */
int mapped_file_set_access(mapped_file_t *mf, int advice)
{
int ret = -1;
if(advice != MF_ACCESS_NORMAL && advice != MF_ACCESS_RANDOM &&
advice != MF_ACCESS_SEQUENTIAL)
goto _err;
if((ret = madvise(mf->address, mf->size, advice)) < 0)
serror("mapped_file_set_access: madvise");
_err:
return ret;
}
/* Equivalent to `ftruncate()' for memory mapped files. */
int mapped_file_truncate(mapped_file_t *mf, size_t size)
{
long pagesize;
void *mf_address = mf->address, *address = NULL;
size_t mf_size = mf->size;
int mf_fd = mf->fd;
if(size > SSIZE_MAX)
goto _err1;
if(size == mf_size)
goto _ok;
/* Truncate the file to the requested size first. If resizing the memory
* mapping fails, we can easily restore the original file size by calling
* `ftruncate()' again.
*/
if(ftruncate(mf_fd, size) != 0)
{
serror("mapped_file_truncate: ftruncate");
goto _err1;
}
/* When shrinking the memory mapped file, just unmap part of the mapping. */
if(size < mf_size)
{
/* According to the manual page, this is the portable way of reading the
* system's page size.
*/
if((pagesize = sysconf(_SC_PAGESIZE)) == -1)
{
serror("mapped_file_truncate: sysconf");
goto _err2;
}
/* Align sizes to the next multiple of the page size, as `munmap()' will
* unmap any page overlapping with the given argument.
*/
size = (size + pagesize - 1) & ~(pagesize - 1);
mf_size = (mf_size + pagesize - 1) & ~(pagesize - 1);
if(size < mf_size &&
munmap((char *)mf_address + size, mf_size - size) != 0)
{
serror("mapped_file_truncate: munmap");
goto _err2;
}
address = mf_address;
}
/* When increasing the size of the memory mapped file, we have to re-map the
* underlying file pages.
*/
else if(size > mf_size)
{
#if defined __linux__ || defined __NetBSD__
/* Linux and NetBSD implement `mremap()'. I haven't tested the code on
* NetBSD, but this is good news anyway.
*/
address = mremap(mf_address, mf_size, size, MREMAP_MAYMOVE);
if(address == MAP_FAILED)
{
serror("mapped_file_truncate: mremap");
goto _err2;
}
#else
/* On other OSes, like MacOS X and FreeBSD, we have to take the slow path
* which involves creating a larger memory mapping and then unmapping the
* old one.
*/
address = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, mf_fd, 0);
if(address == MAP_FAILED)
{
serror("mapped_file_truncate: mmap");
goto _err2;
}
munmap(mf_address, mf_size);
#endif
}
mf->address = address;
mf->size = size;
if(mf->pos > size)
mf->pos = size;
if(mf->eof > size)
mf->eof = size;
_ok:
return 0;
_err2:
if(ftruncate(mf_fd, mf_size) != 0)
serror("mapped_file_truncate: ftruncate");
_err1:
return -1;
}
/* Equivalent to `rename()' for memory mapped files. */
int mapped_file_rename(mapped_file_t *mf, const char *filename)
{
char *filename_p;
int ret = -1;
if((filename_p = PyMem_MALLOC(strlen(filename) + 1)) == NULL)
goto _err;
strcpy(filename_p, filename);
if(rename(mf->filename, filename) != 0)
{
serror("mapped_file_rename: rename");
PyMem_FREE(filename_p);
goto _err;
}
PyMem_FREE(mf->filename);
mf->filename = filename_p;
ret = 0;
_err:
return ret;
}
/* Equivalent to `unlink()' for memory mapped files. */