/* * Copyright(C)2006 Sugimoto,Shigetoshi * All rights reserved. */ #include static int push_item(general_queue_info_t *info, void *); static void *get_item(general_queue_info_t *info); static void print_queue(general_queue_info_t *info); /* * queue初期化関数 */ general_queue_info_t *init_general_queue(int size) { general_queue_info_t *info; if (size <= 0) { return NULL; } info = (general_queue_info_t*)malloc(sizeof(general_queue_info_t)); if (info == NULL) { return NULL; } info->head = NULL; info->tail = NULL; info->free = NULL; info->push_item = push_item; info->get_item = get_item; info->print_queue = print_queue; while (size--) { general_queue_item_t *q; q = (general_queue_item_t*)malloc(sizeof(general_queue_item_t)); if (q == NULL) { break; } q->next = info->free; q->prev = NULL; q->data = NULL; info->free = q; } return info; } /* * queue要素を末尾に追加 */ static int push_item(general_queue_info_t *info, void *data) { general_queue_item_t *q; if (info->free == NULL) { return FALSE; } q = info->free; info->free = q->next; q->data = data; q->next = NULL; q->prev = info->tail; if (info->tail != NULL) { info->tail->next = q; } info->tail = q; if (info->head == NULL) { info->head = q; } return TRUE; } /* * queue要素を先頭から取り出し */ static void *get_item(general_queue_info_t *info) { general_queue_item_t *q; void *data; if (info->head == NULL) { return NULL; } q = info->head; data = q->data; info->head = q->next; if (info->tail == q) { info->tail = NULL; } q->next = info->free; q->prev = NULL; q->data = NULL; info->free = q; return data; } void print_queue(general_queue_info_t *info) { general_queue_item_t *item; if (info == NULL) { printf("info: 00000000\n"); return; } printf("head: %08x\n", info->head); printf("tail: %08x\n", info->tail); printf("free: %08x\n", info->free); for (item = info->head; item; item = item->next) { printf("item %08x next %08x prev %08x\n", item, item->next, item->prev); } } // end of file