/* * Copyright 2011 Institute of Communication and Computer Systems (ICCS) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ #include "linked_lists/linked_lists.h" #include "block_header.h" void set_next(void *ptr, void *next_block) { get_header(ptr)->pointers.next = (list_node_ptr) next_block; } void * get_next(void *ptr) { return (void *) get_header(ptr)->pointers.next; } #ifdef BLOCKS_IN_DLL void set_previous(void *ptr, void *previous_block) { if( ptr != NULL) { get_header(ptr)->pointers.previous = (list_node_ptr) previous_block; } } void * get_previous(void *ptr) { return (void *) get_header(ptr)->pointers.previous; } #endif /* BLOCKS_IN_DLL */ void push_block(void **block, void **starting_node) { if(*starting_node != NULL) { #ifdef BLOCKS_IN_DLL set_previous(*starting_node, *block); #endif /* BLOCKS_IN_DLL */ set_next(*block, *starting_node); } else { set_next(*block, NULL); } #ifdef BLOCKS_IN_DLL set_previous(*block, NULL); #endif /* BLOCKS_IN_DLL */ *starting_node = *block; } #ifdef BLOCKS_IN_SLL void remove_block(void **block, void **starting_node) { void *current_node, *previous_node; /* Traverse a list starting from the starting node until block is found. */ for(current_node = *starting_node; current_node != NULL; current_node = get_next(current_node)) { if(current_node == *block) { if(current_node == *starting_node) { *starting_node = get_next(*block); } else { set_next(previous_node, get_next(*block)); } break; } previous_node = current_node; } } #endif #ifdef BLOCKS_IN_DLL void remove_block(void **block, void **starting_node) { void *previous_block, *next_block; /* No need to traverse the list, just check if the block is the starting * node of the list. */ previous_block = get_previous(*block); next_block = get_next(*block); if(*block == *starting_node) { *starting_node = previous_block; } if(previous_block != NULL) { set_next(previous_block, next_block); } if(next_block != NULL) { set_previous(next_block, previous_block); } } #endif /* BLOCKS_IN_DLL */