{"id":13730207,"url":"https://github.com/laserallan/malloc_geiger","last_synced_at":"2025-05-08T02:32:05.476Z","repository":{"id":188123251,"uuid":"201584299","full_name":"laserallan/malloc_geiger","owner":"laserallan","description":null,"archived":false,"fork":false,"pushed_at":"2019-09-15T14:28:03.000Z","size":17,"stargazers_count":329,"open_issues_count":0,"forks_count":5,"subscribers_count":8,"default_branch":"master","last_synced_at":"2024-11-14T21:37:52.580Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/laserallan.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2019-08-10T05:25:26.000Z","updated_at":"2024-08-26T15:07:14.000Z","dependencies_parsed_at":"2023-08-13T21:46:51.035Z","dependency_job_id":"c987605f-4192-4a50-90d3-45dc79efd725","html_url":"https://github.com/laserallan/malloc_geiger","commit_stats":null,"previous_names":["laserallan/malloc_geiger"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/laserallan%2Fmalloc_geiger","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/laserallan%2Fmalloc_geiger/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/laserallan%2Fmalloc_geiger/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/laserallan%2Fmalloc_geiger/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/laserallan","download_url":"https://codeload.github.com/laserallan/malloc_geiger/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252986899,"owners_count":21836250,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-03T02:01:11.504Z","updated_at":"2025-05-08T02:32:05.251Z","avatar_url":"https://github.com/laserallan.png","language":"C++","readme":"# Malloc Geiger\nMalloc 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.\n\nA video of malloc_geiger in action can be found [here](https://youtu.be/7vn6aGgLKfQ)\n\n## API\nThe API is minimal:\n```cpp\n// Installs the geiger clicking malloc handler\n// saturation_rate, the amount of mallocs required in a cycle to max out the clicking\n//\n// interval the time in microseconds between each check for whether a click should be played or not.\n// lower values allows more extreme rates of clicking. A good start value tends to be 10000 meaning\n// a maximum of 100 clicks per second when saturating the amount of allocations\n//\n// The probability of a click happening in each interval is \n// min(number_of_mallocs_in_interval/saturation_rate, 1.0)\nMALLOC_GEIGER_API MG_Status install_malloc_geiger(size_t saturation_rate, size_t interval);\n\n// Uninstalls the geiger clicking malloc handler\nMALLOC_GEIGER_API MG_Status uninstall_malloc_geiger();\n```\nA typical initialization looks something like this:\n```cpp\nif(install_malloc_geiger(1000, 10000) != MG_STATUS_SUCCESS) {\n    // error handling\n}\n```\n\nThis 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.\n\n## Compatiblity\nmalloc_geiger only works on Windows at this point. It has been tested on Win64 using visual studio 2017\n\n## Installing and Building\nWhen you have cloned the repository you need to sync the submodules.\nEnter the directory you synced and run\n```\ngit submodule update --init\n```\nCreate and go to a directory for the build\n```\nmd build\ncd build\n```\nRun the cmake configuration, there is a script for doing that provided for Ninja and Release Builds installing in build/installed\n```\n../scripts/createproj.bat\n```\nNow you should be ready to build\n```\nninja --j4 install\n```\nIf everything worked you can run the test application\n```\ninstalled/bin/test_app.exe\n```\n\n## Python injection\nSince 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.\n\nNote that this only works if the runtime libraries matches between geiger_malloc and the host application.\nHere is a sample script for installing it in an application\n```python\nimport ctypes\nmg = ctypes.windll.LoadLibrary(\"\u003cpath_to_install_dir\u003e/malloc_geiger.dll\") \nres = mg.install_malloc_geiger(1000, 10000)\nif res != 0:\n    raise BaseException('Failed to install malloc geiger')\n```\n\n## Caveats\nToo many to mention all. Here are some:\n* 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.\n* It only overrides malloc, any allocation not passing through malloc is going to be missed.\n* 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.\n* 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.\n* The replacement malloc has an additional lock and does some extra work so it affects performance negatively.\n* Probably a million other things\n  \n## Credits\nThe application works thanks to two external libraries\n### gperftools\nA small part of gperftools is used to override the malloc/free functions at runtime\n\nhttps://github.com/gperftools/gperftools\n\n### cute_headers\nThe cute_sound library is used to play sounds.\n\nhttps://github.com/RandyGaul/cute_headers/\n\n### Geiger sound\nCut out from a sound found at wikipedia, here are the credits for it\nhttps://upload.wikimedia.org/wikipedia/commons/5/58/Geiger_calm.ogg\nSnaily [CC BY-SA 3.0 (http://creativecommons.org/licenses/by-sa/3.0/)]\n","funding_links":[],"categories":["C++"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flaserallan%2Fmalloc_geiger","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flaserallan%2Fmalloc_geiger","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flaserallan%2Fmalloc_geiger/lists"}