bool generate_default_ids(std::vector<std::string> & IDs) { // this leave one CPU to attend to everything else but our work int cpu_cnt = get_available_cpu_count(); if (1 > cpu_cnt) { M_NOTICE("CPU count does not meet minimum requirements"); return false; } M_DEBUG("cpu_cnt = %d\n", cpu_cnt); char host_name[MAXHOSTNAMELEN] = { '\0' }; if (gethostname(host_name, sizeof(host_name))) { M_ERROR("could not get host name %s", strerror(errno)); return false; } char domain_name[MAXHOSTNAMELEN] = { '\0' }; if (getdomainname(domain_name, sizeof(domain_name))) { M_ERROR("could not get domain name %s", strerror(errno)); return false; } IDs.clear(); std::string id; for (int n = 0; n < cpu_cnt; ++n) { id = host_name; id += "."; id += domain_name; id += n; IDs.push_back(id); } return true; }
/* delete attribute from map * MdelAttribute deletes an attribute * from a map, if the attribute is available. * returns * the id argument if the attribute is succesfully deleted, * or 0 in case of error or if the attribute is not found. * * Merrno * NOACCESS * WRITE_ERROR */ CSF_ATTR_ID MdelAttribute( MAP *m, /* map handle */ CSF_ATTR_ID id) /* identification of attribute */ { ATTR_CNTRL_BLOCK b; CSF_FADDR32 pos; if (! WRITE_ENABLE(m)) { M_ERROR(NOACCESS); goto error; } pos = CsfGetAttrBlock(m, id, &b); if (pos == 0) goto error; b.attrs[CsfGetAttrIndex(id, &b)].attrId = ATTR_NOT_USED; if (CsfWriteAttrBlock(m, pos, &b)) { M_ERROR(WRITE_ERROR); goto error; } return id ; error: return 0 ; /* not found or an error */ }
static void expect_item_remove(struct ybc *const cache, struct ybc_key *const key) { if (!ybc_item_remove(cache, key)) { M_ERROR("error when deleting item from the cache"); } expect_item_miss(cache, key); if (ybc_item_remove(cache, key)) { M_ERROR("unexpected item found in the cache"); } }
static void test_anonymous_cache_create(struct ybc *const cache) { /* Non-forced open must fail. */ if (ybc_open(cache, NULL, 0)) { M_ERROR("anonymous cache shouldn't be opened without force"); } /* Forced open must succeed. */ if (!ybc_open(cache, NULL, 1)) { M_ERROR("cannot open anonymous cache with force"); } ybc_close(cache); }
/* write a legend * MputLegend writes a (version 2) legend to a map replacing * the old one if existent. * See csfattr.h for the legend structure. * * returns * 0 in case of an error, * nonzero otherwise * * Merrno * NOACCESS * WRITE_ERROR */ int MputLegend( MAP *m, /* Map handle */ CSF_LEGEND *l, /* read-write, array with name and entries, the entries * are sorted before writing to the file. * Strings are padded with zeros. */ size_t nrEntries) /* number of array elements. That is name plus real legend entries */ { int i = NrLegendEntries(m); CSF_ATTR_ID id = i < 0 ? ATTR_ID_LEGEND_V1 : ATTR_ID_LEGEND_V2; if (i) if (! MdelAttribute(m, id)) return 0; SortEntries(l, nrEntries); if (CsfSeekAttrSpace(m, ATTR_ID_LEGEND_V2, (size_t)(nrEntries*CSF_LEGEND_ENTRY_SIZE)) == 0) return 0; for(i = 0; i < (int)nrEntries; i++) { if( m->write(&(l[i].nr), sizeof(INT4), (size_t)1, m->fp) != 1 || m->write( CsfStringPad(l[i].descr,(size_t)CSF_LEGEND_DESCR_SIZE), sizeof(char), (size_t)CSF_LEGEND_DESCR_SIZE, m->fp) != CSF_LEGEND_DESCR_SIZE ) { M_ERROR(WRITE_ERROR); return 0; } } return 1; }
/* write an attribute to a map (LIBRARY_INTERNAL) * MputAttribute writes exactly the number of bytes specified * by the size argument starting at the address of argument * attr. Which means that you can't simply pass a structure or an * array of structures as argument attr, due to the alignment * of fields within a structure and internal swapping. You can * only pass an array of elementary types (UINT1, REAL4, etc.) * or character string. * If one wants to refresh an attribute, one should first * call MdelAttribute to delete the attribute and then use * MputAttribute to write the new value. * returns argument id or 0 in case of error. * * Merrno * ATTRDUPL * NOACCESS * WRITE_ERROR */ CSF_ATTR_ID CsfPutAttribute( MAP *m, /* map handle */ CSF_ATTR_ID id, /* attribute identification */ size_t itemSize, /* size of each attribute element. * 1 or sizeof(char) in case of a * string */ size_t nitems, /* number of attribute elements or * strlen+1 in case of a variable character * string field. Don't forget to pad a * non-variable field with '\0'! */ void *attr) /* buffer containing attribute */ { size_t size = nitems * itemSize; PRECOND(CsfValidSize(itemSize)); PRECOND(size > 0); if (CsfSeekAttrSpace(m,id,size) == 0) goto error; if (m->write(attr, itemSize, nitems, m->fp) != nitems) { M_ERROR(WRITE_ERROR); goto error; } return(id); /* succes */ error: return(0); /* failure */ }
static bool *uf_string_fill_bool(t_string *v_this, const char *charset) { size_t i; size_t size; bool *active; i = 0; if ((active = uf_malloc_s(v_this->v_size, sizeof(*active))) == NULL) return ((bool *)M_ERROR((size_t)NULL, "Bad alloc")); uf_memset(active, false, v_this->v_size * sizeof(*active)); size = uf_str_len(charset); if (size == 0) i = v_this->v_size; while (i < v_this->v_size) { if (uf_strncmp(v_this->v_str + i, charset, size) == 0) { uf_memset(active + i, true, size * sizeof(*active)); i = i + size; } else i = i + 1; } return (active); }
static bool uf_getopt_init_option(t_getopt *v_this, size_t ac, const char **av) { size_t i; size_t size; t_string string; i = 1; if (D_STRING(init)(&string, 0) == false) return (M_ERROR(false, "Could not initialize string")); while (i < ac) { size = uf_str_len(av[i]); if (uf_fmt_av(v_this, &string, size == 2 && av[i][0] == '-' && av[i][1] != '-', av[i] + 1) == false || uf_fmt_av(v_this, &string, size > 3 && av[i][0] == '-' && av[i][1] == '-' && av[i][2] != '-', av[i] + 2) == false) break ; i = i + 1; } if (i == ac) { v_this->v_option = D_STRING(split)(&string, " "); if (v_this->v_option == NULL) i = 0; } D_STRING(destroy)(&string); return (i == ac); }
bool uf_u_add(unsigned int a, unsigned int b, unsigned int *sum) { if (UINT_MAX - a < b) return (M_ERROR(false, "Unsigned int overflow")); *sum = a + b; return (true); }
bool uf_hu_add(unsigned short a, unsigned short b, unsigned short *sum) { if (USHRT_MAX - a < b) return (M_ERROR(false, "Unsigned short overflow")); *sum = a + b; return (true); }
bool uf_hhu_add(unsigned char a, unsigned char b, unsigned char *sum) { if (UCHAR_MAX - a < b) return (M_ERROR(false, "Unsigned char overflow")); *sum = a + b; return (true); }
/* test value scale on compatibility CSF version 1 and 2 * RvalueScaleIs tests if the map's value scale is compatible * with a certain value scale. Here is list of compatible but * different value scales: * * VS_NOTDETERMINED: always returns 0 * * VS_CLASSIFIED: VS_NOTDETERMINED * * VS_CONTINUOUS: VS_NOTDETERMINED * * VS_BOOLEAN: VS_CLASSIFIED, VS_NOTDETERMINED * * VS_NOMINAL: VS_CLASSIFIED, VS_NOTDETERMINED * * VS_ORDINAL: VS_CLASSIFIED, VS_NOTDETERMINED * * VS_LDD: VS_CLASSIFIED, VS_NOTDETERMINED (only if cell representation is * UINT1 or INT2) * * VS_SCALAR: VS_CONTINUOUS, VS_NOTDETERMINED * * VS_DIRECTION: none * * returns * 0 if not compatible or if vs argument is VS_NOTDETERMINED or in case of * error, nonzero if * compatible. * * Merrno * BAD_VALUESCALE * * EXAMPLE * .so examples/maskdump.tr */ int RvalueScaleIs( const MAP *m, /* a version 1 map handle */ CSF_VS vs) /* a version 2 value scale that is compatible with map's value * scale yes or no? */ { CSF_VS mapsVS = RgetValueScale(m); if (vs == VS_NOTDETERMINED) return 0; if (vs == mapsVS) return 1; switch(vs) { case VS_CLASSIFIED: return mapsVS == VS_NOTDETERMINED; case VS_CONTINUOUS: return mapsVS == VS_NOTDETERMINED; case VS_LDD: { CSF_CR cr = RgetCellRepr(m); if (cr != CR_UINT1 && cr != CR_INT2) return 0; } /* fall through */ case VS_BOOLEAN: case VS_NOMINAL: case VS_ORDINAL: return mapsVS == VS_CLASSIFIED || mapsVS == VS_NOTDETERMINED; case VS_SCALAR: return mapsVS == VS_CONTINUOUS || mapsVS == VS_NOTDETERMINED; /* direction isn't compatible with anything */ case VS_DIRECTION: return 0; default : M_ERROR(BAD_VALUESCALE); return 0; } }
/* compute (fractional) row, column index from true world co-ordinate. * Rcoord2RowCol computes row, column index from true world co-ordinate. * The row and column co-ordinate are returned as fractions (See parameters * section). * The x,y co-ordinate * don't have to be on the map. They are just relative to upper left position. * * returns * 0 if the co-ordinate is outside the map, * 1 if inside, * -1 in case of an error * * Merrno * ILL_CELLSIZE */ int Rcoords2RowCol( const MAP *m, /* map handle */ double x, /* x of true co-ordinate */ double y, /* y of true co-ordinate */ double *row, /* write-only. Row index (y-pos). floor(row) is row number, * if (row >= 0) then fmod(row, 1) is in-pixel displacement from pixel-top, * if (row <0) then fmod(row, 1) is in-pixel displacement from pixel-bottom. */ double *col) /* write-only. Column index (x-pos). floor(col) is column number, * if (col >= 0) then fmod(col, 1) is in-pixel displacement from pixel-left, * if (col <0) then fmod(col, 1) is in-pixel displacement from pixel-right. */ { double row_,col_; /* use copies, func( , , ,&dummy, &dummy) will fail * otherwise */ if (m->raster.cellSize <= 0 || (m->raster.cellSize != m->raster.cellSizeDupl ) ) { /* CW we should put this in Mopen */ M_ERROR(ILL_CELLSIZE); goto error; } RasterCoords2RowCol(&(m->raster),x,y,&row_,&col_); *row = row_; *col = col_; return( row_ >= 0 && col_ >= 0 && (m->raster.nrRows > row_) && (m->raster.nrCols > col_) ); error: return(-1); }
bool mf_unit_add_test(t_unit *v_this, const char *context, const char *name, void (*test)(t_unit_test *)) { t_list_cell *cell; t_unit_test *t; cell = D_LIST(begin)(&v_this->v_context); while (cell != NULL) { if (uf_strcmp(context, ((t_unit_context*)cell->v_data)->v_name) == 0) break ; cell = cell->v_next; } if (cell == NULL) return (M_ERROR(false, "Could not find %s context", context)); t = uf_unit_alloc_test(name, test); if (t == NULL) return (false); if (D_LIST(push_back)(&((t_unit_context*)cell->v_data)->v_test, t) == false) { uf_free_s((void **)&t); return (false); } return (true); }
bool uf_hu_mul(unsigned short a, unsigned short b, unsigned short *sum) { if (a > USHRT_MAX / b) return (M_ERROR(false, "Unsigned short overflow")); *sum = (unsigned short)(a * b); return (true); }
bool uf_u_mul(unsigned int a, unsigned int b, unsigned int *sum) { if (a > UINT_MAX / b) return (M_ERROR(false, "Unsigned int overflow")); *sum = (unsigned int)(a * b); return (true); }
bool uf_hhu_mul(unsigned char a, unsigned char b, unsigned char *sum) { if (a > UCHAR_MAX / b) return (M_ERROR(false, "Unsigned char overflow")); *sum = (unsigned char)(a * b); return (true); }
/* compare 2 maps for their location attributes * Rcompare compares 2 maps for all location attributes: * * projection, * * xUL, yUL, angle, * * cell size and * * number of rows and columns * * returns 0 if one of these attributes differ or in case of an error, 1 * if they are all equal. * * Merrno * NOT_RASTER */ int Rcompare( const MAP *m1, /* map handle 1 */ const MAP *m2) /* map handle 2 */ { CHECKHANDLE_GOTO(m1, error); /* check if mapType is T_RASTER */ if ((m1->main.mapType != T_RASTER) || (m2->main.mapType != T_RASTER)) { M_ERROR(NOT_RASTER); goto error; } if ( MgetProjection(m1) == MgetProjection(m2) && m1->raster.xUL == m2->raster.xUL && m1->raster.yUL == m2->raster.yUL && m1->raster.cellSize == m2->raster.cellSize && m1->raster.cellSizeDupl == m2->raster.cellSizeDupl && m1->raster.angle == m2->raster.angle && m1->raster.nrRows == m2->raster.nrRows && m1->raster.nrCols == m2->raster.nrCols ) return(1); error: return(0); }
/* create a new map by cloning another one * Rdup creates a new empty map from the specifications of another map. * No cell values are copied. It uses a call to Rcreate to create the * map. See Rcreate for legal values of the args cellRepr and valueScale. * returns the map handle of the newly created map or NULL in case of an * error * * Merrno * NOT_RASTER plus the Merrno codes of Rcreate * * EXAMPLE * .so examples/dupbool.tr */ MAP *Rdup( const char *toFile, /* file name of map to be created */ const MAP *from, /* map to clone from */ CSF_CR cellRepr, /* cell representation of new map */ CSF_VS dataType) /* datatype/valuescale of new map */ { MAP *newMap = NULL; /* set NULL for goto error */ CHECKHANDLE_GOTO(from, error); /* check if mapType is T_RASTER */ if (from->main.mapType != T_RASTER) { M_ERROR(NOT_RASTER); goto error; } newMap = Rcreate(toFile, (size_t)from->raster.nrRows, (size_t)from->raster.nrCols, cellRepr, dataType, from->main.projection, from->raster.xUL, from->raster.yUL, from->raster.angle, from->raster.cellSize); error: return newMap ; }
static void test_set_txn_failure(struct ybc *const cache, struct ybc_set_txn *const txn, const struct ybc_key *const key, const size_t value_size) { if (ybc_set_txn_begin(cache, txn, key, value_size, YBC_MAX_TTL)) { M_ERROR("unexpected transaction success"); } }
static void expect_item_set_no_acquire(struct ybc *const cache, const struct ybc_key *const key, const struct ybc_value *const value) { if (!ybc_item_set(cache, key, value)) { M_ERROR("error when storing item in the cache"); } expect_item_hit(cache, key, value); }
/* make all cells missing value in map * RputAllMV writes a missing values to all the cells in a * map. For this is allocates a buffer to hold one row at a * time. * returns 1 if succesfull, 0 in case of an error */ int RputAllMV( MAP *m) { size_t i,nc,nr; void *buffer; CSF_CR cr; CHECKHANDLE_GOTO(m, error); if(! WRITE_ENABLE(m)) { M_ERROR(NOACCESS); goto error; } cr = RgetCellRepr(m); nc = RgetNrCols(m); buffer = Rmalloc(m,nc); if(buffer == NULL) { M_ERROR(NOCORE); goto error; } /* Fill buffer with determined Missingvalue*/ SetMemMV(buffer, nc, cr); nr = RgetNrRows(m); for(i = 0 ; i < nr; i++) if (RputRow(m, i, buffer) != nc) { M_ERROR(WRITE_ERROR); goto error_f; } CSF_FREE(buffer); CsfSetVarTypeMV( &(m->raster.minVal), cr); CsfSetVarTypeMV( &(m->raster.maxVal), cr); return(1); error_f: CSF_FREE(buffer); error: return(0); }
static void expect_item_miss(struct ybc *const cache, const struct ybc_key *const key) { char item_buf[ybc_item_get_size()]; struct ybc_item *const item = (struct ybc_item *)item_buf; if (ybc_item_get(cache, item, key)) { M_ERROR("unexpected item found"); } }
static void expect_item_miss_de(struct ybc *const cache, const struct ybc_key *const key, const size_t grace_ttl) { char item_buf[ybc_item_get_size()]; struct ybc_item *const item = (struct ybc_item *)item_buf; if (ybc_item_get_de(cache, item, key, grace_ttl) == YBC_DE_SUCCESS) { M_ERROR("unexpected item found"); } }
bool f_arqueue_init(t_arqueue *v_this, size_t elem_size, size_t nb_elem) { v_this->v_start = 0; v_this->v_end = 0; v_this->v_size = 0; if ((v_this->v_array = uf_malloc_s(nb_elem, elem_size)) == NULL) return (M_ERROR(false, "Bad alloc")); v_this->v_capacity = nb_elem; v_this->v_sizeof = elem_size; return (true); }
bool f_threadpool_init(t_threadpool *v_this, size_t nb_thread) { t_threadpool_data *data; v_this->pv_data.v_run = false; if ((v_this->v_id = uf_malloc_s(nb_thread, sizeof(*v_this->v_id))) == NULL) return (M_ERROR(false, "Bad alloc")); D_QUEUE(init)(&v_this->pv_data.v_tasks, free); if (D_LOCK(init)(&v_this->v_data, &v_this->pv_data, e_lock_default) == false) { uf_free_s((void **)&v_this->v_id); return (M_ERROR(false, "Couldn't initialize lock")); } if (D_LOCK(lock)(&v_this->v_data, (void **)&data) == true) return (D_THREADPOOL(create)(v_this, data, nb_thread)); D_LOCK(destroy)(&v_this->v_data); uf_free_s((void **)&v_this->v_id); return (M_ERROR(false, "An error has occured")); }
static bool uf_getopt_split_posibility(t_getopt *v_this, const char *posibility) { t_string string; if (D_STRING(init)(&string, 0) == false) return (M_ERROR(false, "Could not initialize string")); if (D_STRING(add_str)(&string, posibility) == false) { D_STRING(destroy)(&string); return (M_ERROR(false, "Could not add [%s]", posibility)); } v_this->v_posibility = D_STRING(split)(&string, ":"); if (v_this->v_posibility == NULL) { D_STRING(destroy)(&string); return (M_ERROR(false, "Could not split [%s]", posibility)); } D_STRING(destroy)(&string); return (true); }
/* put cell size * returns the new cell size or -1 * in case of an error. * * Merrno * ILLHANDLE * NOACCESS * ILL_CELLSIZE */ REAL8 RputCellSize( MAP *map, /* map handle */ REAL8 cellSize) /* new cell size */ { CHECKHANDLE_GOTO(map, error); if(! WRITE_ENABLE(map)) { M_ERROR(NOACCESS); goto error; } if (cellSize <= 0.0) { M_ERROR(ILL_CELLSIZE); goto error; } map->raster.cellSize = cellSize; map->raster.cellSizeDupl = cellSize; return(cellSize); error: return(-1.0); }
std::string Memory::getCurAPI() { ModelServer *info = getCurServer(); if (info != nullptr) { return info->api(); } M_ERROR("no api url"); return ""; }
static void test_persistent_cache_create(struct ybc *const cache) { char config_buf[ybc_config_get_size()]; struct ybc_config *const config = (struct ybc_config *)config_buf; ybc_config_init(config); ybc_config_set_index_file(config, "./tmp_cache.index"); ybc_config_set_data_file(config, "./tmp_cache.data"); ybc_config_set_max_items_count(config, 1000); ybc_config_set_data_file_size(config, 1024 * 1024); /* Non-forced open must fail. */ if (ybc_open(cache, config, 0)) { M_ERROR("non-existing persistent cache shouldn't be opened " "without force"); } /* Forced open must succeed. */ if (!ybc_open(cache, config, 1)) { M_ERROR("cannot create persistent cache"); } ybc_close(cache); /* Non-forced open must succeed now. */ if (!ybc_open(cache, config, 0)) { M_ERROR("cannot open existing persistent cache"); } ybc_close(cache); /* Remove files associated with the cache. */ ybc_remove(config); /* Non-forced open must fail again. */ if (ybc_open(cache, config, 0)) { M_ERROR("non-existing persistent cache shouldn't be opened " "without force"); } ybc_config_destroy(config); }