/* ============== Cache_Alloc ============== */ void *Cache_Alloc (cache_user_t *c, int size, const char *name) { cache_system_t *cs; if (c->data) Sys_Error ("Cache_Alloc: already allocated"); if (size <= 0) Sys_Error ("Cache_Alloc: size %i", size); size = (size + sizeof(cache_system_t) + 15) & ~15; // find memory for it while (1) { cs = Cache_TryAlloc (size, false); if (cs) { Q_strncpy(cs->name, name, sizeof(cs->name)); c->data = (void *)(cs+1); cs->user = c; break; } // free the least recently used entry // assume if it's a locked entry that we've run out. if (cache_head.lru_prev == &cache_head || cache_head.lru_prev->locked) Sys_Error ("Cache_Alloc: out of memory"); Cache_Free ( cache_head.lru_prev->user ); } return Cache_Check (c); }
/* ============== Cache_Alloc ============== */ void *Cache_Alloc (cache_user_t *c, int size, const char *name) { cache_system_t *cs; if (c->data) Sys_Error ("Cache_Alloc: allready allocated"); if (size <= 0) Sys_Error ("Cache_Alloc: size %i", size); size = (size + sizeof(cache_system_t) + 15) & ~15; // find memory for it while (1) { cs = Cache_TryAlloc (size, false); if (cs) { strncpy (cs->name, name, sizeof(cs->name)-1); c->data = (void *)(cs+1); cs->user = c; break; } // free the least recently used cahedat if (cache_head.lru_prev == &cache_head) Sys_Error ("Cache_Alloc: out of memory"); // not enough memory at all Cache_Free ( cache_head.lru_prev->user ); } return Cache_Check (c); }
/* ============== Cache_Alloc ============== */ void *Cache_Alloc (cache_user_t *c, int size, const char *name) { cache_system_t *cs; if (c->data) Sys_Error ("%s: %s is already allocated", __thisfunc__, name); if (size <= 0) Sys_Error ("%s: bad size %i for %s", __thisfunc__, size, name); size = (size + sizeof(cache_system_t) + 15) & ~15; // find memory for it while (1) { cs = Cache_TryAlloc (size, false); if (cs) { q_strlcpy (cs->name, name, CACHENAME_LEN); c->data = (void *)(cs + 1); cs->user = c; break; } // free the least recently used cahedat if (cache_head.lru_prev == &cache_head) // not enough memory at all Sys_Error ("%s: out of memory", __thisfunc__); Cache_Free ( cache_head.lru_prev->user ); } return Cache_Check (c); }
/* * ============== * Cache_AllocPadded * ============== */ void * Cache_AllocPadded(cache_user_t *c, int pad, int size, const char *name) { cache_system_t *cs; if (c->data) Sys_Error("%s: allready allocated", __func__); if (size <= 0) Sys_Error("%s: size %i", __func__, size); size = (size + pad + sizeof(cache_system_t) + 15) & ~15; /* find memory for it */ while (1) { cs = Cache_TryAlloc(size, false); if (cs) { strncpy(cs->name, name, sizeof(cs->name) - 1); cs->user = c; c->pad = pad; c->data = Cache_Data(cs); break; } /* free the least recently used cache data */ if (cache_head.lru_prev == &cache_head) Sys_Error("%s: out of memory", __func__); /* not enough memory at all */ Cache_Free(cache_head.lru_prev->user); } return Cache_Check(c); }
/* =========== Cache_Move =========== */ void Cache_Move(cache_system_t *c) { cache_system_t *new_block; // we are clearing up space at the bottom, so only allocate it late new_block = Cache_TryAlloc(c->size, true); if (new_block) { memcpy(new_block + 1, c + 1, c->size - sizeof(cache_system_t)); new_block->user = c->user; memcpy(new_block->name, c->name, sizeof(new_block->name)); Cache_Free(c->user); new_block->user->data = (void *)(new_block + 1); } else { Cache_Free(c->user); // tough luck... } }
/* =========== Cache_Move =========== */ static void Cache_Move ( cache_system_t *c) { cache_system_t *new_cs; // we are clearing up space at the bottom, so only allocate it late new_cs = Cache_TryAlloc (c->size, true); if (new_cs) { // Con_Printf ("cache_move ok\n"); memcpy (new_cs+1, c+1, c->size - sizeof(cache_system_t)); new_cs->user = c->user; memcpy (new_cs->name, c->name, sizeof(new_cs->name)); Cache_Free (c->user); new_cs->user->data = (void *)(new_cs + 1); } else { // Con_Printf ("cache_move failed\n"); Cache_Free (c->user); // tough luck... } }
/* * =========== * Cache_Move * =========== */ static void Cache_Move(cache_system_t *c) { cache_system_t *newobj; int pad; /* we are clearing up space at the bottom, so only allocate it late */ newobj = Cache_TryAlloc(c->size, true); if (newobj) { memcpy(newobj + 1, c + 1, c->size - sizeof(cache_system_t)); newobj->user = c->user; memcpy(newobj->name, c->name, sizeof(newobj->name)); pad = c->user->pad; Cache_Free(c->user); newobj->user->pad = pad; newobj->user->data = Cache_Data(newobj); } else { /* tough luck... */ Cache_Free(c->user); } }