slab.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * Copyright Institute of Communication and Computer Systems (ICCS)
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. /**
  18. * \file slab.h
  19. * \author Iraklis Anagnostopoulos (irasklis@microlab.ntua.gr)
  20. * \date March 2014
  21. * \brief Slab-related prototypes
  22. */
  23. #ifndef DEALY_SLAB_H
  24. #define DEALY_SLAB_H
  25. #include <stddef.h>
  26. #include "dmmlib/lists.h"
  27. /** The header structure of every memory block inside a heap. */
  28. typedef struct slab_block_header_s {
  29. SLIST_ENTRY(slab_block_header_s) pointers;
  30. } slab_block_header_t;
  31. /** The head element of a singly-linked list of slab_block_header_t records */
  32. SLIST_HEAD(slab_head_s, slab_block_header_s);
  33. /** Data structure of the required elements for a free-list organized raw block.
  34. */
  35. typedef struct slab_rb_s {
  36. size_t remaining_size; /**< The remaining size for new memory blocks. */
  37. size_t handling_size; /**< The size that the slab raw block handles. */
  38. slab_block_header_t *border_ptr; /**< Pointer to the memory block
  39. initialized last. */
  40. struct slab_head_s fl_head; /**< Head of the free list of
  41. memory blocks. */
  42. slab_block_header_t *fl_tail; /**< The tail of the free list. */
  43. } slab_rb_t;
  44. /** Initializes meta-data of a slab raw block.
  45. *
  46. * @param address The address of the slab metadata
  47. * @param available_size The available size of the raw block for slab
  48. * metadata and memory blocks
  49. */
  50. void initialize_slab(void *address, size_t available_size);
  51. /** Tries to allocate memory from a specific slab raw block.
  52. *
  53. * @param raw_block Pointer to the slab raw block.
  54. * @param size Requested allocation size in bytes.
  55. *
  56. * @return The pointer to the memory location.
  57. */
  58. void * slab_malloc(slab_rb_t *raw_block, size_t size);
  59. /** Frees the memory block inside of a specific slab raw block.
  60. *
  61. * @param raw_block Pointer to the slab raw block.
  62. * @param ptr Pointer to the memory block to be freed.
  63. */
  64. void slab_free(slab_rb_t *raw_block, void *ptr);
  65. #endif /* DEALY_SLAB_H */