/
mappedbytearray.pmc
638 lines (501 loc) · 15.6 KB
/
mappedbytearray.pmc
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
/*
Copyright (C) 2010-2014, Parrot Foundation.
=head1 NAME
src/pmc/mappedbytearray.pmc - MappedByteArray PMC
=head1 DESCRIPTION
A memory mapped file or shared memory segment accessible as array of bytes.
Bare minimal functionality available, currently only for posix platforms
with <sys/mman.h> available, or on mingw32.
=cut
*/
#include "parrot/parrot.h"
#include "../src/io/io_private.h"
#define CAN_READ 1
#define CAN_WRITE 2
#ifdef PARROT_HAS_HEADER_SYSMMAN
# include <sys/mman.h>
# define ENABLED
#else
# ifdef __MINGW32__
# define ENABLED
# define MAPW32
# include <Windows.h>
# endif
#endif
/* HEADERIZER HFILE: none */
/* HEADERIZER BEGIN: static */
/* Don't modify between HEADERIZER BEGIN / HEADERIZER END. Your changes will be lost. */
PARROT_CANNOT_RETURN_NULL
static STRING * build_string(PARROT_INTERP,
ARGIN_NULLOK(const unsigned char *content),
INTVAL size,
ARGIN(const STR_VTABLE *encoding))
__attribute__nonnull__(1)
__attribute__nonnull__(4);
PARROT_CANNOT_RETURN_NULL
static STRING * get_encoded_chars(PARROT_INTERP,
ARGIN(PMC *self),
INTVAL pos,
INTVAL length,
ARGIN(const STR_VTABLE *encoding))
__attribute__nonnull__(1)
__attribute__nonnull__(2)
__attribute__nonnull__(5);
PARROT_CAN_RETURN_NULL
static void * mapfromfilehandle(
PIOHANDLE handle,
unsigned long size,
int flag);
PARROT_CAN_RETURN_NULL
static void * mapfromfilename(PARROT_INTERP,
ARGIN(STRING *name),
ARGMOD(unsigned long *size),
int flag)
__attribute__nonnull__(1)
__attribute__nonnull__(2)
__attribute__nonnull__(3)
FUNC_MODIFIES(*size);
PARROT_DOES_NOT_RETURN
static void unavailable(PARROT_INTERP, ARGIN(const char *msg))
__attribute__nonnull__(1)
__attribute__nonnull__(2);
#define ASSERT_ARGS_build_string __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp) \
, PARROT_ASSERT_ARG(encoding))
#define ASSERT_ARGS_get_encoded_chars __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp) \
, PARROT_ASSERT_ARG(self) \
, PARROT_ASSERT_ARG(encoding))
#define ASSERT_ARGS_mapfromfilehandle __attribute__unused__ int _ASSERT_ARGS_CHECK = (0)
#define ASSERT_ARGS_mapfromfilename __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp) \
, PARROT_ASSERT_ARG(name) \
, PARROT_ASSERT_ARG(size))
#define ASSERT_ARGS_unavailable __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp) \
, PARROT_ASSERT_ARG(msg))
/* Don't modify between HEADERIZER BEGIN / HEADERIZER END. Your changes will be lost. */
/* HEADERIZER END: static */
pmclass MappedByteArray auto_attrs {
ATTR unsigned char *buffer;
ATTR UINTVAL size;
/*
=head2 Vtable Functions
=over 4
=item C<init()>
Initialize without doing any mapping.
=item C<init_pmc(PMC *init)>
The argument must be a String PMC with a file name. Maps the whole file.
Other options still not available.
=item C<destroy()>
Free all resources used.
=cut
*/
VTABLE void init() {
#ifdef ENABLED
PObj_custom_destroy_SET(SELF);
#endif
}
VTABLE void init_pmc(PMC * init) {
#ifdef ENABLED
void *mapping = NULL;
PObj_custom_destroy_SET(SELF);
if (VTABLE_isa(INTERP, init, CONST_STRING(INTERP, "String"))) {
unsigned long length = 0;
STRING * const name = VTABLE_get_string(INTERP, init);
mapping = mapfromfilename(interp, name, &length, CAN_READ);
if (mapping == 0 || mapping == (void *)-1)
Parrot_ex_throw_from_c_args(INTERP, NULL,
EXCEPTION_INVALID_OPERATION,
"mmap failed: %s", strerror(errno));
PObj_custom_destroy_SET(SELF);
SET_ATTR_size(INTERP, SELF, length);
SET_ATTR_buffer(INTERP, SELF, (unsigned char*)mapping);
}
else {
unavailable(INTERP, "*feature unknown or not implemented yet*");
}
#else
unavailable(INTERP, "MappedByteArray");
#endif
}
VTABLE void destroy() :no_wb {
#ifdef ENABLED
unsigned char *buffer;
UINTVAL size;
GET_ATTR_size(INTERP, SELF, size);
GET_ATTR_buffer(INTERP, SELF, buffer);
if (size > 0) {
# ifdef MAPW32
UnmapViewOfFile(buffer);
# else
munmap(buffer, size);
# endif
}
#endif
}
/*
=item C<get_bool()>
Return true if active, false otherwise.
=cut
*/
VTABLE INTVAL get_bool() :no_wb {
#ifdef ENABLED
unsigned char *buffer;
GET_ATTR_buffer(INTERP, SELF, buffer);
return buffer != NULL;
#else
return 0;
#endif
}
/*
=item C<elements()>
Return the size of the mapped area.
=cut
*/
VTABLE INTVAL elements() :no_wb {
#ifdef ENABLED
UINTVAL size;
GET_ATTR_size(INTERP, SELF, size);
return size;
#else
return 0;
#endif
}
/*
=item C<get_integer_keyed_int(INTVAL pos)>
Get the byte at pos.
=item C<set_integer_keyed_int(INTVAL pos, INTVAL value)>
Set the byte at pos to value.
=cut
*/
VTABLE INTVAL get_integer_keyed_int(INTVAL pos) :no_wb {
INTVAL size;
unsigned char *buffer;
GET_ATTR_size(INTERP, SELF, size);
if (size <= 0)
Parrot_ex_throw_from_c_noargs(INTERP,
EXCEPTION_INVALID_OPERATION, "not mapped");
if (pos < 0 || pos >= size)
Parrot_ex_throw_from_c_noargs(INTERP, EXCEPTION_OUT_OF_BOUNDS,
"index out of bounds");
GET_ATTR_buffer(INTERP, SELF, buffer);
return buffer[pos];
}
VTABLE void set_integer_keyed_int(INTVAL pos, INTVAL value) {
INTVAL size;
unsigned char *buffer;
GET_ATTR_size(INTERP, SELF, size);
if (size <= 0)
Parrot_ex_throw_from_c_noargs(INTERP,
EXCEPTION_INVALID_OPERATION, "not mapped");
if (pos < 0 || pos >= size)
Parrot_ex_throw_from_c_noargs(INTERP, EXCEPTION_OUT_OF_BOUNDS,
"index out of bounds");
GET_ATTR_buffer(INTERP, SELF, buffer);
buffer[pos] = value;
}
/*
=back
=head2 Methods
=over 4
=item C<supported()>
Return 0 if file mapping is not supported, non zero otherwise.
=cut
*/
METHOD supported() :no_wb {
#ifdef ENABLED
RETURN(INTVAL 1);
#else
RETURN(INTVAL 0);
#endif
}
/*
=item C<open(STRING *filename, STRING *mode :optional)>
Map a file by its name. The mode argument can be "r", "w" or "rw",
"r" is assumed if omitted.
=cut
*/
METHOD open(STRING *filename, STRING *mode :optional) {
#ifdef ENABLED
unsigned long length = 0;
int flag = 0;
void *mapping;
if (STRING_IS_NULL(mode))
flag = CAN_READ;
else {
const INTVAL len = Parrot_str_length(INTERP, mode);
INTVAL i;
for (i = 0; i < len; ++i) {
switch (STRING_ord(INTERP, mode, i)) {
case 'r':
flag |= CAN_READ;
break;
case 'w':
flag |= CAN_WRITE;
break;
default:
Parrot_ex_throw_from_c_noargs(INTERP, EXCEPTION_INVALID_OPERATION,
"invalid open mapped mode");
}
}
}
mapping = mapfromfilename(interp, filename, &length, flag);
if (mapping == 0 || mapping == (void *)-1)
Parrot_ex_throw_from_c_args(INTERP, NULL,
EXCEPTION_INVALID_OPERATION,
"mmap failed: %s", strerror(errno));
SET_ATTR_size(INTERP, SELF, length);
SET_ATTR_buffer(INTERP, SELF, (unsigned char*)mapping);
#endif
RETURN(void);
}
/*
=item C<close()>
Close the mapping. Return 0 if the mapping was opened aand the unmap operation
does not fail, non zero otherwise.
=cut
*/
METHOD close() {
INTVAL result = 1;
#ifdef ENABLED
unsigned char *buffer;
GET_ATTR_buffer(INTERP, SELF, buffer);
if (buffer) {
INTVAL size;
GET_ATTR_size(INTERP, SELF, size);
# ifdef MAPW32
result = ! UnmapViewOfFile(buffer);
# else
result = munmap(buffer, size);
# endif
if (result == 0) {
buffer = NULL;
SET_ATTR_buffer(INTERP, SELF, buffer);
SET_ATTR_size(INTERP, SELF, 0);
}
}
#endif
RETURN(INTVAL result);
}
/*
=item C<get_string(INTVAL pos, INTVAL bytelength, STRING *encodingname)>
Get a string from the buffer content with the specified encoding.
=cut
*/
METHOD get_string(INTVAL pos, INTVAL bytelength, STRING *encodingname) :no_wb {
#ifdef ENABLED
STRING *result;
unsigned char *buffer;
INTVAL size;
const STR_VTABLE * const encoding = Parrot_find_encoding_by_string(INTERP, encodingname);
GET_ATTR_buffer(INTERP, SELF, buffer);
GET_ATTR_size(INTERP, SELF, size);
if (pos < 0 || bytelength < 0 || size < 0 || pos + bytelength > size)
Parrot_ex_throw_from_c_noargs(INTERP,
EXCEPTION_OUT_OF_BOUNDS, "index out of bounds");
result = build_string(INTERP, buffer + pos, bytelength, encoding);
RETURN(STRING *result);
#endif
}
/*
=item C<get_chars(INTVAL pos, INTVAL length)>
Get a string from the buffer content with the specified encoding and
length in codepoints.
=cut
*/
METHOD get_chars(INTVAL pos, INTVAL length, STRING *encodingname) :no_wb {
#ifdef ENABLED
STRING * const result = get_encoded_chars(INTERP, SELF, pos, length,
Parrot_find_encoding_by_string(INTERP, encodingname));
RETURN(STRING *result);
#endif
}
/*
=item C<get_utf8(INTVAL pos, INTVAL length)>
Get a utf8 string from the buffer content with the specified length
in codepoints.
=cut
*/
METHOD get_utf8(INTVAL pos, INTVAL length) :no_wb {
#ifdef ENABLED
STRING * const result = get_encoded_chars(INTERP, SELF, pos, length,
Parrot_utf8_encoding_ptr);
RETURN(STRING *result);
#endif
}
} /* pmclass end */
/*
=back
=head2 Helper functions
=over 4
=item C<static void unavailable(PARROT_INTERP, const char *msg)>
Throws appropriately for functionality not available in the current platform.
=item C<static void * mapfromfilehandle(PIOHANDLE handle, unsigned long size,
int flag)>
Maps a file by its OS handle.
=item C<static void * mapfromfilename(PARROT_INTERP, STRING *name, unsigned long
*size, int flag)>
Maps a file by its filename, throw if the file can't be opened.
=cut
*/
PARROT_DOES_NOT_RETURN
static void
unavailable(PARROT_INTERP, ARGIN(const char *msg))
{
ASSERT_ARGS(unavailable)
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_UNIMPLEMENTED,
"%s not implemented in this platform", msg);
}
PARROT_CAN_RETURN_NULL
static void *
mapfromfilehandle(PIOHANDLE handle, unsigned long size, int flag)
{
ASSERT_ARGS(mapfromfilehandle)
void *mapping = (void *)-1;
#ifdef ENABLED
# ifdef MAPW32
HANDLE hmapping;
DWORD prot, access;
if (flag & CAN_WRITE) {
prot = PAGE_READWRITE;
access = FILE_MAP_WRITE;
}
else {
prot = PAGE_READONLY;
access = FILE_MAP_READ;
}
hmapping = CreateFileMapping(handle, NULL, prot, 0, size, NULL);
if (hmapping != NULL) {
mapping = MapViewOfFile(hmapping, access, 0, 0, size);
CloseHandle(hmapping);
}
# else
int prot = 0;
if (flag & CAN_READ) prot |= PROT_READ;
if (flag & CAN_WRITE) prot |= PROT_WRITE;
mapping = mmap(0, size, prot, MAP_SHARED, handle, 0);
# endif
#endif
return mapping;
}
PARROT_CAN_RETURN_NULL
static void *
mapfromfilename(PARROT_INTERP, ARGIN(STRING *name), ARGMOD(unsigned long *size), int flag)
{
ASSERT_ARGS(mapfromfilename)
void *mapping = (void *)-1;
#ifdef ENABLED
PIOHANDLE handle;
# ifdef MAPW32
DWORD openmode = 0;
switch (flag & (CAN_READ | CAN_WRITE)) {
case CAN_READ:
openmode = GENERIC_READ;
break;
case CAN_READ | CAN_WRITE:
case CAN_WRITE:
openmode = GENERIC_READ | GENERIC_WRITE;
break;
default:
/* Should never happen, invalid modes must be caught before
* calling this function.
*/
Parrot_ex_throw_from_c_noargs(interp,
EXCEPTION_INTERNAL_NOT_IMPLEMENTED, "invalid mmap mode");
}
{
char * const name_str = Parrot_str_to_cstring(interp, name);
handle = CreateFile(name_str, openmode, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
Parrot_str_free_cstring(name_str);
}
# else
int openmode = 0;
switch (flag & (CAN_READ | CAN_WRITE)) {
case CAN_READ:
openmode = O_RDONLY;
break;
case CAN_READ | CAN_WRITE:
case CAN_WRITE:
openmode = O_RDWR;
break;
default:
/* Should never happen, invalid modes must be caught before
* calling this function.
*/
Parrot_ex_throw_from_c_noargs(interp,
EXCEPTION_INTERNAL_NOT_IMPLEMENTED, "invalid mmap mode");
}
{
char * const name_str = Parrot_str_to_cstring(interp, name);
handle = open(name_str, openmode);
Parrot_str_free_cstring(name_str);
}
# endif
if (handle == PIO_INVALID_HANDLE)
Parrot_ex_throw_from_c_args(interp, NULL,
EXCEPTION_INVALID_OPERATION, "cannot open file '%Ss'", name);
if (*size == 0) {
# ifdef MAPW32
*size = (unsigned long)GetFileSize(handle, NULL);
# else
*size = lseek(handle, 0, SEEK_END);
# endif
}
mapping = mapfromfilehandle(handle, *size, flag);
#endif
return mapping;
}
/*
=item C<static STRING * build_string(PARROT_INTERP, const unsigned char
*content, INTVAL size, const STR_VTABLE *encoding)>
Build a string from the buffer content with the encoding specified.
=cut
*/
PARROT_CANNOT_RETURN_NULL
static STRING *
build_string(PARROT_INTERP, ARGIN_NULLOK(const unsigned char *content),
INTVAL size,
ARGIN(const STR_VTABLE *encoding))
{
ASSERT_ARGS(build_string)
if (encoding == NULL)
Parrot_ex_throw_from_c_noargs(interp, EXCEPTION_INVALID_ENCODING,
"Invalid encoding");
else
return Parrot_str_new_init(interp,
(const char *)content, size, encoding, 0);
}
/*
=item C<static STRING * get_encoded_chars(PARROT_INTERP, PMC *self, INTVAL pos,
INTVAL length, const STR_VTABLE *encoding)>
Return the characters starting at pos in the specified encoding.
=cut
*/
PARROT_CANNOT_RETURN_NULL
static STRING *
get_encoded_chars(PARROT_INTERP, ARGIN(PMC *self), INTVAL pos, INTVAL length,
ARGIN(const STR_VTABLE *encoding))
{
ASSERT_ARGS(get_encoded_chars)
unsigned char *buffer;
INTVAL size;
GETATTR_MappedByteArray_buffer(interp, self, buffer);
GETATTR_MappedByteArray_size(interp, self, size);
if (pos < 0 || pos > size)
Parrot_ex_throw_from_c_noargs(interp,
EXCEPTION_OUT_OF_BOUNDS, "index out of bounds");
return Parrot_str_extract_chars(interp, (char *)buffer + pos, size - pos,
length, encoding);
}
/*
=back
=cut
*/
/*
* Local variables:
* c-file-style: "parrot"
* End:
* vim: expandtab shiftwidth=4 cinoptions='\:2=2' :
*/