Last active
September 17, 2023 21:28
-
-
Save pietmichal/c76f03754d05167b81c463c5b38c00bb to your computer and use it in GitHub Desktop.
Cross-platfrom Example of Arena Allocator And Object Pool Working With C99 And C++ Compilers.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Example implementation of simple Arena Allocator and simple Object Pool that works with C99 and C++ compilers. | |
| // | |
| // Check the bottom of the file to see the license. | |
| // | |
| // Enjoy! | |
| #include <stdio.h> | |
| #include <malloc.h> | |
| #include <string.h> | |
| #include <stdint.h> | |
| #include <stdbool.h> | |
| #if defined(_DEBUG) || !defined(NDEBUG) | |
| #define DEBUG true | |
| #endif | |
| // | |
| // Sample structs | |
| // | |
| typedef struct Person | |
| { | |
| uint64_t createdAt; | |
| uint64_t modifiedAt; | |
| uint64_t age; | |
| char name[255]; | |
| } Person; | |
| typedef struct Car | |
| { | |
| char brand[128]; | |
| } Car; | |
| // | |
| // Example implementation of Arena Allocator | |
| // | |
| typedef struct { | |
| char* bytes; | |
| uint64_t size; | |
| uint64_t used; | |
| } Arena; | |
| Arena* Arena_Create(uint64_t size) | |
| { | |
| Arena* arena = (Arena*)malloc(sizeof(Arena)); | |
| #ifdef DEBUG | |
| if (arena == NULL) | |
| { | |
| return NULL; | |
| } | |
| #endif | |
| arena->bytes = (char*)malloc(size); | |
| #ifdef DEBUG | |
| if (arena->bytes == NULL) | |
| { | |
| free(arena); | |
| return NULL; | |
| } | |
| #endif | |
| arena->size = size; | |
| arena->used = 0; | |
| return arena; | |
| } | |
| void* Arena_Allocate(Arena* arena, uint64_t size) | |
| { | |
| if (arena->used + size > arena->size) | |
| { | |
| #ifdef DEBUG | |
| printf("[Arena] Failed to allocate %lld bytes. %lld bytes out of %lld bytes are taken.\n", size, arena->used, arena->size); | |
| #endif | |
| return NULL; | |
| } | |
| else | |
| { | |
| void* ptr = &arena->bytes[arena->used]; | |
| arena->used += size; | |
| #ifdef DEBUG | |
| printf("[Arena] Allocated %lld bytes. %lld bytes out of %lld bytes are taken.\n", size, arena->used, arena->size); | |
| #endif | |
| return ptr; | |
| } | |
| } | |
| void Arena_Destroy(Arena* arena) | |
| { | |
| free(arena->bytes); | |
| free(arena); | |
| } | |
| // | |
| // Example of object pool implementation for `Person` struct. | |
| // Please keep in mind that some consider not cleaning objects when returning them to the pool an anti-pattern. | |
| // I think it depends on the context but please keep this in mind since this implementation is not doing that and | |
| // your problem-domain might benefit from it. | |
| // | |
| typedef struct | |
| { | |
| uint64_t id; | |
| Person person; | |
| bool isAvailable; | |
| } PersonObjectPoolEntry; | |
| // Simple way to hide `isAvailable` flag. Check `PersonObjectPool_GetPerson` implementation for more details. | |
| typedef struct | |
| { | |
| uint64_t id; | |
| Person person; | |
| } PersonObjectPoolPerson; | |
| typedef struct | |
| { | |
| uint64_t size; | |
| PersonObjectPoolEntry* entries; | |
| } PersonObjectPool; | |
| PersonObjectPool* PersonObjectPool_Create(uint64_t size) | |
| { | |
| PersonObjectPool* pool = (PersonObjectPool*)malloc(sizeof(PersonObjectPool)); | |
| #ifdef DEBUG | |
| if (pool == NULL) | |
| { | |
| return NULL; | |
| } | |
| #endif | |
| pool->size = size; | |
| pool->entries = (PersonObjectPoolEntry*)malloc(sizeof(PersonObjectPoolEntry) * pool->size); | |
| #ifdef DEBUG | |
| if (pool->entries == NULL) | |
| { | |
| free(pool); | |
| return NULL; | |
| } | |
| #endif | |
| for (uint64_t i = 0; i < pool->size; ++i) | |
| { | |
| pool->entries[i].id = i; | |
| pool->entries[i].isAvailable = true; | |
| } | |
| return pool; | |
| } | |
| PersonObjectPoolPerson* PersonObjectPool_GetPerson(PersonObjectPool* pool) | |
| { | |
| for (uint64_t i = 0; i < pool->size; ++i) | |
| { | |
| PersonObjectPoolEntry* entry = &pool->entries[i]; | |
| if (entry->isAvailable) | |
| { | |
| entry->isAvailable = false; | |
| return (PersonObjectPoolPerson*)entry; | |
| } | |
| } | |
| return NULL; | |
| } | |
| void PersonObjectPool_FreePerson(PersonObjectPool* pool, uint64_t id) | |
| { | |
| pool->entries[id].isAvailable = true; | |
| } | |
| void PersonObjectPool_Destroy(PersonObjectPool* pool) | |
| { | |
| free(pool->entries); | |
| free(pool); | |
| } | |
| #undef DEBUG | |
| // | |
| // Example usage | |
| // | |
| int main() | |
| { | |
| // | |
| // Arena | |
| // | |
| Arena* arena = Arena_Create(1024 * 1024); // 1MB | |
| Person* person = (Person*)Arena_Allocate(arena, sizeof(Person)); | |
| person->createdAt = 1000; | |
| person->modifiedAt = 0; | |
| person->age = 20; | |
| snprintf(person->name, 255, "Jonathan Joestar"); | |
| Car* car = (Car*)Arena_Allocate(arena, sizeof(Car)); | |
| snprintf(car->brand, 128, "Toyota"); | |
| printf("%s is %lld years old \n", person->name, person->age); | |
| printf("The car brand is %s \n", car->brand); | |
| Arena_Destroy(arena); | |
| // | |
| // Object Pool | |
| // | |
| PersonObjectPool* pool = PersonObjectPool_Create(100); | |
| PersonObjectPoolPerson* p2 = PersonObjectPool_GetPerson(pool); | |
| p2->person.createdAt = 378282; | |
| p2->person.modifiedAt = 0; | |
| p2->person.age = 1000; | |
| snprintf(p2->person.name, 255, "Edward Old"); | |
| printf("%s is %lld years old \n", p2->person.name, p2->person.age); | |
| PersonObjectPool_FreePerson(pool, p2->id); | |
| PersonObjectPoolPerson* p3 = PersonObjectPool_GetPerson(pool); | |
| p3->person.createdAt = 0; | |
| p3->person.modifiedAt = 0; | |
| p3->person.age = 1007; | |
| snprintf(p3->person.name, 128, "Edward Older"); | |
| printf("%s is %lld years old \n", p3->person.name, p3->person.age); | |
| PersonObjectPool_Destroy(pool); | |
| return 0; | |
| } | |
| /* | |
| ------------------------------------------------------------------------------ | |
| This software is available under 2 licenses -- choose whichever you prefer. | |
| ------------------------------------------------------------------------------ | |
| ALTERNATIVE A - MIT License | |
| Copyright (c) 2023 Michal Pietraszko | |
| Permission is hereby granted, free of charge, to any person obtaining a copy of | |
| this software and associated documentation files (the "Software"), to deal in | |
| the Software without restriction, including without limitation the rights to | |
| use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies | |
| of the Software, and to permit persons to whom the Software is furnished to do | |
| so, subject to the following conditions: | |
| The above copyright notice and this permission notice shall be included in all | |
| copies or substantial portions of the Software. | |
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
| SOFTWARE. | |
| ------------------------------------------------------------------------------ | |
| ALTERNATIVE B - Public Domain (www.unlicense.org) | |
| This is free and unencumbered software released into the public domain. | |
| Anyone is free to copy, modify, publish, use, compile, sell, or distribute this | |
| software, either in source code form or as a compiled binary, for any purpose, | |
| commercial or non-commercial, and by any means. | |
| In jurisdictions that recognize copyright laws, the author or authors of this | |
| software dedicate any and all copyright interest in the software to the public | |
| domain. We make this dedication for the benefit of the public at large and to | |
| the detriment of our heirs and successors. We intend this dedication to be an | |
| overt act of relinquishment in perpetuity of all present and future rights to | |
| this software under copyright law. | |
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
| AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN | |
| ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | |
| WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
| ------------------------------------------------------------------------------ | |
| */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment