static void textblock_vappend_c(textblock *tb, byte attr, const char *fmt, va_list vp) { while (1) { va_list args; size_t len; size_t remaining = tb->size - tb->strlen; char *fmt_start = tb->text + tb->strlen; VA_COPY(args, vp); len = vstrnfmt(fmt_start, remaining, fmt, args); va_end(args); if (len < remaining - 1) { byte *attr_start = tb->attrs + (tb->strlen * sizeof *tb->attrs); memset(attr_start, attr, len * sizeof *tb->attrs); tb->strlen += len; break; } tb->size = TEXTBLOCK_LEN_INCR(tb->size); tb->text = mem_realloc(tb->text, tb->size); tb->attrs = mem_realloc(tb->attrs, tb->size * sizeof *tb->attrs); } }
int bits_init (void) { ASSERT (comp_zero == NULL); comp_zero = mem_alloc (BIT_SECTSIZE + 1); if (!comp_zero) return (-1); /* Could not allocate new block */ memset (compressed, BIT_SECTSIZE, 0x00); comp_zero_size = compress_bits (compressed, comp_zero, BIT_SECTSIZE); comp_zero = mem_realloc (comp_zero, comp_zero_size); comp_ones = mem_alloc (BIT_SECTSIZE + 1); if (!comp_ones) { mem_free (comp_ones); return (-1); /* Could not allocate new block */ } memset (compressed, BIT_SECTSIZE, 0xFF); comp_ones_size = compress_bits (compressed, comp_ones, BIT_SECTSIZE); comp_ones = mem_realloc (comp_ones, comp_ones_size); return (0); }
struct string *add_bytes_to_string__( struct string *string, unsigned char *bytes, int length ) { int ecx; int edx; int newlength; if ( assert_failed == 0 ) { if ( bytes[0] && string && length >= 0 ) assert_failed = 0; else { assert_failed = 1; errfile = "/home/naftali/source/elinks-0.12~pre5/src/util/string.h"; errline = 255; elinks_internal( "assertion string && bytes && length >= 0 failed: [add_bytes_to_string]" ); if ( assert_failed ) goto B2; } if ( length ) { newlength = string->length + length; if ( ( ( string->length + 255 ) & -256 ) < ( ( newlength + 256 ) & -256 ) ) { ebp_32 = bytes[0]; *ebp_36 = length; if ( mem_realloc( (void*)string->source, length ) ) { string = (struct string*)mem_realloc( (void*)string->source, length ); memset( string[0].source + ( ( string->length + 255 ) & -256 ), 0, ( ( newlength + 256 ) & -256 ) - ( ( string->length + 255 ) & -256 ) ); } else { string[0].source = 0; return &string[0]; } } if ( string->source ) { memcpy( string->length + string->source, &bytes[0], length ); string->length = newlength; return &string[0]; } string[0].source = 0; return &string[0]; } else { return &string[0]; } } B2: assert_failed = 0; string[0].source = 0; return &string[0]; }
void alloc_term_screen(struct terminal *term, int x, int y) { unsigned *s, *t; if (x && (unsigned)x * (unsigned)y / (unsigned)x != (unsigned)y) overalloc(); if ((unsigned)x * (unsigned)y > MAXINT / sizeof(unsigned)) overalloc(); s = mem_realloc(term->screen, x * y * sizeof(unsigned)); t = mem_realloc(term->last_screen, x * y * sizeof(unsigned)); memset(t, -1, x * y * sizeof(unsigned)); term->x = x; term->y = y; term->last_screen = t; memset(s, 0, x * y * sizeof(unsigned)); term->screen = s; term->dirty = 1; }
/* ------------------------------------------------------------ */ char *substr(const char *s, size_t pos, ssize_t length, char**buffer, size_t *n) { size_t i; size_t sl = length; if(length < 0) { sl = -length; if(pos+1 < sl) { sl -= pos - 1; pos = 0; } else { pos = pos + length + 1; } } const char *orig = s + pos; if(!s) { return NULL; } if (!*buffer) { *buffer=(char *)mem_malloc(sl+1); *n = sl+1; } if (*n<=sl) { *buffer=(char *)mem_realloc(*buffer, sl+1); *n = sl+1; } for (i=0; i<sl && orig[i]; i++) { (*buffer)[i] = orig[i]; } (*buffer)[i]='\0'; return *buffer; }
void set_td_width(struct table *t, int x, int width, int f) { if (x >= t->xc) { int n = t->xc ? t->xc : 1; int i; int *nc; while (x >= n) { if ((unsigned)n > MAXINT / 2) overalloc(); n *= 2; } if ((unsigned)n > MAXINT / sizeof(int)) overalloc(); nc = mem_realloc(t->xcols, n * sizeof(int)); for (i = t->xc; i < n; i++) nc[i] = W_AUTO; t->xc = n; t->xcols = nc; } if (t->xcols[x] == W_AUTO || f) { set: t->xcols[x] = width; return; } if (width == W_AUTO) return; if (width < 0 && t->xcols[x] >= 0) goto set; if (width >= 0 && t->xcols[x] < 0) return; t->xcols[x] = (t->xcols[x] + width) / 2; }
int cstring_extend(cstring s, size_t size) { size_t bytes_needed, newsize; char* data; assert(s != NULL); assert(s->data != NULL); assert(s->cbAllocated >= CSTRING_INITIAL_SIZE); assert(s->cbUsed > 0 && s->cbUsed <= s->cbAllocated); /* Check for available space and reallocate if necessary */ bytes_needed = s->cbUsed + size; if(bytes_needed > s->cbAllocated) { /* Double the size of the buffer */ newsize = s->cbAllocated * 2; if(newsize < bytes_needed) { /* Doubling wasn't sufficient */ newsize = s->cbAllocated + size; } if( (data = mem_realloc(s->data, newsize)) == NULL) return 0; else { s->data = data; s->cbAllocated = newsize; } } assert(s->cbUsed == strlen(s->data) + 1); return 1; }
/** @returns 1 on success, 0 on allocation failure */ static int alloc_leafset(struct fastfind_info *info) { struct ff_node **leafsets; struct ff_node *leafset; assert(info->leafsets_count < FF_MAX_LEAFSETS); if_assert_failed return 0; /* info->leafsets[0] is never used since l=0 marks no leaf * in struct ff_node. That's the reason of that + 2. */ leafsets = mem_realloc(info->leafsets, sizeof(*leafsets) * (info->leafsets_count + 2)); if (!leafsets) return 0; info->leafsets = leafsets; leafset = mem_calloc(info->uniq_chars_count, sizeof(*leafset)); if (!leafset) return 0; FF_DBG_mem(info, sizeof(*leafsets)); FF_DBG_mem(info, sizeof(*leafset) * info->uniq_chars_count); info->leafsets_count++; info->leafsets[info->leafsets_count] = leafset; return 1; }
static long _str_split(char *s, const char *delims, char ***out, int max) { /* TODO: get rid of str_len */ long long len = str_len(s) + 1; char esc = '\\', prev = '\0'; int parts = 0; int size = max; char **res = *out; for (long long i = 0, start = 0; i < len; ++i) { if ((is_delim(s[i], delims) && prev != esc) || (i + 1) == len) { parts++; if (max == 0 && parts > size) { size = size == 0 ? 2 : size * 2; res = mem_realloc(res, size * sizeof(char*)); } else if (max != 0 && parts > max) { parts = -1; break; } res[parts - 1] = s + start; s[i] = '\0'; start = i + 1; } prev = (prev == esc) ? '\0' : s[i]; } *out = res; return parts; }
/** * Save data data to the partial storage. * * @param ctx Pointer to storage context * @param buf Buffer containing data to store. * @param len Number of bytes of data. * @return Total number of bytes stored to this storage. */ int partial_store_collect( struct partial_store *ctx, uint8_t *buf, int len) { int remaining; if ( ctx->partial_buf == NULL ) { ctx->partial_size = len * 2; TRACE("Initial size of partial buffer is %d \n", ctx->partial_size); ctx->partial_buf = mem_alloc( ctx->partial_size ); ctx->partial_len = 0; } remaining = ctx->partial_size - ctx->partial_len; if (remaining < len ) { /* need to reallocate the buffer */ ctx->partial_size = ctx->partial_size * 2; if (ctx->partial_size < ctx->partial_len + len) ctx->partial_size = ctx->partial_len + len; ctx->partial_buf = mem_realloc( ctx->partial_buf, ctx->partial_size ); TRACE("Reallocated partial buffer, length now %d \n", ctx->partial_size); } /* Copy the received data to the end of the partial buffer */ memcpy(ctx->partial_buf + ctx->partial_len, buf, len ); ctx->partial_len += len; return ctx->partial_len; }
/** * Decode SDES items from a buffer * * @param mb Buffer to decode from * @param sdes RTCP SDES to decode into * * @return 0 if success, otherwise errorcode */ int rtcp_sdes_decode(struct mbuf *mb, struct rtcp_sdes *sdes) { size_t start; if (!sdes) return EINVAL; if (mbuf_get_left(mb) < RTCP_SRC_SIZE) return EBADMSG; start = mb->pos; sdes->src = ntohl(mbuf_read_u32(mb)); /* Decode all SDES items */ while (mbuf_get_left(mb) >= RTCP_SDES_MIN_SIZE) { uint8_t type; struct rtcp_sdes_item *item; type = mbuf_read_u8(mb); if (type == RTCP_SDES_END) break; if (mbuf_get_left(mb) < 1) return EBADMSG; if (!sdes->itemv) { sdes->itemv = mem_alloc(sizeof(*sdes->itemv), NULL); if (!sdes->itemv) return ENOMEM; } else { const size_t sz = (sdes->n + 1) * sizeof(*sdes->itemv); struct rtcp_sdes_item *itemv; itemv = mem_realloc(sdes->itemv, sz); if (!itemv) return ENOMEM; sdes->itemv = itemv; } item = &sdes->itemv[sdes->n]; item->type = (enum rtcp_sdes_type)type; item->length = mbuf_read_u8(mb); if (mbuf_get_left(mb) < item->length) return EBADMSG; item->data = mem_alloc(item->length, NULL); if (!item->data) return ENOMEM; (void)mbuf_read_mem(mb, (uint8_t *)item->data, item->length); sdes->n++; } /* slurp padding */ while ((mb->pos - start) & 0x3 && mbuf_get_left(mb)) ++mb->pos; return 0; }
s_string Input() { s_string s_input; int32_t c; char* i_temp_ptr; int32_t i_buffer = 64; int32_t i = 0; i_temp_ptr = mem_alloc(sizeof(char)*i_buffer,id_string); printf("input fce! ---------------- \n"); while((c = getchar()) != '\n') { if( i == i_buffer - 2) { i_buffer*=2; i_temp_ptr = mem_realloc(i_temp_ptr,sizeof(char)*i_buffer); } i_temp_ptr[i] = (char)c; i++; i_temp_ptr[i] = '\0'; } s_input.input = i_temp_ptr; s_input.length = String_length(s_input.input); s_input.size = i_buffer; return s_input; }
/** * Allocate a standard size (use it at startup; good speed optimization) */ void dbt_prealloc(dbt_t t, int num, int recsize) { if (vmt[t].flags & 1) { // use file int newsize; newsize = num - vmt[t].size; if (newsize > 0) { dbt_file_append(t, newsize, recsize); } } else { // use memory int i; if (vmt[t].size < num) { mem_unlock(vmt[t].m_handle); vmt[t].m_handle = mem_realloc(vmt[t].m_handle, sizeof(mem_t) * num); vmt[t].m_table = (mem_t *)mem_lock(vmt[t].m_handle); for (i = vmt[t].size; i < num; i++) { vmt[t].m_table[i] = mem_alloc(recsize); } vmt[t].size = num; } } }
void itrm_queue_event(struct itrm *itrm, unsigned char *data, int len) { int w = 0; if (!len) return; if (!itrm->out.queue.len && can_write(itrm->out.sock)) { w = safe_write(itrm->out.sock, data, len); if (w <= 0 && HPUX_PIPE) { register_bottom_half(free_itrm, itrm); return; } } if (w < len) { int left = len - w; unsigned char *c = mem_realloc(itrm->out.queue.data, itrm->out.queue.len + left); if (!c) { free_itrm(itrm); return; } itrm->out.queue.data = c; memcpy(itrm->out.queue.data + itrm->out.queue.len, data + w, left); itrm->out.queue.len += left; set_handlers(itrm->out.sock, get_handler(itrm->out.sock, SELECT_HANDLER_READ), (select_handler_T) itrm_queue_write, (select_handler_T) free_itrm, itrm); } }
void add_object_to_line(struct g_part *pp, struct g_object_line **lp, struct g_object *go) { struct g_object_line *l; if (go && (go->xw < 0 || go->yw < 0)) { internal("object has negative size: %d,%d", go->xw, go->yw); return; } if (!*lp) { l = mem_calloc(sizeof(struct g_object_line) + sizeof(struct g_object_text *)); l->mouse_event = g_line_mouse; l->draw = g_line_draw; l->destruct = g_line_destruct; l->get_list = g_line_get_list; /*l->x = 0; l->y = 0; l->xw = 0; l->yw = 0;*/ l->bg = pp->root->bg; if (!go) { *lp = l; return; } l->n_entries = 1; } else { if (!go) return; (*lp)->n_entries++; if ((unsigned)(*lp)->n_entries > (MAXINT - sizeof(struct g_object_line)) / sizeof(struct g_object *)) overalloc(); l = mem_realloc(*lp, sizeof(struct g_object_line) + sizeof(struct g_object *) * (*lp)->n_entries); *lp = l; } l->entries[l->n_entries - 1] = go; *lp = l; if (pp->cx == -1) pp->cx = par_format.leftmargin * G_HTML_MARGIN; pp->cx += go->xw; }
void dsExpand(DynamicStack *ds, int num_frames) { size_t new_size, total_bytes; char *new_base; if ( num_frames < 1 ) return; if ( DynStk_CurSize(*ds) > 0 ) new_size = 2 * DynStk_CurSize(*ds); else new_size = DynStk_InitSize(*ds); if ( new_size < DynStk_CurSize(*ds) + num_frames ) new_size = new_size + num_frames; xsb_dbgmsg((LOG_TRIE_STACK, "Expanding %s: %d -> %d", DynStk_Name(*ds), DynStk_CurSize(*ds), new_size)); dbg_dsPrint(LOG_TRIE_STACK, *ds, "Before expansion"); total_bytes = new_size * DynStk_FrameSize(*ds); new_base = mem_realloc(DynStk_Base(*ds),DynStk_CurSize(*ds)*DynStk_FrameSize(*ds),total_bytes,TABLE_SPACE); if ( IsNULL(new_base) && total_bytes > 0) xsb_abort("Ran out of memory during expansion of %s", DynStk_Name(*ds)); DynStk_Top(*ds) = new_base + ((char *)DynStk_Top(*ds) - (char *)DynStk_Base(*ds)); DynStk_Base(*ds) = new_base; DynStk_Ceiling(*ds) = new_base + total_bytes; DynStk_CurSize(*ds) = new_size; dbg_dsPrint(LOG_TRIE_STACK, *ds, "After expansion"); }
// Reserve nbytes in buffer void Outbuffer::reserve(unsigned nbytes) { if (pend - p < nbytes) { unsigned oldlen = len; unsigned used = p - buf; if (inc > nbytes) { len = used + inc; } else { len = used + nbytes; if (len < 2 * oldlen) { len = 2 * oldlen; if (len < 8) len = 8; } } #if MEM_DEBUG buf = (unsigned char *)mem_realloc(buf, len); #else if (buf) buf = (unsigned char *) realloc(buf,len); else buf = (unsigned char *) malloc(len); #endif pend = buf + len; p = buf + used; } }
uint16_t bas_find_or_create_global(Basic* kernel, const char* name) { uint16_t slot = bas_find_global(kernel, name); if (slot == BAD_GLOBAL) { GlobalVariable** new_vars; if (kernel->num_globals == BAD_GLOBAL-1) return BAD_GLOBAL; GlobalVariable* var = (GlobalVariable*)mem_malloc( sizeof(GlobalVariable) + strlen(name) + 1 ); if (var == NULL) return BAD_GLOBAL; var->value.i = 0; var->type = kTypeInteger; strcpy( (char*)(var+1), name ); new_vars = mem_realloc(kernel->global_array, sizeof(GlobalVariable*)*(kernel->num_globals + 1) ); if (new_vars == NULL) { mem_free(var); return BAD_GLOBAL; } kernel->global_array = new_vars; kernel->global_array[kernel->num_globals] = var; slot = (uint16_t)kernel->num_globals; kernel->num_globals ++; } return slot; }
/** * Shrink a pbuf chain to a desired length. * * @param p pbuf to shrink. * @param new_len desired new length of pbuf chain * * Depending on the desired length, the first few pbufs in a chain might * be skipped and left unchanged. The new last pbuf in the chain will be * resized, and any remaining pbufs will be freed. * * @note If the pbuf is ROM/REF, only the ->tot_len and ->len fields are adjusted. * @note May not be called on a packet queue. * * @note Despite its name, pbuf_realloc cannot grow the size of a pbuf (chain). */ void pbuf_realloc(struct pbuf *p, u16_t new_len) { struct pbuf *q; u16_t rem_len; /* remaining length */ s32_t grow; LWIP_ASSERT("pbuf_realloc: p != NULL", p != NULL); LWIP_ASSERT("pbuf_realloc: sane p->type", p->type == PBUF_POOL || p->type == PBUF_ROM || p->type == PBUF_RAM || p->type == PBUF_REF); /* desired length larger than current length? */ if (new_len >= p->tot_len) { /* enlarging not yet supported */ return; } /* the pbuf chain grows by (new_len - p->tot_len) bytes * (which may be negative in case of shrinking) */ grow = new_len - p->tot_len; /* first, step over any pbufs that should remain in the chain */ rem_len = new_len; q = p; /* should this pbuf be kept? */ while (rem_len > q->len) { /* decrease remaining length by pbuf length */ rem_len -= q->len; /* decrease total length indicator */ LWIP_ASSERT("grow < max_u16_t", grow < 0xffff); q->tot_len += (u16_t)grow; /* proceed to next pbuf in chain */ q = q->next; LWIP_ASSERT("pbuf_realloc: q != NULL", q != NULL); } /* we have now reached the new last pbuf (in q) */ /* rem_len == desired length for pbuf q */ /* shrink allocated memory for PBUF_RAM */ /* (other types merely adjust their length fields */ if ((q->type == PBUF_RAM) && (rem_len != q->len)) { /* reallocate and adjust the length of the pbuf that will be split */ q = mem_realloc(q, (u8_t *)q->payload - (u8_t *)q + rem_len); LWIP_ASSERT("mem_realloc give q == NULL", q != NULL); } /* adjust length fields for new last pbuf */ q->len = rem_len; q->tot_len = q->len; /* any remaining pbufs in chain? */ if (q->next != NULL) { /* free remaining pbufs in chain */ pbuf_free(q->next); } /* q is last packet in chain */ q->next = NULL; }
/** First information such as permissions is gathered for each directory entry. * All entries are then sorted. */ static struct directory_entry * get_smb_directory_entries(int dir, struct string *prefix) { struct directory_entry *entries = NULL; int size = 0; struct smbc_dirent *entry; while ((entry = smbc_readdir(dir))) { struct stat st, *stp; struct directory_entry *new_entries; struct string attrib; struct string name; if (!strcmp(entry->name, ".")) continue; new_entries = mem_realloc(entries, (size + 2) * sizeof(*new_entries)); if (!new_entries) continue; entries = new_entries; if (!init_string(&attrib)) { continue; } if (!init_string(&name)) { done_string(&attrib); continue; } add_string_to_string(&name, prefix); add_to_string(&name, entry->name); stp = (smbc_stat(name.source, &st)) ? NULL : &st; stat_type(&attrib, stp); stat_mode(&attrib, stp); stat_links(&attrib, stp); stat_user(&attrib, stp); stat_group(&attrib, stp); stat_size(&attrib, stp); stat_date(&attrib, stp); entries[size].name = stracpy(entry->name); entries[size].attrib = attrib.source; done_string(&name); size++; } smbc_closedir(dir); if (!size) { /* We may have allocated space for entries but added none. */ mem_free_if(entries); return NULL; } qsort(entries, size, sizeof(*entries), compare_dir_entries); memset(&entries[size], 0, sizeof(*entries)); return entries; }
void EStackPush(Stack S,ELogic temp) { if(S->count == S->size) { S->size*=2; S->element = (ELogic*)mem_realloc(S->element,((S->size)*sizeof(ELogic))); } S->element[S->count++] = temp; }
void main() { uint8_t* pointers[NUM_PTRS] = {0}; size_t sizes[NUM_PTRS] = {0}; int n; mem_init( (uint32_t*)&memory, (uint32_t*)(memory + NUM_PTRS*64) ); for(n=0; n<1000000; ++n) { int slot = (rand() % 100); int new_size = (rand() & 255); int old_size = sizes[slot]; int q; //mem_dump(); printf( "Resizing %d at %p from %d to %d: ", slot, pointers[slot], old_size, new_size ); char* newmem = mem_realloc( pointers[slot], new_size ); if ((newmem != NULL) || (new_size == 0)) { int s; pointers[slot] = newmem; sizes[slot] = new_size; for(s=old_size; s<new_size; ++s) { newmem[s] = (slot + s*3) & 255; } printf( "Success.\n" ); } else { printf( "Failed.\n" ); } // verify all memory. for(q=0; q<NUM_PTRS; ++q) { int size = sizes[q]; for(int s=0; s<size; ++s) if (pointers[q][s] != ((q + s*3) & 255)) printf( "Corruption in slot %d offset %d\n", q, s ); } } int y; for(y=0; y<NUM_PTRS; ++y) { if (pointers[y] != NULL) mem_free(pointers[y]); } mem_coalesce_free_space(); mem_dump(); }
void sbuf_ncat(StringBuf* sbuf, const char* s, int64 n) { if (sbuf->size + n > sbuf->alloc_size){ sbuf->alloc_size += n; sbuf->str = (char*) mem_realloc(sbuf->str, sbuf->alloc_size); } strncat(sbuf->str, s, n); sbuf->size += n; }
void r_xpand_spaces(struct part *p, int l) { unsigned char *c; if ((unsigned)l >= MAXINT) overalloc(); c = mem_realloc(p->spaces, l + 1); memset(c + p->spl, 0, l - p->spl + 1); p->spl = l + 1; p->spaces = c; }
void ExprInsert(Expr temp, QVarEle element) { if(temp->count == temp->size) { temp->size*=2; temp->elements = (QVarEle*)mem_realloc(temp->elements,sizeof(QVar)*temp->size); } temp->elements[temp->count] = element; temp->count++; }
void sbuf_ncpy(StringBuf* sbuf, const char* s, int64 n) { if (sbuf->alloc_size < (uint64) n + 1){ sbuf->alloc_size = n + 1; sbuf->str = (char*) mem_realloc(sbuf->str, n + 1); } strncpy(sbuf->str, s, n); sbuf->str[n] = 0; sbuf->size = n+1; }
void sbuf_cat(StringBuf* sbuf, const char* s) { int64 l = strlen(s); if (sbuf->size + l > sbuf->alloc_size){ sbuf->alloc_size += l; sbuf->str = (char*) mem_realloc(sbuf->str, sbuf->alloc_size); } strcat(sbuf->str, s); sbuf->size += l; }
void sbuf_catc(StringBuf* sbuf, const char c) { if (sbuf->size + 1 > sbuf->alloc_size){ sbuf->alloc_size *= 2; sbuf->str = (char*) mem_realloc(sbuf->str, sbuf->alloc_size); } sbuf->str[sbuf->size - 1] = c; sbuf->str[sbuf->size] = 0; sbuf->size++; }
/* Add the point to the given point set, making more space if there is * no more space left. */ void add_to_point_set(struct point_set *ps, int y, int x) { ps->pts[ps->n].x = x; ps->pts[ps->n].y = y; ps->n++; if (ps->n >= ps->allocated) { ps->allocated *= 2; ps->pts = mem_realloc(ps->pts, sizeof(*(ps->pts)) * ps->allocated); } }
struct link *new_link(struct f_data *f) { if (!f) return NULL; if (!(f->nlinks & (ALLOC_GR - 1))) { if ((unsigned)f->nlinks > MAXINT / sizeof(struct link) - ALLOC_GR) overalloc(); f->links = mem_realloc(f->links, (f->nlinks + ALLOC_GR) * sizeof(struct link)); } memset(&f->links[f->nlinks], 0, sizeof(struct link)); return &f->links[f->nlinks++]; }