Skip to content

Commit

Permalink
Initialize C libs before main, using a class variable (workaround unt…
Browse files Browse the repository at this point in the history
…il solved)
  • Loading branch information
asterite committed May 17, 2016
1 parent 7efdf26 commit d408415
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 22 deletions.
17 changes: 12 additions & 5 deletions src/big/lib_gmp.cr
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,15 @@ lib LibGMP
fun set_memory_functions = __gmp_set_memory_functions(malloc : SizeT -> Void*, realloc : Void*, SizeT, SizeT -> Void*, free : Void*, SizeT ->)
end

LibGMP.set_memory_functions(
->(size) { GC.malloc(size) },
->(ptr, old_size, new_size) { GC.realloc(ptr, new_size) },
->(ptr, size) { GC.free(ptr) }
)
# :nodoc:
struct BigInt::Init
# Workaround to force this initialization as soon as the program starts, before main
@@init = begin
LibGMP.set_memory_functions(
->(size) { GC.malloc(size) },
->(ptr, old_size, new_size) { GC.realloc(ptr, new_size) },
->(ptr, size) { GC.free(ptr) }
)
nil
end
end
12 changes: 12 additions & 0 deletions src/openssl/lib_ssl.cr
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,15 @@ lib LibSSL
fun ssl_ctx_use_privatekey_file = SSL_CTX_use_PrivateKey_file(ctx : SSLContext, file : UInt8*, filetype : SSLFileType) : Int
fun ssl_set_bio = SSL_set_bio(handle : SSL, rbio : LibCrypto::Bio*, wbio : LibCrypto::Bio*)
end

# :nodoc:
module OpenSSL::Init
# Workaround to force this initialization as soon as the program starts, before main
@@init = begin
LibSSL.ssl_library_init
LibSSL.ssl_load_error_strings
LibCrypto.openssl_add_all_algorithms
LibCrypto.err_load_crypto_strings
nil
end
end
5 changes: 0 additions & 5 deletions src/openssl/openssl.cr
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@ module OpenSSL
super(msg)
end
end

LibSSL.ssl_library_init
LibSSL.ssl_load_error_strings
LibCrypto.openssl_add_all_algorithms
LibCrypto.err_load_crypto_strings
end

require "./bio"
Expand Down
31 changes: 19 additions & 12 deletions src/xml/libxml2.cr
Original file line number Diff line number Diff line change
Expand Up @@ -252,15 +252,22 @@ lib LibXML
fun xmlGetNsList(doc : Doc*, node : Node*) : NS**
end

LibXML.xmlGcMemSetup(
->GC.free,
->GC.malloc(LibC::SizeT),
->GC.malloc(LibC::SizeT),
->GC.realloc(Void*, LibC::SizeT),
->(str) {
len = LibC.strlen(str)
copy = Pointer(UInt8).malloc(len)
copy.copy_from(str, len)
copy
}
)
# :nodoc:
module XML::Init
# Workaround to force this initialization as soon as the program starts, before main
@@init = begin
LibXML.xmlGcMemSetup(
->GC.free,
->GC.malloc(LibC::SizeT),
->GC.malloc(LibC::SizeT),
->GC.realloc(Void*, LibC::SizeT),
->(str) {
len = LibC.strlen(str)
copy = Pointer(UInt8).malloc(len)
copy.copy_from(str, len)
copy
}
)
nil
end
end

0 comments on commit d408415

Please sign in to comment.