Plugin & strings

Andrew C. Greenberg werdna at gate.net
Thu Dec 9 02:54:13 UTC 1999


The code works because the only value taken from C was converted to 
an integer value and stored.  There are no pointers to the Smalltalk 
object space.

You should presume that any call to "instantiateClass:indexableSize" 
will trigger a garbageCollection, invalidating any pointers (oops or 
pointers inside oops) and requiring you to reload or use the 
remappable object stack.

For example, had your plugin example loaded a pointer to an array in 
Smalltalk space, that pointer must be presumed to be invalid after 
the instantiation of a new string, and then reloaded from the stack.

>Here's an example of a primitive which allocates and returns a string. You
>can also pass a string as a parameter on the stack, which is safer from a
>memory management point of view.  I've heard that doing the allocation in the
>primitive can get you into trouble if you run out of object memory, so use
>this with caution and add some error handling.

*snip*

>----- example follows -----
>
>!UnixOSProcessAccessor methodsFor: 'primitives - OS process access' 
>stamp: 'dtl 10/26/1999 11:21'!
>primitiveArgumentAt
>	"Answer a string containing the OS process argument at index 
>(an Integer) in the
>	argument list."
>
>	| index aString start argVec argCnt len argStrLen |
>	self var: 'start' declareC: 'unsigned char *start'.
>	self var: 'argCnt' declareC: 'extern int argCnt'.
>	self var: 'argVec' declareC: 'extern char **argVec'.
>	index _ interpreterProxy stackIntegerValue: 0.
>	((index > argCnt) | (index < 1))
>	ifTrue: [ interpreterProxy primitiveFail ]
>	ifFalse: [
>		index := index - 1.	"Map to C convention where 
>array index starts at 0."
>		argStrLen _ self cCode: 'strlen(argVec[index])'.
>		aString _ interpreterProxy
>			instantiateClass: interpreterProxy classString
>			indexableSize: argStrLen.
>		start _ interpreterProxy arrayValueOf: aString.
>		len _ interpreterProxy sizeOfSTArrayFromCPrimitive: start.
>		self cCode: '(char *)strncpy(start, argVec[index], len)'.
>		interpreterProxy pop: 2; push: aString ]





More information about the Squeak-dev mailing list