aboutsummaryrefslogtreecommitdiffstats
path: root/src/util/slab.c
blob: af7515265ef8757aa9891a08512b229f6eb04858 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
 * Copyright 2010 Marek Olšák <maraeo@gmail.com>
 * Copyright 2016 Advanced Micro Devices, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * on the rights to use, copy, modify, merge, publish, distribute, sub
 * license, and/or sell copies of the Software, and to permit persons to whom
 * the Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
 * USE OR OTHER DEALINGS IN THE SOFTWARE. */

#include "slab.h"
#include "macros.h"
#include "simple_list.h"
#include <stdint.h>
#include <stdbool.h>
#include <string.h>

#define ALIGN(value, align) (((value) + (align) - 1) & ~((align) - 1))

#ifdef DEBUG
#define SLAB_MAGIC 0xcafe4321
#define SET_MAGIC(element)   (element)->magic = SLAB_MAGIC
#define CHECK_MAGIC(element) assert((element)->magic == SLAB_MAGIC)
#else
#define SET_MAGIC(element)
#define CHECK_MAGIC(element)
#endif

/* One array element within a big buffer. */
struct slab_element_header {
   /* The next free element. */
   struct slab_element_header *next_free;

#ifdef DEBUG
   /* Use intptr_t to keep the header aligned to a pointer size. */
   intptr_t magic;
#endif
};

static struct slab_element_header *
slab_get_element(struct slab_mempool *pool,
                 struct slab_page_header *page, unsigned index)
{
   return (struct slab_element_header*)
          ((uint8_t*)&page[1] + (pool->element_size * index));
}

static bool
slab_add_new_page(struct slab_mempool *pool)
{
   struct slab_page_header *page;
   struct slab_element_header *element;
   unsigned i;

   page = malloc(sizeof(struct slab_page_header) +
                 pool->num_elements * pool->element_size);
   if (!page)
      return false;

   if (!pool->list.prev)
      make_empty_list(&pool->list);

   insert_at_tail(&pool->list, page);

   /* Mark all elements as free. */
   for (i = 0; i < pool->num_elements-1; i++) {
      element = slab_get_element(pool, page, i);
      element->next_free = slab_get_element(pool, page, i + 1);
      SET_MAGIC(element);
   }

   element = slab_get_element(pool, page, pool->num_elements - 1);
   element->next_free = pool->first_free;
   SET_MAGIC(element);
   pool->first_free = slab_get_element(pool, page, 0);
   return true;
}

/**
 * Allocate an object from the slab. Single-threaded (no mutex).
 */
void *
slab_alloc_st(struct slab_mempool *pool)
{
   struct slab_element_header *element;

   /* Allocate a new page. */
   if (!pool->first_free &&
       !slab_add_new_page(pool))
      return NULL;

   element = pool->first_free;
   CHECK_MAGIC(element);
   pool->first_free = element->next_free;
   return &element[1];
}

/**
 * Free an object allocated from the slab. Single-threaded (no mutex).
 */
void
slab_free_st(struct slab_mempool *pool, void *ptr)
{
   struct slab_element_header *element =
      ((struct slab_element_header*)ptr - 1);

   CHECK_MAGIC(element);
   element->next_free = pool->first_free;
   pool->first_free = element;
}

/**
 * Allocate an object from the slab. Thread-safe.
 */
void *
slab_alloc_mt(struct slab_mempool *pool)
{
   void *mem;

   mtx_lock(&pool->mutex);
   mem = slab_alloc_st(pool);
   mtx_unlock(&pool->mutex);
   return mem;
}

/**
 * Free an object allocated from the slab. Thread-safe.
 */
void
slab_free_mt(struct slab_mempool *pool, void *ptr)
{
   mtx_lock(&pool->mutex);
   slab_free_st(pool, ptr);
   mtx_unlock(&pool->mutex);
}

void
slab_destroy(struct slab_mempool *pool)
{
   struct slab_page_header *page, *temp;

   if (pool->list.next) {
      foreach_s(page, temp, &pool->list) {
         remove_from_list(page);
         free(page);
      }
   }

   mtx_destroy(&pool->mutex);
}

/**
 * Create an allocator for same-sized objects.
 *
 * \param item_size     Size of one object.
 * \param num_items     Number of objects to allocate at once.
 */
void
slab_create(struct slab_mempool *pool,
            unsigned item_size,
            unsigned num_items)
{
   mtx_init(&pool->mutex, mtx_plain);
   pool->element_size = ALIGN(sizeof(struct slab_element_header) + item_size,
                              sizeof(intptr_t));
   pool->num_elements = num_items;
   pool->first_free = NULL;
}