recycleContainers.h (3076B)
1 // MIT License 2 // 3 // Copyright (c) 2026 Remy Noulin 4 // 5 // Permission is hereby granted, free of charge, to any person obtaining a copy 6 // of this software and associated documentation files (the "Software"), to deal 7 // in the Software without restriction, including without limitation the rights 8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 // copies of the Software, and to permit persons to whom the Software is 10 // furnished to do so, subject to the following conditions: 11 // 12 // The above copyright notice and this permission notice shall be included in all 13 // copies or substantial portions of the Software. 14 // 15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 // SOFTWARE. 22 23 #define recycleArrayT staticArrayT 24 #define recycleBool bool 25 #define recycleThreadLocalStorage __thread 26 #define recycleArrayInit staticArrayInit 27 #define recycleSingleThread(arg) 28 #define recycleArrayAcquire recycleSingleThread 29 #define recycleArrayRelease recycleSingleThread 30 31 /** 32 * declare thread local variable for the recycle container system 33 * to be used in the class *Internal.h 34 */ 35 #define declareRecycle(typeName)\ 36 recycleArrayT(recyclet, typeName *, recycleContainersAmount);\ 37 \ 38 internal recycleThreadLocalStorage recycleBool initRecycle = true;\ 39 internal recycleThreadLocalStorage recyclet recycle; 40 41 /** 42 * initialize the recycle container system in initiateAllocate 43 * and recycle the unused containers 44 */ 45 #define initAllocateRecycle(typeName)\ 46 /* initialize recycle during the first allocation */\ 47 if (initRecycle) { \ 48 recycleArrayInit(recycle);\ 49 initRecycle = false;\ 50 }\ 51 recycleArrayAcquire(recycle);\ 52 bool doMalloc = false;\ 53 if (staticArrayIsEmpty(recycle))\ 54 doMalloc = true;\ 55 else {\ 56 (*self) = staticArrayFirst(recycle);\ 57 staticArrayDequeue(recycle);\ 58 }\ 59 recycleArrayRelease(recycle);\ 60 if (doMalloc) {\ 61 *self = malloc(sizeof(typeName));\ 62 if (!(*self)) return;\ 63 } 64 65 /** 66 * free the unused container at program or thread exit 67 */ 68 #define finalizeRecycle\ 69 if (!initRecycle) {\ 70 /* recycle has been initialized, free containers */\ 71 recycleArrayAcquire(recycle);\ 72 if (!staticArrayIsEmpty(recycle)) {\ 73 range(i, staticArrayCount(recycle)) {\ 74 free(staticArrayRef(recycle, (int)i));\ 75 }\ 76 }\ 77 initRecycle = true;\ 78 } 79 80 /** 81 * add container to the unused list 82 */ 83 #define finishRecycle\ 84 recycleArrayAcquire(recycle);\ 85 bool doFree = false;\ 86 if (staticArrayIsFull(recycle))\ 87 doFree = true;\ 88 else {\ 89 staticArrayPush(recycle);\ 90 staticArrayLast(recycle) = *self;\ 91 }\ 92 recycleArrayRelease(recycle);\ 93 if (doFree)\ 94 free(*self);