📄 allocring.cc
字号:
// This may look like C code, but it is really -*- C++ -*-/* Copyright (C) 1989 Free Software Foundation written by Doug Lea (dl@rocky.oswego.edu)This file is part of the GNU C++ Library. This library is freesoftware; you can redistribute it and/or modify it under the terms ofthe GNU Library General Public License as published by the FreeSoftware Foundation; either version 2 of the License, or (at youroption) any later version. This library is distributed in the hopethat it will be useful, but WITHOUT ANY WARRANTY; without even theimplied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULARPURPOSE. See the GNU Library General Public License for more details.You should have received a copy of the GNU Library General PublicLicense along with this library; if not, write to the Free SoftwareFoundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.*/#ifdef __GNUG__#pragma implementation#endif#include <std.h>#include <AllocRing.h>#include <new.h>AllocRing::AllocRing(int max) :n(max), current(0), nodes(new AllocQNode[max]){ for (int i = 0; i < n; ++i) { nodes[i].ptr = 0; nodes[i].sz = 0; }}int AllocRing::find(void* p){ if (p == 0) return -1; for (int i = 0; i < n; ++i) if (nodes[i].ptr == p) return i; return -1;}void AllocRing::clear(){ for (int i = 0; i < n; ++i) { if (nodes[i].ptr != 0) { delete(nodes[i].ptr); nodes[i].ptr = 0; } nodes[i].sz = 0; } current = 0;}void AllocRing::free(void* p){ int idx = find(p); if (idx >= 0) { delete nodes[idx].ptr; nodes[idx].ptr = 0; }}AllocRing::~AllocRing(){ clear();}int AllocRing::contains(void* p){ return find(p) >= 0;}static inline unsigned int good_size(unsigned int s){ unsigned int req = s + 4; unsigned int good = 8; while (good < req) good <<= 1; return good - 4;}void* AllocRing::alloc(int s){ unsigned int size = good_size(s); void* p; if (nodes[current].ptr != 0 && nodes[current].sz >= int(size) && nodes[current].sz < int(4 * size)) p = nodes[current].ptr; else { if (nodes[current].ptr != 0) operator delete (nodes[current].ptr); p = operator new (size); nodes[current].ptr = p; nodes[current].sz = size; } ++current; if (current >= n) current = 0; return p;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -