Skip to content

Made mmap-related code compatible with Cygwin #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# csvmonkey
# csvmonkey - patched to work under Cygwin

This is a header-only vectorized, lazy-decoding, zero-copy CSV file parser.
Given appropriate input data and hardware, the C++ version can tokenize ~1.9
Expand Down
9 changes: 9 additions & 0 deletions include/csvmonkey.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,14 @@ class MappedFileCursor
throw Error("fstat", strerror(errno));
}

#ifdef __CYGWIN__
startp_ = (char *) mmap(0, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
::close(fd);
if(! startp_) {
throw Error("mmap", "could not allocate mmap");
}
#else

// UNIX sucks. We can't use MAP_FIXED to ensure a guard page appears
// after the file data because it'll silently overwrite mappings for
// unrelated stuff in RAM (causing bizarro unrelated errors and
Expand Down Expand Up @@ -186,6 +194,7 @@ class MappedFileCursor
guardp_, startp, startp_);
throw Error("mmap", "could not place data below guard page");
}
#endif

::madvise(startp_, st.st_size, MADV_SEQUENTIAL);
::madvise(startp_, st.st_size, MADV_WILLNEED);
Expand Down
20 changes: 20 additions & 0 deletions tests/csvmonkey_test_mmap.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

import unittest
import os

import csvmonkey


class LengthTest(unittest.TestCase):
def test_mmap_last_field(self):
dirname = os.path.join(os.path.dirname(__file__), 'data')

for mod_length in range(15):
filename = os.path.join(dirname, f'length-{mod_length}.csv')
for row in csvmonkey.from_path(filename, header=True):
f = row["ResourceId"]
self.assertEqual(f, 'x' * (mod_length + 1))


if __name__ == '__main__':
unittest.main()
131 changes: 131 additions & 0 deletions tests/data/length-0.csv

Large diffs are not rendered by default.

131 changes: 131 additions & 0 deletions tests/data/length-1.csv

Large diffs are not rendered by default.

131 changes: 131 additions & 0 deletions tests/data/length-10.csv

Large diffs are not rendered by default.

131 changes: 131 additions & 0 deletions tests/data/length-11.csv

Large diffs are not rendered by default.

131 changes: 131 additions & 0 deletions tests/data/length-12.csv

Large diffs are not rendered by default.

131 changes: 131 additions & 0 deletions tests/data/length-13.csv

Large diffs are not rendered by default.

131 changes: 131 additions & 0 deletions tests/data/length-14.csv

Large diffs are not rendered by default.

131 changes: 131 additions & 0 deletions tests/data/length-15.csv

Large diffs are not rendered by default.

131 changes: 131 additions & 0 deletions tests/data/length-2.csv

Large diffs are not rendered by default.

131 changes: 131 additions & 0 deletions tests/data/length-3.csv

Large diffs are not rendered by default.

131 changes: 131 additions & 0 deletions tests/data/length-4.csv

Large diffs are not rendered by default.

131 changes: 131 additions & 0 deletions tests/data/length-5.csv

Large diffs are not rendered by default.

131 changes: 131 additions & 0 deletions tests/data/length-6.csv

Large diffs are not rendered by default.

131 changes: 131 additions & 0 deletions tests/data/length-7.csv

Large diffs are not rendered by default.

131 changes: 131 additions & 0 deletions tests/data/length-8.csv

Large diffs are not rendered by default.

131 changes: 131 additions & 0 deletions tests/data/length-9.csv

Large diffs are not rendered by default.

10 changes: 6 additions & 4 deletions third_party/cpuid.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@

import platform
import os
import sys
import ctypes
from ctypes import c_uint32, c_int, c_size_t, c_void_p, POINTER, CFUNCTYPE
from ctypes import c_uint32, c_int, c_long, c_ulong, c_size_t, c_void_p, POINTER, CFUNCTYPE

# Posix x86_64:
# Two first call registers : RDI, RSI
Expand Down Expand Up @@ -66,7 +65,7 @@
0xc3 # ret
]

is_windows = os.name == "nt" or sys.platform == "cygwin"
is_windows = os.name == "nt"
is_64bit = ctypes.sizeof(ctypes.c_voidp) == 8

class CPUID_struct(ctypes.Structure):
Expand Down Expand Up @@ -99,6 +98,8 @@ def __init__(self):
self.r = CPUID_struct()

if is_windows:
self.win.VirtualAlloc.restype = c_void_p
self.win.VirtualAlloc.argtypes = [ctypes.c_void_p, ctypes.c_size_t, ctypes.c_ulong, ctypes.c_ulong]
self.addr = self.win.VirtualAlloc(None, size, 0x1000, 0x40)
if not self.addr:
raise MemoryError("Could not allocate RWX memory")
Expand Down Expand Up @@ -128,6 +129,8 @@ def __call__(self, eax):

def __del__(self):
if is_windows:
self.win.VirtualFree.restype = c_long
self.win.VirtualFree.argtypes = [c_void_p, c_size_t, c_ulong]
self.win.VirtualFree(self.addr, 0, 0x8000)
elif self.libc:
# Seems to throw exception when the program ends and
Expand All @@ -149,4 +152,3 @@ def valid_inputs():
print(" ".join(x.ljust(8) for x in ("CPUID", "A", "B", "C", "D")).strip())
for eax, regs in valid_inputs():
print("%08x" % eax, " ".join("%08x" % reg for reg in regs))