Line data Source code
1 : /** 2 : Copyright (c) 2020-2022 Roman Katuntsev <sbkarr@stappler.org> 3 : Copyright (c) 2023 Stappler LLC <admin@stappler.dev> 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 13 : all 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 21 : THE SOFTWARE. 22 : **/ 23 : 24 : #include "SPMemPoolStruct.h" 25 : 26 : namespace STAPPLER_VERSIONIZED stappler::mempool::custom { 27 : 28 219290694 : void AllocManager::reset(void *p) { 29 219290694 : memset(this, 0, sizeof(AllocManager)); 30 219290694 : pool = p; 31 219290694 : } 32 : 33 2998450 : void *AllocManager::alloc(size_t &sizeInBytes, AllocFn allocFn) { 34 2998450 : if (buffered) { 35 : MemAddr *c, **lastp; 36 : 37 845967 : c = buffered; 38 845967 : lastp = &buffered; 39 2274576 : while (c) { 40 1923933 : if (c->size > sizeInBytes * 2) { 41 15451 : break; 42 1908482 : } else if (c->size >= sizeInBytes) { 43 479873 : *lastp = c->next; 44 479873 : c->next = free_buffered; 45 479873 : free_buffered = c; 46 479873 : sizeInBytes = c->size; 47 479873 : increment_return(sizeInBytes); 48 479881 : return c->address; 49 : } 50 : 51 1428609 : lastp = &c->next; 52 1428609 : c = c->next; 53 : } 54 : } 55 2518577 : increment_alloc(sizeInBytes); 56 2519003 : return allocFn(pool, sizeInBytes); 57 : } 58 : 59 1455935 : void AllocManager::free(void *ptr, size_t sizeInBytes, AllocFn allocFn) { 60 1455935 : MemAddr *addr = nullptr; 61 1455935 : if (allocated == 0) { 62 5825 : return; 63 : } 64 : 65 1450110 : if (free_buffered) { 66 478570 : addr = free_buffered; 67 478570 : free_buffered = addr->next; 68 : } else { 69 971540 : addr = (MemAddr *)allocFn(pool, sizeof(MemAddr)); 70 971853 : increment_alloc(sizeof(MemAddr)); 71 : } 72 : 73 1450068 : if (addr) { 74 1450068 : addr->size = sizeInBytes; 75 1450068 : addr->address = ptr; 76 1450068 : addr->next = nullptr; 77 : 78 1450068 : if (buffered) { 79 : MemAddr *c, **lastp; 80 : 81 1235119 : c = buffered; 82 1235119 : lastp = &buffered; 83 4342779 : while (c) { 84 3972918 : if (c->size >= sizeInBytes) { 85 865258 : addr->next = c; 86 865258 : *lastp = addr; 87 865258 : break; 88 : } 89 : 90 3107660 : lastp = &c->next; 91 3107660 : c = c->next; 92 : } 93 : 94 1235119 : if (!addr->next) { 95 370216 : *lastp = addr; 96 : } 97 : } else { 98 214949 : buffered = addr; 99 214949 : addr->next = nullptr; 100 : } 101 : } 102 : } 103 : 104 817668 : void MemNode::insert(MemNode *point) { 105 817668 : this->ref = point->ref; 106 817668 : *this->ref = this; 107 817668 : this->next = point; 108 817668 : point->ref = &this->next; 109 817668 : } 110 : 111 181716 : void MemNode::remove() { 112 181716 : *this->ref = this->next; 113 181716 : this->next->ref = this->ref; 114 181716 : } 115 : 116 26903173 : size_t MemNode::free_space() const { 117 26903173 : return endp - first_avail; 118 : } 119 : 120 438859366 : void Cleanup::run(Cleanup **cref) { 121 438859366 : Cleanup *c = *cref; 122 438877595 : while (c) { 123 18229 : *cref = c->next; 124 18229 : if (c->fn) { 125 18229 : (*c->fn)((void *)c->data); 126 : } 127 18229 : c = *cref; 128 : } 129 438859366 : } 130 : 131 : }