-
-
Notifications
You must be signed in to change notification settings - Fork 549
Add optional runtime swappable custom allocator #2230
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ | |
| // found online at https://opensource.org/licenses/MIT. | ||
| // | ||
|
|
||
| #include "core/defs.h" | ||
| #include "core/nng_impl.h" | ||
| #include "core/pipe.h" | ||
| #include "core/platform.h" | ||
|
|
@@ -47,6 +48,24 @@ nng_socket_id(nng_socket s) | |
| return (((int) s.id > 0) ? (int) s.id : -1); | ||
| } | ||
|
|
||
| #ifdef NNG_ENABLE_CUSTOM_ALLOC | ||
| void | ||
| nng_alloc_set(void* (*malloc)(size_t), void* (*calloc)(size_t, size_t), void (*free)(void*)) | ||
| { | ||
| if (malloc) { | ||
| nni_malloc_fn = malloc; | ||
| } | ||
|
|
||
| if (calloc) { | ||
| nni_calloc_fn = calloc; | ||
| } | ||
|
|
||
| if (free) { | ||
| nni_free_fn = free; | ||
| } | ||
| } | ||
|
Comment on lines
+51
to
+66
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: find . -type f -name "*.c" | grep -E "(posix_alloc|win_thread|nng\.c)" | head -20Repository: nanomsg/nng Length of output: 137 🏁 Script executed: # Check the exact location of src/nng.c and the nng_alloc_set function
fd "nng\.c$" --type fRepository: nanomsg/nng Length of output: 64 🏁 Script executed: # List the structure to understand the layout
ls -la src/ 2>/dev/null | head -20Repository: nanomsg/nng Length of output: 707 🏁 Script executed: # Read src/nng.c around lines 51-66 to see the nng_alloc_set function
sed -n '1,80p' src/nng.c | cat -nRepository: nanomsg/nng Length of output: 2379 🏁 Script executed: # Search for the global function pointer declarations
rg "nni_malloc_fn|nni_calloc_fn|nni_free_fn" src/nng.c -B 2 -A 2 -nRepository: nanomsg/nng Length of output: 225 🏁 Script executed: # Check posix_alloc.c to see how globals are used
cat -n src/platform/posix/posix_alloc.cRepository: nanomsg/nng Length of output: 1365 🏁 Script executed: # Check win_thread.c to see allocator usage
sed -n '1,100p' src/platform/windows/win_thread.c | cat -nRepository: nanomsg/nng Length of output: 2973 🏁 Script executed: # Check core/nng_impl.h or core/defs.h for declarations and synchronization
grep -r "nni_malloc_fn\|nni_calloc_fn\|nni_free_fn" src/core/ -B 2 -A 2 -nRepository: nanomsg/nng Length of output: 463 🏁 Script executed: # Search for any mutex or synchronization around allocator setting
rg "nng_alloc_set|nni_malloc_fn|nni_calloc_fn|nni_free_fn" src/ --type c -B 3 -A 3 | head -100Repository: nanomsg/nng Length of output: 4318 🏁 Script executed: # Check if there's any initialization guard or documentation
rg "ENABLE_CUSTOM_ALLOC" src/ -B 5 -A 5 | head -150Repository: nanomsg/nng Length of output: 1662 🏁 Script executed: # Check if there's any documentation about intended usage or timing
grep -r "nng_alloc_set\|custom.*alloc" src/ --type c --type h -B 2 -A 2 | grep -i "comment\|doc\|usage\|before\|after" | head -30Repository: nanomsg/nng Length of output: 172 🏁 Script executed: # Check if there are any thread synchronization mechanisms or atomic operations used
rg "atomic|mutex|lock|synchroni" src/core/defs.h -B 3 -A 3Repository: nanomsg/nng Length of output: 310 🏁 Script executed: # Verify that the globals are truly not protected by any locks
rg "static.*nni_malloc_fn|static.*nni_calloc_fn|static.*nni_free_fn" src/ -B 5 -A 5Repository: nanomsg/nng Length of output: 37 🏁 Script executed: # Check if there's any documentation about nng_alloc_set in header files
rg "nng_alloc_set" src/ --type c --type h -B 5 -A 5Repository: nanomsg/nng Length of output: 389 🏁 Script executed: # Look for any public header that might document expected usage
fd "\.h$" src/ -x grep -l "nng_alloc_set" {}Repository: nanomsg/nng Length of output: 37 🏁 Script executed: # Check if initialization happens before any allocations
rg "nng_init|nng_open_pipe|nng_socket" src/nng.c -A 5 | head -50Repository: nanomsg/nng Length of output: 966 🏁 Script executed: # Look for README or documentation files mentioning custom allocators
fd -e md -e rst -e txt . | xargs grep -l "custom.*alloc\|nng_alloc_set" 2>/dev/nullRepository: nanomsg/nng Length of output: 39 🏁 Script executed: # Check git history or comments in the file for intent
head -100 src/nng.c | grep -A 10 -B 10 "NNG_ENABLE_CUSTOM_ALLOC"Repository: nanomsg/nng Length of output: 396 🏁 Script executed: # Verify if allocations happen within socket/pipe creation (confirming early initialization would be required)
rg "nni_alloc|nni_zalloc" src/core/ --type c | head -30Repository: nanomsg/nng Length of output: 2114 🏁 Script executed: # Double-check: Are the globals truly extern in defs.h, confirming they are shared across compilation units?
cat -n src/core/defs.h | sed -n '245,260p'Repository: nanomsg/nng Length of output: 732 🏁 Script executed: # Verify there is no initialization guard or one-shot check anywhere
rg "nng_alloc_set|nni_malloc_fn.*=" src/nng.c -B 10 -A 10Repository: nanomsg/nng Length of output: 401 🏁 Script executed: # Check if there's any documentation or comments about thread-safety in platform headers
cat -n src/core/platform.h | grep -A 10 -B 10 "alloc"Repository: nanomsg/nng Length of output: 4921 🏁 Script executed: # Final verification: confirm multiple definitions in different compilation units
rg "void\* \(\*nni_malloc_fn\)" src/ --type cRepository: nanomsg/nng Length of output: 258 🏁 Script executed: # Verify no atomic or synchronization wrapper exists around the setter
rg "static|atomic|volatile|lock|mutex" src/nng.c | grep -A 5 -B 5 "nng_alloc_set"Repository: nanomsg/nng Length of output: 37 Synchronize and restrict allocator changes to one-shot initialization. The globals 🤖 Prompt for AI Agents |
||
| #endif | ||
|
|
||
| void * | ||
| nng_alloc(size_t sz) | ||
| { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't hide a public API behind
NNG_ENABLE_CUSTOM_ALLOC.With the current wiring,
NNG_ENABLE_CUSTOM_ALLOCis private tonngand always public onnng_testing, so enabled builds can hidenng_alloc_set()from real consumers while OFF builds still expose it to tests. This declaration needs to be always visible with a stub/error implementation behind it, or the macro needs to come from a generated public config header that is exported to consumers.🤖 Prompt for AI Agents