static void delete_lines_from_scrobble_log (GSList **lines_to_remove_ptr, gchar *queuepath) { GSList *lines_to_remove = *lines_to_remove_ptr; gchar *contents = NULL; gchar **lines = NULL; gchar **finallines = g_malloc_n(1, sizeof(gchar *)); int n_finallines; if (lines_to_remove == NULL) { return; } lines_to_remove = g_slist_reverse(lines_to_remove); pthread_mutex_lock(&log_access_mutex); gboolean success = g_file_get_contents(queuepath, &contents, NULL, NULL); if (!success) { AUDDBG("Could not read scrobbler.log contents.\n"); } else { lines = g_strsplit(contents, "\n", 0); n_finallines = 0; for (int i = 0 ; lines[i] != NULL && strlen(lines[i]) > 0; i++) { if (lines_to_remove != NULL && *((int *) (lines_to_remove->data)) == i) { //this line is to remove lines_to_remove = g_slist_next(lines_to_remove); } else { //keep this line n_finallines++; finallines = g_realloc_n(finallines, n_finallines, sizeof(gchar *)); finallines[n_finallines-1] = g_strdup(lines[i]); } } finallines = g_realloc_n(finallines, n_finallines+2, sizeof(gchar *)); finallines[n_finallines] = g_strdup(""); finallines[n_finallines+1] = NULL; g_free(contents); contents = g_strjoinv("\n", finallines); success = g_file_set_contents(queuepath, contents, -1, NULL); if (!success) { AUDDBG("Could not write to scrobbler.log!\n"); } } pthread_mutex_unlock(&log_access_mutex); g_strfreev(finallines); g_strfreev(lines); g_free(contents); }
static void nimf_settings_page_key_update_gsettings_strv (NimfSettingsPageKey *page_key, GtkTreeModel *model) { gchar **vals; GtkTreeIter iter; gint i; vals = g_malloc0_n (1, sizeof (gchar *)); if (gtk_tree_model_get_iter_first (model, &iter)) { i = 0; do { gtk_tree_model_get (model, &iter, 0, &vals[i], -1); vals = g_realloc_n (vals, sizeof (gchar *), i + 2); vals[i + 1] = NULL; i++; } while (gtk_tree_model_iter_next (model, &iter)); } g_settings_set_strv (page_key->gsettings, page_key->key, (const gchar **) vals); g_strfreev (vals); }
static inline libvlc_instance_t * create_vlc_instance(void) { gchar **vlc_argv; gint vlc_argc = 0; libvlc_instance_t *ret; vlc_argv = g_malloc_n(2, sizeof(vlc_argv[0])); vlc_argv[vlc_argc++] = g_strdup(g_get_prgname()); #if LIBVLC_VERSION_INT < LIBVLC_VERSION(2,0,0,0) if (g_getenv("VLC_PLUGIN_PATH") != NULL) { vlc_argv = g_realloc_n(vlc_argv, vlc_argc + 2 + 1, sizeof(vlc_argv[0])); vlc_argv[vlc_argc++] = g_strdup("--plugin-path"); vlc_argv[vlc_argc++] = g_strdup(g_getenv("VLC_PLUGIN_PATH")); } #endif vlc_argv[vlc_argc] = NULL; ret = libvlc_new((int)vlc_argc, (const char *const *)vlc_argv); g_strfreev(vlc_argv); return ret; }
void *g_try_realloc_n(void *ptr, size_t nmemb, size_t size) { int nomem; if (nomem) { return NULL; } return g_realloc_n(ptr, nmemb, size); }
/** * irc_strv_append: * @array: (transfer full) (nullable): Array to append * @str: String to append * * If @array is %NULL new array is allocated. * If @str is %NULL nothing is appended. * * Returns: (transfer full): Newly re-allocated array. Free with g_strfreev() */ GStrv irc_strv_append (GStrv array, const char *str) { if (str == NULL) return array; const gsize len = array ? g_strv_length (array) : 0; GStrv new_array = g_realloc_n (array, len + 2, sizeof(char*)); new_array[len] = g_strdup (str); new_array[len + 1] = NULL; return new_array; }
/** * uber_buffer_set_size: * @buffer: A #UberBuffer. * @size: The number of elements that @buffer should contain. * * Resizes the circular buffer. * * Returns: None. * Side effects: None. */ void uber_buffer_set_size (UberBuffer *buffer, /* IN */ gint size) /* IN */ { gint count; g_return_if_fail(buffer != NULL); g_return_if_fail(size > 0); if (size == buffer->len) { return; } if (size > buffer->len) { buffer->buffer = g_realloc_n(buffer->buffer, size, sizeof(gdouble)); uber_buffer_clear_range(buffer, buffer->len, size); if ((count = buffer->len - buffer->pos)) { memmove(&buffer->buffer[size - count], &buffer->buffer[buffer->pos], count * sizeof(gdouble)); if (size - count > buffer->pos) { uber_buffer_clear_range(buffer, 0, size - count - buffer->pos); } } buffer->len = size; return; } if (size >= buffer->pos) { memmove(&buffer->buffer[buffer->pos], &buffer->buffer[size], (buffer->len - size) * sizeof(gdouble)); buffer->buffer = g_realloc_n(buffer->buffer, size, sizeof(gdouble)); buffer->len = size; return; } memmove(buffer->buffer, &buffer->buffer[buffer->pos - size], size * sizeof(gdouble)); buffer->buffer = g_realloc_n(buffer->buffer, size, sizeof(gdouble)); buffer->pos = 0; buffer->len = size; }
static void torchat_parse_latency(struct im_connection *ic, char *address, char* line) { bee_user_t *bu = bee_user_by_handle(ic->bee, ic, address); int size = 1; char** argv = g_realloc_n(NULL, size, sizeof(char*)); char **pieces, **pieceptr, *piece; pieceptr = pieces = g_strsplit(line, " ", 0); while ((piece = *pieceptr++) && strlen(piece)) { argv = g_realloc_n(argv, ++size, sizeof(char*)); argv[size - 2] = piece; } argv[size - 1] = 0; imcb_buddy_action_response(bu, "PING", argv, NULL); g_free(argv); g_strfreev(pieces); }
static void ensure_seat_slot_allocated (ClutterSeatEvdev *seat, int seat_slot) { if (seat_slot >= seat->n_alloc_touch_states) { const int size_increase = 5; int i; seat->n_alloc_touch_states += size_increase; seat->touch_states = g_realloc_n (seat->touch_states, seat->n_alloc_touch_states, sizeof (ClutterTouchState *)); for (i = 0; i < size_increase; i++) seat->touch_states[seat->n_alloc_touch_states - (i + 1)] = NULL; } }
static int sqlfs_write(const char *path, const char *buf, size_t size, off_t offset, struct fuse_file_info *fi) { int err = 0; sqlfs_file_t *fsfile = g_hash_table_lookup(cache.open_table, &(fi->fh)); if (fsfile && fsfile->buffer) { size_t len = strlen(fsfile->buffer); if (len < size + offset) fsfile->buffer = g_realloc_n(fsfile->buffer, size + offset + 1, sizeof(gchar )); g_strlcpy(fsfile->buffer + offset, buf, size + 1); err = size; fsfile->flush = TRUE; } else err = -ENOENT; return err; }
void libwacom_update_match(WacomDevice *device, const WacomMatch *newmatch) { int i; for (i = 0; i < device->nmatches; i++) { const char *matchstr = libwacom_match_get_match_string(device->matches[i]); if (g_strcmp0(matchstr, newmatch->match) == 0) { device->match = i; return; } } device->nmatches++; device->matches = g_realloc_n(device->matches, device->nmatches + 1, sizeof(WacomMatch*)); device->matches[device->nmatches] = NULL; device->matches[device->nmatches - 1] = libwacom_copy_match(newmatch); device->match = device->nmatches - 1; }
/** * dfl_time_sequence_append: * @sequence: a #DflTimeSequence * @timestamp: TODO * * TODO * * Note that the pointers returned from dfl_time_sequence_append() may change * if dfl_time_sequence_append() is called again, due to internal reallocations. * The returned pointer is only valid until the next call to * dfl_time_sequence_append(). * * Returns: (transfer none): TODO * Since: 0.1.0 */ gpointer dfl_time_sequence_append (DflTimeSequence *sequence, DflTimestamp timestamp) { DflTimeSequenceReal *self = (DflTimeSequenceReal *) sequence; DflTimestamp last_timestamp; gpointer last_element; DflTimeSequenceElement *element; g_return_val_if_fail (sequence != NULL, NULL); g_return_val_if_fail (self->n_elements_valid < G_MAXSIZE, NULL); /* Check @timestamp is monotonically increasing. */ last_element = dfl_time_sequence_get_last_element (sequence, &last_timestamp); g_return_val_if_fail (last_element == NULL || timestamp >= last_timestamp, NULL); /* Do we need to expand the array first? */ if (self->n_elements_valid == self->n_elements_allocated) { self->n_elements_allocated = ((gsize) 1 << (g_bit_nth_msf (self->n_elements_allocated, -1) + 1)); self->elements = g_realloc_n (self->elements, self->n_elements_allocated, sizeof (DflTimeSequenceElement) + self->element_size); } g_assert (self->n_elements_allocated > self->n_elements_valid); /* Append the new element. */ self->n_elements_valid++; element = dfl_time_sequence_index (sequence, self->n_elements_valid - 1); element->timestamp = timestamp; return element->data; }
void *g_realloc(void *ptr, size_t size) { return g_realloc_n(ptr, 1, size); }
static void mem_overflow (void) { gsize a = G_MAXSIZE / 10 + 10; gsize b = 10; gpointer p, q; typedef char X[10]; #define CHECK_PASS(P) p = (P); g_assert (p == NULL); #define CHECK_FAIL(P) p = (P); g_assert (p != NULL); CHECK_PASS (g_try_malloc_n (a, a)); CHECK_PASS (g_try_malloc_n (a, b)); CHECK_PASS (g_try_malloc_n (b, a)); CHECK_FAIL (g_try_malloc_n (b, b)); CHECK_PASS (g_try_malloc0_n (a, a)); CHECK_PASS (g_try_malloc0_n (a, b)); CHECK_PASS (g_try_malloc0_n (b, a)); CHECK_FAIL (g_try_malloc0_n (b, b)); q = g_malloc (1); CHECK_PASS (g_try_realloc_n (q, a, a)); CHECK_PASS (g_try_realloc_n (q, a, b)); CHECK_PASS (g_try_realloc_n (q, b, a)); CHECK_FAIL (g_try_realloc_n (q, b, b)); free (p); CHECK_PASS (g_try_new (X, a)); CHECK_FAIL (g_try_new (X, b)); CHECK_PASS (g_try_new0 (X, a)); CHECK_FAIL (g_try_new0 (X, b)); q = g_try_malloc (1); CHECK_PASS (g_try_renew (X, q, a)); CHECK_FAIL (g_try_renew (X, q, b)); free (p); #undef CHECK_FAIL #undef CHECK_PASS #define CHECK_FAIL(P) if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR)) { p = (P); exit (0); } g_test_trap_assert_failed(); #define CHECK_PASS(P) if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR)) { p = (P); exit (0); } g_test_trap_assert_passed(); CHECK_FAIL (g_malloc_n (a, a)); CHECK_FAIL (g_malloc_n (a, b)); CHECK_FAIL (g_malloc_n (b, a)); CHECK_PASS (g_malloc_n (b, b)); CHECK_FAIL (g_malloc0_n (a, a)); CHECK_FAIL (g_malloc0_n (a, b)); CHECK_FAIL (g_malloc0_n (b, a)); CHECK_PASS (g_malloc0_n (b, b)); q = g_malloc (1); CHECK_FAIL (g_realloc_n (q, a, a)); CHECK_FAIL (g_realloc_n (q, a, b)); CHECK_FAIL (g_realloc_n (q, b, a)); CHECK_PASS (g_realloc_n (q, b, b)); free (q); CHECK_FAIL (g_new (X, a)); CHECK_PASS (g_new (X, b)); CHECK_FAIL (g_new0 (X, a)); CHECK_PASS (g_new0 (X, b)); q = g_malloc (1); CHECK_FAIL (g_renew (X, q, a)); CHECK_PASS (g_renew (X, q, b)); free (q); }
static void gimp_color_history_set_property (GObject *object, guint property_id, const GValue *value, GParamSpec *pspec) { GimpColorHistory *history = GIMP_COLOR_HISTORY (object); switch (property_id) { case PROP_CONTEXT: history->context = g_value_get_object (value); break; case PROP_HISTORY_SIZE: { GtkWidget *button; gint i; /* Destroy previous color buttons. */ gtk_container_foreach (GTK_CONTAINER (history), (GtkCallback) gtk_widget_destroy, NULL); history->history_size = g_value_get_int (value); gtk_table_resize (GTK_TABLE (history), 2, (history->history_size + 1)/ 2); gtk_table_set_row_spacings (GTK_TABLE (history), 2); gtk_table_set_col_spacings (GTK_TABLE (history), 2); history->color_areas = g_realloc_n (history->color_areas, history->history_size, sizeof (GtkWidget*)); for (i = 0; i < history->history_size; i++) { GimpRGB black = { 0.0, 0.0, 0.0, 1.0 }; gint row, column; column = i % (history->history_size / 2); row = i / (history->history_size / 2); button = gtk_button_new (); gtk_widget_set_size_request (button, COLOR_AREA_SIZE, COLOR_AREA_SIZE); gtk_table_attach_defaults (GTK_TABLE (history), button, column, column + 1, row, row + 1); gtk_widget_show (button); history->color_areas[i] = gimp_color_area_new (&black, GIMP_COLOR_AREA_SMALL_CHECKS, GDK_BUTTON2_MASK); gtk_container_add (GTK_CONTAINER (button), history->color_areas[i]); gtk_widget_show (history->color_areas[i]); g_signal_connect (button, "clicked", G_CALLBACK (gimp_color_history_color_clicked), history); g_signal_connect (history->color_areas[i], "color-changed", G_CALLBACK (gimp_color_history_color_changed), GINT_TO_POINTER (i)); } } break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); break; } }