static int doOneCmd( IoContext *pCtx, u8 *aData, int pgsz, char *zCmd, char **pzOut ){ char c; char *z = zCmd; while( safe_isspace(*z) ) z++; c = *z; if( c==0 ){ if( pzOut ) *pzOut = z; return 0; } if( c=='s' || c=='S' ){ if( pzOut ) *pzOut = &z[1]; return fdatasync(pCtx->fd); } if( safe_isdigit(c) ){ i64 iOff = 0; int nByte = 0; int rc = 0; int nPg; int iPg; nByte = getNextSize(z, &z, &rc); if( rc || *z!='@' ) goto bad_command; z++; iOff = getNextSize(z, &z, &rc); if( rc || (safe_isspace(*z)==0 && *z!='\0') ) goto bad_command; if( pzOut ) *pzOut = z; nPg = (nByte+pgsz-1) / pgsz; lseek(pCtx->fd, iOff, SEEK_SET); for(iPg=0; iPg<nPg; iPg++){ write(pCtx->fd, aData, pgsz); } pCtx->nWrite += nByte/1024; return 0; } bad_command: testPrintError("unrecognized command: %s", zCmd); return 1; }
static void* find_fit(size_t size) //find best place { #ifdef __DEBUG__ fprintf(stderr, "find fitting place - size : %d\n", size); #endif void* bp; void* s; size_t sizeIndex = size; for(s = getSegBySize(size); ; s = getSegBySize(sizeIndex)) //increase seg size { sizeIndex = getNextSize(sizeIndex); for(bp = s; ; bp = getNextNode(bp)) //iterate list { if(bp==NULL) break; #ifdef __DEBUG__ fprintf(stderr, "searching : %u / allocated? : %u / size? : %u\n", getBlockHeader(bp), isAllocated(getBlockHeader(bp)), getSize(getBlockHeader(bp))); #endif if(!isAllocated(getBlockHeader(bp)) && size <= getSize(getBlockHeader(bp))) { return bp; } if(isTail(bp)) break; } if(s==seg_inf) break; } return NULL; }
int do_io(int nArg, char **azArg){ IoContext ctx; int pgsz; char *zFile; char *zPgsz; int i; int rc = 0; char *zStdin = 0; char *z; u8 *aData; memset(&ctx, 0, sizeof(IoContext)); if( nArg<2 ){ testPrintUsage("FILE PGSZ ?CMD-1 ...?"); return -1; } zFile = azArg[0]; zPgsz = azArg[1]; pgsz = getNextSize(zPgsz, 0, &rc); if( pgsz<=0 ){ testPrintError("Ridiculous page size: %d", pgsz); return -1; } aData = malloc(pgsz); memset(aData, 0x77, pgsz); ctx.fd = open(zFile, O_RDWR|O_CREAT, 0644); if( ctx.fd<0 ){ perror("open: "); return -1; } if( nArg==2 ){ readStdin(&zStdin); testTimeInit(); z = zStdin; while( *z && rc==0 ){ rc = doOneCmd(&ctx, aData, pgsz, z, &z); } }else{ testTimeInit(); for(i=2; i<nArg; i++){ rc = doOneCmd(&ctx, aData, pgsz, azArg[i], 0); } } printf("%dK written in %d ms\n", ctx.nWrite, testTimeGet()); free(zStdin); close(ctx.fd); return 0; }
void RtlDatasetBuilder::ensure(size32_t required) { if (required > maxSize) { maxSize = getNextSize(maxSize, required); buffer = (byte *)realloc(buffer, maxSize); if (!buffer) throw MakeStringException(-1, "Failed to allocate temporary dataset (requesting %d bytes)", maxSize); } }
bool TinyBitmap::add(const uint32_t val){ const uint16_t val_div = val >> 16; const uint16_t val_mod = val & 0xFFFF; if (tiny_bmp == nullptr){ const int aligned_alloc = posix_memalign(reinterpret_cast<void**>(&tiny_bmp), 8, sizes[0] * sizeof(uint16_t)); if (aligned_alloc != 0){ cerr << "TinyBitmap::add(): Aligned memory could not be allocated with error " << aligned_alloc << endl; exit(1); } std::memset(tiny_bmp, 0, sizes[0] * sizeof(uint16_t)); tiny_bmp[0] = (sizes[0] << 3) | bmp_mode | bits_16; tiny_bmp[2] = val_div; } if (getOffset() != val_div) return false; uint16_t sz = getSize(); uint16_t mode = getMode(); uint16_t cardinality = getCardinality(); // Compute if inserting new value triggers an increase of the container size if (((mode == bmp_mode) && (val_mod >= ((sz - 3) << 4))) || ((mode != bmp_mode) && (cardinality >= (sz - 3 - (mode == rle_list_mode))))){ // Means that in its current mode, container size must be increased to add a value // We need to compute if which mode has the smaller container size if ((mode != bmp_mode) && contains(val)) return true; runOptimize(); sz = getSize(); mode = getMode(); cardinality = getCardinality(); if ((mode != rle_list_mode) || (cardinality > (sz - 5))){ const uint16_t nb_uint_bmp = getNextSize((std::max(val_mod, static_cast<const uint16_t>(maximum() & 0xFFFF)) >> 4) + 4); bool res; if (mode == rle_list_mode){ const uint16_t nb_val = size(); const uint16_t nb_val_rle_list = getNextSize(getNextSize(sz) + 1); const uint16_t nb_val_list = getNextSize(nb_val + 4); const uint16_t nb_val_min = (nb_val > (0xFFFF - 48)) ? 0xFFFF : std::min(nb_val_rle_list, std::min(nb_val_list, nb_uint_bmp)); //cout << "TinyBitmap::add(): Size must be increased" << endl; //cout << "TinyBitmap::add(): nb_val_rle_list = " << nb_val_rle_list << endl; //cout << "TinyBitmap::add(): nb_val_list = " << nb_val_list << endl; //cout << "TinyBitmap::add(): nb_uint_bmp = " << nb_uint_bmp << endl; //cout << "TinyBitmap::add(): nb_val_min = " << nb_val_min << endl; if (nb_val_min > sizes[nb_sizes - 1]) return false; res = (nb_val_rle_list == nb_val_min); res = res ? change_sz(nb_val_min) : switch_mode(nb_val_min, (nb_val_list <= nb_uint_bmp) ? list_mode : bmp_mode); cardinality = getCardinality(); } else { const uint16_t nb_val_list = getNextSize(cardinality + 4); //cout << "TinyBitmap::add(): Size must be increased" << endl; //cout << "TinyBitmap::add(): max_val_mode = " << max_val_mode << endl; //cout << "TinyBitmap::add(): nb_uint_bmp = " << nb_uint_bmp << endl; //cout << "TinyBitmap::add(): nb_val_list = " << nb_val_list << endl; if (mode == bmp_mode) res = (nb_uint_bmp <= nb_val_list) ? change_sz(nb_uint_bmp) : switch_mode(nb_val_list, list_mode); else res = (nb_val_list <= nb_uint_bmp) ? change_sz(nb_val_list) : switch_mode(nb_uint_bmp, bmp_mode); } if (!res) return false; mode = getMode(); } } //cout << "TinyBitmap::add(): mode = " << mode << endl; if (mode == bmp_mode){ // Bitmap mode uint16_t& div = tiny_bmp[(val_mod >> 4) + 3]; const uint16_t mod = 1U << (val_mod & 0xF); tiny_bmp[1] += ((div & mod) == 0); // += (1 << 8) if not already set (increase cardinality in header), 0 otherwise div |= mod; // Insert new value }