forked from DataSoft/Honeyd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pool.h
91 lines (77 loc) · 2.58 KB
/
pool.h
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
/*
* Copyright (c) 2003, 2004 Niels Provos <[email protected]>
* All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef _POOL_
#define _POOL_
#include <syslog.h>
#define POOL_PAGE_SIZE 4096
struct pool_entry {
SLIST_ENTRY(pool_entry) next;
void *data;
size_t size;
};
struct pool {
SLIST_HEAD(poolq, pool_entry) entries;
size_t size;
int nalloc;
};
struct pool *pool_init(size_t);
void *pool_alloc_size(struct pool *, size_t);
/*
* The pool interface cached allocation of fixed sized objects,
* but it can also be used to allocate larger buffers if necessary.
*/
static __inline void *
pool_alloc(struct pool *pool)
{
struct pool_entry *entry;
if ((entry = SLIST_FIRST(&pool->entries)) == NULL)
return (pool_alloc_size(pool, 0));
SLIST_REMOVE_HEAD(&pool->entries, next);
return (entry->data);
}
static __inline void
pool_free(struct pool *pool, void *addr)
{
struct pool_entry *entry = addr - sizeof(struct pool_entry);
if (entry->data != addr)
{
syslog(LOG_ERR, "%s: bad address: %p != %p", __func__, addr, entry->data);
exit(EXIT_FAILURE);
}
if (entry->size == pool->size)
SLIST_INSERT_HEAD(&pool->entries, entry, next);
else {
free(entry);
pool->nalloc--;
}
}
#endif /* _POOL_ */