/* * Copyright 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. * */ /** * \file slab.h * \author Iraklis Anagnostopoulos (irasklis@microlab.ntua.gr) * \date March 2014 * \brief Slab-related prototypes */ #ifndef DEALY_SLAB_H #define DEALY_SLAB_H #include #include "dmmlib/lists.h" /** The header structure of every memory block inside a heap. */ typedef struct slab_block_header_s { SLIST_ENTRY(slab_block_header_s) pointers; } slab_block_header_t; /** The head element of a singly-linked list of slab_block_header_t records */ SLIST_HEAD(slab_head_s, slab_block_header_s); /** Data structure of the required elements for a free-list organized raw block. */ typedef struct slab_rb_s { size_t remaining_size; /**< The remaining size for new memory blocks. */ size_t handling_size; /**< The size that the slab raw block handles. */ slab_block_header_t *border_ptr; /**< Pointer to the memory block initialized last. */ struct slab_head_s fl_head; /**< Head of the free list of memory blocks. */ slab_block_header_t *fl_tail; /**< The tail of the free list. */ } slab_rb_t; /** Initializes meta-data of a slab raw block. * * @param address The address of the slab metadata * @param available_size The available size of the raw block for slab * metadata and memory blocks */ void initialize_slab(void *address, size_t available_size); /** Tries to allocate memory from a specific slab raw block. * * @param raw_block Pointer to the slab raw block. * @param size Requested allocation size in bytes. * * @return The pointer to the memory location. */ void * slab_malloc(slab_rb_t *raw_block, size_t size); /** Frees the memory block inside of a specific slab raw block. * * @param raw_block Pointer to the slab raw block. * @param ptr Pointer to the memory block to be freed. */ void slab_free(slab_rb_t *raw_block, void *ptr); #endif /* DEALY_SLAB_H */