123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- /*
- * 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 "block_header.h"
- /**
- * \brief Get the address of the block header of a memory block.
- *
- * \param ptr The data part of the memory block.
- */
- static block_header_t * get_header(void *ptr);
- static block_header_t * get_header(void *ptr) {
- return (block_header_t *) ((char *) ptr - HEADER_SIZE);
- }
- void * get_next(void *ptr) {
- return get_header(ptr)->next;
- }
- size_t get_size(void *ptr) {
- return get_header(ptr)->size >> 1;
- }
- size_t get_requested_size(void *ptr) {
- return get_header(ptr)->requested_size;
- }
- size_t get_size_availability(void *ptr) {
- return get_header(ptr)->size;
- }
- void set_size(void *ptr, size_t size) {
- get_header(ptr)->size = size << 1;
- }
- void set_requested_size(void *ptr, size_t size) {
- get_header(ptr)->requested_size = size;
- }
- void mark_used(void *ptr) {
- get_header(ptr)->size |= 1;
- }
- void mark_free(void *ptr) {
- get_header(ptr)->size &= (~ 0x1);
- }
- void set_next(void *ptr, void *next_block) {
- get_header(ptr)->next = next_block;
- }
- #ifdef WITH_OWNERSHIP
- void set_owner(void *ptr, heap_t *heap_owner) {
- get_header(ptr)->heap_owner = heap_owner;
- }
- heap_t * get_owner(void *ptr) {
- return get_header(ptr)->heap_owner;
- }
- #endif /* WITH_OWNERSHIP */
- bool is_previous_free(void *ptr) {
- if (get_header(ptr)->previous_size & (size_t) 1) {
- return false;
- } else {
- return true;
- }
- }
- void set_previous_size_availability(void *ptr, size_t previous_size_availability) {
- get_header(ptr)->previous_size = previous_size_availability;
- }
- size_t get_previous_size(void *ptr) {
- return get_header(ptr)->previous_size >> 1;
- }
- size_t get_previous_size_availability(void *ptr) {
- return get_header(ptr)->previous_size;
- }
- void * get_previous(void *ptr) {
- return (void *) ((char *) ptr - HEADER_SIZE - get_previous_size(ptr));
- }
- 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;
- }
- }
|