static void test_profiler (void) { if (g_test_subprocess ()) { gpointer p; g_mem_set_vtable (glib_mem_profiler_table); p = g_malloc (100); p = g_realloc (p, 200); g_free (p); p = g_malloc0 (1000); g_free (p); p = g_try_malloc (2000); p = g_try_realloc (p, 3000); g_free (p); p = g_malloc (0); p = g_malloc0 (0); p = g_realloc (NULL, 0); p = g_try_malloc (0); p = g_try_realloc (NULL, 0); g_mem_profile (); exit (0); } g_test_trap_subprocess (NULL, 0, 0); g_test_trap_assert_passed (); g_test_trap_assert_stdout ("*GLib Memory statistics*"); }
static gboolean save_to_buffer_callback ( const gchar *data, gsize count, GError **error, gpointer user_data ) { struct SaveToBufferData *sdata = user_data; gchar *new_buffer; gsize new_max; if (sdata->len + count > sdata->max) { new_max = sdata->len + count + 16; new_buffer = g_try_realloc (sdata->buffer, new_max); if (!new_buffer) { /*Insufficient memory to save image into a buffer*/ return FALSE; } sdata->buffer = new_buffer; sdata->max = new_max; } memcpy (sdata->buffer + sdata->len, data, count); sdata->len += count; return TRUE; }
static void* buffer_g_realloc(void *mem, gsize bytes, void *allocator_data) { return g_try_realloc(mem, bytes); }
static gboolean gth_buffer_data_alloc_new_space (GthBufferData *buffer_data, gsize len, GError **error) { if (buffer_data->current_pos + len > buffer_data->buffer_size) buffer_data->buffer_size = buffer_data->current_pos + len; if (buffer_data->buffer_size > buffer_data->allocated_size) { gsize new_allocated_size; char *new_buffer; new_allocated_size = MAX (buffer_data->allocated_size * 2, buffer_data->buffer_size + len); new_buffer = g_try_realloc (buffer_data->buffer, new_allocated_size); if (new_buffer == NULL) { g_set_error_literal (error, GTH_ERROR, GTH_ERROR_GENERIC, _("Insufficient memory")); return FALSE; } buffer_data->buffer = new_buffer; buffer_data->allocated_size = new_allocated_size; } return TRUE; }
static int _sr_session_source_remove(gintptr poll_object) { struct source *new_sources; GPollFD *new_pollfds; unsigned int old; if (!session->sources || !session->num_sources) { sr_err("session: %s: sources was NULL", __func__); return SR_ERR_BUG; } for (old = 0; old < session->num_sources; old++) { if (session->sources[old].poll_object == poll_object) break; } /* fd not found, nothing to do */ if (old == session->num_sources) return SR_OK; session->num_sources -= 1; if (old != session->num_sources) { memmove(&session->pollfds[old], &session->pollfds[old+1], (session->num_sources - old) * sizeof(GPollFD)); memmove(&session->sources[old], &session->sources[old+1], (session->num_sources - old) * sizeof(struct source)); } new_pollfds = g_try_realloc(session->pollfds, sizeof(GPollFD) * session->num_sources); if (!new_pollfds && session->num_sources > 0) { sr_err("session: %s: new_pollfds malloc failed", __func__); return SR_ERR_MALLOC; } new_sources = g_try_realloc(session->sources, sizeof(struct source) * session->num_sources); if (!new_sources && session->num_sources > 0) { sr_err("session: %s: new_sources malloc failed", __func__); return SR_ERR_MALLOC; } session->pollfds = new_pollfds; session->sources = new_sources; return SR_OK; }
int store_proxy_input(DBusConnection *connection, DBusMessage *message, char *name, int num_args, char *argv[]) { int i, j, k; int error = 0; gchar **servers = NULL; gchar **excludes = NULL; for (i = 0; strcmp(argv[i], "excludes") != 0; i++) { servers = g_try_realloc(servers, (i + 1) * sizeof(char *)); if (servers == NULL) { fprintf(stderr, "Could not allocate memory for list\n"); return -ENOMEM; } servers[i] = g_strdup(argv[i]); /* In case the user doesn't enter "excludes" */ if (i + 1 == num_args) { i++; j = 0; goto free_servers; } } for (j = 0; j + (i + 1) != num_args; j++) { excludes = g_try_realloc(excludes, (j + 1) * sizeof(char *)); if (excludes == NULL) { fprintf(stderr, "Could not allocate memory for list\n"); return -ENOMEM; } excludes[j] = g_strdup(argv[j + (i + 1)]); } free_servers: error = set_proxy_manual(connection, message, name, servers, excludes, i, j); for (k = 0; k < j - 1; k++) g_free(excludes[k]); g_free(excludes); for (k = 0; k < i - 1; k++) g_free(servers[k]); g_free(servers); return error; }
void * jb_try_realloc (void *p, const int n) { register void *q; q = g_try_realloc (p, n); if (!q) g_free (p); return q; }
/* Parse a DN and return an array-ized one. This is not a validating parser and it does not support any old-stylish syntax; KSBA is expected to return only rfc2253 compatible strings. */ static struct dn_array_s * parse_dn (const char *string) { struct dn_array_s *array; size_t arrayidx, arraysize; int i; arraysize = 7; /* C,ST,L,O,OU,CN,email */ arrayidx = 0; array = g_try_malloc ((arraysize+1) * sizeof *array); if (!array) return NULL; while (*string) { while (*string == ' ') string++; if (!*string) break; /* Ready. */ if (arrayidx >= arraysize) { struct dn_array_s *a2; arraysize += 5; a2 = g_try_realloc (array, (arraysize+1) * sizeof *array); if (!a2) goto failure; array = a2; } array[arrayidx].key = NULL; array[arrayidx].value = NULL; string = parse_dn_part (array+arrayidx, string); if (!string) goto failure; while (*string == ' ') string++; array[arrayidx].multivalued = (*string == '+'); array[arrayidx].done = 0; arrayidx++; if (*string && *string != ',' && *string != ';' && *string != '+') goto failure; /* Invalid delimiter. */ if (*string) string++; } array[arrayidx].key = NULL; array[arrayidx].value = NULL; return array; failure: for (i=0; i < arrayidx; i++) { g_free (array[i].key); g_free (array[i].value); } g_free (array); return NULL; }
/* * resize_cache: * @stream: A #CtplInputStream * @new_size: the requested new size of the stream's cache * @error: Return location for errors, or %NULL to ignore them * * Tries to resize the cache of a #CtplInputStream to @new_size. This may grow * or shrink the cache; but it will never loose unread content. * Note that the new cache size might not be the requested one even on success: * there can be too much still useful cached data or the underlying stream can * be too close to the end to fill the full size. In both cases, the cache will * be resized to the closest size possible to the request. * * Returns: %TRUE on success, %FALSE otherwise */ static gboolean resize_cache (CtplInputStream *stream, gsize new_size, GError **error) { gboolean success = TRUE; g_return_val_if_fail (new_size > 0, FALSE); if (new_size > stream->buf_size) { gssize read_size; gchar *new_buffer; new_buffer = g_try_realloc (stream->buffer, new_size); if (G_UNLIKELY (! new_buffer)) { g_set_error (error, G_IO_ERROR, G_IO_ERROR_NO_SPACE, "Not enough memory to cache %"G_GSIZE_FORMAT" bytes " "from input", new_size); success = FALSE; } else { stream->buffer = new_buffer; read_size = g_input_stream_read (stream->stream, &stream->buffer[stream->buf_size], new_size - stream->buf_size, NULL, error); if (read_size < 0) { success = FALSE; } else { stream->buf_size += (gsize)read_size; } } } else if (new_size < stream->buf_size) { if (stream->buf_pos >= stream->buf_size) { /* we are at the end of the buffer, no need to care about its content, * just retrieve next data */ stream->buf_size = new_size; stream->buffer = g_realloc (stream->buffer, stream->buf_size); success = ensure_cache_filled (stream, error); } else { gsize new_start = stream->buf_size - new_size; if (new_start > stream->buf_pos) { /* we have too much data in the buffer, cannot shrink to the requested * size; shrink as much as we can */ new_start = stream->buf_size - stream->buf_pos; new_size = stream->buf_size - new_start; } /* OK, move the data and resize */ memmove (stream->buffer, &stream->buffer[new_start], new_size); stream->buf_size = new_size; stream->buffer = g_realloc (stream->buffer, stream->buf_size); } } return success; }
/** * g_try_realloc_n: * @mem: (nullable): previously-allocated memory, or %NULL. * @n_blocks: the number of blocks to allocate * @n_block_bytes: the size of each block in bytes * * This function is similar to g_try_realloc(), allocating (@n_blocks * @n_block_bytes) bytes, * but care is taken to detect possible overflow during multiplication. * * Since: 2.24 * Returns: the allocated memory, or %NULL. */ gpointer g_try_realloc_n (gpointer mem, gsize n_blocks, gsize n_block_bytes) { if (SIZE_OVERFLOWS (n_blocks, n_block_bytes)) return NULL; return g_try_realloc (mem, n_blocks * n_block_bytes); }
/** * Add an event source for a file descriptor. * * @param pollfd The GPollFD. * @param timeout Max time to wait before the callback is called, ignored if 0. * @param cb Callback function to add. Must not be NULL. * @param cb_data Data for the callback function. Can be NULL. * @param poll_object TODO. * * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or * SR_ERR_MALLOC upon memory allocation errors. */ static int _sr_session_source_add(GPollFD *pollfd, int timeout, sr_receive_data_callback_t cb, void *cb_data, gintptr poll_object) { struct source *new_sources, *s; GPollFD *new_pollfds; if (!cb) { sr_err("%s: cb was NULL", __func__); return SR_ERR_ARG; } /* Note: cb_data can be NULL, that's not a bug. */ new_pollfds = g_try_realloc(session->pollfds, sizeof(GPollFD) * (session->num_sources + 1)); if (!new_pollfds) { sr_err("%s: new_pollfds malloc failed", __func__); return SR_ERR_MALLOC; } new_sources = g_try_realloc(session->sources, sizeof(struct source) * (session->num_sources + 1)); if (!new_sources) { sr_err("%s: new_sources malloc failed", __func__); return SR_ERR_MALLOC; } new_pollfds[session->num_sources] = *pollfd; s = &new_sources[session->num_sources++]; s->timeout = timeout; s->cb = cb; s->cb_data = cb_data; s->poll_object = poll_object; session->pollfds = new_pollfds; session->sources = new_sources; if (timeout != session->source_timeout && timeout > 0 && (session->source_timeout == -1 || timeout < session->source_timeout)) session->source_timeout = timeout; return SR_OK; }
void UT_ByteBuf::truncate(UT_uint32 position) { if (position < m_iSize) m_iSize = position; UT_uint32 newSpace = ((m_iSize+m_iChunk-1)/m_iChunk)*m_iChunk; //Calculate the new space needed if (newSpace != m_iSpace) { m_pBuf = static_cast<UT_Byte *>(g_try_realloc(m_pBuf, newSpace*sizeof(*m_pBuf))); //Re-allocate to the smaller size m_iSpace = newSpace; //update m_iSpace to the new figure } }
static int characteristic_set_value(struct characteristic *chr, const uint8_t *value, size_t vlen) { chr->value = g_try_realloc(chr->value, vlen); if (chr->value == NULL) return -ENOMEM; memcpy(chr->value, value, vlen); chr->vlen = vlen; return 0; }
static const char* guess_type_glib(const char* path) { gboolean uncertain = FALSE; const char* content_type = g_content_type_guess(path, NULL, 0, &uncertain); if (content_type == NULL) { girara_debug("g_content_type failed\n"); } else { if (uncertain == FALSE) { girara_debug("g_content_type detected filetype: %s", content_type); return content_type; } girara_debug("g_content_type is uncertain, guess: %s", content_type); } FILE* f = fopen(path, "rb"); if (f == NULL) { return NULL; } const int fd = fileno(f); guchar* content = NULL; size_t length = 0u; ssize_t bytes_read = -1; while (uncertain == TRUE && length < GT_MAX_READ && bytes_read != 0) { g_free((void*)content_type); content_type = NULL; guchar* temp_content = g_try_realloc(content, length + BUFSIZ); if (temp_content == NULL) { break; } content = temp_content; bytes_read = read(fd, content + length, BUFSIZ); if (bytes_read == -1) { break; } length += bytes_read; content_type = g_content_type_guess(NULL, content, length, &uncertain); girara_debug("new guess: %s uncertain: %d, read: %zu", content_type, uncertain, length); } fclose(f); g_free(content); if (uncertain == FALSE) { return content_type; } g_free((void*)content_type); return NULL; }
static inline gboolean gst_jpeg_dec_ensure_buffers (GstJpegDec * dec, guint maxrowbytes) { gint i; if (G_LIKELY (dec->idr_width_allocated == maxrowbytes)) return TRUE; /* FIXME: maybe just alloc one or three blocks altogether? */ for (i = 0; i < 16; i++) { dec->idr_y[i] = g_try_realloc (dec->idr_y[i], maxrowbytes); dec->idr_u[i] = g_try_realloc (dec->idr_u[i], maxrowbytes); dec->idr_v[i] = g_try_realloc (dec->idr_v[i], maxrowbytes); if (G_UNLIKELY (!dec->idr_y[i] || !dec->idr_u[i] || !dec->idr_v[i])) { GST_WARNING_OBJECT (dec, "out of memory, i=%d, bytes=%u", i, maxrowbytes); return FALSE; } } dec->idr_width_allocated = maxrowbytes; GST_LOG_OBJECT (dec, "allocated temp memory, %u bytes/row", maxrowbytes); return TRUE; }
static gboolean grow_list (dir_list * list) { if (list == NULL) return FALSE; list->list = g_try_realloc (list->list, sizeof (file_entry) * (list->size + RESIZE_STEPS)); if (list->list == NULL) return FALSE; list->size += RESIZE_STEPS; return TRUE; }
void UT_GrowBuf::truncate(UT_uint32 position) { if ((m_pBuf == 0) && (position == 0)) return; if (position < m_iSize) m_iSize = position; UT_uint32 newSpace = ((m_iSize+m_iChunk-1)/m_iChunk)*m_iChunk; //Calculate the new space needed if (newSpace == 0) newSpace = m_iChunk; // In case of UT_GrowBuf::truncate (0) if (newSpace != m_iSpace) { m_pBuf = static_cast<UT_GrowBufElement *>(g_try_realloc(m_pBuf, newSpace*sizeof(*m_pBuf))); //Re-allocate to the smaller size m_iSpace = newSpace; //update m_iSpace to the new figure } }
static void read_blob_helper(guint8 status, const guint8 *rpdu, guint16 rlen, gpointer user_data) { struct read_long_data *long_read = user_data; uint8_t *buf; int buflen; guint8 *tmp; guint16 plen; guint id; if (status != 0 || rlen == 1) { status = 0; goto done; } tmp = g_try_realloc(long_read->buffer, long_read->size + rlen - 1); if (tmp == NULL) { status = ATT_ECODE_INSUFF_RESOURCES; goto done; } memcpy(&tmp[long_read->size], &rpdu[1], rlen - 1); long_read->buffer = tmp; long_read->size += rlen - 1; buf = g_attrib_get_buffer(long_read->attrib, &buflen); if (rlen < buflen) goto done; plen = enc_read_blob_req(long_read->handle, long_read->size - 1, buf, buflen); id = g_attrib_send(long_read->attrib, long_read->id, ATT_OP_READ_BLOB_REQ, buf, plen, read_blob_helper, long_read, read_long_destroy); if (id != 0) { g_atomic_int_inc(&long_read->ref); return; } status = ATT_ECODE_IO; done: long_read->func(status, long_read->buffer, long_read->size, long_read->user_data); }
static tsize_t write_to_stream(thandle_t handle, tdata_t buffer, tsize_t size) { Priv *p = (Priv*) handle; GError *error = NULL; gchar *new_buffer; gsize new_size; gssize written = -1; g_assert(p->stream); if (p->can_seek) { written = g_output_stream_write(G_OUTPUT_STREAM(p->stream), (void *) buffer, (gsize) size, NULL, &error); if (written < 0) { g_warning("%s", error->message); g_error_free(error); } } else { if (p->position + size > p->allocated) { new_size = p->position + size; new_buffer = g_try_realloc(p->buffer, new_size); if (!new_buffer) return -1; p->allocated = new_size; p->buffer = new_buffer; } g_assert(p->position + size >= p->allocated); memcpy(p->buffer + p->position, buffer, size); p->position += size; written = size; } return (tsize_t) written; }
bool XAP_ResourceManager::grow () { if (m_resource_count < m_resource_max) return true; if (m_resource == 0) { m_resource = static_cast<XAP_Resource **>(g_try_malloc (8 * sizeof (XAP_Resource *))); if (m_resource == 0) return false; m_resource_max = 8; return true; } XAP_Resource ** more = static_cast<XAP_Resource **>(g_try_realloc (m_resource, (m_resource_max + 8) * sizeof (XAP_Resource *))); if (more == 0) return false; m_resource = more; m_resource_max += 8; return true; }
bool UT_XML::grow (char *& buffer, UT_uint32 & length, UT_uint32 & max, UT_uint32 require) { if (length + require + 1 <= max) return true; if (buffer == 0) { buffer = static_cast<char *>(g_try_malloc (require + 1)); if (buffer == 0) return false; buffer[0] = 0; max = require + 1; return true; } char * more = static_cast<char *>(g_try_realloc (buffer, max + require + 1)); if (more == 0) return false; buffer = more; max += require + 1; return true; }
static gboolean alloc_buffer (GIOPRecvBuffer *buf, gpointer old_alloc, gulong body_size) { buf->message_body = g_try_realloc (old_alloc, body_size + 12); if (!buf->message_body) return TRUE; /* * We assume that this is 8 byte aligned, for efficiency - * so we can align to the memory address rather than the offset * into the buffer. */ g_assert (((gulong)buf->message_body & 0x3) == 0); buf->free_body = TRUE; buf->cur = buf->message_body + 12; buf->end = buf->cur + body_size; buf->left_to_read = body_size; return FALSE; }
/** * gst_byte_writer_ensure_free_space: * @writer: #GstByteWriter instance * @size: Number of bytes that should be available * * Checks if enough free space from the current write cursor is * available and reallocates if necessary. * * Returns: %TRUE if at least @size bytes are still available * * Since: 0.10.26 */ gboolean gst_byte_writer_ensure_free_space (GstByteWriter * writer, guint size) { guint8 *data; if (G_UNLIKELY (size <= writer->alloc_size - writer->parent.byte)) return TRUE; if (G_UNLIKELY (writer->fixed || !writer->owned)) return FALSE; if (G_UNLIKELY (writer->parent.byte > G_MAXUINT - size)) return FALSE; writer->alloc_size = _next_pow2 (writer->parent.byte + size); data = g_try_realloc ((guint8 *) writer->parent.data, writer->alloc_size); if (G_UNLIKELY (data == NULL)) return FALSE; writer->parent.data = data; return TRUE; }
/* * Check that all reallocs return no errors */ gpointer grg_realloc(gpointer ptr, gulong length) { gpointer ret = g_try_realloc(ptr, length); #ifdef MAINTAINER_MODE // warn if a realloc(0) is attempted if (!length) g_warning("zero-length realloc() requested!"); #endif if (ret) return ret; printf("error: malloc failed. Probably this indicates a memory " "problem, such as resource exhaustion. Attempting " "to exit cleanly..."); emergency_quit(); return ret; }
/** * mate_keyring_memory_try_realloc: * @p: The pointer to reallocate or NULL to allocate a new block. * @sz: The new desired size of the memory block. * * Reallocate a block of mate-keyring non-pageable memory. * * Glib memory is also reallocated correctly when passed to this function. * If called with a null pointer, then a new block of memory is allocated. * If called with a zero size, then the block of memory is freed. * * If memory cannot be allocated, NULL is returned and the original block * of memory remains intact. * * Return value: The new block, or NULL if memory cannot be allocated. * The memory block should be freed with mate_keyring_memory_free() */ gpointer mate_keyring_memory_try_realloc (gpointer p, gulong sz) { gpointer n; if (!p) { return mate_keyring_memory_try_alloc (sz); } else if (!sz) { mate_keyring_memory_free (p); return NULL; } else if (!egg_secure_check (p)) { return g_try_realloc (p, sz); } /* First try and ask secure memory to reallocate */ n = egg_secure_realloc_full (p, sz, 0); g_assert (n); return n; }
static gint accept_add_any (ACCEPT_POOL ap, int srv, GError **err) { gint rc = 0; if (!ap || srv<0) { GSETERROR (err, "Invalid parameter"); return 0; } g_rec_mutex_lock (&(ap->mut)); /*allocates an array if it is necessary*/ if (!ap->srv) { GSETERROR(err, "AcceptPool not initialized (should not happen)"); goto exitLabel; } /*make the arrays grows if it is too small*/ if (ap->size <= ap->count) { gint *newSrv, newSize; newSize = ap->size + 2; newSrv = g_try_realloc (ap->srv, sizeof(gint) * newSize); if (!newSrv) { GSETERROR(err, "Memory allocation error (%s)", strerror(errno)); goto exitLabel; } memset (newSrv+ap->size, 0x00, sizeof(gint) * 2); ap->size = newSize; ap->srv = newSrv; } ap->srv [ap->count++] = srv; rc = 1; exitLabel: g_rec_mutex_unlock (&(ap->mut)); return rc; }
GHashTable * meta0_info_list_map_by_addr(GSList * mL, GError ** err) { GSList *l; GHashTable *mH; (void) err; mH = g_hash_table_new_full(addr_info_hash, addr_info_equal, NULL, func_free_meta0); if (!mH) { errno = ENOMEM; return NULL; } for (l = mL; l; l = l->next) { meta0_info_t *arg, *m0i; if (!(l->data)) continue; arg = (meta0_info_t *) l->data; m0i = g_hash_table_lookup(mH, arg); if (m0i) { /*appends the prefix */ guint8 *b = g_try_realloc(m0i->prefixes, m0i->prefixes_size + arg->prefixes_size); if (b) { m0i->prefixes = b; g_memmove(m0i->prefixes + m0i->prefixes_size, arg->prefixes, arg->prefixes_size); m0i->prefixes_size += arg->prefixes_size; } } else { /*insert a copy */ m0i = meta0_info_copy(arg); g_hash_table_insert(mH, m0i, m0i); } } errno = 0; return mH; }
static gboolean make_available_at_least (TiffContext *context, guint needed) { guchar *new_buffer = NULL; guint need_alloc; need_alloc = context->used + needed; if (need_alloc > context->allocated) { guint new_size = 1; while (new_size < need_alloc) new_size *= 2; new_buffer = g_try_realloc (context->buffer, new_size); if (new_buffer) { context->buffer = new_buffer; context->allocated = new_size; return TRUE; } return FALSE; } return TRUE; }
bool UT_GrowBuf::del(UT_uint32 position, UT_uint32 amount) { if (!amount) return true; if (!m_pBuf) return false; UT_ASSERT(position < m_iSize); UT_ASSERT(position+amount <= m_iSize); memmove(m_pBuf+position,m_pBuf+position+amount,(m_iSize-position-amount)*sizeof(*m_pBuf)); m_iSize -= amount; UT_uint32 newSpace = ((m_iSize+m_iChunk-1)/m_iChunk)*m_iChunk; //Calculate the new space needed if (newSpace != m_iSpace) { m_pBuf = static_cast<UT_GrowBufElement *>(g_try_realloc(m_pBuf, newSpace*sizeof(*m_pBuf))); //Re-allocate to the smaller size m_iSpace = newSpace; //update m_iSpace to the new figure } return true; }
static int insert_text (WInput * in, char *text, ssize_t size) { int buff_len = str_length (in->buffer); size = min (size, (ssize_t) strlen (text)) + start - end; if (strlen (in->buffer) + size >= (size_t) in->current_max_size) { /* Expand the buffer */ char *narea = g_try_realloc (in->buffer, in->current_max_size + size + in->field_width); if (narea != NULL) { in->buffer = narea; in->current_max_size += size + in->field_width; } } if (strlen (in->buffer) + 1 < (size_t) in->current_max_size) { if (size > 0) { int i = strlen (&in->buffer[end]); for (; i >= 0; i--) in->buffer[end + size + i] = in->buffer[end + i]; } else if (size < 0) { char *p = in->buffer + end + size, *q = in->buffer + end; while (*q) *(p++) = *(q++); *p = 0; } memcpy (in->buffer + start, text, size - start + end); in->point += str_length (in->buffer) - buff_len; input_update (in, TRUE); end += size; } return size != 0; }