/* Resize an animation; pixels are left uninitialized. */ int AG_AnimResize(AG_Anim *a, Uint w, Uint h) { Uint8 *pixelsNew; int i; int pitchNew; AG_MutexLock(&a->lock); pitchNew = w*a->format->BytesPerPixel; for (i = 0; i < a->n; i++) { AG_AnimFrame *af = &a->f[i]; if ((pixelsNew = TryRealloc(af->pixels, h*pitchNew)) == NULL) { for (i--; i >= 0; i--) { Free(af->pixels); } goto fail; } af->pixels = pixelsNew; } a->pitch = pitchNew; a->w = w; a->h = h; a->clipRect = AG_RECT(0,0,w,h); AG_MutexUnlock(&a->lock); return (0); fail: AG_MutexUnlock(&a->lock); return (-1); }
static void store_newline(void) { if (!nl_buff) return; if (nl_free == nl_size) { /* allocated array is full; try to increase its size */ size_t new_size = 2 * nl_size; struct newline *new_buff = (struct newline *)TryRealloc( (char *)nl_buff, sizeof (struct newline) * new_size ); if (!new_buff) { /* we failed */ abandon_nl_buff(); return; } nl_buff = new_buff, nl_size = new_size; } /* now we are sure there is room enough */ { struct newline *nl = &nl_buff[nl_free++]; size_t tk_diff = lex_tk_cnt - last_tk_cnt; nl->nl_tk_diff = (nl_tk_diff_t) tk_diff; if (nl->nl_tk_diff != tk_diff) { /* tk_diff does not fit in nl_tk_diff */ abandon_nl_buff(); } } }
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); }
/* * Append a line segment to a polygon. * Return segment index on success, -1 on failure. * XXX TODO sanity check point order */ int M_PolygonAddLine(M_Polygon *P, M_Line2 L) { M_Vector2 *vNew; if ((vNew = TryRealloc(P->v, (P->n+1)*sizeof(M_Vector2))) == NULL) { return (-1); } P->v = vNew; P->v[P->n] = L.p; return (P->n++); }
/* 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); }
void Store_Token(Token tk) { if (tk_free == tk_size) { /* allocated array is full; try to increase its size */ unsigned int new_size = tk_size + TK_INCR; Token *new_array = (Token *)TryRealloc( (char *)Token_Array, sizeof (Token) * new_size ); if (!new_array) { /* we failed */ fatal("out of memory"); } if (new_size < tk_free) fatal("internal error: TK_INCR causes numeric overflow"); Token_Array = new_array, tk_size = new_size; } /* now we are sure there is room enough */ Token_Array[tk_free++] = tk; }
void Store_Token(Token tk) { if (tk_free == tk_size) { /* allocated array is full; try to increase its size */ size_t new_size = 2 * tk_size; if (new_size < tk_free) fatal("internal error: TK_INCR causes numeric overflow"); fprintf (stderr,"X64: Growing Token_Array to 0x%016zx\n", new_size); Token *new_array = (Token *)TryRealloc( (char *)Token_Array, sizeof (Token) * new_size ); if (!new_array) { /* we failed */ fatal("out of memory"); } Token_Array = new_array, tk_size = new_size; } /* now we are sure there is room enough */ Token_Array[tk_free++] = tk; }
void Store_Token(Token tk) { if (tk_free == tk_size) { /* allocated array is full; try to increase its size */ size_t new_size = tk_size + tk_size/2; if (new_size < tk_free) fatal("out of address space"); Token *new_array = (Token *)TryRealloc( (char *)Token_Array, sizeof (Token) * new_size ); if (!new_array) { /* we failed */ fatal("out of memory"); } Token_Array = new_array, tk_size = new_size; } /* now we are sure there is room enough */ Token_Array[tk_free++] = tk; }