forked from DataSoft/Honeyd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pool.c
114 lines (97 loc) · 3.02 KB
/
pool.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
/*
* 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
*/
#include <sys/types.h>
#include <sys/param.h>
#include "config.h"
#include <sys/queue.h>
#include <err.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <syslog.h>
#include "pool.h"
struct pool*
pool_init(size_t size)
{
struct pool *pool;
if (POOL_PAGE_SIZE / (sizeof(struct pool_entry) + size) < 1)
{
syslog(LOG_ERR, "%s: object size too large for pool", __func__);
exit(EXIT_FAILURE);
}
if ((pool = calloc(1, sizeof(struct pool))) == NULL)
{
syslog(LOG_ERR, "%s: calloc", __func__);
exit(EXIT_FAILURE);
}
SLIST_INIT(&pool->entries);
pool->size = size;
return (pool);
}
void *
pool_alloc_size(struct pool *pool, size_t size)
{
struct pool_entry *entry = NULL;
if (size) {
entry = malloc(size + sizeof(struct pool_entry));
if (entry == NULL)
{
syslog(LOG_ERR, "%s: malloc", __func__);
exit(EXIT_FAILURE);
}
entry->data = (void *)entry + sizeof(struct pool_entry);;
entry->size = size;
pool->nalloc++;
} else {
void *p = malloc(POOL_PAGE_SIZE);
int i, max;
if (p == NULL)
{
syslog(LOG_ERR, "%s: malloc", __func__);
exit(EXIT_FAILURE);
}
size = pool->size;
max = POOL_PAGE_SIZE / (sizeof(struct pool_entry) + size);
pool->nalloc += max;
for (i = 0; i < max; i++) {
entry = p;
entry->data = (void *)entry+ sizeof(struct pool_entry);
entry->size = size;
/* We want to use the last one as return */
if (i < max - 1) {
SLIST_INSERT_HEAD(&pool->entries, entry, next);
p += sizeof(struct pool_entry) + size;
}
}
}
return (entry->data);
}