Need help with malloc_geiger?
Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.

About the developer

laserallan
304 Stars 5 Forks MIT License 6 Commits 0 Opened issues

Services available

!
?

Need anything else?

Contributors list

# 409,992
Objecti...
cpp11
real-ti...
vfx
5 commits
# 185,223
C
google-...
Perl
swagger
1 commit

Malloc Geiger

Malloc geiger is a hook for malloc that plays geiger counter blips in proportion to the amount of calls to malloc as a way of knowing what an application does. It's largely meant as a joke so don't expect it to work properly in every situation. It only looks at malloc at this point so it won't react to any other way an application may allocate memory.

A video of malloc_geiger in action can be found here

API

The API is minimal: ```cpp // Installs the geiger clicking malloc handler // saturationrate, the amount of mallocs required in a cycle to max out the clicking // // interval the time in microseconds between each check for whether a click should be played or not. // lower values allows more extreme rates of clicking. A good start value tends to be 10000 meaning // a maximum of 100 clicks per second when saturating the amount of allocations // // The probability of a click happening in each interval is // min(numberofmallocsininterval/saturationrate, 1.0) MALLOCGEIGERAPI MGStatus installmallocgeiger(sizet saturationrate, sizet interval);

// Uninstalls the geiger clicking malloc handler MALLOCGEIGERAPI MGStatus uninstallmallocgeiger();

A typical initialization looks something like this:
cpp if(installmallocgeiger(1000, 10000) != MGSTATUS_SUCCESS) { // error handling } ```

This call should ideally before the application has started any other threads to make sure the patching doesn't happen while another thread is doing a call to malloc or free.

Compatiblity

malloc_geiger only works on Windows at this point. It has been tested on Win64 using visual studio 2017

Installing and Building

When you have cloned the repository you need to sync the submodules. Enter the directory you synced and run

git submodule update --init
Create and go to a directory for the build
md build
cd build
Run the cmake configuration, there is a script for doing that provided for Ninja and Release Builds installing in build/installed
../scripts/createproj.bat
Now you should be ready to build
ninja --j4 install
If everything worked you can run the test application
installed/bin/test_app.exe

Python injection

Since the library is built as a dll and does dynamic patching of the malloc functions it can be installed in a running application. If the application has a python interpreter it's an excellent vector to do the installation.

Note that this only works if the runtime libraries matches between geigermalloc and the host application. Here is a sample script for installing it in an application ```python import ctypes mg = ctypes.windll.LoadLibrary("<pathtoinstalldir>/mallocgeiger.dll") res = mg.installmalloc_geiger(1000, 10000) if res != 0: raise BaseException('Failed to install malloc geiger') ```

Caveats

Too many to mention all. Here are some: * With the current setup where malloc_geiger is built as a dll it requires the application using it to use the dynamic runtime library. If using it in an application with static runtime library it needs to be linked statically. * It only overrides malloc, any allocation not passing through malloc is going to be missed. * There is a potential deadlock in the malloc functions since there is a lock in the sound code too. Have not invested time in figuring out whether it can happen and properly avoided. * It overrides the malloc the dll uses. If the host application uses a different runtime library you need to configure the build settings to match for it to work. * The replacement malloc has an additional lock and does some extra work so it affects performance negatively. * Probably a million other things

Credits

The application works thanks to two external libraries

gperftools

A small part of gperftools is used to override the malloc/free functions at runtime

https://github.com/gperftools/gperftools

cute_headers

The cute_sound library is used to play sounds.

https://github.com/RandyGaul/cute_headers/

Geiger sound

Cut out from a sound found at wikipedia, here are the credits for it https://upload.wikimedia.org/wikipedia/commons/5/58/Geiger_calm.ogg Snaily [CC BY-SA 3.0 (http://creativecommons.org/licenses/by-sa/3.0/)]

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.