gcMalloc can fail when garbage collection could make space

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

gcMalloc can fail when garbage collection could make space

David Lattimore
While we're on the topic of memory allocation....

If you call CType>>gcMalloc: repeatedly, only using the returned memory
for a short time, you can quickly get allocation failures where running
a GC would free previous allocations allowing subsequent ones to succeed.

For example the following line fails fairly quickly.
1000 timesRepeat: [CIntegerType unsignedChar gcMalloc: 20971520].

Changing CType>>primMalloc:pointerKind: to do a quick GC then retry
makes the above code run without error.

primMalloc: dataSize pointerKind: pointerKind
    <primitive: 397>
    ObjectMemory quickGC.
    ^self primMallocNoRetry: dataSize pointerKind: pointerKind

primMallocNoRetry: dataSize pointerKind: pointerKind
    <primitive: 397>
    ^self primitiveFailed

David