Allow overriding malloc and free at compile time - #1917
Conversation
|
There are a few things to note here: Do We Want to Provide More Guarantees than malloc/free?The docs added in the current version of the PR say that the semantics should be the same as malloc/free. This can be quite demanding if you don't have a full version of malloc available. We could provide more guarantees for specific use cases. For example, have a SECP256K1_SIMPLE_MALLOC and guarantee that all frees will be in a row (so a simple bump allocator can be used). SECP256K1_SIMPLE_MALLOC could just be defined as SECP256K1_MALLOC. I wonder if this is overkill, but providing such guarantees for SECP256K1_MALLOC in general may bite us in the future. Another subtle detail is that New Approach for OverridesOne thing to note here is that diverges from our previous approach of overriding functions that we used for external error callbacks: requiring the user to set a flag macro such as When I worked on #595, the approach in this PR (namely simply using macro for the function call) didn't occur to me. I only saw this later in another crypto library (I can't remember which one). I think the two approaches functionally equivalent but I believe this one here is more straight-forward and a bit more convenient to use (in particular if you can pass We want more overrides in the futures, e.g., a compile-time override for SHA256, and potentially overrides for What If You Really Don't Have malloc?We also need to deal with this situation (probably in a separate PR). There should probably be a macro |
9abaf6c to
c6fb252
Compare
All allocations now go through the macros SECP256K1_MALLOC and SECP256K1_FREE, which the user can define when compiling the library. SECP256K1_FREE also receives the size of the allocation.
The new macros SECP256K1_ILLEGAL_CALLBACK_FN and SECP256K1_ERROR_CALLBACK_FN work like the malloc and free overrides. The link time mechanism behind USE_EXTERNAL_DEFAULT_CALLBACKS is reimplemented on top of them and deprecated.
c6fb252 to
8fed6e1
Compare
|
Thanks for the helpful feedback @real-or-random , per your comment in #1789 I made the change to
Makes sense. I documented only minimal guarantees instead. So
I personally prefer this. The second commit adds
Added I will take a deeper look at #1461 for the includes part. Could you rebase it maybe? Let me know if you think these changes should be coordinated in some special way. |
|
Concept ACK
+1 on preferring the approach in this PR. From my past I'm personally more familiar with the current way on master (i.e. provide symbols at link time), but using macros for function calls seems indeed a bit nicer and more convenient to use, and seem to have no drawbacks. |
Defining SECP256K1_NO_MALLOC removes all uses of malloc and free from the library. The context functions that allocate are then not built and the preallocated context functions or the static context must be used instead.
The override configuration uses the functions in ci/malloc_override.h, which also makes direct calls to malloc and free fail to compile. Benchmarks are disabled there because they call malloc directly. The no-malloc configuration checks that the library does not reference malloc and free.
8fed6e1 to
f933871
Compare
|
The CI job where I had added the coverage for |
Addresses the suggestion by real_or_random here: #1789 (comment)
Allows to override the default
mallocandfreeat compile time using-DSECP256K1_MALLOC=my_malloc -DSECP256K1_FREE=my_free. With helps users that can not link againstmalloc, such as the rust-secp256k1 wasm build.Afaict, this is complementary to #1095 and #1461: with the macros defined, the library needs nothing from
<stdlib.h>besidesabortin the default callbacks. But #1461 would need to keep the allocating functions enabled whenSECP256K1_MALLOCis defined.