Pointer Allocator::alloc(size_t size) { for (int i = 0; i < this->pointers.size() - 1; i++) { char *end_addr = this->pointers[i].addr + this->pointers[i].size; if (canAlloc(end_addr, size, this->pointers[i + 1].addr)) { SmartPointer *new_pointer = new SmartPointer(this, i + 1); for (auto iter = this->pointers.begin() + i + 1; iter != this->pointers.end() - 1; iter++) { iter->p->inc(); } this->pointers.insert(this->pointers.begin() + i + 1, MyPar(end_addr, size, new_pointer)); return Pointer(new_pointer); } } throw AllocError(AllocErrorType::NoMemory, "Alloc failed. No Memory"); }
void Allocator::realloc(Pointer &p, size_t size) { auto it = std::find(this->pointers.begin() + 1, this->pointers.end() - 1, p); if (it == this->pointers.end() - 1) { try { p = this->alloc(size); } catch (...) { throw AllocError(AllocErrorType::NoMemory, "Realloc Failed. Pointer was not found."); } return; } if (canAlloc(p.get(), size, (it + 1)->addr)) { it->setSize(size); } else { this->free(p); try { p = this->alloc(size); } catch (...) { throw AllocError(AllocErrorType::NoMemory, "Realloc Failed. No Memory."); } } }
// Allocates the specified amount of bytes in the buffer virtual void* alloc(size_t bytes) { alignNextAlloc(); if (!canAlloc(bytes)) return NULL; return doAlloc(bytes); }