jckarter / clay

The Clay programming language
http://claylabs.com/clay
Other
404 stars 34 forks source link

tempCString utility #442

Closed stepancheg closed 11 years ago

stepancheg commented 11 years ago

tempCString produces C string for given string. Result is wrapped in CStringHolder record.

If input is already a C string, then input is simply stored in a POD CStringHolder.

If input is a string but not a C string, then memory for C string is allocated, string data is copied to that string, and allocated C string is stored in CStringHolder record. In this case CStringHolder owns cstring reference and releases it on destroy.

tempCString is convenient to call C APIs that accept C strings. Code example:

mysqlConnect(host: Pointer[Byte]) { ... }

[S when String?(S)]
myCode(host: S) = mysqlConnect(tempCString(host).ptr);

Currently memory is allocated using malloc. Later tempCString may be rewritten to allocate memory in a thread-local fixed size memory chunk pool (in this case the cost of memory allocation will be comparable to alloca).

jckarter commented 11 years ago

Sounds good.