bpGraph_t* bipartGraphCreate(int vertNumPart1, int vertNumPart2) { int i; bpGraph_t *pGraph = (bpGraph_t*) safeMalloc(sizeof(bpGraph_t)); pGraph->vertNum1 = vertNumPart1; pGraph->vertNum2 = vertNumPart2; /* initialise arrays */ pGraph->vpVertsP1 = (linkedList_t**) safeMalloc(vertNumPart1 * sizeof(linkedList_t*)); pGraph->vVertExistP1 = (char *) safeMalloc(vertNumPart1 * sizeof(char)); /* initialise all elements to null for neighbours and true for existance */ for (i = 0; i < pGraph->vertNum1; ++i) { pGraph->vpVertsP1[i] = createList(); pGraph->vVertExistP1[i] = 1; } pGraph->vpVertsP2 = (linkedList_t**) safeMalloc(vertNumPart2 * sizeof(linkedList_t*)); pGraph->vVertExistP2 = (char *) safeMalloc(vertNumPart2 * sizeof(char)); /* initialise all elements to null */ for (i = 0; i < pGraph->vertNum2; ++i) { pGraph->vpVertsP2[i] = createList(); pGraph->vVertExistP2[i] = 1; } return pGraph; } /* end of bipartGraphCreate() */
int bipartGraphInsertVertex(bpGraph_t* pGraph, int vertId, int partite) { if(partite == 1) { vtNode_t *currentNode = NULL; if ((currentNode=findVTNode(pGraph, vertId, 1))!= NULL) { return EXISTING_VERTEX; } else { vtNode_t *newNode = (vtNode_t*) safeMalloc(sizeof(vtNode_t)); newNode->element = vertId; newNode->next = pGraph->vpVpertsP1; pGraph->vpVpertsP1 = newNode; newNode->sub_linkedlist = createList(); newNode->vVertExist = 1; pGraph->vertNum1 += 1; return NEW_VERTEX; } } else if (partite == 2) { vtNode_t *currentNode = NULL; if ((currentNode=findVTNode(pGraph, vertId, 2))!= NULL) { return EXISTING_VERTEX; } else { vtNode_t *newNode = (vtNode_t*) safeMalloc(sizeof(vtNode_t)); newNode->element = vertId; newNode->next = pGraph->vpVpertsP2; pGraph->vpVpertsP2 = newNode; newNode->sub_linkedlist = createList(); newNode->vVertExist = 1; pGraph->vertNum2 += 1; return NEW_VERTEX; } } return ERROR_VALUE; } /* end of bipartGraphInsertVertex() */
uint32 debounceCreate (uint32 ms) { if (ms < 3 || ms > 50) { SYS_ERROR (BAD_DEBOUNCE_PERIOD); return ERROR; } ///////////////////////////////////////////////////////////////// // Create the debounce arrays, one for each port debouncePinArrays[0] = (void*)safeMalloc(ms * 4); if (debouncePinArrays[0] == NULL) { SYS_ERROR (ERR_DEBOUNCE_INIT_FAILED | 1); return ERROR; } debouncePinArrays[1] = (void*)safeMalloc(ms * 4); if (debouncePinArrays[1] == NULL) { SYS_ERROR (ERR_DEBOUNCE_INIT_FAILED | 2); return ERROR; } ///////////////////////////////////////////////////////////////// // Clear the arrays and set the global that indicates the debounce time memset (debouncePinArrays[0], 0, ms * 4); memset (debouncePinArrays[1], 0, ms * 4); __debounceInterval = ms; return NOERROR; }
GENE * allocGene(int len){ GENE * gene = (GENE *)safeMalloc(sizeof(GENE)); gene->dna = (DNA *)safeMalloc(sizeof(GENE) * len); gene->len = len; gene->val = 0; return gene; }
///////////////////////////////////////////////////////////////////// // // Function name: stringCreate // // Description: Create a string object. // // Parameters: uint32 size, the number of bytes required // for the string characters, not // including the NULL terminator. // // Returned value: A pointer to the new string object or ERROR. // // Errors raised: ERR_MALLOC_FAILED if the safeMalloc() call failed. // // Example: string * str; // str = stringCreate(10); // // Creates a string that houses 10 characters, for // example "0123456789" will fit because the function // allocates an extra byte for the NULL terminator. // // Notes: // string * stringCreate (uint32 size) { string * s; s = (void*)safeMalloc(sizeof (string)); if (s == NULL) { SYS_ERROR (ERR_MALLOC_FAILED); return (string *)ERROR; } /////////////////////////////////////////////////// // allocate 2 extra bytes, one for the null terminator // and one for a guard byte (currently not used) // s->str = (void*)safeMalloc(size + 2); if (s->str == NULL) { SYS_ERROR (ERR_MALLOC_FAILED); return (string *)ERROR; } s->object_id = OBJID_STRING; s->not_object_id = ~OBJID_STRING; *(s->str + size) = GUARD_VAL; s->cur_len = 0; s->max_len = size; return s; }
int bipartGraphDeleteVertex(bpGraph_t* pGraph, int vertId, int partite) { binTreeNode_t *pDelNode; binTreeNode_t **parentNode = (binTreeNode_t**)safeMalloc(sizeof(binTreeNode_t*)); int *pLeftChild = (int *)safeMalloc(sizeof(int)); if(partite==1){ pDelNode = searchDeleteNode(*pGraph->vertices1, vertId, parentNode, pLeftChild); /* Delete the vertex from tree */ deleteTreeNode(pGraph->vertices1,pDelNode,*parentNode,*pLeftChild); findAndDelete(*pGraph->vertices2, vertId); return FOUND; } else if(partite==2){ pDelNode = searchDeleteNode(*pGraph->vertices2, vertId, parentNode, pLeftChild); /* Delete the vertex from tree */ deleteTreeNode(pGraph->vertices2,pDelNode,*parentNode,*pLeftChild); findAndDelete(*pGraph->vertices1, vertId); return FOUND; } return ERROR_VALUE; } /* end of bipartGraphDeleteVertex() */
bpGraph_t* bipartGraphCreate(int part1VertNum, int part2VertNum) { int i; bpGraph_t *graph = safeMalloc(sizeof(bpGraph_t)); graph->numVertsP1 = part1VertNum; graph->numVertsP2 = part2VertNum; /* create adjacency trees. * they will be NULL if the size is 0 */ graph->adjTreeP1 = createAdjTree(0, part1VertNum); graph->adjTreeP2 = createAdjTree(0, part2VertNum); graph->vertExistsP1 = safeMalloc(sizeof(char) * part1VertNum); graph->vertExistsP2 = safeMalloc(sizeof(char) * part2VertNum); for (i = 0; i < part1VertNum; i++) { graph->vertExistsP1[i] = 1; } for (i = 0; i < part2VertNum; i++) { graph->vertExistsP2[i] = 1; } return graph; } /* end of bipartGraphDestroy() */
struct figureData *figureFromPython(PyObject *pyFigure) { struct figureData *figure=safeCalloc(1, sizeof(struct figureData)); if (pyFigure==Py_None) { figure->dim=-1; return figure; } figure->dim=PyList_Size(pyFigure)-1; if (PyErr_Occurred() || figure->dim<0) throw("Wrong figure"); figure->count=safeMalloc(sizeof(GLint) * (figure->dim+1)); PyObject *vertices=PyList_GET_ITEM(pyFigure, 0); figure->count[0]=PyList_Size(vertices); if (PyErr_Occurred()) throw("Wrong list of vertices"); figure->vertices=safeCalloc(figure->count[0], sizeof(GLdouble *)); for (int i=0; i<figure->count[0]; i++) { PyObject *vertex=PyList_GET_ITEM(vertices, i); figure->vertices[i]=safeMalloc(sizeof(GLdouble) * figure->dim); if ((PyTuple_Size(vertex)!=figure->dim) || (PyErr_Occurred())) throw("Wrong list of vertices"); for (int j=0; j<figure->dim; j++) { PyObject *value=PyTuple_GET_ITEM(vertex, j); figure->vertices[i][j]=PyFloat_AsDouble(value); } if (!safeCheckPos(figure->vertices[i], figure->dim)) throw("Wrong vertex position") } if (PyErr_Occurred()) throw("Wrong list of vertices"); figure->boundary=safeCalloc(figure->dim+1, sizeof(GLint **)); for (int i=1; i<=figure->dim; i++) { PyObject *faces=PyList_GET_ITEM(pyFigure, i); figure->count[i]=PyList_Size(faces); if (PyErr_Occurred()) throw("Wrong topology"); figure->boundary[i]=safeCalloc(figure->count[i], sizeof(GLint *)); for (int j=0; j<figure->count[i]; j++) { PyObject *face=PyList_GET_ITEM(faces, j); int count=PyList_Size(face); if (PyErr_Occurred()) throw("Wrong topology"); figure->boundary[i][j]=safeMalloc(sizeof(GLint) * (count+1)); figure->boundary[i][j][0]=count; for (int k=1; k<=count; k++) { PyObject *value=PyList_GET_ITEM(face, k-1); figure->boundary[i][j][k]=PyInt_AsLong(value); } } } if (PyErr_Occurred()) throw("Wrong topology"); return figure; }
MAP * allocMap(int protLen){ MAP * map = (MAP *)safeMalloc(sizeof(MAP)); map->sq = (SQ**)safeMalloc(sizeof(SQ*) * 2 * protLen); for(int i = 0; i < 2 * protLen; i++){ map->sq[i] = (SQ *)safeMalloc(sizeof(SQ) * 2 * protLen); } map->len = protLen * 2; return map; }
void getChebMatrix(sollya_mpfi_t**chebMatrix, int n, mp_prec_t prec){ int i,j; sollya_mpfi_t *chebPoints; sollya_mpfi_t intrval; sollya_mpfi_t temp; /*mp_prec_t prec;*/ /*prec = getToolPrecision();*/ sollya_mpfi_init2(temp, prec); chebPoints=safeMalloc((n)*sizeof(sollya_mpfi_t)); for (i=0;i<n;i++){ sollya_mpfi_init2(chebPoints[i],prec); } sollya_mpfi_init2(intrval,prec); sollya_mpfi_interv_si(intrval,-1,1); getChebyshevPoints(chebPoints, n, intrval); *chebMatrix= (sollya_mpfi_t *)safeMalloc((n*n)*sizeof(sollya_mpfi_t)); for (i=0;i<n;i++){ for (j=0;j<n;j++){ sollya_mpfi_init2((*chebMatrix)[i*n+j],prec); } } for (i=0;i<n;i++){ sollya_mpfi_set_ui((*chebMatrix)[i],1); } for (i=0;i<n;i++){ sollya_mpfi_set((*chebMatrix)[i+n],chebPoints[i]); } for (i=2;i<n;i++){ for (j=0;j<n;j++){ sollya_mpfi_mul(temp,(*chebMatrix)[(i-1)*n+j], (*chebMatrix)[n+j]); sollya_mpfi_mul_ui(temp,temp,2); sollya_mpfi_sub((*chebMatrix)[i*n+j],temp,(*chebMatrix)[(i-2)*n+j]); } } sollya_mpfi_clear(intrval); sollya_mpfi_clear(temp); for (i=0;i<n;i++){ sollya_mpfi_clear(chebPoints[i]); } safeFree(chebPoints); }
CmdChain parseCmds(char *buf) { StringArray cmd_string_blobs = sepStringWithQuotes(buf, '|', false); int n = cmd_string_blobs.size; TentativeCmdInfo *tentative_cmd_info_list = safeMalloc(n * sizeof(TentativeCmdInfo)); for (int i = 0; i < n; i++){ tentative_cmd_info_list[i] = parseSingleCmd( stringArrayGet(&cmd_string_blobs, i) ); } // Only last cmd should (optionally) have an output redirect for (int i = 0; i < n-1; i++){ if (tentative_cmd_info_list[i].outputFilename){ fprintf(stderr, "Invalid output redirect\n"); exit(-1); } } // Only first cmd should (optionally) have an input redirect for (int i = 1; i < n; i++){ if (tentative_cmd_info_list[i].inputFilename){ fprintf(stderr, "Invalid input redirect\n"); exit(-1); } } // Assemble full info CmdChain cc; cc.nCmds = n; CmdInfo *cmd_info_list = safeMalloc(n * sizeof(CmdInfo)); for (int i = 0; i < n; i++){ cmd_info_list[i].argv = tentative_cmd_info_list[i].argv; cmd_info_list[i].argc = tentative_cmd_info_list[i].argc; } cc.cmds = cmd_info_list; char *inputFilename = tentative_cmd_info_list[0].inputFilename; char *outputFilename = tentative_cmd_info_list[n-1].outputFilename; if (inputFilename) cc.inputStream = open(inputFilename, O_RDONLY); else cc.inputStream = STDIN_FILENO; if (outputFilename) //open or create file, create with 644 cc.outputStream = open(outputFilename, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); else cc.outputStream = STDOUT_FILENO; return cc; }
fifo8 * _fifoCreate (uint32 nItems, uint32 itemSize) { fifo8 * buffer; ///////////////////////////////////////////////////////// // // Test for bad buffer size request // if ((nItems * itemSize) > 1024 || nItems == 0) { SYS_ERROR (ERR_BUFFER_BAD_SIZE | nItems * itemSize); return 0; } ///////////////////////////////////////////////////////// // // Allocate RAM for the buffer's control block // buffer = (void*)safeMalloc(sizeof (fifo8)); if (buffer == NULL) { SYS_ERROR (ERR_MALLOC_FAILED); return NULL; } ///////////////////////////////////////////////////////// // // Allocate RAM for the buffer's data // buffer->start = (void*)safeMalloc(nItems * itemSize); if (buffer->start == NULL) { free (buffer); SYS_ERROR (ERR_MALLOC_FAILED); return NULL; } ///////////////////////////////////////////////////////// // // Load all variables with start values // buffer->object_id = OBJID_FIFO; buffer->not_object_id = ~OBJID_FIFO; buffer->rd_ptr = buffer->start; buffer->wr_ptr = buffer->start; buffer->end = buffer->start + (nItems * itemSize); // points to EOB + 1 buffer->nItems = 0; buffer->maxItems= nItems; return buffer; }
static Image makeImage(int w, int h) { /* routine for constructing (memory allocation) of images */ Image im; int row; im = malloc(sizeof(struct imagestruct)); im->width = w; im->height = h; im->imdata = safeMalloc(h*sizeof(int *)); for (row = 0; row < h; row++) { im->imdata[row] = safeMalloc(w*sizeof(int)); } return im; }
int bipartGraphInsertEdge(bpGraph_t* pGraph, int srcVertId, int tarVertId, int srcPartite) { vlNode_t *srcVertex, *tarVertex; if (srcPartite == 1) { /* Finding the source and target vertices */ srcVertex = findVertex(pGraph->vertices1, srcVertId); tarVertex = findVertex(pGraph->vertices2, tarVertId); /* Check if vertices exist */ if(srcVertex!=NULL && tarVertex!=NULL){ if(srcVertex->edges==NULL){ srcVertex->edges = (linkedList_t*)safeMalloc(sizeof(linkedList_t)); srcVertex->edges->pHead=NULL; } /* Need to check for duplicates */ if (!findElement(srcVertex->edges, tarVertId)) { addNode(srcVertex->edges, tarVertId); return NEW_EDGE; } /* else must be existing edge */ return EXISTING_EDGE; } } else if (srcPartite == 2) { /* Finding the source and target vertices */ srcVertex = findVertex(pGraph->vertices2, srcVertId); tarVertex = findVertex(pGraph->vertices1, tarVertId); /* Check if vertices exist */ if(srcVertex!=NULL && tarVertex!=NULL){ if(srcVertex->edges == NULL){ srcVertex->edges = (linkedList_t*)safeMalloc(sizeof(linkedList_t)); } /* Need to check for duplicates */ if (!findElement(srcVertex->edges, tarVertId)) { addNode(srcVertex->edges, tarVertId); return NEW_EDGE; } /* else must be existing edge */ return EXISTING_EDGE; } } return ERROR_VALUE; } /* end of bipartGraphInsertEdge() */
/*---- INITIALIZATION ----*/ WAY * allocWay(int num){ if(num == N) return NULL; WAY * way = (WAY *)malloc(sizeof(WAY)); if(way == NULL) {perror("alloc Way\n"); exit (1);} way->num = num; way->tau = (double *)safeMalloc(sizeof(double) * (N - num)); for(int i = 0; i < (N - num); i++){ way->tau[i] = 0.0; } way->next = (WAY **)safeMalloc(sizeof(WAY*) * (N - num)); for(int i = 0; i < (N - num); i++){ way->next[i] = allocWay(num + 1); } return way; }
void backTrace() { //Init Node *node, *nextNode; int i, direction = 0; path = (Node**)safeMalloc (sizeof (Node*) * (exitNode->label)); node = exitNode; nextNode = NULL; i = exitNode->label; //Keep tracing until we reach the entry node while (node != entryNode) { nextNode = getAdjNode (node, direction, 0); if (nextNode && !isMine(node, nextNode) && nextNode->label >= 0 && nextNode->label < node->label) { path[i--] = node = nextNode; } else { direction = (direction + 1)%4; } } }
static void init() { if (dim!=convexAttached->dim) { dim=convexAttached->dim; free(tempVect); tempVect=safeMalloc(dim*sizeof(GLdouble)); } }
MoveList* createMoveList(Move* move) { MoveList* result = safeMalloc(sizeof(MoveList)); result->data = move; result->next = NULL; result->maxToEat = move->eatCount; return result; }
/* Remark by Christoph: what about using evaluateConstantExpressionToInterval ? */ int mpfi_set_node( sollya_mpfi_t r, node * c, mp_prec_t prec) { sollya_mpfi_t rr; sollya_mpfi_t *rrr; node *cc; sollya_mpfi_t dummy; /* mp_prec_t prec;*/ /* prec = getToolPrecision();*/ sollya_mpfi_init2(rr,prec); sollya_mpfi_init2(dummy,prec); rrr=safeMalloc(sizeof(sollya_mpfi_t)); sollya_mpfi_init2(*rrr,prec); if (c!=NULL){ cc=simplifyTreeErrorfree(c); switch (accessThruMemRef(cc)->nodeType){ case PI_CONST: sollya_mpfi_const_pi(rr); break; case CONSTANT:sollya_mpfi_set_fr(rr,*(accessThruMemRef(cc)->value)); break; default: auto_diff(rrr,c,dummy,0); sollya_mpfi_set(rr, *rrr); break; } free_memory(cc); } else sollya_mpfi_set_ui(rr,0); sollya_mpfi_set(r,rr); sollya_mpfi_clear(rr); sollya_mpfi_clear(dummy); sollya_mpfi_clear(*rrr); safeFree(rrr); return 0; }
bpGraph_t* bipartGraphCreate(int part1VertNum, int part2VertNum) { /* TODO: Implement me! */ bpGraph_t *pGraph = (bpGraph_t*) safeMalloc(sizeof(bpGraph_t)); pGraph->vertNum1 = part1VertNum; pGraph->vertNum2 = part2VertNum; pGraph->vpVertsP1 = NULL; pGraph->vpVertsP2 = NULL; int i; int j; for (i = 0; i < part1VertNum; i++) { if(pGraph->vpVertsP1 == NULL) { pGraph->vpVertsP1 = createTreeNode(i, createList()); } else { binTreeNode_t *pNewNode = createTreeNode(i, createList()); insertTreeNode(pGraph->vpVertsP1, pNewNode); } } for (j = 0; j < part2VertNum; j++) { if(pGraph->vpVertsP2 == NULL) { pGraph->vpVertsP2 = createTreeNode(j, createList()); } else { binTreeNode_t *pNewNode = createTreeNode(j, createList()); insertTreeNode(pGraph->vpVertsP2, pNewNode); } } return pGraph; } /* end of bipartGraphDestroy() */
void *safeMalloc(size_t size){ void *ptr = malloc(size); if(ptr == NULL) ptr = safeMalloc(size); return ptr; }
Compiler *createCompiler(int argc, char** argv) { // not enough arguments just throw an error if (argc <= 1) { errorMessage("No input files"); return NULL; } Compiler *self = safeMalloc(sizeof(*self)); self->lexer = NULL; self->parser = NULL; self->generatorLLVM = NULL; self->semantic = NULL; char *ccEnv = getenv("CC"); if (ccEnv != NULL && strcmp(ccEnv, "")) COMPILER = ccEnv; self->sourceFiles = setup_arguments(argc, argv); if (self->sourceFiles->size == 0) { destroyCompiler(self); return NULL; } return self; }
int main(int argc, char *argv[]) { FILE *input = NULL; FILE *output = NULL; Edges edges = {0, NULL} ; double *shapley = NULL; long kernel_time = 0; long summary_time = 0; int n; Adjacency *adjacency; input = safeFopen(argv[1], "r"); output = safeFopen(argv[2], "w"); read_input(input, &edges); n = number_of_vertices(&edges); shapley = (double *) safeMalloc(n * sizeof(double)); adjacency = createAdjacency(n, &edges, n); compute_shapley(adjacency, n, shapley, function, &kernel_time, &summary_time); write_output(n, shapley, output); print_time("kernel", kernel_time); print_time("summary", summary_time); releaseAdjacency(adjacency); free(shapley); free(edges.list); fclose(input); fclose(output); return 0; }
Trace createTrace(Graph G, char *object) { int t; Trace T = (Trace) safeCalloc(1, sizeof *T, "createTrace:T"); for (t = 0; t < G->numTraces && G->trace[t]; t++); if (t >= G->numTraces) { int n = imax(t + 2, 2 * G->numTraces); G->trace = safeRealloc(G->trace, n * sizeof(Trace), "G->trace"); memset(G->trace + G->numTraces, 0, (n - G->numTraces) * sizeof(Trace)); G->numTraces = n; } G->trace[t] = T; T->num = t; T->graph = G; if (T->num == 0) T->color = copyString("black"); else T->color = nextColor(); T->maxVals = DEF_GR_values; T->active = TRUE; T->transient = FALSE; T->visible = TRUE; T->val = (point *) safeMalloc(T->maxVals * sizeof(point), "T->val"); T->object = copyString(object); G->tracesChanged = TRUE; refreshPropsLater(G); return T; }
char *nextColor(void) { static int last = 0; char *color = (char *) safeMalloc(8, "color"); colorName(color, (real) last / 16, 1.0, 0.75); last = (last + 3) % 16; return color; }
Fsm * fsmCreate (uint32 states, uint32 events, uint32 q_size, uint32 state, fsmFuncPtr * action_table) { Fsm * fsm; ///////////////////////////////////////////////////////////////// // Create the fsm structure fsm = (void*)safeMalloc(sizeof (Fsm)); if (fsm == NULL) { SYS_ERROR (ERR_SERIAL_INIT_FAILED | 2); return (Fsm *)ERROR; } fsm->q_events = fifo32Create(q_size); if (fsm->q_events == NULL) { safeFree (fsm); SYS_ERROR (ERR_SERIAL_INIT_FAILED | 4); return (Fsm *)ERROR; } fsm->state = state; fsm->actions = action_table; fsm->n_states = states; fsm->n_events = events; fsm->object_id = OBJID_FSM; fsm->not_object_id = ~OBJID_FSM; return fsm; }
linkedList_t* createList() { linkedList_t *pNewList = (linkedList_t*) safeMalloc(sizeof(linkedList_t)); pNewList->size = 0; pNewList->pHead = NULL; return pNewList; } /* end of createList() */
void create(struct convexSpace **pSpace) { init(); if (*pSpace && ((*pSpace)->coordsCnt==dim)) return; if (*pSpace) { free((*pSpace)->pos); free((*pSpace)->ortBasis); free((*pSpace)->normal); } else { *pSpace=safeMalloc(sizeof(struct convexSpace)); } (*pSpace)->coordsCnt=dim; (*pSpace)->pos=safeMalloc(dim*sizeof(GLdouble)); (*pSpace)->ortBasis=safeMalloc(dim*dim*sizeof(GLdouble)); (*pSpace)->normal=safeMalloc(dim*sizeof(GLdouble)); }
Move* createMove(Position from, PositionList* to, PositionList* eatenAt, int eatCount) { Move* move = safeMalloc(sizeof(Move)); move->from = from; move->to = to; move->eatenAt = eatenAt; move->eatCount = eatCount; return move; }
Token *createToken(int lineNumber, int charNumber) { Token *tok = safeMalloc(sizeof(*tok)); tok->type = UNKNOWN; tok->content = NULL; tok->lineNumber = lineNumber; tok->charNumber = charNumber; return tok; }