Repository navigation
Implement mmap.mmap.move() #1370
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -53,6 +53,7 @@ | |
| import static com.oracle.graal.python.nodes.ErrorMessages.MMAP_INDICES_MUST_BE_INTEGER; | ||
| import static com.oracle.graal.python.nodes.ErrorMessages.MMAP_ITEM_VALUE_MUST_BE_AN_INT; | ||
| import static com.oracle.graal.python.nodes.ErrorMessages.MMAP_ITEM_VALUE_MUST_BE_IN_RANGE; | ||
| import static com.oracle.graal.python.nodes.ErrorMessages.MMAP_MOVE_OUT_OF_RANGE; | ||
| import static com.oracle.graal.python.nodes.ErrorMessages.MMAP_OBJECT_DOESNT_SUPPORT_ITEM_DELETION; | ||
| import static com.oracle.graal.python.nodes.ErrorMessages.MMAP_OBJECT_DOESNT_SUPPORT_SLICE_DELETION; | ||
| import static com.oracle.graal.python.nodes.ErrorMessages.MMAP_SLICE_ASSIGNMENT_IS_WRONG_SIZE; | ||
|
|
@@ -125,6 +126,7 @@ | |
| import com.oracle.graal.python.nodes.function.PythonBuiltinNode; | ||
| import com.oracle.graal.python.nodes.function.builtins.PythonBinaryClinicBuiltinNode; | ||
| import com.oracle.graal.python.nodes.function.builtins.PythonClinicBuiltinNode; | ||
| import com.oracle.graal.python.nodes.function.builtins.PythonQuaternaryBuiltinNode; | ||
| import com.oracle.graal.python.nodes.function.builtins.PythonQuaternaryClinicBuiltinNode; | ||
| import com.oracle.graal.python.nodes.function.builtins.PythonTernaryClinicBuiltinNode; | ||
| import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode; | ||
|
|
@@ -832,6 +834,76 @@ static int doIt(VirtualFrame frame, PMMap self, Object dataBuffer, | |
| } | ||
| } | ||
|
|
||
| @Builtin(name = "move", minNumOfPositionalArgs = 4, numOfPositionalOnlyArgs = 4, parameterNames = {"$self", "dest", "src", "count"}) | ||
| @GenerateNodeFactory | ||
| abstract static class MoveNode extends PythonQuaternaryBuiltinNode { | ||
| private static final int BUFFER_SIZE = 64 * 1024; | ||
|
|
||
| @Specialization | ||
| static Object move(VirtualFrame frame, PMMap self, Object destObj, Object srcObj, Object countObj, | ||
| @Bind Node inliningTarget, | ||
| @Bind PythonContext context, | ||
| @Cached LongIndexConverterNode indexConverter, | ||
| @Cached PConstructAndRaiseNode.Lazy constructAndRaiseNode, | ||
| @Cached PRaiseNode raiseNode) { | ||
| // Like CPython: check validity before converting the arguments, and again after the | ||
| // conversion, because __index__ can run arbitrary code that closes the mapping. | ||
| if (self.isClosed()) { | ||
| throw raiseNode.raise(inliningTarget, ValueError, MMAP_CLOSED_OR_INVALID); | ||
| } | ||
| long dest = indexConverter.executeLong(frame, destObj); | ||
| long src = indexConverter.executeLong(frame, srcObj); | ||
| long count = indexConverter.executeLong(frame, countObj); | ||
| if (!self.isWriteable()) { | ||
| throw raiseNode.raise(inliningTarget, TypeError, MMAP_CANNOT_MODIFY_READONLY_MEMORY); | ||
| } | ||
| long len = self.getLength(); | ||
| if (dest < 0 || src < 0 || count < 0 || len - dest < count || len - src < count) { | ||
| throw raiseNode.raise(inliningTarget, ValueError, MMAP_MOVE_OUT_OF_RANGE); | ||
| } | ||
| if (self.isClosed()) { | ||
| throw raiseNode.raise(inliningTarget, ValueError, MMAP_CLOSED_OR_INVALID); | ||
| } | ||
| if (count == 0 || dest == src) { | ||
| return PNone.NONE; | ||
| } | ||
| PosixSupport posixSupport = context.getPosixSupport(); | ||
| Object handle = self.getPosixSupportHandle(); | ||
| /* | ||
| * Equivalent of memmove: copy through an intermediate buffer in chunks. If the | ||
| * destination is after the source, copy from the end so that overlapping source bytes | ||
| * are read before they are overwritten. | ||
| */ | ||
| byte[] buffer = new byte[(int) Math.min(count, BUFFER_SIZE)]; | ||
| try { | ||
| if (dest < src) { | ||
| for (long off = 0; off < count; off += buffer.length) { | ||
| int n = (int) Math.min(buffer.length, count - off); | ||
| copyChunk(inliningTarget, posixSupport, handle, src + off, dest + off, buffer, n, raiseNode); | ||
| } | ||
| } else { | ||
| for (long end = count; end > 0; end -= buffer.length) { | ||
| int n = (int) Math.min(buffer.length, end); | ||
| long off = end - n; | ||
| copyChunk(inliningTarget, posixSupport, handle, src + off, dest + off, buffer, n, raiseNode); | ||
| } | ||
| } | ||
| } catch (PosixException e) { | ||
| throw constructAndRaiseNode.get(inliningTarget).raiseOSErrorFromPosixException(frame, e); | ||
| } | ||
| return PNone.NONE; | ||
| } | ||
|
|
||
| private static void copyChunk(Node inliningTarget, PosixSupport posixSupport, Object handle, long from, long to, byte[] buffer, int n, PRaiseNode raiseNode) | ||
| throws PosixException { | ||
| int nread = posixSupport.mmapReadBytes(handle, from, buffer, n); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [P2] Support moves on write-only file mappings in the Java backend With |
||
| if (nread != n) { | ||
| throw raiseNode.raise(inliningTarget, PythonBuiltinClassType.SystemError, MMAP_CHANGED_LENGTH); | ||
| } | ||
| posixSupport.mmapWriteBytes(handle, to, buffer, n); | ||
|
Comment on lines
+899
to
+903
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [P3] Handle short transfers when moving bytes on Java-backed maps A |
||
| } | ||
| } | ||
|
|
||
| @Builtin(name = "seek", parameterNames = {"$self", "dist", "how"}) | ||
| @ArgumentClinic(name = "dist", conversion = ClinicConversion.LongIndex) | ||
| @ArgumentClinic(name = "how", conversion = ClinicConversion.Int) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[P3] Check for a closed mapping before converting move arguments
PythonQuaternaryClinicBuiltinNodeconverts the arguments beforeMoveNode.movechecksself.isClosed(). On a closed map,m.move(None, 0, 1)therefore raisesTypeError, while CPython 3.13 raisesValueErrorwithout inspecting the arguments. Check for closure before conversion and keep the check after conversion because__index__can run Python code and close the mapping.