extern "rtlib"

Forum for discussion about the documentation project.
Post Reply
coderJeff
Site Admin
Posts: 4326
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

extern "rtlib"

Post by coderJeff »

Recently added in fbc 1.08 is the extern "rtlib" block:
* fbc: add extern "rtlib"

- add extern "rtlib" block, to make convenient, declaring functions within fb source code, but defined in fb's rtlib
- combines the behaviour of extern "c" with fbc default calling convention
- used in a namespace, has the effect of respecting the namespace within fb source code while also linking to regular C function
documentation updated in wiki: EXTERN...END EXTERN

For example, rtlib has function fb_MemCopyClear() that wraps a crt memcpy() call followed by a memset() call.

Code: Select all

namespace FB
	extern "rtlib"
		declare sub MemCopyClear alias "fb_MemCopyClear"_
			( _
				byval dst as any ptr, _ 
				byval dstlen as integer, _ 
				byval src as const any ptr, _ 
				byval srclen as integer _ 
			)
	end extern
end namespace

dim src as zstring * 10 = "FreeBASIC"
dim dst as zstring * 10 = "012345678"

print "before:"
print "src = " & src
print "dst = " & dst
print

'' copy first 4 bytes and clear the rest
FB.MemCopyClear( @dst, sizeof(dst), @src, 4 )

print "after:"
print "src = " & src
print "dst = " & dst
print
OUTPUT:

Code: Select all

before:
src = FreeBASIC
dst = 012345678

after:
src = FreeBASIC
dst = Free
Related:
Updating ASM documentation only insofar as default calling conventions are concerned.
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: extern "rtlib"

Post by D.J.Peters »

Length memory size is size_t (so far i know) = unsigned so it should be uinteger in FB ?

Joshy
coderJeff
Site Admin
Posts: 4326
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Re: extern "rtlib"

Post by coderJeff »

D.J.Peters wrote:Length memory size is size_t (so far i know) = unsigned so it should be uinteger in FB ?
Take care with fb strings, a size or length of '-1' indicates special conditions, and a high bit set indicates temporary string:

from ./src/rtlib/mem_copyclear.c:

Code: Select all

FBCALL void fb_MemCopyClear
	(
		unsigned char *dst,
		ssize_t dstlen,
		unsigned char *src,
		ssize_t srclen
	)
ssize_t = integer is expected.
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: extern "rtlib"

Post by fxm »

coderJeff wrote:Take care with fb strings, a size or length of '-1' indicates special conditions, and a high bit set indicates temporary string
I never seen that on a temporary string!

Code: Select all

Print Len(Type<String>("FreeBASIC"))
Print Cptr(Integer Ptr, @Type<String>("FreeBASIC"))[0]
Print Cptr(Integer Ptr, @Type<String>("FreeBASIC"))[1]
Print Cptr(Integer Ptr, @Type<String>("FreeBASIC"))[2]
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: extern "rtlib"

Post by MrSwiss »

fxm wrote:I never seen that on a temporary string!
AFAIK, this referres to 'internally used temporary string' within string functions,
such as, e.g. a string concatenation, that requires a memory reallocation.

The temp. string holds "source string data" during memory reallocation, before
the actual concatenation takes place. Thereafter, instantly destroyed.
coderJeff
Site Admin
Posts: 4326
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Re: extern "rtlib"

Post by coderJeff »

Temporary strings are created from results of string expressions. When a temporary string gets passed to another rtlib function, which includes the assignment and result loading functions, the temporary string gets deleted.

In normal fb code, you should never see the temp string bit as an end result. But every time you have a string expression there is a temporary string that gets created.

32-bit -gen gas ONLY. (This hack won't work with -gen gcc)

Code: Select all

type FBSTRING
	data as zstring ptr
	length as integer
	size as integer
end type

'' take control of the rtlib left function
#undef fb_LEFT

function fb_LEFT alias "fb_LEFT" ( byval s as FBSTRING ptr, byval count as integer ) as FBSTRING ptr
	function = 0
	print s->data
	print s->length
	print s->size
	'' NOTE: die here because we don't follow any rtlib rules 
	'' i.e. thread locking and temp string clean-up
	end 1
end function

dim p as string

dim x as string  = "hi there"

p = left( right( x, 5 ), 3 ) 
Pseudo code is:
assign( x, "hi there" )
tmp1 <= right(x, 5)
tmp2 <= left(tmp1, 3)
delete( tmp1 )
assign( p, tmp2 )
delete( tmp2 )

EDIT: here's the output:
6368960
-2147483643
36
coderJeff
Site Admin
Posts: 4326
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Re: extern "rtlib"

Post by coderJeff »

D.J.Peters wrote:Length memory size is size_t (so far i know) = unsigned so it should be uinteger in FB ?
Left overs from way-way back. Probably can be changed. Only "L|RSET struct, struct" calls fb_MemCopyClear(), so no string expression should ever reach it and unsigned integer should be OK.
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: extern "rtlib"

Post by D.J.Peters »

D.J.Peters wrote:size is size_t (so far i know)
that's where the confusion came from :-)

memmove(dest as any ptr, src as any ptr, n as size_t) as any ptr
memset(s as any ptr, c as integer, n as size_t) as any ptr


Joshy
Post Reply