/** * Allocate storage within oi_corr struct * * Sets the oi_corr::ncorr attribute of @a pCorr. * * @param pCorr pointer to corr data struct, see exchange.h * @param ncorr number of correlated data (table rows) to allocate */ void alloc_oi_corr(oi_corr *pCorr, int ncorr) { pCorr->iindx = chkmalloc(ncorr * sizeof(pCorr->iindx[0])); pCorr->jindx = chkmalloc(ncorr * sizeof(pCorr->jindx[0])); pCorr->corr = chkmalloc(ncorr * sizeof(pCorr->corr[0])); pCorr->ncorr = ncorr; }
static inline void brainflayer_init_globals() { /* only initialize stuff once */ if (!brainflayer_is_init) { /* initialize buffers */ mem = chkmalloc(4096); unhexed = chkmalloc(unhexed_sz); /* initialize hashs */ sha256_ctx = chkmalloc(sizeof(*sha256_ctx)); /* set the flag */ brainflayer_is_init = 1; } }
/** * Allocate storage within oi_vis2 struct * * Sets the oi_vis2::numrec and oi_vis2::nwave attributes of @a pVis2. * * @param pVis2 pointer to data struct, see exchange.h * @param numrec number of records (table rows) to allocate * @param nwave number of wavelength channels to allocate */ void alloc_oi_vis2(oi_vis2 *pVis2, long numrec, int nwave) { int i; oi_vis2_record *pRec; pVis2->record = chkmalloc(numrec * sizeof(oi_vis2_record)); for (i = 0; i < numrec; i++) { pRec = &pVis2->record[i]; pRec->vis2data = chkmalloc(nwave * sizeof(pRec->vis2data[0])); pRec->vis2err = chkmalloc(nwave * sizeof(pRec->vis2err[0])); pRec->flag = chkmalloc(nwave * sizeof(pRec->flag[0])); } pVis2->numrec = numrec; pVis2->nwave = nwave; }
/** * Allocate storage within oi_flux struct * * Sets the oi_flux::numrec and oi_flux::nwave attributes of * @a pFlux. * * @param pFlux pointer to data struct, see exchange.h * @param numrec number of records (table rows) to allocate * @param nwave number of wavelength channels to allocate */ void alloc_oi_flux(oi_flux *pFlux, long numrec, int nwave) { int i; oi_flux_record *pRec; pFlux->record = chkmalloc(numrec * sizeof(oi_flux_record)); for (i = 0; i < numrec; i++) { pRec = &pFlux->record[i]; pRec->fluxdata = chkmalloc(nwave * sizeof(pRec->fluxdata[0])); pRec->fluxerr = chkmalloc(nwave * sizeof(pRec->fluxerr[0])); pRec->flag = chkmalloc(nwave * sizeof(pRec->flag[0])); } pFlux->numrec = numrec; pFlux->nwave = nwave; }
/** * Allocate storage within oi_inspol struct * * Sets the oi_inspol::numrec and oi_inspol::nwave attributes of @a pInspol. * * @param pInspol pointer to inspol data struct, see exchange.h * @param numrec number of records (table rows) to allocate * @param nwave number of wavelength channels to allocate */ void alloc_oi_inspol(oi_inspol *pInspol, long numrec, int nwave) { int i; oi_inspol_record *pRec; pInspol->record = chkmalloc(numrec * sizeof(oi_inspol_record)); for (i = 0; i < numrec; i++) { pRec = &pInspol->record[i]; pRec->jxx = chkmalloc(nwave * sizeof(pRec->jxx[0])); pRec->jyy = chkmalloc(nwave * sizeof(pRec->jyy[0])); pRec->jxy = chkmalloc(nwave * sizeof(pRec->jxy[0])); pRec->jyx = chkmalloc(nwave * sizeof(pRec->jyx[0])); } pInspol->numrec = numrec; pInspol->nwave = nwave; }
void CreateCubeHeightMaps(TextureData* terrainTextures[6], mat4 terrainTransformationMatrix[6], struct PlanetStruct* planet) { GLuint i; for(i = 0; i < 6; i++) { terrainTextures[i] = chkmalloc(sizeof(TextureData)); } // Load terrain data for (i = 0; i < 6; i++) if(fuzzy == 0) LoadTGATextureData("textures/fft-terrain.tga", terrainTextures[i]); else LoadTGATextureData("testTGA.tga", terrainTextures[i]); //Generate terrain model matrix planet->terrainWidth = (GLfloat)terrainTextures[0]->width; planet->terrainHeight = (GLfloat)terrainTextures[0]->height; GLfloat distanceToMiddleX = ((GLfloat)terrainTextures[0]->width*0.5f); GLfloat distanceToMiddleZ = ((GLfloat)terrainTextures[0]->height*0.5f); GLfloat distanceToMiddleY = planet->radius; for (i = 0; i < 4; ++i) { terrainTransformationMatrix[i] = T(-distanceToMiddleX, distanceToMiddleY, -distanceToMiddleZ); terrainTransformationMatrix[i] = Mult( Rz(M_PI*0.5f*(GLfloat)i), terrainTransformationMatrix[i] ); } //Last two sides for (i = 0; i < 2; ++i) { terrainTransformationMatrix[4+i] = T(-distanceToMiddleX, distanceToMiddleY, -distanceToMiddleZ); terrainTransformationMatrix[4+i] = Mult( Rx(M_PI*(0.5f+(GLfloat)i)), terrainTransformationMatrix[4+i] ); } //Weird offset: (Probably size dependent) terrainTransformationMatrix[1] = Mult(T(0, 1, 0), terrainTransformationMatrix[1]); terrainTransformationMatrix[2] = Mult(T(-1, 1, 0), terrainTransformationMatrix[2]); terrainTransformationMatrix[3] = Mult(T(-1, 0, 0), terrainTransformationMatrix[3]); terrainTransformationMatrix[4] = Mult(T(0, 0, -1), terrainTransformationMatrix[4]); terrainTransformationMatrix[5] = Mult(T(0, 1, 0), terrainTransformationMatrix[5]); }
void *jsonz_object_new(int type) { jsonz_object_t *obj; jsonz_number_t *num; jsonz_string_t *str; jsonz_array_t *arr; jsonz_dict_t *dic; void *o; switch (type) { case JSONZ_TYPE_NULL: o = chkmalloc(sizeof(jsonz_null_t)); break; case JSONZ_TYPE_BOOL: case JSONZ_TYPE_INT: o = chkmalloc(sizeof(jsonz_number_t)); num = o; num->val.intval = 0; break; case JSONZ_TYPE_FLOAT: o = chkmalloc(sizeof(jsonz_number_t)); num = o; num->val.fltval = 0.0; break; case JSONZ_TYPE_STRING: o = chkmalloc(sizeof(jsonz_string_t)); str = o; str->str = NULL; break; case JSONZ_TYPE_ARRAY: o = chkmalloc(sizeof(jsonz_array_t)); arr = o; arr->size = 0; arr->vals = NULL; break; case JSONZ_TYPE_DICT: o = chkmalloc(sizeof(jsonz_dict_t)); dic = o; dic->size = 0; dic->keys = NULL; dic->vals = NULL; break; default: return NULL; } obj = o; obj->type = type; return obj; }
/** * Allocate storage within oi_t3 struct * * Sets the oi_t3::numrec and oi_t3::nwave attributes of @a pT3. * * @param pT3 pointer to data struct, see exchange.h * @param numrec number of records (table rows) to allocate * @param nwave number of wavelength channels to allocate */ void alloc_oi_t3(oi_t3 *pT3, long numrec, int nwave) { int i; oi_t3_record *pRec; pT3->record = chkmalloc(numrec * sizeof(oi_t3_record)); for (i = 0; i < numrec; i++) { pRec = &pT3->record[i]; pRec->t3amp = chkmalloc(nwave * sizeof(pRec->t3amp[0])); pRec->t3amperr = chkmalloc(nwave * sizeof(pRec->t3amperr[0])); pRec->t3phi = chkmalloc(nwave * sizeof(pRec->t3phi[0])); pRec->t3phierr = chkmalloc(nwave * sizeof(pRec->t3phierr[0])); pRec->flag = chkmalloc(nwave * sizeof(pRec->flag[0])); } pT3->numrec = numrec; pT3->nwave = nwave; }
/** * Allocate storage within oi_vis struct * * Sets the oi_vis::numrec and oi_vis::nwave attributes of @a pVis. * * @param pVis pointer to data struct, see exchange.h * @param numrec number of records (table rows) to allocate * @param nwave number of wavelength channels to allocate */ void alloc_oi_vis(oi_vis *pVis, long numrec, int nwave) { int i; oi_vis_record *pRec; pVis->record = chkmalloc(numrec * sizeof(oi_vis_record)); for (i = 0; i < numrec; i++) { pRec = &pVis->record[i]; pRec->visamp = chkmalloc(nwave * sizeof(pRec->visamp[0])); pRec->visamperr = chkmalloc(nwave * sizeof(pRec->visamperr[0])); pRec->visphi = chkmalloc(nwave * sizeof(pRec->visphi[0])); pRec->visphierr = chkmalloc(nwave * sizeof(pRec->visphierr[0])); pRec->flag = chkmalloc(nwave * sizeof(pRec->flag[0])); pRec->visrefmap = NULL; pRec->rvis = NULL; pRec->rviserr = NULL; pRec->ivis = NULL; pRec->iviserr = NULL; } pVis->numrec = numrec; pVis->nwave = nwave; pVis->usevisrefmap = FALSE; pVis->usecomplex = FALSE; }
int main(int argc, char **argv) { FILE *ifile = stdin; FILE *ofile = stdout; FILE *ffile = NULL; int ret; float alpha, ilines_rate, ilines_rate_avg; int64_t raw_lines = -1; uint64_t report_mask = 0; uint64_t time_last, time_curr, time_delta; uint64_t time_start, time_elapsed; uint64_t ilines_last, ilines_curr, ilines_delta; uint64_t olines; int skipping = 0, tty = 0; char *line = NULL; size_t line_sz = 0; int line_read; int c, spok = 0, aopt = 0, vopt = 0, wopt = 16, Lopt = 0; int nopt_mod = 0, nopt_rem = 0; uint64_t kopt = 0; unsigned char *bopt = NULL, *iopt = NULL, *oopt = NULL; unsigned char *topt = NULL, *sopt = NULL, *popt = NULL; unsigned char *mopt = NULL, *fopt = NULL, *ropt = NULL; while ((c = getopt(argc, argv, "avb:hi:k:f:m:n:o:p:s:r:t:w:L")) != -1) { switch (c) { case 'a': aopt = 1; // open output file in append mode break; case 'k': kopt = strtoull(optarg, NULL, 10); // skip first k lines of input skipping = 1; break; case 'n': // only try the rem'th of every mod lines (one indexed) nopt_rem = atoi(optarg) - 1; optarg = strchr(optarg, '/'); if (optarg != NULL) { nopt_mod = atoi(optarg+1); } skipping = 1; break; case 'w': if (wopt > 1) wopt = atoi(optarg); break; case 'm': mopt = optarg; // table file wopt = 1; // auto break; case 'v': vopt = 1; // verbose break; case 'b': bopt = optarg; // bloom filter file break; case 'f': fopt = optarg; // full filter file break; case 'i': iopt = optarg; // input file break; case 'o': oopt = optarg; // output file break; case 's': sopt = optarg; // salt break; case 'p': popt = optarg; // passphrase break; case 'r': ropt = optarg; // rushwallet break; case 't': topt = optarg; // type of input break; case 'L': Lopt = 1; // lookup output break; case 'h': // show help usage(argv[0]); return 0; case '?': // show error return 1; default: // should never be reached... printf("got option '%c' (%d)\n", c, c); return 1; } } if (optind < argc) { if (optind == 1 && argc == 2) { // older versions of brainflayer had the bloom filter file as a // single optional argument, this keeps compatibility with that bopt = argv[1]; } else { fprintf(stderr, "Invalid arguments:\n"); while (optind < argc) { fprintf(stderr, " '%s'\n", argv[optind++]); } exit(1); } } if (nopt_rem != 0 || nopt_mod != 0) { // note that nopt_rem has had one subtracted at option parsing if (nopt_rem >= nopt_mod) { bail(1, "Invalid '-n' argument, remainder '%d' must be <= modulus '%d'\n", nopt_rem+1, nopt_mod); } else if (nopt_rem < 0) { bail(1, "Invalid '-n' argument, remainder '%d' must be > 0\n", nopt_rem+1); } else if (nopt_mod < 1) { bail(1, "Invalid '-n' argument, modulus '%d' must be > 0\n", nopt_mod); } } if (wopt < 1 || wopt > 28) { bail(1, "Invalid window size '%d' - must be >= 1 and <= 28\n", wopt); } else { // very rough sanity check of window size struct sysinfo info; sysinfo(&info); uint64_t sysram = info.mem_unit * info.totalram; if (3584LLU*(1<<wopt) > sysram) { bail(1, "Not enough ram for requested window size '%d'\n", wopt); } } if (topt != NULL) { if (strcmp(topt, "str") == 0) { input2hash160 = &pass2hash160; } else if (strcmp(topt, "hex") == 0) { input2hash160 = &hexpass2hash160; } else if (strcmp(topt, "priv") == 0) { input2hash160 = &hexpriv2hash160; } else if (strcmp(topt, "warp") == 0) { spok = 1; input2hash160 = popt ? &warpsalt2hash160 : &warppass2hash160; } else if (strcmp(topt, "bwio") == 0) { spok = 1; input2hash160 = popt ? &bwiosalt2hash160 : &bwiopass2hash160; } else if (strcmp(topt, "bv2") == 0) { spok = 1; input2hash160 = popt ? &brainv2salt2hash160 : &brainv2pass2hash160; } else if (strcmp(topt, "rush") == 0) { input2hash160 = &rush2hash160; } else { bail(1, "Unknown input type '%s'.\n", topt); } } else { topt = "str"; input2hash160 = &pass2hash160; } if (spok) { if (sopt && popt) { bail(1, "Cannot specify both a salt and a passphrase\n"); } if (popt) { kdfpass = popt; kdfpass_sz = strlen(popt); } else { if (sopt) { kdfsalt = sopt; kdfsalt_sz = strlen(kdfsalt); } else { kdfsalt = chkmalloc(0); kdfsalt_sz = 0; } } } else { if (popt) { bail(1, "Specifying a passphrase not supported with input type '%s'\n", topt); } else if (sopt) { bail(1, "Specifying a salt not supported with this input type '%s'\n", topt); } } if (ropt) { if (input2hash160 != &rush2hash160) { bail(1, "Specifying a url fragment only supported with input type 'rush'\n"); } kdfsalt = ropt; kdfsalt_sz = strlen(kdfsalt) - sizeof(rushchk)*2; if (kdfsalt[kdfsalt_sz-1] != '!') { bail(1, "Invalid rushwallet url fragment '%s'\n", kdfsalt); } unhex(kdfsalt+kdfsalt_sz, sizeof(rushchk)*2, rushchk, sizeof(rushchk)); kdfsalt[kdfsalt_sz] = '\0'; } else if (input2hash160 == &rush2hash160) { bail(1, "The '-r' option is required for rushwallet.\n"); } if (bopt) { if (Lopt) { bail(1, "The '-L' option cannot be used with a bloom filter\n"); } if ((ret = mmapf(&bloom_mmapf, bopt, BLOOM_SIZE, MMAPF_RNDRD)) != MMAPF_OKAY) { bail(1, "failed to open bloom filter '%s': %s\n", bopt, mmapf_strerror(ret)); } else if (bloom_mmapf.mem == NULL) { bail(1, "got NULL pointer trying to set up bloom filter\n"); } bloom = bloom_mmapf.mem; } if (fopt) { if (!bopt) { bail(1, "The '-f' option must be used with a bloom filter\n"); } if ((ffile = fopen(fopt, "r")) == NULL) { bail(1, "failed to open '%s' for reading: %s\n", fopt, strerror(errno)); } } if (iopt) { if ((ifile = fopen(iopt, "r")) == NULL) { bail(1, "failed to open '%s' for reading: %s\n", iopt, strerror(errno)); } // increases readahead window, don't really care if it fails posix_fadvise(fileno(ifile), 0, 0, POSIX_FADV_SEQUENTIAL); } if (oopt && (ofile = fopen(oopt, (aopt ? "a" : "w"))) == NULL) { bail(1, "failed to open '%s' for writing: %s\n", oopt, strerror(errno)); } /* line buffer output */ setvbuf(ofile, NULL, _IOLBF, 0); /* line buffer stderr */ setvbuf(stderr, NULL, _IOLBF, 0); if (vopt && ofile == stdout && isatty(fileno(stdout))) { tty = 1; } brainflayer_init_globals(); if (secp256k1_ec_pubkey_precomp_table(wopt, mopt) != 0) { bail(1, "failed to initialize precomputed table\n"); } if (vopt) { /* initialize timing data */ time_start = time_last = getns(); olines = ilines_last = ilines_curr = 0; ilines_rate_avg = -1; alpha = 0.500; } else { time_start = time_last = 0; // prevent compiler warning about uninitialized use } for (;;) { if ((line_read = getline(&line, &line_sz, ifile)-1) > -1) { if (skipping) { ++raw_lines; if (kopt && raw_lines < kopt) { continue; } if (nopt_mod && raw_lines % nopt_mod != nopt_rem) { continue; } } line[line_read] = 0; if (input2hash160(line, line_read) == 0) { if (bloom) { if (bloom_chk_hash160(bloom, hash160_uncmp.ul)) { if (!fopt || hsearchf(ffile, &hash160_uncmp)) { if (tty) { fprintf(ofile, "\033[0K"); } fprintresult(ofile, &hash160_uncmp, 'u', topt, line); ++olines; } } if (bloom_chk_hash160(bloom, hash160_compr.ul)) { if (!fopt || hsearchf(ffile, &hash160_compr)) { if (tty) { fprintf(ofile, "\033[0K"); } fprintresult(ofile, &hash160_compr, 'c', topt, line); ++olines; } } } else if (Lopt) { fprintlookup(ofile, &hash160_uncmp, &hash160_compr, priv256, topt, line); } else { fprintresult(ofile, &hash160_uncmp, 'u', topt, line); fprintresult(ofile, &hash160_compr, 'c', topt, line); } } } else { if (!vopt) break; } if (vopt) { ++ilines_curr; if (line_read < 0 || (ilines_curr & report_mask) == 0) { time_curr = getns(); time_delta = time_curr - time_last; time_elapsed = time_curr - time_start; time_last = time_curr; ilines_delta = ilines_curr - ilines_last; ilines_last = ilines_curr; ilines_rate = (ilines_delta * 1.0e9) / (time_delta * 1.0); if (line_read < 0) { /* report overall average on last status update */ ilines_rate_avg = (--ilines_curr * 1.0e9) / (time_elapsed * 1.0); } else if (ilines_rate_avg < 0) { ilines_rate_avg = ilines_rate; /* target reporting frequency to about once every five seconds */ } else if (time_delta < 2500000000) { report_mask = (report_mask << 1) | 1; ilines_rate_avg = ilines_rate; /* reset EMA */ } else if (time_delta > 10000000000) { report_mask >>= 1; ilines_rate_avg = ilines_rate; /* reset EMA */ } else { /* exponetial moving average */ ilines_rate_avg = alpha * ilines_rate + (1 - alpha) * ilines_rate_avg; } fprintf(stderr, "\033[0G\033[2K" " rate: %9.2f p/s" " found: %5zu/%-10zu" " elapsed: %8.3f s" "\033[0G", ilines_rate_avg, olines, ilines_curr, time_elapsed / 1.0e9 ); if (line_read < 0) { fprintf(stderr, "\n"); break; } else { fflush(stderr); } }
/** * Allocate storage within oi_wavelength struct * * Sets the oi_wavelength::nwave attribute of @a pWave. * * @param pWave pointer to wavelength data struct, see exchange.h * @param nwave number of wavelength channels (table rows) to allocate */ void alloc_oi_wavelength(oi_wavelength *pWave, int nwave) { pWave->eff_wave = chkmalloc(nwave * sizeof(pWave->eff_band[0])); pWave->eff_band = chkmalloc(nwave * sizeof(pWave->eff_band[0])); pWave->nwave = nwave; }
/** * Allocate storage within oi_target struct * * Sets the oi_target::ntarget attribute of @a pTargets. * * @param pTargets pointer to targets data struct, see exchange.h * @param ntarget number of targets (table rows) to allocate */ void alloc_oi_target(oi_target *pTargets, int ntarget) { pTargets->targ = chkmalloc(ntarget * sizeof(target)); pTargets->ntarget = ntarget; }
/** * Allocate storage within oi_array struct * * Sets the oi_array::nelement attribute of @a pArray. * * @param pArray pointer to array data struct, see exchange.h * @param nelement number of array elements (table rows) to allocate */ void alloc_oi_array(oi_array *pArray, int nelement) { pArray->elem = chkmalloc(nelement * sizeof(element)); pArray->nelement = nelement; }
/* Also guarantee that calloc zeros memory*/ void* chkcalloc(size_t size, size_t nelems) { return chkmalloc(size*nelems); }