FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
cpython/Modules/mmapmodule.c at main · python/cpython · GitHub
python
cpython
Repository navigation
Code
Issues
5k+
(5k+)
Pull requests
2.4k
(2.4k)
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
cpython
/
Modules
/
mmapmodule.c
Copy path
More file actions
More file actions
Latest commit
History
History
History
2430 lines (2164 loc) · 68.5 KB
Breadcrumbs
cpython
/
Modules
/
mmapmodule.c
Copy path
File metadata and controls
2430 lines (2164 loc) · 68.5 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
/*
/ Author: Sam Rushing <rushing@nightmare.com>
/ Hacked for Unix by AMK
/ $Id$
/ Modified to support mmap with offset - to map a 'window' of a file
/ Author: Yotam Medini yotamm@mellanox.co.il
/
/ mmapmodule.cpp -- map a view of a file into memory
/
/ todo: need permission flags, perhaps a 'chsize' analog
/ not all functions check range yet!!!
/
/
/ This version of mmapmodule.c has been changed significantly
/ from the original mmapfile.c on which it was based.
/ The original version of mmapfile is maintained by Sam at
/ ftp://squirl.nightmare.com/pub/python/python-ext.
*/
#ifndef
Py_BUILD_CORE_BUILTIN
# define
Py_BUILD_CORE_MODULE
1
#endif
#include
<Python.h>
#include
"pycore_abstract.h"
// _Py_convert_optional_to_ssize_t()
#include
"pycore_bytesobject.h"
// _PyBytes_Find()
#include
"pycore_fileutils.h"
// _Py_stat_struct
#include
"pycore_mmap.h"
// _PyAnnotateMemoryMap()
#include
"pycore_weakref.h"
// FT_CLEAR_WEAKREFS()
#include
<stddef.h>
// offsetof()
#ifndef
MS_WINDOWS
# include
<unistd.h>
// close()
#endif
#ifndef
MS_WINDOWS
#define
UNIX
# ifdef
HAVE_FCNTL_H
# include
<fcntl.h>
# endif
/* HAVE_FCNTL_H */
#endif
#ifdef
MS_WINDOWS
#include
<windows.h>
#include
<ntsecapi.h>
// LsaNtStatusToWinError
static
int
my_getpagesize
(
void
)
{
SYSTEM_INFO
si
;
GetSystemInfo
(
&
si
);
return
si
.
dwPageSize
;
}
static
int
my_getallocationgranularity
(
void
)
{
SYSTEM_INFO
si
;
GetSystemInfo
(
&
si
);
return
si
.
dwAllocationGranularity
;
}
#endif
#ifdef
UNIX
#include
<sys/mman.h>
#include
<sys/stat.h>
#if
defined(
HAVE_SYSCONF
)
&&
defined(
_SC_PAGESIZE
)
static
int
my_getpagesize
(
void
)
{
return
sysconf
(
_SC_PAGESIZE
);
}
#define
my_getallocationgranularity
my_getpagesize
#else
#define
my_getpagesize
getpagesize
#endif
#endif
/* UNIX */
#include
<string.h>
#ifdef
HAVE_SYS_TYPES_H
#include
<sys/types.h>
#endif
/* HAVE_SYS_TYPES_H */
/* Prefer MAP_ANONYMOUS since MAP_ANON is deprecated according to man page. */
#if
!defined(
MAP_ANONYMOUS
)
&&
defined(
MAP_ANON
)
# define
MAP_ANONYMOUS
MAP_ANON
#endif
/*[clinic input]
module mmap
class mmap.mmap "mmap_object *" ""
[clinic start generated code]*/
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=82a9f8a529905b9b]*/
typedef
enum
{
ACCESS_DEFAULT
,
ACCESS_READ
,
ACCESS_WRITE
,
ACCESS_COPY
}
access_mode
;
typedef
struct
{
PyObject_HEAD
char
*
data
;
Py_ssize_t
size
;
Py_ssize_t
pos
;
/* relative to offset */
#ifdef
MS_WINDOWS
long long
offset
;
#else
off_t
offset
;
#endif
Py_ssize_t
exports
;
#ifdef
MS_WINDOWS
HANDLE
map_handle
;
HANDLE
file_handle
;
wchar_t
*
tagname
;
#endif
#ifdef
UNIX
int
fd
;
int
flags
;
#endif
PyObject
*
weakreflist
;
access_mode
access
;
_Bool
trackfd
;
}
mmap_object
;
#define
mmap_object_CAST
(
op
) ((mmap_object *)(op))
#include
"clinic/mmapmodule.c.h"
/* Return a Py_ssize_t from the object arg. This conversion logic is similar
to what AC uses for `Py_ssize_t` arguments.
Returns -1 on error. Use PyErr_Occurred() to disambiguate.
*/
static
Py_ssize_t
_As_Py_ssize_t
(
PyObject
*
arg
) {
assert
(
arg
!=
NULL
);
Py_ssize_t
ival
=
-1
;
PyObject
*
iobj
=
_PyNumber_Index
(
arg
);
if
(
iobj
!=
NULL
) {
ival
=
PyLong_AsSsize_t
(
iobj
);
Py_DECREF
(
iobj
);
}
return
ival
;
}
static
void
mmap_object_dealloc
(
PyObject
*
op
)
{
mmap_object
*
m_obj
=
mmap_object_CAST
(
op
);
PyTypeObject
*
tp
=
Py_TYPE
(
m_obj
);
PyObject_GC_UnTrack
(
m_obj
);
#ifdef
MS_WINDOWS
Py_BEGIN_ALLOW_THREADS
if
(
m_obj
->
data
!=
NULL)
UnmapViewOfFile
(
m_obj
-
>
data
);
if
(
m_obj
->
map_handle
!=
NULL
)
CloseHandle
(
m_obj
->
map_handle
);
if
(
m_obj
->
file_handle
!=
INVALID_HANDLE_VALUE
)
CloseHandle
(
m_obj
->
file_handle
);
Py_END_ALLOW_THREADS
if
(
m_obj
->
tagname
)
PyMem_Free
(
m_obj
-
>
tagname
);
#endif
/* MS_WINDOWS */
#ifdef
UNIX
Py_BEGIN_ALLOW_THREADS
if
(
m_obj
->
fd
>=
0
)
(
void
)
close
(
m_obj
-
>
fd
);
if
(
m_obj
->
data
!=
NULL
) {
munmap
(
m_obj
->
data
,
m_obj
->
size
);
}
Py_END_ALLOW_THREADS
#endif
/* UNIX */
FT_CLEAR_WEAKREFS
(
op
,
m_obj
->
weakreflist
);
tp
->
tp_free
(
m_obj
);
Py_DECREF
(
tp
);
}
/*[clinic input]
@critical_section
mmap.mmap.close
[clinic start generated code]*/
static
PyObject
*
mmap_mmap_close_impl
(
mmap_object
*
self
)
/*[clinic end generated code: output=a1ae0c727546f78d input=25020035f047eae1]*/
{
if
(
self
->
exports
>
0
) {
PyErr_SetString
(
PyExc_BufferError
,
"cannot close "
\
"exported pointers exist"
);
return
NULL
;
}
#ifdef
MS_WINDOWS
/* For each resource we maintain, we need to check
the value is valid, and if so, free the resource
and set the member value to an invalid value so
the dealloc does not attempt to resource clearing
again.
TODO - should we check for errors in the close operations???
*/
HANDLE
map_handle
=
self
->
map_handle
;
HANDLE
file_handle
=
self
->
file_handle
;
char
*
data
=
self
->
data
;
self
->
map_handle
=
NULL
;
self
->
file_handle
=
INVALID_HANDLE_VALUE
;
self
->
data
=
NULL
;
Py_BEGIN_ALLOW_THREADS
if
(
data
!=
NULL) {
UnmapViewOfFile
(
data
);
}
if
(
map_handle
!=
NULL
) {
CloseHandle
(
map_handle
);
}
if
(
file_handle
!=
INVALID_HANDLE_VALUE
) {
CloseHandle
(
file_handle
);
}
Py_END_ALLOW_THREADS
#endif
/* MS_WINDOWS */
#ifdef
UNIX
int
fd
=
self
->
fd
;
char
*
data
=
self
->
data
;
self
->
fd
=
-1
;
self
->
data
=
NULL
;
Py_BEGIN_ALLOW_THREADS
if
(
0
<=
fd
)
(
void
)
close
(
fd
);
if
(
data
!=
NULL
) {
munmap
(
data
,
self
->
size
);
}
Py_END_ALLOW_THREADS
#endif
Py_RETURN_NONE
;
}
#ifdef
MS_WINDOWS
#define
CHECK_VALID
(
err
) \
do { \
if (self->map_handle == NULL) { \
PyErr_SetString(PyExc_ValueError, "mmap closed or invalid"); \
return err; \
} \
} while (0)
#define
CHECK_VALID_OR_RELEASE
(
err
,
buffer
) \
do { \
if (self->map_handle == NULL) { \
PyErr_SetString(PyExc_ValueError, "mmap closed or invalid"); \
PyBuffer_Release(&(buffer)); \
return (err); \
} \
} while (0)
#endif
/* MS_WINDOWS */
#ifdef
UNIX
#define
CHECK_VALID
(
err
) \
do { \
if (self->data == NULL) { \
PyErr_SetString(PyExc_ValueError, "mmap closed or invalid"); \
return err; \
} \
} while (0)
#define
CHECK_VALID_OR_RELEASE
(
err
,
buffer
) \
do { \
if (self->data == NULL) { \
PyErr_SetString(PyExc_ValueError, "mmap closed or invalid"); \
PyBuffer_Release(&(buffer)); \
return (err); \
} \
} while (0)
#endif
/* UNIX */
#if
defined(
MS_WINDOWS
)
&&
!defined(
DONT_USE_SEH
)
static
DWORD
filter_page_exception
(
EXCEPTION_POINTERS
*
ptrs
,
EXCEPTION_RECORD
*
record
)
{
*
record
=
*
ptrs
->
ExceptionRecord
;
if
(
record
->
ExceptionCode
==
EXCEPTION_IN_PAGE_ERROR
||
record
->
ExceptionCode
==
EXCEPTION_ACCESS_VIOLATION
)
{
return
EXCEPTION_EXECUTE_HANDLER
;
}
return
EXCEPTION_CONTINUE_SEARCH
;
}
static
DWORD
filter_page_exception_method
(
mmap_object
*
self
,
EXCEPTION_POINTERS
*
ptrs
,
EXCEPTION_RECORD
*
record
)
{
*
record
=
*
ptrs
->
ExceptionRecord
;
if
(
record
->
ExceptionCode
==
EXCEPTION_IN_PAGE_ERROR
||
record
->
ExceptionCode
==
EXCEPTION_ACCESS_VIOLATION
)
{
ULONG_PTR
address
=
record
->
ExceptionInformation
[
1
];
if
(
address
>= (
ULONG_PTR
)
self
->
data
&&
address
<
(
ULONG_PTR
)
self
->
data
+
(
ULONG_PTR
)
self
->
size
)
{
return
EXCEPTION_EXECUTE_HANDLER
;
}
}
return
EXCEPTION_CONTINUE_SEARCH
;
}
static
void
_PyErr_SetFromNTSTATUS
(
ULONG
status
)
{
#if
defined(
MS_WINDOWS_DESKTOP
)
||
defined(
MS_WINDOWS_SYSTEM
)
PyErr_SetFromWindowsErr
(
LsaNtStatusToWinError
((
NTSTATUS
)
status
));
#else
if
(
status
&
0x80000000
) {
// HRESULT-shaped codes are supported by PyErr_SetFromWindowsErr
PyErr_SetFromWindowsErr
((
int
)
status
);
}
else
{
// No mapping for NTSTATUS values, so just return it for diagnostic purposes
// If we provide it as winerror it could incorrectly change the type of the exception.
PyErr_Format
(
PyExc_OSError
,
"Operating system error NTSTATUS=0x%08lX"
,
status
);
}
#endif
}
#endif
#if
defined(
MS_WINDOWS_DESKTOP
)
&&
!defined(
DONT_USE_SEH
)
#define
HANDLE_INVALID_MEM
(
sourcecode
) \
do { \
EXCEPTION_RECORD record; \
__try { \
sourcecode \
} \
__except (filter_page_exception(GetExceptionInformation(), &record)) { \
assert(record.ExceptionCode == EXCEPTION_IN_PAGE_ERROR || \
record.ExceptionCode == EXCEPTION_ACCESS_VIOLATION); \
if (record.ExceptionCode == EXCEPTION_IN_PAGE_ERROR) { \
_PyErr_SetFromNTSTATUS((ULONG)record.ExceptionInformation[2]); \
} \
else if (record.ExceptionCode == EXCEPTION_ACCESS_VIOLATION) { \
PyErr_SetFromWindowsErr(ERROR_NOACCESS); \
} \
return -1; \
} \
} while (0)
#else
#define
HANDLE_INVALID_MEM
(
sourcecode
) \
do { \
sourcecode \
} while (0)
#endif
#if
defined(
MS_WINDOWS_DESKTOP
)
&&
!defined(
DONT_USE_SEH
)
#define
HANDLE_INVALID_MEM_METHOD
(
self
,
sourcecode
) \
do { \
EXCEPTION_RECORD record; \
__try { \
sourcecode \
} \
__except (filter_page_exception_method(self, GetExceptionInformation(), \
&record)) { \
assert(record.ExceptionCode == EXCEPTION_IN_PAGE_ERROR || \
record.ExceptionCode == EXCEPTION_ACCESS_VIOLATION); \
if (record.ExceptionCode == EXCEPTION_IN_PAGE_ERROR) { \
_PyErr_SetFromNTSTATUS((ULONG)record.ExceptionInformation[2]); \
} \
else if (record.ExceptionCode == EXCEPTION_ACCESS_VIOLATION) { \
PyErr_SetFromWindowsErr(ERROR_NOACCESS); \
} \
return -1; \
} \
} while (0)
#else
#define
HANDLE_INVALID_MEM_METHOD
(
self
,
sourcecode
) \
do { \
sourcecode \
} while (0)
#endif
int
safe_memcpy
(
void
*
dest
,
const
void
*
src
,
size_t
count
)
{
HANDLE_INVALID_MEM
(
memcpy
(
dest
,
src
,
count
);
);
return
0
;
}
int
safe_byte_copy
(
char
*
dest
,
const
char
*
src
)
{
HANDLE_INVALID_MEM
(
*
dest
=
*
src
;
);
return
0
;
}
int
safe_memchr
(
char
*
*
out
,
const
void
*
ptr
,
int
ch
,
size_t
count
)
{
HANDLE_INVALID_MEM
(
*
out
=
(
char
*
)
memchr
(
ptr
,
ch
,
count
);
);
return
0
;
}
int
safe_memmove
(
void
*
dest
,
const
void
*
src
,
size_t
count
)
{
HANDLE_INVALID_MEM
(
memmove
(
dest
,
src
,
count
);
);
return
0
;
}
int
safe_copy_from_slice
(
char
*
dest
,
const
char
*
src
,
Py_ssize_t
start
,
Py_ssize_t
step
,
Py_ssize_t
slicelen
)
{
HANDLE_INVALID_MEM
(
size_t
cur
;
Py_ssize_t
i
;
for
(
cur
=
start
,
i
=
0
;
i
<
slicelen
;
cur
+=
step
,
i
++
) {
dest
[
cur
]
=
src
[
i
];
}
);
return
0
;
}
int
safe_copy_to_slice
(
char
*
dest
,
const
char
*
src
,
Py_ssize_t
start
,
Py_ssize_t
step
,
Py_ssize_t
slicelen
)
{
HANDLE_INVALID_MEM
(
size_t
cur
;
Py_ssize_t
i
;
for
(
cur
=
start
,
i
=
0
;
i
<
slicelen
;
cur
+=
step
,
i
++
) {
dest
[
i
]
=
src
[
cur
];
}
);
return
0
;
}
int
_safe_PyBytes_Find
(
Py_ssize_t
*
out
,
mmap_object
*
self
,
const
char
*
haystack
,
Py_ssize_t
len_haystack
,
const
char
*
needle
,
Py_ssize_t
len_needle
,
Py_ssize_t
offset
)
{
HANDLE_INVALID_MEM_METHOD
(
self
,
*
out
=
_PyBytes_Find
(
haystack
,
len_haystack
,
needle
,
len_needle
,
offset
);
);
return
0
;
}
int
_safe_PyBytes_ReverseFind
(
Py_ssize_t
*
out
,
mmap_object
*
self
,
const
char
*
haystack
,
Py_ssize_t
len_haystack
,
const
char
*
needle
,
Py_ssize_t
len_needle
,
Py_ssize_t
offset
)
{
HANDLE_INVALID_MEM_METHOD
(
self
,
*
out
=
_PyBytes_ReverseFind
(
haystack
,
len_haystack
,
needle
,
len_needle
,
offset
);
);
return
0
;
}
PyObject
*
_safe_PyBytes_FromStringAndSize
(
char
*
start
,
size_t
num_bytes
)
{
if
(
num_bytes
==
1
) {
char
dest
;
if
(
safe_byte_copy
(
&
dest
,
start
)
<
0
) {
return
NULL
;
}
else
{
return
PyBytes_FromStringAndSize
(
&
dest
,
1
);
}
}
else
{
PyBytesWriter
*
writer
=
PyBytesWriter_Create
(
num_bytes
);
if
(
writer
==
NULL
) {
return
NULL
;
}
if
(
safe_memcpy
(
PyBytesWriter_GetData
(
writer
),
start
,
num_bytes
)
<
0
) {
PyBytesWriter_Discard
(
writer
);
return
NULL
;
}
return
PyBytesWriter_Finish
(
writer
);
}
}
/*[clinic input]
@critical_section
mmap.mmap.read_byte
[clinic start generated code]*/
static
PyObject
*
mmap_mmap_read_byte_impl
(
mmap_object
*
self
)
/*[clinic end generated code: output=d931da1319f3869b input=5b8c6a904bdddda9]*/
{
CHECK_VALID
(
NULL
);
if
(
self
->
pos
>=
self
->
size
) {
PyErr_SetString
(
PyExc_ValueError
,
"read byte out of range"
);
return
NULL
;
}
char
dest
;
if
(
safe_byte_copy
(
&
dest
,
self
->
data
+
self
->
pos
)
<
0
) {
return
NULL
;
}
self
->
pos
++
;
return
PyLong_FromLong
((
unsigned
char
)
dest
);
}
/*[clinic input]
@critical_section
mmap.mmap.readline
[clinic start generated code]*/
static
PyObject
*
mmap_mmap_readline_impl
(
mmap_object
*
self
)
/*[clinic end generated code: output=b9d2bf9999283311 input=2c4efd1d06e1cdd1]*/
{
Py_ssize_t
remaining
;
char
*
start
,
*
eol
;
CHECK_VALID
(
NULL
);
remaining
=
(
self
->
pos
<
self
->
size
) ?
self
->
size
-
self
->
pos
:
0
;
if
(!
remaining
)
return
Py_GetConstant
(
Py_CONSTANT_EMPTY_BYTES
);
start
=
self
->
data
+
self
->
pos
;
if
(
safe_memchr
(
&
eol
,
start
,
'\n'
,
remaining
)
<
0
) {
return
NULL
;
}
if
(!
eol
)
eol
=
self
->
data
+
self
->
size
;
else
++
eol
;
/* advance past newline */
PyObject
*
result
=
_safe_PyBytes_FromStringAndSize
(
start
,
eol
-
start
);
if
(
result
!=
NULL
) {
self
->
pos
+=
(
eol
-
start
);
}
return
result
;
}
/*[clinic input]
@critical_section
mmap.mmap.read
n as num_bytes: object(converter='_Py_convert_optional_to_ssize_t', type='Py_ssize_t', c_default='PY_SSIZE_T_MAX') = None
/
[clinic start generated code]*/
static
PyObject
*
mmap_mmap_read_impl
(
mmap_object
*
self
,
Py_ssize_t
num_bytes
)
/*[clinic end generated code: output=3b4d4f3704ed0969 input=8f97f361d435e357]*/
{
Py_ssize_t
remaining
;
CHECK_VALID
(
NULL
);
/* silently 'adjust' out-of-range requests */
remaining
=
(
self
->
pos
<
self
->
size
) ?
self
->
size
-
self
->
pos
:
0
;
if
(
num_bytes
<
0
||
num_bytes
>
remaining
)
num_bytes
=
remaining
;
PyObject
*
result
=
_safe_PyBytes_FromStringAndSize
(
self
->
data
+
self
->
pos
,
num_bytes
);
if
(
result
!=
NULL
) {
self
->
pos
+=
num_bytes
;
}
return
result
;
}
static
PyObject
*
mmap_gfind_lock_held
(
mmap_object
*
self
,
Py_buffer
*
view
,
PyObject
*
start_obj
,
PyObject
*
end_obj
,
int
reverse
)
{
Py_ssize_t
start
=
self
->
pos
;
Py_ssize_t
end
=
self
->
size
;
CHECK_VALID
(
NULL
);
if
(
start_obj
!=
Py_None
) {
start
=
_As_Py_ssize_t
(
start_obj
);
if
(
start
==
-1
&&
PyErr_Occurred
()) {
return
NULL
;
}
if
(
end_obj
!=
Py_None
) {
end
=
_As_Py_ssize_t
(
end_obj
);
if
(
end
==
-1
&&
PyErr_Occurred
()) {
return
NULL
;
}
}
}
if
(
start
<
0
)
start
+=
self
->
size
;
if
(
start
<
0
)
start
=
0
;
if
(
end
<
0
)
end
+=
self
->
size
;
if
(
end
<
0
)
end
=
0
;
else
if
(
end
>
self
->
size
)
end
=
self
->
size
;
Py_ssize_t
index
;
PyObject
*
result
;
CHECK_VALID
(
NULL
);
if
(
end
<
start
) {
result
=
PyLong_FromSsize_t
(
-1
);
}
else
if
(
reverse
) {
assert
(
0
<=
start
&&
start
<=
end
&&
end
<=
self
->
size
);
if
(
_safe_PyBytes_ReverseFind
(
&
index
,
self
,
self
->
data
+
start
,
end
-
start
,
view
->
buf
,
view
->
len
,
start
)
<
0
)
{
result
=
NULL
;
}
else
{
result
=
PyLong_FromSsize_t
(
index
);
}
}
else
{
assert
(
0
<=
start
&&
start
<=
end
&&
end
<=
self
->
size
);
if
(
_safe_PyBytes_Find
(
&
index
,
self
,
self
->
data
+
start
,
end
-
start
,
view
->
buf
,
view
->
len
,
start
)
<
0
)
{
result
=
NULL
;
}
else
{
result
=
PyLong_FromSsize_t
(
index
);
}
}
return
result
;
}
/*[clinic input]
@critical_section
mmap.mmap.find
view: Py_buffer
start: object = None
end: object = None
/
[clinic start generated code]*/
static
PyObject
*
mmap_mmap_find_impl
(
mmap_object
*
self
,
Py_buffer
*
view
,
PyObject
*
start
,
PyObject
*
end
)
/*[clinic end generated code: output=ef8878a322f00192 input=0135504494b52c2b]*/
{
return
mmap_gfind_lock_held
(
self
,
view
,
start
,
end
,
0
);
}
/*[clinic input]
@critical_section
mmap.mmap.rfind = mmap.mmap.find
[clinic start generated code]*/
static
PyObject
*
mmap_mmap_rfind_impl
(
mmap_object
*
self
,
Py_buffer
*
view
,
PyObject
*
start
,
PyObject
*
end
)
/*[clinic end generated code: output=73b918940d67c2b8 input=8aecdd1f70c06c62]*/
{
return
mmap_gfind_lock_held
(
self
,
view
,
start
,
end
,
1
);
}
static
int
is_writable
(
mmap_object
*
self
)
{
if
(
self
->
access
!=
ACCESS_READ
)
return
1
;
PyErr_Format
(
PyExc_TypeError
,
"mmap can't modify a readonly memory map."
);
return
0
;
}
#if
defined(
MS_WINDOWS
)
||
defined(
HAVE_MREMAP
)
static
int
is_resizeable
(
mmap_object
*
self
)
{
if
(
self
->
exports
>
0
) {
PyErr_SetString
(
PyExc_BufferError
,
"mmap can't resize with extant buffers exported."
);
return
0
;
}
if
(!
self
->
trackfd
) {
PyErr_SetString
(
PyExc_ValueError
,
"mmap can't resize with trackfd=False."
);
return
0
;
}
if
((
self
->
access
==
ACCESS_WRITE
)
||
(
self
->
access
==
ACCESS_DEFAULT
))
return
1
;
PyErr_Format
(
PyExc_TypeError
,
"mmap can't resize a readonly or copy-on-write memory map."
);
return
0
;
}
#endif
/* MS_WINDOWS || HAVE_MREMAP */
/*[clinic input]
@critical_section
mmap.mmap.write
bytes as data: Py_buffer
/
[clinic start generated code]*/
static
PyObject
*
mmap_mmap_write_impl
(
mmap_object
*
self
,
Py_buffer
*
data
)
/*[clinic end generated code: output=9e97063efb6fb27b input=3f16fa79aa89d6f7]*/
{
CHECK_VALID
(
NULL
);
if
(!
is_writable
(
self
)) {
return
NULL
;
}
if
(
self
->
pos
>
self
->
size
||
self
->
size
-
self
->
pos
<
data
->
len
) {
PyErr_SetString
(
PyExc_ValueError
,
"data out of range"
);
return
NULL
;
}
CHECK_VALID
(
NULL
);
PyObject
*
result
;
if
(
safe_memcpy
(
self
->
data
+
self
->
pos
,
data
->
buf
,
data
->
len
)
<
0
) {
result
=
NULL
;
}
else
{
self
->
pos
+=
data
->
len
;
result
=
PyLong_FromSsize_t
(
data
->
len
);
}
return
result
;
}
/*[clinic input]
@critical_section
mmap.mmap.write_byte
byte as value: unsigned_char
/
[clinic start generated code]*/
static
PyObject
*
mmap_mmap_write_byte_impl
(
mmap_object
*
self
,
unsigned
char
value
)
/*[clinic end generated code: output=aa11adada9b17510 input=32740bfa174f0991]*/
{
CHECK_VALID
(
NULL
);
if
(!
is_writable
(
self
))
return
NULL
;
CHECK_VALID
(
NULL
);
if
(
self
->
pos
>=
self
->
size
) {
PyErr_SetString
(
PyExc_ValueError
,
"write byte out of range"
);
return
NULL
;
}
if
(
safe_byte_copy
(
self
->
data
+
self
->
pos
, (
const
char
*
)
&
value
)
<
0
) {
return
NULL
;
}
self
->
pos
++
;
Py_RETURN_NONE
;
}
/*[clinic input]
@critical_section
mmap.mmap.size
[clinic start generated code]*/
static
PyObject
*
mmap_mmap_size_impl
(
mmap_object
*
self
)
/*[clinic end generated code: output=c177e65e83a648ff input=f69c072efd2e1595]*/
{
CHECK_VALID
(
NULL
);
#ifdef
MS_WINDOWS
if
(
self
->
file_handle
!=
INVALID_HANDLE_VALUE
) {
LARGE_INTEGER
size
;
if
(!
GetFileSizeEx
(
self
->
file_handle
,
&
size
)) {
DWORD
error
=
GetLastError
();
return
PyErr_SetFromWindowsErr
(
error
);
}
return
PyLong_FromLongLong
(
size
.
QuadPart
);
}
#endif
/* MS_WINDOWS */
#ifdef
UNIX
if
(
self
->
fd
!=
-1
) {
struct
_Py_stat_struct
status
;
if
(
_Py_fstat
(
self
->
fd
,
&
status
)
==
-1
)
return
NULL
;
#ifdef
HAVE_LARGEFILE_SUPPORT
return
PyLong_FromLongLong
(
status
.
st_size
);
#else
return
PyLong_FromLong
(
status
.
st_size
);
#endif
}
#endif
/* UNIX */
else
if
(
self
->
trackfd
) {
return
PyLong_FromSsize_t
(
self
->
size
);
}
else
{
PyErr_SetString
(
PyExc_ValueError
,
"can't get size with trackfd=False"
);
return
NULL
;
}
}
/* This assumes that you want the entire file mapped,
/ and when recreating the map will make the new file
/ have the new size
/
/ Is this really necessary? This could easily be done
/ from python by just closing and re-opening with the
/ new size?
*/
#if
defined(
MS_WINDOWS
)
||
defined(
HAVE_MREMAP
)
/*[clinic input]
@critical_section
mmap.mmap.resize
newsize as new_size: Py_ssize_t
/
[clinic start generated code]*/
static
PyObject
*
mmap_mmap_resize_impl
(
mmap_object
*
self
,
Py_ssize_t
new_size
)
/*[clinic end generated code: output=6f262537ce9c2dcc input=b6b5dee52a41b79f]*/
{
CHECK_VALID
(
NULL
);
if
(!
is_resizeable
(
self
)) {
return
NULL
;
}
if
(
new_size
<
0
||
PY_SSIZE_T_MAX
-
new_size
<
self
->
offset
) {
PyErr_SetString
(
PyExc_ValueError
,
"new size out of range"
);
return
NULL
;
}
{
#ifdef
MS_WINDOWS
DWORD
error
=
0
,
file_resize_error
=
0
;
char
*
old_data
=
self
->
data
;
LARGE_INTEGER
offset
,
max_size
;
offset
.
QuadPart
=
self
->
offset
;
max_size
.
QuadPart
=
self
->
offset
+
new_size
;
/* close the file mapping */
CloseHandle
(
self
->
map_handle
);
/* if the file mapping still exists, it cannot be resized. */
if
(
self
->
tagname
) {
self
->
map_handle
=
OpenFileMappingW
(
FILE_MAP_WRITE
, FALSE,
self
->
tagname
);
if
(
self
->
map_handle
) {
PyErr_SetFromWindowsErr
(
ERROR_USER_MAPPED_FILE
);
return
NULL
;
}
}
else
{
self
->
map_handle
=
NULL
;
}
/* if it's not the paging file, unmap the view and resize the file */
if
(
self
->
file_handle
!=
INVALID_HANDLE_VALUE
) {
if
(!
UnmapViewOfFile
(
self
->
data
)) {
return
PyErr_SetFromWindowsErr
(
GetLastError
());
};
self
->
data
=
NULL
;
/* resize the file */
if
(!
SetFilePointerEx
(
self
->
file_handle
,
max_size
,
NULL
,
FILE_BEGIN
)
||
!
SetEndOfFile
(
self
->
file_handle
)) {
/* resizing failed. try to remap the file */
file_resize_error
=
GetLastError
();
max_size
.
QuadPart
=
self
->
size
;
new_size
=
self
->
size
;
}
}
/* create a new file mapping and map a new view */
/* FIXME: call CreateFileMappingW with wchar_t tagname */
self
->
map_handle
=
CreateFileMappingW
(
self
->
file_handle
,
NULL
,
PAGE_READWRITE
,
max_size
.
HighPart
,
max_size
.
LowPart
,
self
->
tagname
);
error
=
GetLastError
();
/* ERROR_ALREADY_EXISTS implies that between our closing the handle above and
calling CreateFileMapping here, someone's created a different mapping with
the same name. There's nothing we can usefully do so we invalidate our
mapping and error out.
*/
if
(
error
==
ERROR_ALREADY_EXISTS
) {
CloseHandle
(
self
->
map_handle
);
self
->
map_handle
=
NULL
;
}
else
if
(
self
->
map_handle
!=
NULL
) {
self
->
data
=
MapViewOfFile
(
self
->
map_handle
,
FILE_MAP_WRITE
,
offset
.
HighPart
,
offset
.
LowPart
,
new_size
);
if
(
self
->
data
!=
NULL
) {
/* copy the old view if using the paging file */
if
(
self
->
file_handle
==
INVALID_HANDLE_VALUE
) {
memcpy
(
self
->
data
,
old_data
,
self
->
size
<
new_size
?
self
->
size
:
new_size
);
if
(!
UnmapViewOfFile
(
old_data
)) {
error
=
GetLastError
();
}
}
self
->
size
=
new_size
;
}
else
{
error
=
GetLastError
();
CloseHandle
(
self
->
map_handle
);
self
->
map_handle
=
NULL
;
}
}
if
(
error
) {
return
PyErr_SetFromWindowsErr
(
error
);
}
/* It's possible for a resize to fail, typically because another mapping
is still held against the same underlying file. Even if nothing has
failed -- ie we're still returning a valid file mapping -- raise the
error as an exception as the resize won't have happened
*/
if
(
file_resize_error
) {
PyErr_SetFromWindowsErr
(
file_resize_error
);
return
NULL
;
}
Py_RETURN_NONE
;
#endif
/* MS_WINDOWS */
#ifdef
UNIX
void
*
newmap
;
#if
defined(
__linux__
)
||
defined(
__NetBSD__
)
// Linux mremap() refuses to grow a shared anonymous mapping, and
// NetBSD mremap() returns a mapping whose grown region is not backed,
// so accessing it crashes. Reject it here in both cases.
if
(
self
->
fd
==
-1
&&
!(
self
->
flags
&
MAP_PRIVATE
)
&&
new_size
>
self
->
size
) {
PyErr_Format
(
PyExc_ValueError
,
"mmap: can't expand a shared anonymous mapping"
);
return
NULL
;
}
#endif
if
(
self
->
fd
!=
-1
&&
ftruncate
(
self
->
fd
,
self
->
offset
+
new_size
)
==
-1
) {
PyErr_SetFromErrno
(
PyExc_OSError
);
return
NULL
;
}
#ifdef
MREMAP_MAYMOVE
newmap
=
mremap
(
self
->
data
,
self
->
size
,
new_size
,
MREMAP_MAYMOVE
);
#else
#if
defined(
__NetBSD__
)
newmap
=
mremap
(
self
->
data
,
self
->
size
,
self
->
data
,
new_size
,
0
);
#else
newmap
=
mremap
(
self
->
data
,
self
->
size
,
new_size
,
0
);
#endif
/* __NetBSD__ */
#endif
if
(
newmap
==
(
void
*
)
-1
)
{
PyErr_SetFromErrno
(
PyExc_OSError
);
return
NULL
;
}
View remainder of file in raw view
Back
|
FazBrowse Home
|
New Git URL