eXept Software AG Logo

Smalltalk/X Webserver

Documentation of class 'ExternalBytes':

Home

Documentation
www.exept.de
Everywhere
for:
[back]

Class: ExternalBytes


Inheritance:

   Object
   |
   +--Collection
      |
      +--SequenceableCollection
         |
         +--ArrayedCollection
            |
            +--UninterpretedBytes
               |
               +--ExternalBytes
                  |
                  +--Alien
                  |
                  +--ExternalInt
                  |
                  +--ExternalLong
                  |
                  +--ExternalStructure
                  |
                  +--UnprotectedExternalBytes

Package:
stx:libbasic
Category:
System-Support
Version:
rev: 1.125 date: 2018/05/11 07:49:25
user: cg
file: ExternalBytes.st directory: libbasic
module: stx stc-classLibrary: libbasic
Author:
Claus Gittinger

Description:


This class provides access to any memory in the system. Its main purpose
is to provide a baseclass for objects referencing structured external data.
Normally, instances are created by primitive code which wants to pass C-data
to Smalltalk AND grants smalltalk access to individual bytes afterwards.
Primitives which do not want to grant this access should return instances of
ExternalAddress. See more info there. Also, have a look at ExternalFunction
which is another similar class, but specialized to represent callable C-functions.

Since the memory address of an instance stays fixed (once allocated),
it can also be used to share data with external C-parts
(which are not prepared for objects to change their address).

Use with great care - access is not always checked for out-of-bounds
or valid addresses.

Since the data is allocated outside the garbage collected smalltalk space,
its address stays fix. Thus, it can be passed to external C-functions without
any danger. However, you have to take care for freeing the memory yourself.

To help in avoiding memory bugs, instances created with #new: are
registered in a local classvar and deregistered when the underlying memory
is explicitely freed. Since a life reference (from that classvar) exists,
the garbage collector will never find these to be reclaimable, and the
underlying memory stays allocated (at a fix address) forever.
To release the memory, either #free it or #unprotect it.
The first will immediately release the memory, while the second will delay
freeing until the next garbage collect occurs.

If you need memory which is automatically freed, create
the instance via #unprotectedNew: right away; the underlying malloced-memory
will be released as soon as no smalltalk reference to the ExtBytes object
exists any more (however, you have to know for sure, that no C-references
exist to this memory).

To release all memory call #releaseAllMemory which simply sets the
AllocatedInstances class variable to nil (thus releasing those refs).

Example (automatic freeing as soon as ref to buffer is gone):
    |buffer|

    buffer := ExternalBytes unprotectedNew:100.
    ...


Example (manual freeing - never freed, if ref to buffer is gone):
    |buffer|

    buffer := ExternalBytes new:100.
    ...
    buffer free


Example (delayed automatic freeing as soon as ref to buffer is gone):
    |buffer|

    buffer := ExternalBytes new:100.
    ...
    buffer unregister

This class only supports unstructured external data
- see the companion class ExternalStructure for more.

Notice: support for external data is still being developed -
        a parser for C structure syntax and typedefs is on the way,
        making shared data with C programs much easier in the future.

Also notice, that this class may not be available or behave different
in other smalltalk systems, making code using it very unportable.
It is provided for C interfacing only.

Finally note, that ST/X's memory system is much faster than malloc/free
in the normal case - especially for short term temporary objects,
automatically reclaimed object memory is about 5-10 times faster than
malloc/free.
Things may be different for huge byte-valued objects, which are to be
reclaimed by the oldspace colletor.
Anyway, for portability, we strongly warn from using this as a substitute
for byteArrays; it is meant for shared data with external C-functions ONLY.

Debugging:
    since all manual memory systems are subject of obscure errors,
    you may want to turn malloc-tracing on; this traces all allocations/frees
    done here. To do this, evaluate: 'ExternalBytes mallocTrace:true'.

    In addition, you may turn on full debugging (with 'ExternalBytes mallocDebug:true');
    if turned on, all malloc/realloc requests are remembered and later free / realloc
    requests validated against this list (i.e. to detect freeing unallocated chunks).

    To benefit from this in C-code, we recommend you use __stx_malloc() / __stx_free()
    instead of malloc() / free(). To do so, redefine them in a header file (or cc comand line)
    and recompile your external c-libraries with this.

    I used this here to find memory leaks in the Xt libraries (there are still some in
    the HTML widget ...). If mallocDebug is on, #dumpMallocChunks will print out what is
    leftOver. This may help to find trouble spots in your C-code.


Related information:

    ExternalAddress
    ExternalFunction
    ByteArray
    [how to write primitive code]

Class protocol:

initialization
o  initialize

instance creation
o  address: anAddressInteger
return a new ExternalBytes object to access bytes starting at anAddressInteger.
The memory at anAddressInteger has been allocated elsewhere.
The size is not known, therefore byte accesses will NOT be checked for valid index.
Use this, if you get a pointer from some external source (such as a
C-callBack function) and you have to extract bytes from that.

DANGER ALERT: this method allows very bad things to be done to the
system - use with GREAT care (better: do not use it)

o  address: anAddressInteger size: size
return a new ExternalBytes object to access bytes starting at anAddressInteger.
The memory at anAddressInteger has been allocated elsewhere.
The size is known, which allows byte accesses to be checked for valid index.
Use this, if you get a pointer to a structure from some external source
(such as a C-callBack function) and you have to extract things from that.
The pointer is protected from GC
(i.e. I will not free the heap memory,
once the returned reference is no longer in use).
Be careful to avoid memory leaks, when getting malloc'd memory from an external function.

DANGER ALERT: this method allows very bad things to be done to the
system - use with GREAT care (better: do not use it)

o  new: numberOfBytes
allocate some memory usable for data;
the memory safe from being finalized by the garbage collector.
Return a corresponding ExternalBytes object or raise MallocFailure (if malloc fails).

Use this, if you have to pass a block of bytes to some
external destination (such as a C function) which does not copy the
data, but instead keeps a reference to it. For example, many functions
which expect strings simply keep a ref to the passed string - for those,
an ST/X string-pointer is not the right thing to pass, since ST/X objects
may change their address.

DANGER ALERT: the memory is NOT automatically freed until it is either
MANUALLY freed (see #free) or the returned externalBytes object
is unprotected or the classes releaseAllMemory method is called.

o  newNullTerminatedFromString: aString
allocate a null terminated string containing the chars of aString

o  newNullTerminatedFromWideString: aString
allocate a null terminated wide string containing the U16-chars of aString

o  protectedNew: numberOfBytes
allocate some memory usable for data;
the memory safe from being finalized by the garbage collector.
Return a corresponding ExternalBytes object or raise MallocFailure (if malloc fails).

Use this, if you have to pass a block of bytes to some
external destination (such as a C function) which does not copy the
data, but instead keeps a reference to it. For example, many functions
which expect strings simply keep a ref to the passed string - for those,
an ST/X string-pointer is not the right thing to pass, since ST/X objects
may change their address.

DANGER ALERT: the memory is NOT automatically freed until it is either
MANUALLY freed (see #free) or the returned externalBytes object
is unprotected or the classes releaseAllMemory method is called.

o  unprotectedNew: numberOfBytes
allocate some memory usable for data;
the memory is under the control of the garbage collector (i.e. the instance will
be finalized and the malloc'd memory will be freed, if the instance goes away).
Return a corresponding ExternalBytes object or raise MallocFailure (if malloc fails).

DANGER ALERT: the memory block as allocated will be automatically freed
as soon as the reference to the returned externalBytes object
is gone (by the next garbage collect).
If the memory has been passed to a C-function which
remembers this pointer, bad things may happen ....

instance release
o  releaseAllMemory
... the next GC will get 'em

malloc debug
o  dumpMallocChunks
self dumpMallocChunks

o  freeAllMallocChunks
free all stx_malloc'd memory. Be careful, this does no validation at all;
It simply walks through all chunks and frees them unconditionally.
This may be helpful during debugging memory-leaks, to release memory which
was not correctly freed by C-code. Howeve, only memory which was allocated
by __stx_malloc() is freed here - so you better compile your primitive code with
malloc redefined to stx_malloc.
Also, mallocDebug has to be on to do this.

o  mallocDebug: aBoolean
ExternalBytes mallocDebug:true
ExternalBytes mallocDebug:false

o  mallocStatistics
self mallocStatistics

o  mallocTrace: aBoolean
ExternalBytes mallocTrace:true
ExternalBytes mallocTrace:false

o  numberOfAllocatedChunks
self numberOfAllocatedChunks

queries
o  charTypeIsSigned
return true, if the machine's native chars are signed
usage example(s):
     ExternalBytes charTypeIsSigned

o  doubleAlignment
return the alignement of longs in structs and unions
usage example(s):
     ExternalBytes doubleAlignment

o  isBuiltInClass
return true if this class is known by the run-time-system.
Here, true is returned.

o  longAlignment
return the alignement of longs in structs and unions
usage example(s):
     ExternalBytes longAlignment

o  pointerAlignment
return the alignement of pointers in structs and unions
usage example(s):
     ExternalBytes pointerAlignment

o  sizeofDouble
return the number of bytes used by the machines native doubles
usage example(s):
     ExternalBytes sizeofDouble

o  sizeofEnums
return the number of bytes used by the machine's native enums.
Be aware, that this can be adjusted in some compilers via the __packed__ attribute;
So better double check...
usage example(s):
     ExternalBytes sizeofEnums

o  sizeofFloat
return the number of bytes used by the machine's native floats
usage example(s):
     ExternalBytes sizeofFloat

o  sizeofInt
return the number of bytes used by the machine's native integer
usage example(s):
     ExternalBytes sizeofInt

o  sizeofLong
return the number of bytes used by the machine's native longs
usage example(s):
     ExternalBytes sizeofLong

o  sizeofLongDouble
return the number of bytes used by the machine's native longdouble.
If the machine does not support them, return nil.
usage example(s):
     ExternalBytes sizeofLongDouble

o  sizeofLongLong
return the number of bytes used by the machine's native longlongs.
If the machine does not support them, return nil.
usage example(s):
     ExternalBytes sizeofLongLong

o  sizeofNativeInt
return the number of bytes used by the machine's SmallInteger native values
usage example(s):
     ExternalBytes sizeofNativeInt

o  sizeofPointer
return the number of bytes used by the machine's native pointer.
Notice: this is inlined by the compiler(s) as a constant,
therefore, queries like
'ExternalAddress pointerSize == 8'
cost nothing; they are compiled in as a constant
(and even conditionals are eliminated).
usage example(s):
     ExternalBytes sizeofPointer

o  sizeofShort
return the number of bytes used by the machine's native short
usage example(s):
     ExternalBytes sizeofShort


Instance protocol:

accessing
o  address
return the start address as an integer

o  basicAt: index
return the byte at index, anInteger;
Indices are 1-based, therefore
this is the byte at (address + index - 1)

o  basicAt: index put: value
set the byte at index, anInteger to value which must be 0..255.
Returns value (sigh).
Indices are 1-based, therefore
this is the byte at (address + index - 1)

o  copyCStringFromHeap
fetch a 0-terminated string from my pointed-to address

o  copyUnicodeStringFromHeap
fetch a 0-terminated wide-string from my pointed-to address

o  instVarAt: index
redefined to suppress direct access to my address, which is a non-object

converting
o  asExternalAddress
return the start address as an external address

o  asExternalBytes

o  asString
speed up string conversions
usage example(s):
      #[16r41 16r42 16r43] asExternalBytes asString

filling & replacing
o  replaceBytesFrom: start to: stop with: aCollection startingAt: repStart
replace elements from another collection, which must be a ByteArray-
like collection.

Notice: This operation modifies the receiver, NOT a copy;
therefore the change may affect all others referencing the receiver.
usage example(s):
     ((ExternalBytes unprotectedNew:16)
	    replaceBytesFrom:1 to:8
	    with:#[10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160]
	    startingAt:1) copy

     (ExternalBytes unprotectedNew:16)
	    replaceBytesFrom:3 to:10
	    with:#[10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160]
	    startingAt:4

     (ExternalBytes unprotectedNew:16)
	    replaceBytesFrom:3 to:4
	    with:#[10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160]
	    startingAt:1

     (ExternalBytes unprotectedNew:16)
	    replaceBytesFrom:0 to:9
	    with:#[10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160]
	    startingAt:1

     (ExternalBytes unprotectedNew:16)
	    replaceBytesFrom:1 to:10
	    with:#[10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160]
	    startingAt:0

o  replaceNullTerminatedFromString: aString
replace elements from aString, and add a 0-byte at the end

finalization
o  executor
redefined to return a lightweight copy
- all we need is the memory handle and the size.
usage example(s):
      (ExternalBytes unprotectedNew:10) executor

o  finalizationLobby
answer the registry used for finalization.
ExternalBytes have their own Registry

o  finalize
some ExternalBytes object was finalized;
free the associated heap memory with it

freeing
o  free
free a previously allocated piece of memory - be very careful, there
are no checks done here. All dangers you usually have with malloc/free
are present here ...
usage example(s):
at least, we check for double freeing the same chunk

o  register
register the receiver to be automatically finalized by the GC

pointer arithmetic
o  referenceToBytesFrom: start to: stop
answer a new ExternalBytes referencing a range within the receiver.
BE CAREFUL: after the receiver has been freed, the new ExternalBytes
contents is undefined

printing & storing
o  displayOn: aGCOrStream
return a printed representation of the receiver for displaying

private-accessing
o  invalidateReference
clear the start address and size

o  setAddress: aNumberOrExternalAddress size: sz
set the start address and size

o  setSize: sz
set the size - warning: dangerous if wrong

private-allocation
o  allocateBytes: numberOfBytes
allocate (malloc) numberOfBytes; if doClear is true, the allocated memory is cleared.
Fail if already allocated.
Raise MallocFailure if malloc fails to allocate enough memory

o  allocateBytes: numberOfBytes clear: doClear
allocate (malloc) numberOfBytes; if doClear is true, the allocated memory is cleared.
Fail if already allocated.
Raise MallocFailure if malloc fails to allocate enough memory

queries
o  basicSize
we do not know how many bytes are valid

o  containsNon7BitAscii
return true, if any byte in the receiver has the 7th bit on.
This my look as a too specific operation to be put here,
put it is very helpful for UTF8 string reading (Java class reader),
to quickly determine, if UTF8 decoding is needed or not.
As most strings in a class file are in fact only containing 7bit ascii,
this should speedup class file reading considerably
usage example(s):
     #[1 2 3 1 2 3 1 2 127 ] asExternalBytes containsNon7BitAscii
     #[1 2 3 1 2 3 1 2 250 251 250 251 255] asExternalBytes containsNon7BitAscii

o  isNull

o  isValid
true if I have an address

o  species
when copying, or concatenating, return instances of this class

registration
o  forgetMemory
forget the underlying memory - i.e. it will NOT be freed by me,
and actually no reference to the underlying memory is kept.
Warning:
Unless freed by someone else (typically a C-program/client),
this leads to a memory leak.
Use this only, if memory which was allocated by me
is given to a C-program which frees the memory.

o  protectFromGC
enter a reference to the receiver into the AllocatedInstances
class variable - this prevents it from ever being finalized by
the garbage collector, thus protecting the underlying memory.
usage example(s):
using a Semaphore can cause a deadlock, since unprotectFromGC may be called by
     a finalization method

o  unprotectFromGC
remove the receiver from the AllocatedInstances
class variable - if there is no other reference to the receiver,
and this was ever allocated by me (i.e. not by the outside world),
the next garbage collect will finalize the receiver and the underlying
memory be freed.

resizing
o  grow: numberOfBytes
reallocate (realloc) numberOfBytes.
Raise MallocFailure if realloc fails to allocate enough memory

testing
o  isExternalBytes


Examples:


These examples below are not directly executable; some require primitive code to be compiled, or are simply given as sceletton. Notice, that in C, indexing is 0-based, while in Smalltalk, indices start at 1. allocating memory in ST, passing it to C: in smalltalk: ... bytes := ExternalBytes new:100. ... in C (assuming that the bytes-object has been passed): ... char *bytePtr; bytePtr = (char *)(__externalBytesAddress(bytes)); if (bytePtr) { ... do something with bytes at bytePtr ... } freeing (in ST): ... bytes free. ... allocating memory in C, passing it to ST: in C: ... char *bytePtr; bytePtr = (char *)(malloc(100)); ... RETURN (__MKEXTERNALBYTES(bytePtr)); in Smalltalk (extracting bytes, integers or strings): byteVal := bytes byteAt:1. ... intVal := bytes doubleWordAt:1 MSB:true. ... string := bytes stringAt:20. ... |data bytes2| data := #[0 1 2 3 4 5 6 7 8 9 9 8 7 6 5 4 3 2 1 0] copy. bytes2 := ExternalBytes new:30. bytes2 replaceBytesFrom:1 to:20 with:data startingAt:1. data replaceBytesFrom:2 to:20 with:bytes2 startingAt:1. bytes2 |data bytes1 bytes2| data := #[0 1 2 3 4 5 6 7 8 9 9 8 7 6 5 4 3 2 1 0]. bytes1 := ExternalBytes new:30. bytes2 := ExternalBytes new:30. bytes1 replaceBytesFrom:1 to:20 with:data startingAt:1. bytes2 atAllPut:99. bytes2 replaceBytesFrom:2 to:21 with:bytes1 startingAt:1. bytes2 asByteArray |data1 bytes1 data2| data1 := #[0 1 2 3 4 5 6 7 8 9 9 8 7 6 5 4 3 2 1 0]. bytes1 := ExternalBytes new:30. bytes1 replaceBytesFrom:1 to:20 with:data1 startingAt:1. data2 := ByteArray new:30 withAll:99. data2 replaceBytesFrom:2 to:21 with:bytes1 startingAt:1. data2

ST/X 7.1.0.0; WebServer 1.663 at exept.de:8081; Wed, 21 Nov 2018 18:47:56 GMT