{"id":16723467,"url":"https://github.com/alphaville/static_malloc","last_synced_at":"2025-07-01T08:34:41.175Z","repository":{"id":136252087,"uuid":"158305412","full_name":"alphaville/static_malloc","owner":"alphaville","description":"C: malloc for static allocation! :eyes: :tractor:","archived":false,"fork":false,"pushed_at":"2018-11-20T00:49:33.000Z","size":5,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-15T14:11:15.592Z","etag":null,"topics":["c","cprogramming","embedded","embedded-systems","memory-management","systems-programming"],"latest_commit_sha":null,"homepage":null,"language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/alphaville.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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":"2018-11-19T23:55:36.000Z","updated_at":"2024-07-23T12:22:03.000Z","dependencies_parsed_at":null,"dependency_job_id":"6a85bd8a-2c55-4e79-a339-f22c04d04b4b","html_url":"https://github.com/alphaville/static_malloc","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/alphaville/static_malloc","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alphaville%2Fstatic_malloc","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alphaville%2Fstatic_malloc/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alphaville%2Fstatic_malloc/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alphaville%2Fstatic_malloc/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/alphaville","download_url":"https://codeload.github.com/alphaville/static_malloc/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alphaville%2Fstatic_malloc/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":262928474,"owners_count":23386153,"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","cprogramming","embedded","embedded-systems","memory-management","systems-programming"],"created_at":"2024-10-12T22:38:10.840Z","updated_at":"2025-07-01T08:34:41.147Z","avatar_url":"https://github.com/alphaville.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Static Malloc\n\nIn embedded programming it is often required to be 100% sure that dynamic memory allocation will work.\n\nThis is why, it should be used very carefully to guarantee deterministic behaviour. \n\nIf possible, memory should be allocated when the application starts and no further allocation should take place afterwards.\n\nThis is a simple framework for safe memory allocation in C.\n\nThis framework should be used provided that:\n\n- the exact amount of memory to be allocated is known before the start of the application\n- it is not necessary to free memory while the application runs\n\nThis is a **header library**, meaning, you just need to include `static_malloc.h` in your C file.\n\n## Using static_malloc\n \n**Step 1.** Configure `static_malloc_sizes.h` and define the amount of memory to be allocated.\n\nFor example:\n\n```c\n#define static_malloc_len_double 123456\n#define static_malloc_len_int 1000\n#define static_malloc_len_char 50\n#define static_malloc_len_uint_t 5000\n```\n\nYou may define **any** datatype to be allocated.\n \nFor example, if you have some datatype called `my_datatype_t`, and you need to pre-allocate memory of size `123000`, you only need to define \n\n```c\n#define static_malloc_len_my_datatype_t 123000\n``` \n\n**Step 2.** You may now use `static_malloc`  as follows\n\n```c\n#include \"../include/static_malloc.h\"\n#include \u003cstdio.h\u003e\n\nint main(void){\n    /* Allocate an array of double of length 20 */\n    double * x = STATIC_MALLOC(double)(20);\n    \n     /* Allocate an array of double of length 500 */\n    double * y = STATIC_MALLOC(double)(500);\n    \n    /* You don't need to (and you shouldn't) free the memory */\n    return 0;\n}\n```\n\nFor the datatypes `double`, `int` and `float`, you may use `STATIC_MALLOC` as above.\n\nFor other less frequently used datatypes, e.g., `char`, `short` etc, you need to define `STATIC_MALLOC(...)` as in the following example:\n\n```c\n#if STALLOC_ACTIVE_TYPE(char)\nSTATIC_MALLOC_DFN(char)\n#endif\n```\n\nHere is a complete example\n\n```c\n#include \"include/static_malloc.h\"\n\n#if STALLOC_ACTIVE_TYPE(char)  // if static_malloc_len_char is defined\nSTATIC_MALLOC_DFN(char)        // define STATIC_MALLOC(char)\n#endif\n\nint main(void)\n{\n    /* Allocate 5 chars */\n    char *x = STATIC_MALLOC(char)(5);\n\n    return 0;\n}\n```\n\n## Pros and Cons\n\nPros:\n\n- Safe usage of memory\n- No need to free the memory\n\nCons: \n\n- Need to know how much memory the program needs, before it runs\n- Freeing memory is not possible\n- Reusing memory is \n\n## Installation\n\n**No installation** is necessary!\n\nFor example, to compile `main.c`, use\n\n```c\ngcc main.c\n```\n\nYou only need to include the following line in your file:\n\n```c\n#include \"include/static_malloc.h\"\n```\n\n## Unit tests\n\nTo run the tests, you first need to run `config.sh` (requires `cmake`) and then enter `build` and run `make all test`.\n\nRun the following:\n\n```\n./config.sh\ncd build\nmake all test\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falphaville%2Fstatic_malloc","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falphaville%2Fstatic_malloc","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falphaville%2Fstatic_malloc/lists"}