forked from ph4r05/php_aho_corasick
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mpool.c
197 lines (156 loc) · 4.39 KB
/
mpool.c
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
* mpool.c memory pool management
* This file is part of multifast.
*
Copyright 2010-2015 Kamiar Kanani <[email protected]>
multifast is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
multifast is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with multifast. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "mpool.h"
#define MPOOL_BLOCK_SIZE (24*1024)
#if (MPOOL_BLOCK_SIZE % 16 > 0)
#error "MPOOL_BLOCK_SIZE must be multiple 16"
#endif
#if (MPOOL_BLOCK_SIZE <= AC_PATTRN_MAX_LENGTH)
#error "MPOOL_BLOCK_SIZE must be bigger than AC_PATTRN_MAX_LENGTH"
#endif
struct mpool_block
{
size_t size;
unsigned char *bp; /* Block pointer */
unsigned char *free; /* Free area; End of allocated section */
struct mpool_block *next; /* Next block */
};
struct mpool
{
struct mpool_block *block;
};
/**
* @brief Allocate a new block to the pool
*
* @param size
* @return
******************************************************************************/
static struct mpool_block *mpool_new_block (size_t size)
{
struct mpool_block *block;
if (!size)
size = MPOOL_BLOCK_SIZE;
block = (struct mpool_block *) AC_MALLOC (sizeof(struct mpool_block));
block->bp = block->free = AC_MALLOC(size);
block->size = size;
block->next = NULL;
return block;
}
/**
* @brief Creates a new pool
*
* @param size
* @return
******************************************************************************/
struct mpool *mpool_create (size_t size)
{
struct mpool *ret;
ret = AC_MALLOC (sizeof(struct mpool));
ret->block = mpool_new_block(size);
return ret;
}
/**
* @brief Free a pool
*
* @param pool
******************************************************************************/
void mpool_free (struct mpool *pool)
{
struct mpool_block *p, *p_next;
if (!pool)
return;
if (!pool->block) {
AC_MFREE(pool);
return;
}
p = pool->block;
while (p) {
p_next = p->next;
AC_MFREE(p->bp);
AC_MFREE(p);
p = p_next;
}
AC_MFREE(pool);
}
/**
* @brief Allocate from a pool
*
* @param pool
* @param size
* @return
******************************************************************************/
void *mpool_malloc (struct mpool *pool, size_t size)
{
void *ret = NULL;
struct mpool_block *block, *new_block;
size_t remain, block_size;
if(!pool || !pool->block || !size)
return NULL;
size = (size + 15) & ~0xF; /* This is to align memory allocation on
* multiple 16 boundary */
block = pool->block;
remain = block->size - ((size_t)block->free - (size_t)block->bp);
if (remain < size)
{
/* Allocate a new block */
block_size = ((size > block->size) ? size : block->size);
new_block = mpool_new_block (block_size);
new_block->next = block;
block = pool->block = new_block;
}
ret = block->free;
block->free = block->bp + (block->free - block->bp + size);
return ret;
}
/**
* @brief Makes a copy of a string with known size
*
* @param pool
* @param str
* @param n
* @return
*****************************************************************************/
void *mpool_strndup (struct mpool *pool, const char *str, size_t n)
{
void *ret;
if (!str)
return NULL;
if ((ret = mpool_malloc(pool, n+1)))
{
strncpy((char *)ret, str, n);
((char *)ret)[n] = '\0';
}
return ret;
}
/**
* @brief Makes a copy of zero terminated string
*
* @param pool
* @param str
* @return
******************************************************************************/
void *mpool_strdup (struct mpool *pool, const char *str)
{
size_t len;
if (!str)
return NULL;
len = strlen(str);
return mpool_strndup (pool, str, len);
}