static void test_link_alloc(void) { #define COMPONENT_COMPONENT "/component-component" const char *o_tmpdir; /* idea here is to make sure component-component would optimally hit to the nearest_power value. it has to be big enough to cause requirement for allocation in t_realpath. */ string_t *basedir = t_str_new(256); str_append(basedir, cwd); str_append(basedir, "/"TEMP_DIRNAME); size_t len = nearest_power(I_MAX(127, str_len(basedir) + strlen(COMPONENT_COMPONENT) + 1)) - strlen(COMPONENT_COMPONENT); while(str_len(basedir) < len) { str_append(basedir, COMPONENT_COMPONENT); (void)mkdir(str_c(basedir), 0700); } o_tmpdir = tmpdir; tmpdir = str_c(basedir); create_links(tmpdir); test_link1(); test_link_loop(); tmpdir = o_tmpdir; }
void i_stream_grow_buffer(struct istream_private *stream, size_t bytes) { size_t old_size; i_assert(stream->max_buffer_size > 0); old_size = stream->buffer_size; stream->buffer_size = stream->pos + bytes; if (stream->buffer_size <= stream->init_buffer_size) stream->buffer_size = stream->init_buffer_size; else stream->buffer_size = nearest_power(stream->buffer_size); if (stream->buffer_size > stream->max_buffer_size) stream->buffer_size = stream->max_buffer_size; if (stream->buffer_size <= old_size) stream->buffer_size = old_size; else { stream->w_buffer = i_realloc(stream->w_buffer, old_size, stream->buffer_size); stream->buffer = stream->w_buffer; } }
static char * p_strarray_join_n(pool_t pool, const char *const *arr, unsigned int arr_len, const char *separator) { size_t alloc_len, sep_len, len, pos, needed_space; unsigned int i; char *str; sep_len = strlen(separator); alloc_len = 64; str = t_buffer_get(alloc_len); pos = 0; for (i = 0; i < arr_len; i++) { len = strlen(arr[i]); needed_space = pos + len + sep_len + 1; if (needed_space > alloc_len) { alloc_len = nearest_power(needed_space); str = t_buffer_reget(str, alloc_len); } if (pos != 0) { memcpy(str + pos, separator, sep_len); pos += sep_len; } memcpy(str + pos, arr[i], len); pos += len; } str[pos] = '\0'; if (!pool->datastack_pool) return p_memdup(pool, str, pos + 1); t_buffer_alloc(pos + 1); return str; }
char *vstrconcat(const char *str1, va_list args, size_t *ret_len) { const char *str; char *temp; size_t bufsize, i, len; i_assert(str1 != NULL); str = str1; bufsize = STRCONCAT_BUFSIZE; temp = t_buffer_get(bufsize); i = 0; do { len = strlen(str); if (i + len >= bufsize) { /* need more memory */ bufsize = nearest_power(i + len + 1); temp = t_buffer_reget(temp, bufsize); } memcpy(temp + i, str, len); i += len; /* next string */ str = va_arg(args, const char *); } while (str != NULL); i_assert(i < bufsize); temp[i++] = '\0'; *ret_len = i; return temp; }
static MMAPString * mmap_string_maybe_expand (MMAPString* string, size_t len) { if (string->len + len >= string->allocated_len) { size_t old_size; MMAPString * newstring; old_size = string->allocated_len; string->allocated_len = nearest_power (1, string->len + len + 1); #ifndef MMAP_UNAVAILABLE if (string->allocated_len > mmap_string_ceil) newstring = mmap_string_realloc_file(string); else { #endif newstring = mmap_string_realloc_memory(string); #ifndef MMAP_UNAVAILABLE if (newstring == NULL) newstring = mmap_string_realloc_file(string); } #endif if (newstring == NULL) string->allocated_len = old_size; } return string; }
static struct stack_block *mem_block_alloc(size_t min_size) { struct stack_block *block; size_t prev_size, alloc_size; prev_size = current_block == NULL ? 0 : current_block->size; alloc_size = nearest_power(prev_size + min_size); #ifndef USE_GC block = malloc(SIZEOF_MEMBLOCK + alloc_size); #else block = GC_malloc(SIZEOF_MEMBLOCK + alloc_size); #endif if (unlikely(block == NULL)) { if (outofmem) { if (min_size > outofmem_area.block.left) abort(); return &outofmem_area.block; } outofmem = TRUE; i_panic("data stack: Out of memory when allocating %" PRIuSIZE_T" bytes", alloc_size + SIZEOF_MEMBLOCK); } block->size = alloc_size; block->left = 0; block->lowwater = block->size; block->next = NULL; #ifdef DEBUG memset(STACK_BLOCK_DATA(block), CLEAR_CHR, alloc_size); #endif return block; }
static void entry_text_grow(GUI_ENTRY_REC *entry, int grow_size) { if (entry->text_len+grow_size < entry->text_alloc) return; entry->text_alloc = nearest_power(entry->text_alloc+grow_size); entry->text = g_realloc(entry->text, entry->text_alloc); }
/* Strings. */ static void g_string_maybe_expand (GString* string, gsize len) { if (string->len + len >= string->allocated_len) { string->allocated_len = nearest_power (1, string->len + len + 1); string->str = g_realloc (string->str, string->allocated_len); } }
static void gr_init(void) { size_t old_grbuf_size = grbuf_size; if (grbuf == NULL || errno == ERANGE) { grbuf_size = nearest_power(old_grbuf_size + 1); if (grbuf_size < PWBUF_MIN_SIZE) grbuf_size = PWBUF_MIN_SIZE; grbuf = i_realloc(grbuf, old_grbuf_size, grbuf_size); } }
static void pw_init(void) { size_t old_pwbuf_size = pwbuf_size; if (pwbuf == NULL || errno == ERANGE) { pwbuf_size = nearest_power(old_pwbuf_size + 1); if (pwbuf_size < PWBUF_MIN_SIZE) pwbuf_size = PWBUF_MIN_SIZE; pwbuf = i_realloc(pwbuf, old_pwbuf_size, pwbuf_size); } }
static void linebuf_append(LINEBUF_REC *rec, const char *data, int len) { if (rec->len+len > rec->alloc) { rec->alloc = nearest_power(rec->len+len);; rec->str = rec->str == NULL ? g_malloc(rec->alloc) : g_realloc(rec->str, rec->alloc); } memcpy(rec->str + rec->len, data, len); rec->len += len; }
size_t pool_get_exp_grown_size(pool_t pool, size_t old_size, size_t min_size) { size_t exp_size, easy_size; i_assert(old_size < min_size); exp_size = nearest_power(min_size); easy_size = old_size + p_get_max_easy_alloc_size(pool); if (easy_size < exp_size && easy_size >= min_size) exp_size = easy_size; i_assert(exp_size >= min_size); return exp_size; }
GStringChunk* g_string_chunk_new (gsize default_size) { GStringChunk *new_chunk = g_new (GStringChunk, 1); gsize size = 1; size = nearest_power (1, default_size); new_chunk->const_table = NULL; new_chunk->storage_list = NULL; new_chunk->storage_next = size; new_chunk->default_size = size; new_chunk->this_size = size; return new_chunk; }
void *i_stream_alloc(struct istream_private *stream, size_t size) { size_t old_size, avail_size; i_stream_try_alloc(stream, size, &avail_size); if (avail_size < size) { old_size = stream->buffer_size; stream->buffer_size = nearest_power(stream->pos + size); stream->w_buffer = i_realloc(stream->w_buffer, old_size, stream->buffer_size); stream->buffer = stream->w_buffer; i_stream_try_alloc(stream, size, &avail_size); i_assert(avail_size >= size); } return stream->w_buffer + stream->pos; }
int t_get_current_dir(const char **dir_r) { /* @UNSAFE */ char *dir; size_t size = 128; dir = t_buffer_get(size); while (getcwd(dir, size) == NULL) { if (errno != ERANGE) return -1; size = nearest_power(size+1); dir = t_buffer_get(size); } t_buffer_alloc(strlen(dir) + 1); *dir_r = dir; return 0; }
gchar* g_string_chunk_insert (GStringChunk *chunk, const gchar *string) { gsize len = strlen (string); char* pos; g_return_val_if_fail (chunk != NULL, NULL); if ((chunk->storage_next + len + 1) > chunk->this_size) { gsize new_size = nearest_power (chunk->default_size, len + 1); chunk->storage_list = g_slist_prepend (chunk->storage_list, g_new (char, new_size)); chunk->this_size = new_size; chunk->storage_next = 0; }
int t_readlink(const char *path, const char **dest_r) { /* @UNSAFE */ ssize_t ret; char *dest; size_t size = 128; dest = t_buffer_get(size); while ((ret = readlink(path, dest, size)) >= (ssize_t)size) { size = nearest_power(size+1); dest = t_buffer_get(size); } if (ret < 0) return -1; dest[ret] = '\0'; t_buffer_alloc(ret + 1); *dest_r = dest; return 0; }
/** * g_string_chunk_insert_len: * @chunk: a #GStringChunk * @string: bytes to insert * @len: number of bytes of @string to insert, or -1 to insert a * nul-terminated string. * * Adds a copy of the first @len bytes of @string to the #GStringChunk. The * copy is nul-terminated. * * The characters in the string can be changed, if necessary, though you * should not change anything after the end of the string. * * Return value: a pointer to the copy of @string within the #GStringChunk * * Since: 2.4 **/ gchar* g_string_chunk_insert_len (GStringChunk *chunk, const gchar *string, gssize len) { gssize size; gchar* pos; g_return_val_if_fail (chunk != NULL, NULL); if (len < 0) size = strlen (string); else size = len; if ((chunk->storage_next + size + 1) > chunk->this_size) { gsize new_size = nearest_power (chunk->default_size, size + 1); chunk->storage_list = g_slist_prepend (chunk->storage_list, g_new (gchar, new_size)); chunk->this_size = new_size; chunk->storage_next = 0; } pos = ((gchar *) chunk->storage_list->data) + chunk->storage_next; *(pos + size) = '\0'; strncpy (pos, string, size); if (len > 0) size = strlen (pos); chunk->storage_next += size + 1; return pos; }
void buff_grow( buff_t *b, size_t size ) { if( b->size + size >= b->cap ) { b->cap = nearest_power( b->cap + size + 1 ); b->buff = realloc( b->buff, b->cap ); } }