ghash/bli-listbase edits, rename BLI_ghash_pop -> BLI_ghash_popkey (since it takes a key as an arg and isnt popping any element from the hash as you might expect).

add BLI_pophead/tail, since getting the first element from a list and removing it is a common task.
This commit is contained in:
Campbell Barton 2013-08-26 23:37:08 +00:00
parent cdd57d4994
commit 8ef934c73f
29 changed files with 85 additions and 127 deletions

@ -524,9 +524,7 @@ void blf_font_free(FontBLF *font)
GlyphCacheBLF *gc;
font->glyph_cache = NULL;
while (font->cache.first) {
gc = font->cache.first;
BLI_remlink(&font->cache, gc);
while ((gc = BLI_pophead(&font->cache))) {
blf_glyph_cache_free(gc);
}

@ -128,9 +128,7 @@ void blf_glyph_cache_clear(FontBLF *font)
for (gc = font->cache.first; gc; gc = gc->next) {
for (i = 0; i < 257; i++) {
while (gc->bucket[i].first) {
g = gc->bucket[i].first;
BLI_remlink(&(gc->bucket[i]), g);
while ((g = BLI_pophead(&gc->bucket[i]))) {
blf_glyph_free(g);
}
}
@ -145,9 +143,7 @@ void blf_glyph_cache_free(GlyphCacheBLF *gc)
int i;
for (i = 0; i < 257; i++) {
while (gc->bucket[i].first) {
g = gc->bucket[i].first;
BLI_remlink(&(gc->bucket[i]), g);
while ((g = BLI_pophead(&gc->bucket[i]))) {
blf_glyph_free(g);
}
}

@ -924,9 +924,7 @@ int BKE_copybuffer_save(const char *filename, ReportList *reports)
ID *id;
ListBase *lb1 = lbarray[a], *lb2 = fromarray[a];
while (lb2->first) {
id = lb2->first;
BLI_remlink(lb2, id);
while ((id = BLI_pophead(lb2))) {
BLI_addtail(lb1, id);
id_sort_by_name(lb1, id);
}

@ -198,8 +198,7 @@ void CTX_store_free_list(ListBase *contexts)
{
bContextStore *ctx;
while ((ctx = contexts->first)) {
BLI_remlink(contexts, ctx);
while ((ctx = BLI_pophead(contexts))) {
CTX_store_free(ctx);
}
}

@ -81,11 +81,8 @@ void BKE_displist_free(ListBase *lb)
{
DispList *dl;
dl = lb->first;
while (dl) {
BLI_remlink(lb, dl);
while ((dl = BLI_pophead(lb))) {
BKE_displist_elem_free(dl);
dl = lb->first;
}
}

@ -65,9 +65,7 @@ void BKE_group_free(Group *group)
/* don't free group itself */
GroupObject *go;
while (group->gobject.first) {
go = group->gobject.first;
BLI_remlink(&group->gobject, go);
while ((go = BLI_pophead(&group->gobject))) {
free_group_object(go);
}
}

@ -195,9 +195,7 @@ static void image_free_buffers(Image *ima)
{
ImBuf *ibuf;
while ((ibuf = ima->ibufs.first)) {
BLI_remlink(&ima->ibufs, ibuf);
while ((ibuf = BLI_pophead(&ima->ibufs))) {
if (ibuf->userdata) {
MEM_freeN(ibuf->userdata);
ibuf->userdata = NULL;
@ -524,8 +522,7 @@ void BKE_image_merge(Image *dest, Image *source)
/* sanity check */
if (dest && source && dest != source) {
while ((ibuf = source->ibufs.first)) {
BLI_remlink(&source->ibufs, ibuf);
while ((ibuf = BLI_pophead(&source->ibufs))) {
image_assign_ibuf(dest, ibuf, IMA_INDEX_PASS(ibuf->index), IMA_INDEX_FRAME(ibuf->index));
}

@ -85,29 +85,23 @@ void BKE_key_free(Key *key)
KeyBlock *kb;
BKE_free_animdata((ID *)key);
while ( (kb = key->block.first) ) {
if (kb->data) MEM_freeN(kb->data);
BLI_remlink(&key->block, kb);
while ((kb = BLI_pophead(&key->block))) {
if (kb->data)
MEM_freeN(kb->data);
MEM_freeN(kb);
}
}
void BKE_key_free_nolib(Key *key)
{
KeyBlock *kb;
while ( (kb = key->block.first) ) {
if (kb->data) MEM_freeN(kb->data);
BLI_remlink(&key->block, kb);
while ((kb = BLI_pophead(&key->block))) {
if (kb->data)
MEM_freeN(kb->data);
MEM_freeN(kb);
}
}
Key *BKE_key_add(ID *id) /* common function */

@ -2666,7 +2666,7 @@ void BKE_node_instance_hash_clear(bNodeInstanceHash *hash, bNodeInstanceValueFP
void *BKE_node_instance_hash_pop(bNodeInstanceHash *hash, bNodeInstanceKey key)
{
return BLI_ghash_pop(hash->ghash, &key, NULL);
return BLI_ghash_popkey(hash->ghash, &key, NULL);
}
int BKE_node_instance_hash_haskey(bNodeInstanceHash *hash, bNodeInstanceKey key)

@ -142,11 +142,9 @@ void BKE_object_update_base_layer(struct Scene *scene, Object *ob)
void BKE_object_free_particlesystems(Object *ob)
{
while (ob->particlesystem.first) {
ParticleSystem *psys = ob->particlesystem.first;
BLI_remlink(&ob->particlesystem, psys);
ParticleSystem *psys;
while ((psys = BLI_pophead(&ob->particlesystem))) {
psys_free(ob, psys);
}
}
@ -182,11 +180,9 @@ void BKE_object_free_curve_cache(Object *ob)
void BKE_object_free_modifiers(Object *ob)
{
while (ob->modifiers.first) {
ModifierData *md = ob->modifiers.first;
BLI_remlink(&ob->modifiers, md);
ModifierData *md;
while ((md = BLI_pophead(&ob->modifiers))) {
modifier_free(md);
}

@ -2969,12 +2969,10 @@ void BKE_ptcache_free(PointCache *cache)
}
void BKE_ptcache_free_list(ListBase *ptcaches)
{
PointCache *cache = ptcaches->first;
PointCache *cache;
while (cache) {
BLI_remlink(ptcaches, cache);
while ((cache = BLI_pophead(ptcaches))) {
BKE_ptcache_free(cache);
cache = ptcaches->first;
}
}

@ -59,9 +59,8 @@ void BKE_bproperty_free(bProperty *prop)
void BKE_bproperty_free_list(ListBase *lb)
{
bProperty *prop;
while ( (prop = lb->first) ) {
BLI_remlink(lb, prop);
while ((prop = BLI_pophead(lb))) {
BKE_bproperty_free(prop);
}
}

@ -63,8 +63,7 @@ void free_sensors(ListBase *lb)
{
bSensor *sens;
while ((sens= lb->first)) {
BLI_remlink(lb, sens);
while ((sens = BLI_pophead(lb))) {
free_sensor(sens);
}
}
@ -227,9 +226,9 @@ void free_controllers(ListBase *lb)
{
bController *cont;
while ((cont= lb->first)) {
BLI_remlink(lb, cont);
if (cont->slinks) MEM_freeN(cont->slinks);
while ((cont = BLI_pophead(lb))) {
if (cont->slinks)
MEM_freeN(cont->slinks);
free_controller(cont);
}
}
@ -346,8 +345,7 @@ void free_actuators(ListBase *lb)
{
bActuator *act;
while ((act= lb->first)) {
BLI_remlink(lb, act);
while ((act = BLI_pophead(lb))) {
free_actuator(act);
}
}

@ -865,8 +865,7 @@ void BKE_sequencer_sort(Scene *scene)
seqbase.first = seqbase.last = NULL;
effbase.first = effbase.last = NULL;
while ( (seq = ed->seqbasep->first) ) {
BLI_remlink(ed->seqbasep, seq);
while ((seq = BLI_pophead(ed->seqbasep))) {
if (seq->type & SEQ_TYPE_EFFECT) {
seqt = effbase.first;

@ -70,7 +70,7 @@ bool BLI_ghash_remove(GHash *gh, void *key, GHashKeyFreeFP keyfreefp, GHashVal
void BLI_ghash_clear(GHash *gh, GHashKeyFreeFP keyfreefp, GHashValFreeFP valfreefp);
void BLI_ghash_clear_ex(GHash *gh, GHashKeyFreeFP keyfreefp, GHashValFreeFP valfreefp,
const unsigned int nentries_reserve);
void *BLI_ghash_pop(GHash *gh, void *key, GHashKeyFreeFP keyfreefp);
void *BLI_ghash_popkey(GHash *gh, void *key, GHashKeyFreeFP keyfreefp);
bool BLI_ghash_haskey(GHash *gh, const void *key);
int BLI_ghash_size(GHash *gh);
void BLI_ghash_flag_set(GHash *gh, unsigned int flag);

@ -60,6 +60,8 @@ void BLI_freelistN(struct ListBase *listbase);
void BLI_addtail(struct ListBase *listbase, void *vlink);
void BLI_remlink(struct ListBase *listbase, void *vlink);
bool BLI_remlink_safe(struct ListBase *listbase, void *vlink);
void *BLI_pophead(ListBase *listbase);
void *BLI_poptail(ListBase *listbase);
void BLI_addhead(struct ListBase *listbase, void *vlink);
void BLI_insertlinkbefore(struct ListBase *listbase, void *vnextlink, void *vnewlink);

@ -438,7 +438,7 @@ bool BLI_ghash_remove(GHash *gh, void *key, GHashKeyFreeFP keyfreefp, GHashValFr
* \param keyfreefp Optional callback to free the key.
* \return the value of \a key int \a gh or NULL.
*/
void *BLI_ghash_pop(GHash *gh, void *key, GHashKeyFreeFP keyfreefp)
void *BLI_ghash_popkey(GHash *gh, void *key, GHashKeyFreeFP keyfreefp)
{
const unsigned int hash = ghash_keyhash(gh, key);
Entry *e = ghash_remove_ex(gh, key, keyfreefp, NULL, hash);

@ -340,9 +340,9 @@ void BLI_mempool_free(BLI_mempool *pool, void *addr)
BLI_freenode *curnode = NULL;
char *tmpaddr = NULL;
int i;
BLI_mempool_chunk *first = pool->chunks.first;
BLI_mempool_chunk *first;
BLI_remlink(&pool->chunks, first);
first = BLI_pophead(&pool->chunks);
mempool_chunk_free_all(&pool->chunks, pool->flag);
BLI_addtail(&pool->chunks, first);
#ifdef USE_TOTALLOC
@ -543,8 +543,7 @@ void BLI_mempool_clear_ex(BLI_mempool *pool, const int totelem_reserve)
chunks_temp = pool->chunks;
pool->chunks.first = pool->chunks.last = NULL;
while ((mpchunk = chunks_temp.first)) {
BLI_remlink(&chunks_temp, mpchunk);
while ((mpchunk = BLI_pophead(&chunks_temp))) {
lasttail = mempool_chunk_add(pool, mpchunk, lasttail);
}
}

@ -129,6 +129,30 @@ bool BLI_remlink_safe(ListBase *listbase, void *vlink)
}
}
/**
* Removes the head from \a listbase and returns it.
*/
void *BLI_pophead(ListBase *listbase)
{
Link *link;
if ((link = listbase->first)) {
BLI_remlink(listbase, link);
}
return link;
}
/**
* Removes the tail from \a listbase and returns it.
*/
void *BLI_poptail(ListBase *listbase)
{
Link *link;
if ((link = listbase->last)) {
BLI_remlink(listbase, link);
}
return link;
}
/**
* Removes \a vlink from listbase and disposes of it. Assumes it is linked into there!

@ -51,9 +51,9 @@ void BLO_free_memfile(MemFile *memfile)
{
MemFileChunk *chunk;
while ( (chunk = (memfile->chunks.first) ) ) {
if (chunk->ident == 0) MEM_freeN(chunk->buf);
BLI_remlink(&memfile->chunks, chunk);
while ((chunk = BLI_pophead(&memfile->chunks))) {
if (chunk->ident == 0)
MEM_freeN(chunk->buf);
MEM_freeN(chunk);
}
memfile->size = 0;

@ -518,11 +518,8 @@ static void do_version_free_effects_245(ListBase *lb)
{
Effect *eff;
eff = lb->first;
while (eff) {
BLI_remlink(lb, eff);
while ((eff = BLI_pophead(lb))) {
do_version_free_effect_245(eff);
eff = lb->first;
}
}

@ -361,8 +361,7 @@ bool BM_mesh_edgeloops_find_path(BMesh *bm, ListBase *r_eloops,
void BM_mesh_edgeloops_free(ListBase *eloops)
{
BMEdgeLoopStore *el_store;
while ((el_store = eloops->first)) {
BLI_remlink(eloops, el_store);
while ((el_store = BLI_pophead(eloops))) {
BM_edgeloop_free(el_store);
}
}

@ -361,7 +361,7 @@ static CVKeyIndex *getCVKeyIndex(EditNurb *editnurb, void *cv)
static CVKeyIndex *popCVKeyIndex(EditNurb *editnurb, void *cv)
{
return BLI_ghash_pop(editnurb->keyindex, cv, NULL);
return BLI_ghash_popkey(editnurb->keyindex, cv, NULL);
}
static BezTriple *getKeyIndexOrig_bezt(EditNurb *editnurb, BezTriple *bezt)

@ -2230,8 +2230,7 @@ void uiFreeBlock(const bContext *C, uiBlock *block)
{
uiBut *but;
while ( (but = block->buttons.first) ) {
BLI_remlink(&block->buttons, but);
while ((but = BLI_pophead(&block->buttons))) {
ui_free_but(C, but);
}
@ -2255,8 +2254,7 @@ void uiFreeBlocks(const bContext *C, ListBase *lb)
{
uiBlock *block;
while ( (block = lb->first) ) {
BLI_remlink(lb, block);
while ((block = BLI_pophead(lb))) {
uiFreeBlock(C, block);
}
}

@ -501,19 +501,13 @@ bool mouse_mball(bContext *C, const int mval[2], bool extend, bool deselect, boo
/* free all MetaElems from ListBase */
static void freeMetaElemlist(ListBase *lb)
{
MetaElem *ml, *next;
MetaElem *ml;
if (lb == NULL) return;
ml = lb->first;
while (ml) {
next = ml->next;
BLI_remlink(lb, ml);
while ((ml = BLI_pophead(lb))) {
MEM_freeN(ml);
ml = next;
}
lb->first = lb->last = NULL;
}

@ -1049,10 +1049,7 @@ static tNearestVertInfo *get_best_nearest_fcurve_vert(ListBase *matches)
/* if list only has 1 item, remove it from the list and return */
if (matches->first == matches->last) {
/* need to remove from the list, otherwise it gets freed and then we can't return it */
nvi = matches->first;
BLI_remlink(matches, nvi);
return nvi;
return BLI_pophead(matches);
}
/* try to find the first selected F-Curve vert, then take the one after it */
@ -1075,9 +1072,7 @@ static tNearestVertInfo *get_best_nearest_fcurve_vert(ListBase *matches)
/* if we're still here, this means that we failed to find anything appropriate in the first pass,
* so just take the first item now...
*/
nvi = matches->first;
BLI_remlink(matches, nvi);
return nvi;
return BLI_pophead(matches);
}
/* Find the nearest vertices (either a handle or the keyframe) that are nearest to the mouse cursor (in area coordinates)

@ -1039,9 +1039,7 @@ static void GPU_nodes_free(ListBase *nodes)
{
GPUNode *node;
while (nodes->first) {
node = nodes->first;
BLI_remlink(nodes, node);
while ((node = BLI_pophead(nodes))) {
GPU_node_free(node);
}
}

@ -156,8 +156,7 @@ void WM_operator_stack_clear(wmWindowManager *wm)
{
wmOperator *op;
while ((op = wm->operators.first)) {
BLI_remlink(&wm->operators, op);
while ((op = BLI_pophead(&wm->operators))) {
WM_operator_free(op);
}
@ -414,20 +413,17 @@ void wm_close_and_free(bContext *C, wmWindowManager *wm)
if (wm->autosavetimer)
wm_autosave_timer_ended(wm);
while ((win = wm->windows.first)) {
BLI_remlink(&wm->windows, win);
while ((win = BLI_pophead(&wm->windows))) {
win->screen = NULL; /* prevent draw clear to use screen */
wm_draw_window_clear(win);
wm_window_free(C, wm, win);
}
while ((op = wm->operators.first)) {
BLI_remlink(&wm->operators, op);
while ((op = BLI_pophead(&wm->operators))) {
WM_operator_free(op);
}
while ((keyconf = wm->keyconfigs.first)) {
BLI_remlink(&wm->keyconfigs, keyconf);
while ((keyconf = BLI_pophead(&wm->keyconfigs))) {
WM_keyconfig_free(keyconf);
}

@ -127,8 +127,7 @@ void wm_event_free_all(wmWindow *win)
{
wmEvent *event;
while ((event = win->queue.first)) {
BLI_remlink(&win->queue, event);
while ((event = BLI_pophead(&win->queue))) {
wm_event_free(event);
}
}
@ -233,14 +232,6 @@ static void wm_notifier_clear(wmNotifier *note)
memset(((char *)note) + sizeof(Link), 0, sizeof(*note) - sizeof(Link));
}
static wmNotifier *wm_notifier_next(wmWindowManager *wm)
{
wmNotifier *note = wm->queue.first;
if (note) BLI_remlink(&wm->queue, note);
return note;
}
/* called in mainloop */
void wm_event_do_notifiers(bContext *C)
{
@ -316,7 +307,7 @@ void wm_event_do_notifiers(bContext *C)
}
/* the notifiers are sent without context, to keep it clean */
while ( (note = wm_notifier_next(wm)) ) {
while ((note = BLI_pophead(&wm->queue))) {
for (win = wm->windows.first; win; win = win->next) {
/* filter out notifiers */
@ -1331,9 +1322,7 @@ void WM_event_remove_handlers(bContext *C, ListBase *handlers)
wmWindowManager *wm = CTX_wm_manager(C);
/* C is zero on freeing database, modal handlers then already were freed */
while ((handler = handlers->first)) {
BLI_remlink(handlers, handler);
while ((handler = BLI_pophead(handlers))) {
if (handler->op) {
if (handler->op->type->cancel) {
ScrArea *area = CTX_wm_area(C);