int M_PolyhedronRead(AG_DataSource *ds, M_Polyhedron *P) { Uint i, j; P->nv = (Uint)AG_ReadUint32(ds); P->ne = (Uint)AG_ReadUint32(ds); P->nf = (Uint)AG_ReadUint32(ds); if ((P->v = TryMalloc(P->nv*sizeof(M_Vector3))) == NULL || (P->e = TryMalloc(P->ne*sizeof(M_Halfedge))) == NULL || (P->f = TryMalloc(P->nf*sizeof(M_Facet))) == NULL) goto fail; /* Read vertices */ for (i = 0; i < P->nv; i++) P->v[i] = M_ReadVector3(ds); /* Read edges */ for (i = 0; i < P->ne; i+=2) { M_Halfedge *eHead = &P->e[i]; M_Halfedge *eTail = &P->e[i+1]; eHead->v = (Uint)AG_ReadUint32(ds); eTail->v = (Uint)AG_ReadUint32(ds); if (eHead->v >= P->nv || eTail->v >= P->nv) { AG_SetError("Edge%d: Bad vertex %d", i, eHead->v); goto fail; } eHead->f = (Uint)AG_ReadUint32(ds); eTail->f = (Uint)AG_ReadUint32(ds); if (eHead->f >= P->nf || eTail->f >= P->nf) { AG_SetError("Edge%d: Bad facet %d", i, eHead->f); goto fail; } eHead->oe = i+1; eTail->oe = i; } /* Read facets */ for (i = 0; i < P->nf; i++) { M_Facet *f = &P->f[i]; f->n = (Uint)AG_ReadUint8(ds); if ((f->e = TryMalloc(f->n*sizeof(Uint))) == NULL) { goto fail; } for (j = 0; j < f->n; j++) { f->e[j] = (Uint)AG_ReadUint32(ds); if (f->e[j] >= P->ne) { AG_SetError("Facet%d[%d]: Bad incident edge %d", i, j, f->e[j]); goto fail; } } } return (0); fail: return (-1); }
/* Return a newly-allocated duplicate of an animation. */ AG_Anim * AG_AnimDup(AG_Anim *sa) { AG_Anim *a; int i; AG_MutexLock(&sa->lock); a = AG_AnimNew(sa->type, sa->w, sa->h, sa->format, (sa->flags & AG_SAVED_ANIM_FLAGS)); if (a == NULL) { AG_MutexUnlock(&sa->lock); return (NULL); } if ((a->f = TryMalloc(sa->n*sizeof(AG_AnimFrame))) == NULL) { goto fail; } for (i = 0; i < sa->n; i++) { AG_AnimFrame *af = &a->f[i]; if ((af->pixels = TryMalloc(sa->h*sa->pitch)) == NULL) { goto fail; } memcpy(af->pixels, sa->f[i].pixels, sa->h*sa->pitch); a->n++; } AG_MutexUnlock(&sa->lock); return (a); fail: AG_MutexUnlock(&sa->lock); AG_AnimFree(a); return (NULL); }
static char * GetAddrNumerical(AG_NetAddr *na) { const char *s; switch (na->family) { case AG_NET_INET4: Free(na->sNum); if ((na->sNum = TryMalloc(INET_ADDRSTRLEN)) == NULL) { return (NULL); } s = inet_ntop(AF_INET, &na->na_inet4.addr, na->sNum, INET_ADDRSTRLEN); break; #ifdef AF_INET6 case AG_NET_INET6: Free(na->sNum); if ((na->sNum = TryMalloc(INET6_ADDRSTRLEN)) == NULL) { return (NULL); } s = inet_ntop(AF_INET6, &na->na_inet4.addr, na->sNum, INET6_ADDRSTRLEN); break; #endif default: AG_SetError("Bad address format"); return (NULL); } if (s == NULL) { AG_SetError("inet_ntop: %s", strerror(errno)); return (NULL); } return (na->sNum); }
int main() { List list1, list2; list1 = CreateListPositive(); list2 = CreateListNegative(); printf("Now insert an node into list1\n"); List newNode1 = TryMalloc(); newNode1->Num = 0; newNode1->Next = NULL; InsertNode(list1, newNode1, 0); List newNode2 = TryMalloc(); newNode2->Num = 4; newNode2->Next = NULL; InsertNode(list1, newNode2, 4); PrintList(list1); printf("Append an element to the end of list2\n"); List appendNode = TryMalloc(); appendNode->Num = 88; appendNode->Next = NULL; AppendNode(list2, appendNode); PrintList(list2); printf("Now reverse list2...\n"); List reversedList = ReverseMyList(list2); PrintList(reversedList); printf("Now switchs 3 and 4 in list2...\n"); SwitchTwoNodes(reversedList, 3, 4); PrintList(reversedList); printf("Input \"y\" to destroy lists..."); char c = getchar(); getchar(); // Remove enter if(tolower(c) == 'y') { printf("Destrying the list...\n"); DestroyList(list1); DestroyList(list2); } }
/* Initialize the clipping rectangle stack. */ static int InitClipRects(AG_DriverSDLGL *sgl, int wView, int hView) { AG_ClipRect *cr; int i; for (i = 0; i < 4; i++) sgl->clipStates[i] = 0; /* Rectangle 0 always covers the whole view. */ if ((sgl->clipRects = TryMalloc(sizeof(AG_ClipRect))) == NULL) return (-1); cr = &sgl->clipRects[0]; cr->r = AG_RECT(0, 0, wView, hView); cr->eqns[0][0] = 1.0; cr->eqns[0][1] = 0.0; cr->eqns[0][2] = 0.0; cr->eqns[0][3] = 0.0; cr->eqns[1][0] = 0.0; cr->eqns[1][1] = 1.0; cr->eqns[1][2] = 0.0; cr->eqns[1][3] = 0.0; cr->eqns[2][0] = -1.0; cr->eqns[2][1] = 0.0; cr->eqns[2][2] = 0.0; cr->eqns[2][3] = (double)wView; cr->eqns[3][0] = 0.0; cr->eqns[3][1] = -1.0; cr->eqns[3][2] = 0.0; cr->eqns[3][3] = (double)hView; sgl->nClipRects = 1; return (0); }
/* Create a new animation of the specified pixel format. */ AG_Anim * AG_AnimNew(enum ag_anim_type type, Uint w, Uint h, const AG_PixelFormat *pf, Uint flags) { AG_Anim *a; if ((a = TryMalloc(sizeof(AG_Anim))) == NULL) { return (NULL); } if ((a->format = AG_PixelFormatDup(pf)) == NULL) { Free(a); return (NULL); } a->type = type; a->flags = flags; a->w = w; a->h = h; a->n = 0; a->pitch = w*pf->BytesPerPixel; a->clipRect = AG_RECT(0,0,w,h); a->f = NULL; a->fpsOrig = 30.0; AG_MutexInitRecursive(&a->lock); return (a); }
static void init_nl_buff(void) { /* Allocate the newline buffer, if possible */ nl_size = 0 + NL_START; nl_buff = (struct newline *)TryMalloc(sizeof (struct newline)*nl_size); nl_free = 0; }
static int GetIfConfig(AG_NetAddrList *nal) { PIP_ADAPTER_INFO pAdapterInfo; PIP_ADAPTER_INFO pAdapter; PIP_ADDR_STRING pAddress; DWORD dwRetVal = 0; ULONG ulOutBufLen = sizeof(IP_ADAPTER_INFO); AG_NetAddrListClear(nal); if ((pAdapterInfo = TryMalloc(sizeof(IP_ADAPTER_INFO))) == NULL) return (-1); AG_MutexLock(&agNetWin32Lock); dwRetVal = GetAdaptersInfo(pAdapterInfo, &ulOutBufLen); if (dwRetVal == ERROR_BUFFER_OVERFLOW) { PIP_ADAPTER_INFO pAdapterInfoNew; if ((pAdapterInfoNew = TryRealloc(pAdapterInfo, ulOutBufLen)) == NULL) { free(pAdapterInfo); goto fail; } pAdapterInfo = pAdapterInfoNew; dwRetVal = GetAdaptersInfo(pAdapterInfo, &ulOutBufLen); } if (dwRetVal != NO_ERROR) { AG_SetError("GetAdaptersInfo() failed"); goto fail; } for (pAdapter = pAdapterInfo; pAdapter != NULL; pAdapter = pAdapter->Next) { for (pAddress = &pAdapterInfo->IpAddressList; pAddress != NULL; pAddress = pAddress->Next) { AG_NetAddr *na; if ((na = AG_NetAddrNew()) == NULL) { AG_NetAddrListClear(nal); goto fail; } na->family = AG_NET_INET4; na->port = 0; na->na_inet4.addr = inet_addr(pAddress->IpAddress.String); TAILQ_INSERT_TAIL(nal, na, addrs); } } AG_MutexUnlock(&agNetWin32Lock); free(pAdapterInfo); return (0); fail: AG_MutexUnlock(&agNetWin32Lock); return (-1); }
/* Duplicate a polygon structure. */ int M_PolygonCopy(M_Polygon *D, const M_Polygon *S) { if ((D->v = TryMalloc(S->n*sizeof(M_Vector2))) == NULL) { return (-1); } D->n = S->n; memcpy(D->v, S->v, S->n*sizeof(M_Vector2)); return (0); }
/* Create a new database-bound object. */ AG_DbObject * AG_DbObjectNew(void) { AG_DbObject *dbo; if ((dbo = TryMalloc(sizeof(AG_DbObject))) == NULL) { return (NULL); } AG_ObjectInit(dbo, &agDbObjectClass); return (dbo); }
List CreateListPositive() { List head, cursor; head = TryMalloc(); head->Num = '\0'; head->Next = NULL; cursor = head; for(int i = 1; i < 5;i++) { List next; next = TryMalloc(); next->Num = i + 1; next->Next = NULL; cursor->Num = i; cursor->Next = next; cursor = next; // Move to next node } return head; }
/* Allocate a new user structure. */ AG_User * AG_UserNew(void) { AG_User *u; if ((u = TryMalloc(sizeof(AG_User))) == NULL) { return (NULL); } u->name[0] = '\0'; u->flags = 0; u->passwd = NULL; u->uid = 0; u->gid = 0; u->loginClass = NULL; u->gecos = NULL; u->home = NULL; u->shell = NULL; u->tmp = NULL; return (u); }
static char * GetAddrNumerical(AG_NetAddr *na) { struct sockaddr_storage sa; socklen_t saLen; Free(na->sNum); if ((na->sNum = TryMalloc(NI_MAXHOST)) == NULL) { return (NULL); } NetAddrToSockAddr(na, &sa, &saLen); if (getnameinfo((struct sockaddr *)&sa, saLen, na->sNum, NI_MAXHOST, NULL, 0, NI_NUMERICHOST) != 0) { AG_SetError("inet_ntop: %s", strerror(errno)); return (NULL); } return (na->sNum); }
/* * Create a polyhedron facet for the specified edges. * Return facet index on success or 0 on failure. */ Uint M_PolyhedronAddFacet(M_Polyhedron *P, Uint n, const Uint *e) { M_Facet *fNew, *f; Uint *eNew; if ((fNew = AG_TryRealloc(P->f, (P->nf+1)*sizeof(M_Facet))) == NULL) { return (0); } if ((eNew = TryMalloc(n*sizeof(Uint))) == NULL) { Free(fNew); return (0); } P->f = fNew; f = &P->f[P->nf]; f->e = eNew; f->n = n; memcpy(f->e, e, n*sizeof(Uint)); return (P->nf++); }
/* Insert a new animation frame. */ int AG_AnimFrameNew(AG_Anim *a, const AG_Surface *su) { AG_AnimFrame *afNew, *af; AG_Surface *suTmp = NULL; int nf; AG_MutexLock(&a->lock); if (su->w != a->w || su->h != a->h) { if (AG_ScaleSurface(su, a->w, a->h, &suTmp) == -1) goto fail; } else { if ((suTmp = AG_SurfaceConvert(su, a->format)) == NULL) goto fail; } if ((afNew = TryRealloc(a->f, (a->n+1)*sizeof(AG_AnimFrame))) == NULL) { goto fail; } a->f = afNew; af = &a->f[a->n]; af->flags = 0; if ((af->pixels = TryMalloc(su->h*a->pitch)) == NULL) { a->n--; goto fail; } memcpy(af->pixels, suTmp->pixels, su->h*a->pitch); nf = a->n++; AG_MutexUnlock(&a->lock); AG_SurfaceFree(suTmp); return (nf); fail: AG_MutexUnlock(&a->lock); if (suTmp != NULL) { AG_SurfaceFree(suTmp); } return (-1); }
/* Initialize the default cursor. */ int AG_SDL_InitDefaultCursor(void *obj) { AG_Driver *drv = AGDRIVER(obj); AG_Cursor *ac; SDL_Cursor *sc; if ((sc = SDL_GetCursor()) == NULL) { AG_SetError("SDL_GetCursor() returned NULL"); return (-1); } if ((drv->cursors = TryMalloc(sizeof(AG_Cursor))) == NULL) { return (-1); } ac = &drv->cursors[0]; drv->nCursors = 1; AG_CursorInit(ac); ac->w = (Uint)sc->area.w; ac->h = (Uint)sc->area.h; ac->xHot = (int)sc->hot_x; ac->yHot = (int)sc->hot_y; ac->p = sc; return (0); }
/* Create a new database handle for the given database backend. */ AG_Db * AG_DbNew(const char *backend) { AG_Db *db; AG_DbClass *dbc = NULL; #ifdef HAVE_DB4 /* XXX */ if (strcmp(backend, "hash")) { dbc = &agDbHashClass; } else if (strcmp(backend, "btree")) { dbc = &agDbBtreeClass; } #endif if (dbc == NULL) { AG_SetError("No such database backend: %s", backend); return (NULL); } if ((db = TryMalloc(sizeof(AG_Db))) == NULL) { return (NULL); } AG_ObjectInit(db, dbc); return (db); }