Open Bug 553634 Opened 14 years ago Updated 2 years ago

Implement ctypes.unsafe.memcpy, memset, memcmp and friends

Categories

(Core :: js-ctypes, enhancement, P3)

enhancement

Tracking

()

People

(Reporter: dwitte, Unassigned)

References

(Blocks 1 open bug)

Details

Say you have two CDatas of PointerType and ArrayType, resp., and you want to (unsafely) treat them as arrays and copy 'n' bytes from one to the other. (In C, this is 'memcpy(dst, src, n)'.)

  let dst_ptr = ctypes.voidptr_t(...);
  let src_arr = ctypes.int32_t.array(m)(...);

Presently you must:

  ctypes.cast(dst_ptr, ctypes.int8_t.array(src_arr.constructor.size * 4).ptr).contents = src_arr;

God help you if you want to actually copy data from one pointer to another.

Ultimately we want something like

  ctypes.unsafe.memcpy(dst_ptr, src_ptr, n)

and corresponding functions for memcmp, and maybe memmove and memset. (memcpy would, of course, throw if you try to copy overlapping regions.)
The spec for these should basically be "these behave as if you'd done, ctypes.unsafe.memcpy = libc.declare("memcpy", ...)" with the appropriate signature. That way all the silly details about which conversions are specified.
The memcpy case in the description is of course really unsafe.

There's also a use case where this is safe as houses.
   const bytearray = ctypes.uint8_t.array();
   var img1 = bytearray(size1), img2 = bytearray(size2);
   populate(img1);
   ctypes.unsafe.memcpy(img1, img2, size1);  // safe assuming size1 < size2

I see 2 nice ways to support these safe array copies safely:

 1. Add a slice() method to CData objects of array type, which returns
    a new CData object referring to a contiguous slice of the same array.
    Then we have:

      img2.slice(0, size1).value = img1;

 2. Make a ctypes.arrayCopy() function that behaves just like Java's
    System.arraycopy:

      ctypes.arrayCopy(img1, 0, size1, img2, 0);

The latter seems more appropriate, since slice() is not very C-like.
P2, nice to have for 1.9.3. Just API additions to make life easier, but people can always copy code from the wiki. That said, not hard to fix.
Priority: -- → P2
Priority: P2 → P3
Type: defect → enhancement
Severity: normal → S3
You need to log in before you can comment on or make changes to this bug.