/** * Basic test for reallocation */ void test_Brealloc(void) { UBFH *p_ub = NULL; p_ub=Balloc(1, 30); assert_not_equal(p_ub, NULL); assert_equal(Badd(p_ub, T_STRING_FLD, BIG_TEST_STRING, 0), FAIL); assert_equal(Berror, BNOSPACE); /* Now reallocate, space should be bigger! */ p_ub=Brealloc(p_ub, 1, strlen(BIG_TEST_STRING)+1+2/* align */); assert_not_equal(p_ub, NULL); assert_equal(Badd(p_ub, T_STRING_FLD, BIG_TEST_STRING, 0), SUCCEED); /* should not allow to reallocate to 0! */ assert_equal(Brealloc(p_ub, 1, 0), NULL); assert_equal(Berror, BEINVAL); /* should be bigger than existing. */ assert_equal(Brealloc(p_ub, 1, strlen(BIG_TEST_STRING)+1), NULL); assert_equal(Berror, BEINVAL); assert_equal(SUCCEED, Bfree(p_ub)); }
void texcache_setupmemcache(void) { if (!glusememcache || texcache.memcache.noalloc || !texcache_enabled()) return; texcache.memcache.size = Bfilelength(texcache.filehandle); if (texcache.memcache.size <= 0) return; texcache.memcache.ptr = (uint8_t *)Brealloc(texcache.memcache.ptr, texcache.memcache.size); if (!texcache.memcache.ptr) { initprintf("Failed allocating %d bytes for memcache, disabling memcache.\n", (int)texcache.memcache.size); texcache_clearmemcache(); texcache.memcache.noalloc = 1; return; } if (Bread(texcache.filehandle, texcache.memcache.ptr, texcache.memcache.size) != (bssize_t)texcache.memcache.size) { initprintf("Failed reading texcache into memcache!\n"); texcache_clearmemcache(); texcache.memcache.noalloc = 1; } }
void texcache_syncmemcache(void) { int32_t len = Bfilelength(texcache.filehandle); if (!texcache.memcache.ptr || texcache.filehandle == -1 || len <= (int32_t)texcache.memcache.size) return; texcache.memcache.ptr = (uint8_t *)Brealloc(texcache.memcache.ptr, len); if (!texcache.memcache.ptr) { texcache_clearmemcache(); initprintf("Failed syncing memcache to texcache, disabling memcache.\n"); texcache.memcache.noalloc = 1; } else { initprintf("Syncing memcache to texcache\n"); Blseek(texcache.filehandle, texcache.memcache.size, BSEEK_SET); if (Bread(texcache.filehandle, texcache.memcache.ptr + texcache.memcache.size, len - texcache.memcache.size) != (bssize_t)(len-texcache.memcache.size)) { initprintf("polymost_cachesync: Failed reading texcache into memcache!\n"); texcache_clearmemcache(); texcache.memcache.noalloc = 1; } else { texcache.memcache.size = len; } } }
// <length> is the file size, for consistency checking. int32_t ANIM_LoadAnim(const uint8_t *buffer, int32_t length) { int32_t i; length -= sizeof(lpfileheader)+128+768; if (length < 0) return -1; anim = (anim_t *)Brealloc(anim, sizeof(anim_t)); anim->curlpnum = 0xffff; anim->currentframe = -1; // this just modifies the data in-place instead of copying it elsewhere now anim->lpheader = (lpfileheader *)(anim->buffer = (uint8_t *)buffer); anim->lpheader->id = B_LITTLE32(anim->lpheader->id); anim->lpheader->maxLps = B_LITTLE16(anim->lpheader->maxLps); anim->lpheader->nLps = B_LITTLE16(anim->lpheader->nLps); anim->lpheader->nRecords = B_LITTLE32(anim->lpheader->nRecords); anim->lpheader->maxRecsPerLp = B_LITTLE16(anim->lpheader->maxRecsPerLp); anim->lpheader->lpfTableOffset = B_LITTLE16(anim->lpheader->lpfTableOffset); anim->lpheader->contentType = B_LITTLE32(anim->lpheader->contentType); anim->lpheader->width = B_LITTLE16(anim->lpheader->width); anim->lpheader->height = B_LITTLE16(anim->lpheader->height); anim->lpheader->nFrames = B_LITTLE32(anim->lpheader->nFrames); anim->lpheader->framesPerSecond = B_LITTLE16(anim->lpheader->framesPerSecond); length -= anim->lpheader->nLps * sizeof(lp_descriptor); if (length < 0) return -2; buffer += sizeof(lpfileheader)+128; // load the color palette for (i = 0; i < 768; i += 3) { anim->pal[i+2] = (*buffer++)>>2; anim->pal[i+1] = (*buffer++)>>2; anim->pal[i] = (*buffer++)>>2; buffer++; } // set up large page descriptors anim->LpArray = (lp_descriptor *)buffer; // theoretically we should be able to play files with more than 256 frames now // assuming the utilities to create them can make them that way for (i = 0; i < anim->lpheader->nLps; i++) { anim->LpArray[i].baseRecord = B_LITTLE16(anim->LpArray[i].baseRecord); anim->LpArray[i].nRecords = B_LITTLE16(anim->LpArray[i].nRecords); anim->LpArray[i].nBytes = B_LITTLE16(anim->LpArray[i].nBytes); } return 0; }