Skip to content

Need realloc/resize feature for mallocForeignPtrBytes allocated memory

Hi,

it would be great if there would be something to reallocate or resize the memory allocated by

  • mallocPlainForeignPtrBytes or
  • mallocForeignPtrBytes

Currently this is only possible with memory allocated via mallocBytes by using reallocBytes, but that's using the system's normal malloc()/realloc() and will not return memory in the GC heap.

My proposal for a reallocForeignPtrBytes would have the same effects as reallocBytes. I.e.

  • if the new size is bigger than the old size, make enough free space available. This probably might result in copying the previous memory area to a newly allocated one
  • if the new size is smaller than the old size, mark mark the memory area as smaller. It should not result in immediate copying of the data, but it would e.g. allow the GC during a compacting phase to release the additional memory (note: I don't know how the GHC GC works internally)
  • if the new size is the same, do nothing

One possible use case for this would be in ByteString. Currently Data.ByteString.Internal.createAndTrim will copy the completely memory if the actual size was less than the expected maximum size. This should be unnecessary and only wastes CPU cycles.

It would also be useful for bindings to native libraries, e.g. for allocating the output buffer for iconv().

Trac metadata
Trac field Value
Version 7.8.3
Type FeatureRequest
TypeOfFailure OtherFailure
Priority low
Resolution Unresolved
Component Core Libraries
Test case
Differential revisions
BlockedBy
Related
Blocking
CC core-libraries-committee@haskell.org
Operating system
Architecture
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information