Access to an array of OSStructure - why this way ?

VA Smalltalk is a "100% VisualAge compatible" IDE that includes the original VisualAge technology and the popular VA Assist and WidgetKit add-ons.

Moderators: Eric Clayberg, wembley, tc, Diane Engles, solveig

Access to an array of OSStructure - why this way ?

Postby marten » Thu Jan 25, 2007 12:49 pm

Today I came across a nice, hidden "feature" when accessing an array of instances of subclasses of OSStructure and I do not understand, why the access was done this way in VA.

When first working with this data structure I wrote the following code:

Code: Select all
osBlobArray := OSBlob calloc: 10
(osBlobArray at: 0) cbSize: 12.


The intention was, that in the first element of this array the structure member cbSize should be set to 12. But this code failed ....

After reading the source code in the upper class I found out, that it should be written like:

Code: Select all
osBlobArray := OSBlob calloc: 10
osBlobArray at: 0 put: ((osBlobArray at: 0) cbSize: 12).


Why all that copying ? What was the intention behind that ? Or is there a better way to do it ?

Marten
marten
[|]
 
Posts: 641
Joined: Sat Oct 14, 2006 7:10 am
Location: Hamburg - Germany

Postby wembley » Thu Feb 08, 2007 8:30 am

Marten -

Yes, this is the most straightforward way of accessing/updating a member of an array of OSStructures. However, you can use something like the following to perform the update directly in OS memory:
Code: Select all
|osBlobArray element |

osBlobArray := OSBlob calloc: 10.
element := 7.
(osBlobArray pointerAt: osBlobArray class fixedSize * element type: osBlobArray class) cbSize: 12.

Regards, John
wembley
Moderator
 
Posts: 405
Joined: Mon Oct 16, 2006 3:01 am
Location: Durham, NC

Postby TheIconoclast » Tue Apr 03, 2007 8:56 am

You can always go the direct route and just muck with things directly:

Code: Select all
| osBlobArray element cbSizeOffset |
osBlobArray := OSBlob calloc: 10.
element := 7.
cbSizeOffset := 0.
osBlobArray uint32At: (cbSizeOffset + osBlobArray class fixedSize * element) put: 12.

You can then get access that value with
Code: Select all
(osBlobArray at: element) cbSize


Sure it defeats the purpose of using a high-level language, but I'm guessing it could lead to some wonderfully obfuscated code.

----------------------
Brian O'Connell
Simulation Domain
Joint Analysis System (JAS)
TheIconoclast
 
Posts: 10
Joined: Tue Mar 06, 2007 11:51 am
Location: Northern VA

Postby wembley » Tue Apr 03, 2007 11:46 am

Brian -

I find almost all code involved with OSPtr and its subclasses to be wonderfully obfuscated. We are always open to suggestions for how to remedy this situation.
John O'Keefe [|], Principal Smalltalk Architect, Instantiations Inc.
wembley
Moderator
 
Posts: 405
Joined: Mon Oct 16, 2006 3:01 am
Location: Durham, NC


Return to VA Smalltalk 7.0, 7.5 & 8.0

Who is online

Users browsing this forum: No registered users and 1 guest