List Info

Thread: Garbage-collected CObjects




Garbage-collected CObjects
user name
2008-05-06 04:49:22
Having to use two mallocs and an #ensure: block just to wrap
a function 
that accepts two double* arguments is just awful.  But
that's what the 
Cairo bindings have to do!

So, here comes a way to use ByteArrays as an alternative
backing storage 
for CObjects.

-        | ox oy |
-       ox := CDouble value: aPoint x.
-       oy := CDouble value: aPoint y.
-       ^ [
-           block value:self value:ox value: oy
-       ] ensure: [
-           ox ifNotNil: [  | x
free ].
-           oy ifNotNil: [ :y | y free ]].
+       ^block
+           value: self
+           value: (CDouble gcValue: aPoint x)
+           value: (CDouble gcValue: aPoint y)

Much nicer, and more efficient since the less you use
finalization the 
better.

However, it requires more care (it can crash badly if
objects are moved 
by a GC under the feet of C functions!), so it is accessed
using special 
#gcNew and #gcValue: methods, instead of changing the
default instance 
creation methods.

Incidentally, this would have been another way to solve the
finalization 
race problems with CStatStruct, that I fixed a while ago.

I took the opportunity to clean up a little the CObject
docs.

Paolo

_______________________________________________
help-smalltalk mailing list
help-smalltalkgnu.org

http://lists.gnu.org/mailman/listinfo/help-smalltalk

  
[1]

about | contact  Other archives ( Real Estate discussion Medical topics )