int btree_elements_iter(struct TREE *t) { int i; struct TREE *tmp; LIFO *lifo = NULL; i = 0; tmp = t; /* Jätteineffektiv, men jag är för trött för att tänka ut en icke-rekursiv som inte suger */ do { i++; for (; tmp;) { lifo = lifo_push(lifo, tmp); if (tmp->low) { tmp = tmp->low; continue; } break; } i++; tmp = lifo_pop(&lifo); if (tmp->high) { tmp = tmp->high; continue; } tmp = NULL; } while (lifo); return i; }
void btree_depth_first_traversal_iterative(struct b_tree_node_t *root) { struct b_tree_node_t *node; lifo_t lifo; lifo_init(&lifo); lifo_push(&lifo, root); do { root = lifo_pop(&lifo); if (root == NULL) { continue; } if (root->right) { lifo_push(&lifo, root->right); } if (root->left) { lifo_push(&lifo, root->left); } } while (root); }
static inline void smfree_batch(struct small_alloc *alloc) { if (alloc->is_delayed_free_mode || lifo_is_empty(&alloc->delayed)) return; const int BATCH = 100; struct mempool *pool = lifo_peek(&alloc->delayed); for (int i = 0; i < BATCH; i++) { void *item = lifo_pop(&pool->delayed); if (item == NULL) { (void) lifo_pop(&alloc->delayed); pool = lifo_peek(&alloc->delayed); if (pool == NULL) break; continue; } mempool_free(pool, item); } }
static inline void smfree_batch(struct small_alloc *alloc) { if (alloc->is_delayed_free_mode || lifo_is_empty(&alloc->delayed)) return; const int BATCH = 100; for (int i = 0; i < BATCH; i++) { void *item = lifo_pop(&alloc->delayed); if (item == NULL) break; smfree(alloc, item); } }
int main() { struct user_node *pUserNode= &(wrapped_user_node.user_node); struct user_node *pPoped; /* STEP 1: init the Head =============================================================*/ /* IMPORTANT: head must be zeroed before using it that way. As it is static here, we */ /* already know it is zeroed, and we have nothing more to do. */ lifo_init_head( P_HEAD, LIFO_DCAS ); /* STEP 2: init the nodes you will use ===============================================*/ lifo_init_node( LIFO_NODE ); /* You would normally put some value in your node. This is your own user data here. */ pUserNode->number = 1; pUserNode->whatever= 0; /* STEP 3: push the node on the queue ================================================*/ lifo_push( P_HEAD, pUserNode ); /* STEP 4: pop a node from the queue ================================================*/ lifo_pop ( P_HEAD, (void **)&pPoped ); /* Now, here normally you got your own data back! */ if (pPoped== NULL) { printf("No node was poped!\n"); } else { printf("We poped the node number %d\n", pPoped->number); } /* We don't need cleanup here, as head & nodes are static ============================*/ printf("OK: all!\n"); return 0; }