Handy Code Snippet

| | Comments (0)

For those of you who used to use the SetRLimit plugin I supplied ages ago to increase the resource limit of your application on OS X -- here's it in declare form:
[rbcode]Module RLimit
Protected Function Get() As Double
Soft Declare Function getrlimit Lib "System.Framework" ( mode as Integer, lim as Ptr ) as Integer

Const RLIMIT_NOFILE = 8
dim lim as new MemoryBlock( 16 )

if getrlimit( RLIMIT_NOFILE, lim ) = 0 then
// The first 8 bytes (long long) are the current
// rlimit
return LongLongToDouble( lim )
end if
End Function

Private Sub IntToLongLong(mb as MemoryBlock, i as Integer, pos as Integer)
mb.Long( pos ) = 0
mb.Long( pos + 4 ) = i
End Sub

Private Function LongLongToDouble(mb as MemoryBlock) As Double
Dim ret as Double

ret = mb.Long( 0 ) * (2 ^ 32)

ret = ret + mb.Long( 4 )

return ret
End Function

Protected Function Set(newLim as Integer) As Boolean
Soft Declare Function setrlimit Lib "System.Framework" ( mode as Integer, lim as Ptr ) as Integer

Const RLIMIT_NOFILE = 8
dim lim as new MemoryBlock( 16 )

IntToLongLong( lim, newLim, 0 )
IntToLongLong( lim, newLim, 8 )

if setrlimit( RLIMIT_NOFILE, lim ) = 0 then
return true
end if

return false
End Function
End Module[/rbcode]
You use this function to set the open resource limit before you start doing things like making a bunch of files or sockets. This is very handy for people making server applications on OS X where the soft limit for the number of sockets is a measly 256 or so.

Note, most of the error checking is left to the user, and this example obviously requires RB2005 since it uses soft declares.

Leave a comment

Disclaimer

I'm currently an employee of REAL Software. My blog is mine. The opinions represented in this blog are mine as well and may not represent my employer's opinions. All original material is copyrighted and property of the author.

REALbasic® is a registered trademark of REAL Software, Inc. REAL SQL Server™ and Lingua™ are pending trademarks of REAL Software, Inc. All rights reserved.