-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsbmalloc.c
More file actions
142 lines (112 loc) · 3.71 KB
/
sbmalloc.c
File metadata and controls
142 lines (112 loc) · 3.71 KB
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
#include "sbmalloc.h"
#include "config.h"
#include <assert.h>
#include "mutex.h"
/**
* Implementation of simple block allocation
* algorithm based on article written by Ben Kenwright
* (ISBN 978-1-61208-222-6).
*
* It works without memory overhead and without loops
* in control procedures.
*
* The idea is to use free blocks to self-store a linked list
* of these blocks by storing the index of the next free block.
* The pointer to the head of the list is stored in the
* pool's control structure.
*/
/** Converts pointer to block index in pool */
static inline size_t addr_to_index(struct sbmalloc_pool *p, void *addr)
{
return ((unsigned char *) addr - p->pool) / p->block_size;
}
/** Converts block index to pointer in pool */
static inline void *index_to_addr(struct sbmalloc_pool *p, size_t idx)
{
return p->pool + idx * p->block_size;
}
/** Checks that pointer belongs to this pool */
static void assert_this_pool(struct sbmalloc_pool *p, void *addr)
{
assert((void *) p->pool <= addr);
assert((void *) ((unsigned char *) p->pool +
p->block_size * p->num_blocks) > addr);
}
/**
* Expected that block_size and num_blocks fields in
* the stucture are already filled (by SBMALLOC_DECLARE_POOL by
* example)
*/
int sb_pool_init(struct sbmalloc_pool *p)
{
int err;
err = sbmalloc_mutex_init(&p->mtx);
if (err != 0) {
return err;
}
p->free_blocks = p->num_blocks;
p->next_free = p->pool;
p->num_initialized = 0;
return 0;
}
int sb_pool_destroy(struct sbmalloc_pool *p)
{
return sbmalloc_mutex_destroy(&p->mtx);
}
void *sbmalloc(struct sbmalloc_pool *p)
{
int err;
err = sbmalloc_mutex_lock(&p->mtx);
if (err != 0) {
return NULL;
}
/* Initialize the next free block if it was not
* initialized yet */
if (p->num_initialized < p->num_blocks) {
unsigned int *el =
(unsigned int *) index_to_addr(p, p->num_initialized);
*el = ++p->num_initialized;
}
void *ret = NULL;
if (p->free_blocks > 0) {
/* Use current head of free blocks list */
ret = (void *) p->next_free;
--p->free_blocks;
if (p->free_blocks != 0) {
/* Shift the head of free blocks list to the next
* available block, index of which is written in
* current free block */
p->next_free = (unsigned char *)
index_to_addr(p, *((size_t *) p->next_free));
} else {
/* No free blocks left, delete head */
p->next_free = NULL;
}
}
err = sbmalloc_mutex_unlock(&p->mtx);
/* error on mutex unlock is a very bad sign */
assert(err == 0);
return ret;
}
int sbfree(struct sbmalloc_pool *p, void *ptr)
{
int err;
err = sbmalloc_mutex_lock(&p->mtx);
if (err != 0) {
return err;
}
assert_this_pool(p, ptr);
if (p->next_free != NULL) {
/* Append current block to free blocks list head */
*((size_t *) ptr) = addr_to_index(p, p->next_free);
p->next_free = (unsigned char *) ptr;
} else {
/* Restore the head of free blocks list */
p->next_free = (unsigned char *) ptr;
}
++p->free_blocks;
err = sbmalloc_mutex_unlock(&p->mtx);
/* error on mutex unlock is a very bad sign */
assert(err == 0);
return 0;
}