{"id":21832707,"url":"https://github.com/winterrdog/lib-xmem","last_synced_at":"2025-03-21T13:43:44.543Z","repository":{"id":236334241,"uuid":"792393425","full_name":"winterrdog/lib-xmem","owner":"winterrdog","description":"a C library with functions to handle memory management with out-of-memory handling","archived":false,"fork":false,"pushed_at":"2024-04-26T16:47:01.000Z","size":6,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-26T09:27:19.170Z","etag":null,"topics":["c","free","library","malloc","memory-management","single-header-lib"],"latest_commit_sha":null,"homepage":"","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/winterrdog.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,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-04-26T15:20:47.000Z","updated_at":"2024-04-26T16:48:28.000Z","dependencies_parsed_at":"2024-04-26T16:50:18.991Z","dependency_job_id":null,"html_url":"https://github.com/winterrdog/lib-xmem","commit_stats":null,"previous_names":["winterrdog/lib-xmem"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/winterrdog%2Flib-xmem","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/winterrdog%2Flib-xmem/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/winterrdog%2Flib-xmem/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/winterrdog%2Flib-xmem/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/winterrdog","download_url":"https://codeload.github.com/winterrdog/lib-xmem/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244807278,"owners_count":20513607,"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":["c","free","library","malloc","memory-management","single-header-lib"],"created_at":"2024-11-27T19:25:03.236Z","updated_at":"2025-03-21T13:43:44.519Z","avatar_url":"https://github.com/winterrdog.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# lib-xmem\n\na C library with functions to handle memory management with out-of-memory handling\n\n## C API\n\nThe library only provides the following functions:\n\n```c\nvoid* xmalloc(size_t size);\nvoid* xcalloc(size_t nmemb, size_t size);\nvoid* xrealloc(void* ptr, size_t size);\nvoid xfree(void* p);\nconst char* xstrdup(const char* s);\n```\n\n## Usage\n\n### `xmalloc`\n\n```c\n#include \u003cxmem.h\u003e\n\nint main() {\n    char* p = xmalloc(10);\n    xfree(p);\n    return 0;\n}\n```\n\nThis code will allocate 10 bytes of memory and free it. If the allocation fails, the program will call `exit()` exit with an error message.\n\n### `xcalloc`\n\n```c\n#include \u003cxmem.h\u003e\n\nint main() {\n    char* p = xcalloc(10, sizeof(char));\n    xfree(p);\n    return 0;\n}\n```\n\nThis code will allocate 10 bytes of memory and free it. If the allocation fails, the program will call `exit()` exit with an error message.\n\n### `xrealloc`\n\n```c\n#include \u003cxmem.h\u003e\n\nint main() {\n    char* p = xmalloc(10);\n    p = xrealloc(p, 20);\n    xfree(p);\n    return 0;\n}\n```\n\nThis code will allocate 10 bytes of memory, reallocate it to 20 bytes and free it. If the allocation fails, the program will call `exit()` exit with an error message.\n\n### `xfree`\n\n```c\n#include \u003cxmem.h\u003e\n\nint main() {\n    char* p = xmalloc(10);\n    xfree(p);\n    return 0;\n}\n```\n\nThis code will allocate 10 bytes of memory and free it. If the allocation fails, the program will call `exit()` exit with an error message. The `xfree` function is safe to call with a `NULL` pointer in which case it will do nothing. After freeing the memory, the pointer is set to `NULL` in order to avoid double freeing and undefined behavior in case the pointer is used after being freed.\n\n### `xstrdup`\n\n```c\n#include \u003cxmem.h\u003e\n\nint main() {\n    char* p = xstrdup(\"Hello, World!\");\n    xfree(p);\n    return 0;\n}\n```\n\nThis code will allocate memory for the string \"Hello, World!\" and free it. If the allocation fails, the program will call `exit()` exit with an error message. It uses `xmalloc` internally to allocate memory for the string and `strlen` to get the length of the string. The caller is expected to free the memory allocated by `xstrdup` using `xfree`. The copying is done using `memcpy` and the string is null-terminated automatically. We used `memcpy` instead of `strcpy` to avoid buffer overflows and undefined behavior but you could use `strcpy` if you are sure that the string is null-terminated and know what you are doing. `memcpy` is handles unaligned memory very well.\n\n## Usage in C projects\n\n- Just include the `xmem.h` header file in your C project and compile since it's a single-header library.\n\n## Contributing\n\nIf you want to contribute to this project and make it better, your help is very welcome. Contributing is also a great way to learn more and improve your skills. I'll be happy to help you with your contributions and any questions you may have.\n\n## License\n\nThis project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwinterrdog%2Flib-xmem","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwinterrdog%2Flib-xmem","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwinterrdog%2Flib-xmem/lists"}