123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- #include "precomp_wasabi_bfc.h"
- #include "freelist.h"
- FreelistPriv::FreelistPriv() {
- total_allocated = 0;
- }
- FreelistPriv::~FreelistPriv() {
- #ifdef ASSERTS_ENABLED
- if (total_allocated != 0) DebugStringW(L"didn't free entire freelist!(1)\n");
- if (blocks.getNumItems() != 0) DebugStringW(L"didn't free entire freelist!(2)\n");
- #endif
- }
- void *FreelistPriv::getRecord(int typesize, int blocksize, int initialblocksize) {
- #ifdef FREELIST_FUCT
- return MALLOC(typesize);
- #else
- ASSERT(typesize >= sizeof(void *));
- FLMemBlock *mem = NULL;
- for (int i = 0; i < blocks.getNumItems(); i++) {
- mem = blocks[i];
- if (mem->freelist != NULL) break;
- mem = NULL;
- }
- if (mem == NULL) {
-
- int siz = (blocks.getNumItems() ? blocksize : initialblocksize);
-
- mem = new FLMemBlock(siz*typesize);
-
- char *record = static_cast<char *>(mem->freelist);
- void **ptr;
- for (int i = 0; i < siz-1; i++) {
- ptr = reinterpret_cast<void **>(record);
- record += typesize;
- *ptr = static_cast<void *>(record);
- }
-
- ptr = reinterpret_cast<void **>(record);
- *ptr = NULL;
- blocks.addItem(mem);
- }
-
- void *ret = mem->freelist;
-
- mem->freelist = *(static_cast<void **>(mem->freelist));
- mem->nallocated++;
- total_allocated++;
- return ret;
- #endif
- }
- void FreelistPriv::freeRecord(void *record) {
- #ifdef FREELIST_FUCT
- FREE(record);
- #else
- FLMemBlock *mem=NULL;
- for (int i = 0; i < blocks.getNumItems(); i++) {
- mem = blocks[i];
- if (mem->isMine(reinterpret_cast<MBT*>(record))) break;
- mem = NULL;
- }
- ASSERTPR(mem != NULL, "attempted to free record with no block");
-
- *reinterpret_cast<void **>(record) = mem->freelist;
- mem->freelist = record;
- ASSERT(mem->nallocated > 0);
- mem->nallocated--;
- if (mem->nallocated == 0) {
- blocks.delItem(mem);
- }
- total_allocated--;
- #endif
- }
|