void Allocator::realloc(Pointer &p, size_t N) { if (p.get() == nullptr) { p = this->alloc(N); return; throw AllocError(AllocErrorType::InvalidFree, ""); } if (N < it(p)->size) { this->free_space -= p.base->parted[p.idx]->size - N; AllocUnit tmp(it(p)->link, N, it(p)->idx); this->allocated.erase(it(p)); this->allocated.insert(tmp); return; } if (N - it(p)->size > this->free_space) { throw AllocError(AllocErrorType::NoMemory, ""); } char *buf = (char *) ::calloc(it(p)->size, sizeof(*buf)); size_t buf_size = it(p)->size; std::memcpy(buf, it(p)->link, it(p)->size); this->allocated.erase(it(p)); this->defrag(); p = this->alloc(N); std::memcpy(it(p)->link, buf, buf_size); ::free(buf); }
Pointer Allocator::alloc(size_t N) { if (this->free_space < N) { throw AllocError(AllocErrorType::NoMemory, ""); } if (this->freed.empty()) { this->freed.push_back(this->freed.capacity()); this->parted.resize(this->freed.capacity(), this->allocated.end()); } if (this->next + N <= this->end) { auto tmp = this->allocated.insert(AllocUnit(this->next, N, this->freed.back())); this->parted[this->freed.back()] = tmp.first; Pointer res(this->freed.back(), this); this->freed.pop_back(); this->free_space -= N; this->next += N; return res; } this->defrag(); return this->alloc(N); }
void Allocator::defrag() { this->defragged = !this->defragged; for(index_t pointer_id = 0; pointer_id < this->total_blocks; ++pointer_id) { if(get_start_block(pointer_id) != -1) { index_t start_block = get_start_block(pointer_id); size_t required_blocks = get_size_blocks(pointer_id); index_t new_position = find_position(required_blocks); if(new_position == -1) { throw AllocError(AllocErrorType::NoMemory); } move(start_block, new_position, required_blocks, required_blocks); set_start_block(pointer_id, new_position); set_n_blocks(pointer_id, required_blocks); } } }
void * ReAllocate( void * optr, unsigned nchars ) { optr = (void *)realloc( (char *)optr, nchars ); if ( optr == (void *)0 ) AllocError( "ReAllocate" ); if ( (unsigned long)optr & 0x3 ) printf( "Warning, pointer not aligned.\n" ); return optr; }
void Allocator::free(Pointer &p) { auto it = std::find(this->pointers.begin() + 1, this->pointers.end() - 1, p); if (it == this->pointers.end() - 1) { throw AllocError(AllocErrorType::InvalidFree, "Invalid Free"); } this->pointers.erase(it); for (auto iter = it; iter != this->pointers.end() - 1; iter++) { iter->p->dec(); } p.setNull(); }
void * Allocate( unsigned nchars ) { void * ptr; ptr = (void *)calloc( nchars, sizeof(char) ); if ( ptr == (void *) 0 ) AllocError( "Allocate" ); if ( (unsigned long)ptr & 0x3 ) printf( "Warning, pointer not aligned.\n" ); return ptr; }
void Allocator::free(Pointer &p) { if (p.idx >= this->parted.size() || this->parted[p.idx] == this->allocated.end()) { throw AllocError(AllocErrorType::InvalidFree, ""); } auto it = this->parted[p.idx]; this->parted[p.idx] = this->allocated.end(); this->free_space += it->size; this->freed.push_back(it->idx); this->allocated.erase(it); }
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."); } } }
bool Allocator::realloc_move(Pointer &p, size_t required_blocks) { index_t pointer_id = p.get_id(); index_t new_position = find_position(required_blocks); if(new_position == -1) { throw AllocError(AllocErrorType::NoMemory); } move(get_start_block(pointer_id), new_position, get_size_blocks(pointer_id), required_blocks); set_n_blocks(pointer_id, required_blocks); set_start_block(pointer_id, new_position); return true; }
Pointer Allocator::alloc(size_t required_bytes) { size_t required_blocks = bytes_to_blocks(required_bytes); index_t position = find_position(required_blocks); if(position == -1) { throw AllocError(AllocErrorType::NoMemory); } fill_map(position, required_blocks); index_t pointer_id = insert(position, required_blocks); return Pointer(this, pointer_id); }
void gzstreambuf::Init(FILE *f_, bool write_) { write = write_; Open(f_); if (error) return; buf = (char *)malloc(sizeof(char) * bufsize); if (!buf) throw AllocError("Out of memory"); //Assert(setbuf(buf, bufsize)); if (write) setp(buf, buf + bufsize - 1); else setg(buf, buf + bufsize, buf + bufsize); error = false; }
inline void add(const lx_s *s) { if (lx_sa_add(&sa, s)) throw AllocError(); }
inline void add(const char *const *pp) { if (lx_sa_addpp(&sa, pp)) throw AllocError(); }
inline void add(const char *const *pp, int n) { if (lx_sa_addppn(&sa, pp, n)) throw AllocError(); }
/** add a char* _with_ its terminating zero. */ inline void add0(const char *s) { if (lx_sa_adds(&sa, s) || lx_post0(&sa.sarray[sa.elem-1])) throw AllocError(); }
inline void add(const char *s) { if (lx_sa_adds(&sa, s)) throw AllocError(); }