/* ============== Cache_Check ============== */ void *Cache_Check (cache_user_t *c) { cache_system_t *cs; if (!c->data) return NULL; cs = ((cache_system_t *)c->data) - 1; // move to head of LRU Cache_UnlinkLRU (cs); Cache_MakeLRU (cs); return c->data; }
/* * ============== * Cache_Check * ============== */ void * Cache_Check(const cache_user_t *c) { cache_system_t *cs; if (!c->data) return NULL; cs = Cache_System(c); /* move to head of LRU */ Cache_UnlinkLRU(cs); Cache_MakeLRU(cs); return c->data; }
/* ============ Cache_TryAlloc Looks for a free block of memory between the high and low hunk marks Size should already include the header and padding ============ */ cache_system_t *Cache_TryAlloc (int size, qboolean nobottom) { cache_system_t *cs, *res; // is the cache completely empty? if (!nobottom && cache_head.prev == &cache_head) { if (hunk_size - hunk_high_used - hunk_low_used < size) Sys_Error ("Cache_TryAlloc: %i is greater then free hunk", size); res = (cache_system_t *) (hunk_base + hunk_low_used); memset (res, 0, sizeof(*res)); res->size = size; cache_head.prev = cache_head.next = res; res->prev = res->next = &cache_head; Cache_MakeLRU (res); return res; } // search from the bottom up for space res = (cache_system_t *) (hunk_base + hunk_low_used); cs = cache_head.next; do { if (!nobottom || cs != cache_head.next) { if ( (byte *)cs - (byte *)res >= size) { // found space memset (res, 0, sizeof(*res)); res->size = size; res->next = cs; res->prev = cs->prev; cs->prev->next = res; cs->prev = res; Cache_MakeLRU (res); return res; } } // continue looking res = (cache_system_t *)((byte *)cs + cs->size); cs = cs->next; } while (cs != &cache_head); // try to allocate one at the very end if ( hunk_base + hunk_size - hunk_high_used - (byte *)res >= size) { memset (res, 0, sizeof(*res)); res->size = size; res->next = &cache_head; res->prev = cache_head.prev; cache_head.prev->next = res; cache_head.prev = res; Cache_MakeLRU (res); return res; } return NULL; // couldn't allocate }
/* ============ Cache_TryAlloc Looks for a free block of memory between the high and low hunk marks Size should already include the header and padding ============ */ static cache_system_t *Cache_TryAlloc (int size, qboolean nobottom) { cache_system_t *cs, *new_cs; // is the cache completely empty? if (!nobottom && cache_head.prev == &cache_head) { if (hunk_size - hunk_high_used - hunk_low_used < size) Sys_Error ("%s: out of hunk memory (failed to allocate %i bytes)", __thisfunc__, size); new_cs = (cache_system_t *) (hunk_base + hunk_low_used); memset (new_cs, 0, sizeof(*new_cs)); new_cs->size = size; cache_head.prev = cache_head.next = new_cs; new_cs->prev = new_cs->next = &cache_head; Cache_MakeLRU (new_cs); return new_cs; } // search from the bottom up for space new_cs = (cache_system_t *) (hunk_base + hunk_low_used); cs = cache_head.next; do { if (!nobottom || cs != cache_head.next) { if ((byte *)cs - (byte *)new_cs >= size) { // found space memset (new_cs, 0, sizeof(*new_cs)); new_cs->size = size; new_cs->next = cs; new_cs->prev = cs->prev; cs->prev->next = new_cs; cs->prev = new_cs; Cache_MakeLRU (new_cs); return new_cs; } } // continue looking new_cs = (cache_system_t *)((byte *)cs + cs->size); cs = cs->next; } while (cs != &cache_head); // try to allocate one at the very end if (hunk_base + hunk_size - hunk_high_used - (byte *)new_cs >= size) { memset (new_cs, 0, sizeof(*new_cs)); new_cs->size = size; new_cs->next = &cache_head; new_cs->prev = cache_head.prev; cache_head.prev->next = new_cs; cache_head.prev = new_cs; Cache_MakeLRU (new_cs); return new_cs; } return NULL; // couldn't allocate }
/* ============ Cache_TryAlloc Looks for a free block of memory between the high and low hunk marks Size should already include the header and padding ============ */ cache_system_t *Cache_TryAlloc(int size, qbool nobottom) { cache_system_t *cs, *new_block; // is the cache completely empty? if (!nobottom && cache_head.prev == &cache_head) { if (hunk_size - hunk_high_used - hunk_low_used < size) { Sys_Error("Cache_TryAlloc: %i is greater than free hunk", size); } new_block = (cache_system_t *)(hunk_base + hunk_low_used); memset(new_block, 0, sizeof(*new_block)); new_block->size = size; cache_head.prev = cache_head.next = new_block; new_block->prev = new_block->next = &cache_head; Cache_MakeLRU(new_block); return new_block; } // search from the bottom up for space new_block = (cache_system_t *)(hunk_base + hunk_low_used); cs = cache_head.next; do { if (!nobottom || cs != cache_head.next) { if ((byte *)cs - (byte *)new_block >= size) { // found space memset(new_block, 0, sizeof(*new_block)); new_block->size = size; new_block->next = cs; new_block->prev = cs->prev; cs->prev->next = new_block; cs->prev = new_block; Cache_MakeLRU(new_block); return new_block; } } // continue looking new_block = (cache_system_t *)((byte *)cs + cs->size); cs = cs->next; } while (cs != &cache_head); // try to allocate one at the very end if (hunk_base + hunk_size - hunk_high_used - (byte *)new_block >= size) { memset(new_block, 0, sizeof(*new_block)); new_block->size = size; new_block->next = &cache_head; new_block->prev = cache_head.prev; cache_head.prev->next = new_block; cache_head.prev = new_block; Cache_MakeLRU(new_block); return new_block; } return NULL; // couldn't allocate }
/* * ============ * Cache_TryAlloc * * Looks for a free block of memory between the high and low hunk marks * Size should already include the header and padding * ============ */ static cache_system_t * Cache_TryAlloc(int size, qboolean nobottom) { cache_system_t *cs, *newobj; /* is the cache completely empty? */ if (!nobottom && cache_head.prev == &cache_head) { if (hunk_size - hunk_high_used - hunk_low_used < size) Sys_Error("%s: %i is greater than free hunk", __func__, size); newobj = (cache_system_t *)(hunk_base + hunk_low_used); memset(newobj, 0, sizeof(*newobj)); newobj->size = size; cache_head.prev = cache_head.next = newobj; newobj->prev = newobj->next = &cache_head; Cache_MakeLRU(newobj); return newobj; } /* search from the bottom up for space */ newobj = (cache_system_t *)(hunk_base + hunk_low_used); cs = cache_head.next; do { if (!nobottom || cs != cache_head.next) { if ((byte *)cs - (byte *)newobj >= size) { /* found space */ memset(newobj, 0, sizeof(*newobj)); newobj->size = size; newobj->next = cs; newobj->prev = cs->prev; cs->prev->next = newobj; cs->prev = newobj; Cache_MakeLRU(newobj); return newobj; } } /* continue looking */ newobj = (cache_system_t *)((byte *)cs + cs->size); cs = cs->next; } while (cs != &cache_head); /* try to allocate one at the very end */ if (hunk_base + hunk_size - hunk_high_used - (byte *)newobj >= size) { memset(newobj, 0, sizeof(*newobj)); newobj->size = size; newobj->next = &cache_head; newobj->prev = cache_head.prev; cache_head.prev->next = newobj; cache_head.prev = newobj; Cache_MakeLRU(newobj); return newobj; } return NULL; /* couldn't allocate */ }