static void plugin_insert_hook (hexchat_hook *new_hook) { GSList *list; hexchat_hook *hook; int new_hook_type; switch (new_hook->type) { case HOOK_PRINT: case HOOK_PRINT_ATTRS: new_hook_type = HOOK_PRINT | HOOK_PRINT_ATTRS; break; case HOOK_SERVER: case HOOK_SERVER_ATTRS: new_hook_type = HOOK_SERVER | HOOK_PRINT_ATTRS; break; default: new_hook_type = new_hook->type; } list = hook_list; while (list) { hook = list->data; if (hook && (hook->type & new_hook_type) && hook->pri <= new_hook->pri) { hook_list = g_slist_insert_before (hook_list, list, new_hook); return; } list = list->next; } hook_list = g_slist_append (hook_list, new_hook); }
void action_database_insert(ActionDatabase* database,Action* action){ GSList* list = g_hash_table_lookup(database->hash_table,action->name); list = g_slist_insert_before(list,list,action); GSList* cut = g_slist_nth(list,8000); if ( cut!= NULL && cut->next != NULL){ GSList* after_cut = g_slist_next(cut); action_list_free(after_cut); cut->next = NULL; } g_hash_table_replace(database->hash_table,action->name,list); }
RESULT test_slist_insert_before () { GSList *foo, *bar, *baz; foo = g_slist_prepend (NULL, "foo"); foo = g_slist_insert_before (foo, NULL, "bar"); bar = g_slist_last (foo); if (strcmp (bar->data, "bar")) return FAILED ("1"); baz = g_slist_insert_before (foo, bar, "baz"); if (foo != baz) return FAILED ("2"); if (strcmp (foo->next->data, "baz")) return FAILED ("3: %s", foo->next->data); g_slist_free (foo); return OK; }
/** * adg_table_row_insert: * @table_row: a valid #AdgTableRow * @table_cell: the #AdgTableCell to insert * @before_cell: (allow-none): an #AdgTableRow * * Inserts @table_cell inside @table_row. If @before_cell * is specified, @table_cell is inserted before it. * * Since: 1.0 **/ void adg_table_row_insert(AdgTableRow *table_row, AdgTableCell *table_cell, AdgTableCell *before_cell) { g_return_if_fail(table_row != NULL); g_return_if_fail(table_cell != NULL); if (before_cell == NULL) { table_row->cells = g_slist_append(table_row->cells, table_cell); } else { GSList *before = g_slist_find(table_row->cells, before_cell); /* This MUST be present, otherwise something really bad happened */ g_return_if_fail(before != NULL); table_row->cells = g_slist_insert_before(table_row->cells, before, table_cell); } }
static void plugin_insert_hook(xchat_hook *new_hook) { GSList *list; xchat_hook *hook; list = hook_list; while (list) { hook = (xchat_hook*)list->data; if (hook->type == new_hook->type && hook->pri <= new_hook->pri) { hook_list = g_slist_insert_before(hook_list, list, new_hook); return; } list = list->next; } hook_list = g_slist_append(hook_list, new_hook); }
/* * helper_find_peek: * @data: helper data struct * @off: stream offset * @size: block size * * Get data pointer within a stream. Keeps a cache of read buffers (partly * for performance reasons, but mostly because pointers returned by us need * to stay valid until typefinding has finished) * * Returns: address of the data or %NULL if buffer does not cover the * requested range. */ static guint8 * helper_find_peek (gpointer data, gint64 offset, guint size) { GstTypeFindHelper *helper; GstBuffer *buffer; GstFlowReturn ret; GSList *insert_pos = NULL; guint buf_size; guint64 buf_offset; GstCaps *caps; helper = (GstTypeFindHelper *) data; GST_LOG_OBJECT (helper->obj, "'%s' called peek (%" G_GINT64_FORMAT ", %u)", GST_PLUGIN_FEATURE_NAME (helper->factory), offset, size); if (size == 0) return NULL; if (offset < 0) { if (helper->size == -1 || helper->size < -offset) return NULL; offset += helper->size; } /* see if we have a matching buffer already in our list */ if (size > 0 && offset <= helper->last_offset) { GSList *walk; for (walk = helper->buffers; walk; walk = walk->next) { GstBuffer *buf = GST_BUFFER_CAST (walk->data); guint64 buf_offset = GST_BUFFER_OFFSET (buf); guint buf_size = GST_BUFFER_SIZE (buf); /* buffers are kept sorted by end offset (highest first) in the list, so * at this point we save the current position and stop searching if * we're after the searched end offset */ if (buf_offset <= offset) { if ((offset + size) < (buf_offset + buf_size)) { return GST_BUFFER_DATA (buf) + (offset - buf_offset); } } else if (offset + size >= buf_offset + buf_size) { insert_pos = walk; break; } } } buffer = NULL; /* some typefinders go in 1 byte steps over 1k of data and request * small buffers. It is really inefficient to pull each time, and pulling * a larger chunk is almost free. Trying to pull a larger chunk at the end * of the file is also not a problem here, we'll just get a truncated buffer * in that case (and we'll have to double-check the size we actually get * anyway, see below) */ ret = helper->func (helper->obj, offset, MAX (size, 4096), &buffer); if (ret != GST_FLOW_OK) goto error; caps = GST_BUFFER_CAPS (buffer); if (caps && !gst_caps_is_empty (caps) && !gst_caps_is_any (caps)) { GST_DEBUG ("buffer has caps %" GST_PTR_FORMAT ", suggest max probability", caps); gst_caps_replace (&helper->caps, caps); helper->best_probability = GST_TYPE_FIND_MAXIMUM; gst_buffer_unref (buffer); return NULL; } /* getrange might silently return shortened buffers at the end of a file, * we must, however, always return either the full requested data or NULL */ buf_offset = GST_BUFFER_OFFSET (buffer); buf_size = GST_BUFFER_SIZE (buffer); if ((buf_offset != -1 && buf_offset != offset) || buf_size < size) { GST_DEBUG ("dropping short buffer: %" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT " instead of %" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT, buf_offset, buf_offset + buf_size - 1, offset, offset + size - 1); gst_buffer_unref (buffer); return NULL; } if (insert_pos) { helper->buffers = g_slist_insert_before (helper->buffers, insert_pos, buffer); } else { /* if insert_pos is not set, our offset is bigger than the largest offset * we have so far; since we keep the list sorted with highest offsets * first, we need to prepend the buffer to the list */ helper->last_offset = GST_BUFFER_OFFSET (buffer) + GST_BUFFER_SIZE (buffer); helper->buffers = g_slist_prepend (helper->buffers, buffer); } return GST_BUFFER_DATA (buffer); error: { GST_INFO ("typefind function returned: %s", gst_flow_get_name (ret)); return NULL; } }
// Tests for slist void tg_slist_tests() { GSList *slist,*st,*rem; gint nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; gint chk_buf[20]; gint i; gint g_slist_insert_data; gint g_slist_insert_before_data; gint ip1 = 10; gint ip2 = 15; gint ip3 = 5; gint ip4 = 12; gint g_slist_nth_data_op,g_slist_find_custom_op; //Trying to use the allocators so that even they get tested! GAllocator* alloc = g_allocator_new ("alloc_slist",5000); g_slist_push_allocator (alloc); slist = NULL; for (i = 0; i < 10; i++) slist = g_slist_append (slist, &nums[i]); //List looks like: // 0 1 2 3 4 5 6 7 8 9 //Test for g_slist_insert....inserted 10 at pos 4 g_slist_insert(slist,&ip1,4); st = g_slist_nth (slist,0); for(i = 0;i < 4;i++) st = st->next; g_slist_insert_data = *((gint*) st->data); g_assert(g_slist_insert_data == 10); /* for (i = 0; i < 10; i++) { st = g_slist_nth (slist, i); chk_buf[i] = *((gint*) st->data); }*/ //List looks like: // 0 1 2 3 10 4 5 6 7 8 9 //Test for g_slist_insert_before....inserted 15 at pos 7 st = g_slist_nth (slist,7); g_slist_insert_before(slist,st,&ip2); st = g_slist_nth (slist,0); for(i = 0;i < 7;i++) st = st->next; g_slist_insert_before_data = *((gint*) st->data); g_assert(g_slist_insert_before_data == 15); //List looks like: // 0 1 2 3 10 4 5 15 6 7 8 9 //Test for g_slist_index....finding 15 at pos 7 st = g_slist_nth (slist,0); g_assert(g_slist_index(st,&ip2)==7); //Test for g_slist_nth_data....getting 6 at position 8 g_slist_nth_data_op = *((gint*) g_slist_nth_data(slist,8)); g_assert(g_slist_nth_data_op == 6) ; //Test for g_slist_position st = g_slist_nth (slist,7); g_assert(g_slist_position (slist,st) == 7); //Test for g_slist_find_custom st = g_slist_find_custom(slist,&ip3,compare_fun_gr); g_slist_find_custom_op = *((gint*) st->data); g_assert(g_slist_find_custom_op == 5); //Test for g_slist_sort_with_data st = g_slist_sort_with_data(slist,compare_fun_gr_data,&ip3); for (i = 0; i < 10; i++) { st = g_slist_nth (slist, i); g_assert (*((gint*) st->data) == i); } //List looks like: // 0 1 2 3 4 5 6 7 8 9 10 15 //Test for g_slist_remove_link st = g_slist_nth (slist, 5); rem = g_slist_remove_link(slist , st); st = g_slist_nth (slist, 5); g_assert (*((gint*) st->data) == 6); //List looks like: // 0 1 2 3 4 6 7 8 9 10 15 //Test for g_slist_remove_all g_slist_insert(slist,&ip4,4); g_slist_insert(slist,&ip4,6); g_slist_insert(slist,&ip4,8); //List looks like: // 0 1 2 3 4 12 6 7 12 8 12 9 10 15 g_slist_remove_all(slist ,&ip4); g_slist_free (slist); g_slist_pop_allocator (); }
/* * helper_find_peek: * @data: helper data struct * @off: stream offset * @size: block size * * Get data pointer within a stream. Keeps a cache of read buffers (partly * for performance reasons, but mostly because pointers returned by us need * to stay valid until typefinding has finished) * * Returns: (nullable): address of the data or %NULL if buffer does not cover * the requested range. */ static const guint8 * helper_find_peek (gpointer data, gint64 offset, guint size) { GstTypeFindHelper *helper; GstBuffer *buffer; GSList *insert_pos = NULL; gsize buf_size; guint64 buf_offset; GstMappedBuffer *bmap; #if 0 GstCaps *caps; #endif helper = (GstTypeFindHelper *) data; GST_LOG_OBJECT (helper->obj, "'%s' called peek (%" G_GINT64_FORMAT ", %u)", GST_OBJECT_NAME (helper->factory), offset, size); if (size == 0) return NULL; if (offset < 0) { if (helper->size == -1 || helper->size < -offset) return NULL; offset += helper->size; } /* see if we have a matching buffer already in our list */ if (size > 0 && offset <= helper->last_offset) { GSList *walk; for (walk = helper->buffers; walk; walk = walk->next) { GstMappedBuffer *bmp = (GstMappedBuffer *) walk->data; GstBuffer *buf = GST_BUFFER_CAST (bmp->buffer); buf_offset = GST_BUFFER_OFFSET (buf); buf_size = bmp->map.size; /* buffers are kept sorted by end offset (highest first) in the list, so * at this point we save the current position and stop searching if * we're after the searched end offset */ if (buf_offset <= offset) { if ((offset + size) < (buf_offset + buf_size)) { /* must already have been mapped before */ return (guint8 *) bmp->map.data + (offset - buf_offset); } } else if (offset + size >= buf_offset + buf_size) { insert_pos = walk; break; } } } buffer = NULL; /* some typefinders go in 1 byte steps over 1k of data and request * small buffers. It is really inefficient to pull each time, and pulling * a larger chunk is almost free. Trying to pull a larger chunk at the end * of the file is also not a problem here, we'll just get a truncated buffer * in that case (and we'll have to double-check the size we actually get * anyway, see below) */ helper->flow_ret = helper->func (helper->obj, helper->parent, offset, MAX (size, 4096), &buffer); if (helper->flow_ret != GST_FLOW_OK) goto error; #if 0 caps = GST_BUFFER_CAPS (buffer); if (caps && !gst_caps_is_empty (caps) && !gst_caps_is_any (caps)) { GST_DEBUG ("buffer has caps %" GST_PTR_FORMAT ", suggest max probability", caps); gst_caps_replace (&helper->caps, caps); helper->best_probability = GST_TYPE_FIND_MAXIMUM; gst_buffer_unref (buffer); return NULL; } #endif /* getrange might silently return shortened buffers at the end of a file, * we must, however, always return either the full requested data or %NULL */ buf_offset = GST_BUFFER_OFFSET (buffer); buf_size = gst_buffer_get_size (buffer); if (buf_size < size) { GST_DEBUG ("dropping short buffer of size %" G_GSIZE_FORMAT "," "requested size was %u", buf_size, size); gst_buffer_unref (buffer); return NULL; } if (buf_offset != -1 && buf_offset != offset) { GST_DEBUG ("dropping buffer with unexpected offset %" G_GUINT64_FORMAT ", " "expected offset was %" G_GUINT64_FORMAT, buf_offset, offset); gst_buffer_unref (buffer); return NULL; } bmap = g_slice_new0 (GstMappedBuffer); if (!gst_buffer_map (buffer, &bmap->map, GST_MAP_READ)) goto map_failed; bmap->buffer = buffer; if (insert_pos) { helper->buffers = g_slist_insert_before (helper->buffers, insert_pos, bmap); } else { /* if insert_pos is not set, our offset is bigger than the largest offset * we have so far; since we keep the list sorted with highest offsets * first, we need to prepend the buffer to the list */ helper->last_offset = GST_BUFFER_OFFSET (buffer) + buf_size; helper->buffers = g_slist_prepend (helper->buffers, bmap); } return bmap->map.data; error: { GST_INFO ("typefind function returned: %s", gst_flow_get_name (helper->flow_ret)); return NULL; } map_failed: { GST_ERROR ("map failed"); gst_buffer_unref (buffer); g_slice_free (GstMappedBuffer, bmap); return NULL; } }
/** * @file: ex-gslist-11.c * @author: hbuyse */ #include <logging.h> #include <glib.h> int main(int argc __attribute__((unused)), char **argv __attribute__((unused)) ) { GSList *list = g_slist_append(NULL, "Anaheim "), *iterator = NULL; list = g_slist_append(list, "Elkton "); iprintf("Before inserting 'Boston', second item is: '%s'\n", g_slist_nth(list, 1)->data); g_slist_insert(list, "Boston ", 1); iprintf("After insertion, second item is: '%s'\n", g_slist_nth(list, 1)->data); list = g_slist_insert_before(list, g_slist_nth(list, 2), "Chicago "); iprintf("After an insert_before, third item is: '%s'\n", g_slist_nth(list, 2)->data); list = g_slist_insert_sorted(list, "Denver ", (GCompareFunc) g_ascii_strcasecmp); iprintf("After inserting 'Denver', here's the final list:\n"); g_slist_foreach(list, (GFunc) printf, NULL); g_slist_free(list); return (0); }