summaryrefslogtreecommitdiff
path: root/accel-pptpd/triton/mempool.c
blob: 34739b43b993250a875143a4c9027ebb949bdec1 (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
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>

#include "triton_p.h"

#include "memdebug.h"

struct _mempool_t
{
	struct list_head entry;
	int size;
	struct list_head items;
	spinlock_t lock;
	uint64_t magic;
};

struct _item_t
{
	struct _mempool_t *owner;
	struct list_head entry;
	uint64_t magic;
	char ptr[0];
};

static LIST_HEAD(pools);
static spinlock_t pools_lock = SPINLOCK_INITIALIZER;

__export mempool_t *mempool_create(int size)
{
	struct _mempool_t *p = _malloc(sizeof(*p));

	memset(p, 0, sizeof(*p));
	INIT_LIST_HEAD(&p->items);
	spinlock_init(&p->lock);
	p->size = size;
	p->magic = (uint64_t)random() * (uint64_t)random();

	spin_lock(&pools_lock);
	list_add_tail(&p->entry, &pools);
	spin_unlock(&pools_lock);

	return (mempool_t *)p;
}

#ifndef MEMDEBUG
__export void *mempool_alloc(mempool_t *pool)
{
	struct _mempool_t *p = (struct _mempool_t *)pool;
	struct _item_t *it;
	uint32_t size = sizeof(*it) + p->size;

	spin_lock(&p->lock);
	if (!list_empty(&p->items)) {
		it = list_entry(p->items.next, typeof(*it), entry);
		list_del(&it->entry);
		spin_unlock(&p->lock);
		
		__sync_fetch_and_sub(&triton_stat.mempool_available, size);
		
		return it->ptr;
	}
	spin_unlock(&p->lock);

	it = _malloc(size);
	if (!it) {
		triton_log_error("mempool: out of memory\n");
		return NULL;
	}
	it->owner = p;
	it->magic = p->magic;

	__sync_fetch_and_add(&triton_stat.mempool_allocated, size);

	return it->ptr;
}
#endif

void __export *mempool_alloc_md(mempool_t *pool, const char *fname, int line)
{
	struct _mempool_t *p = (struct _mempool_t *)pool;
	struct _item_t *it;
	uint32_t size = sizeof(*it) + p->size;

	spin_lock(&p->lock);
	if (!list_empty(&p->items)) {
		it = list_entry(p->items.next, typeof(*it), entry);
		list_del(&it->entry);
		spin_unlock(&p->lock);
		
		__sync_fetch_and_sub(&triton_stat.mempool_available, size);
		
		return it->ptr;
	}
	spin_unlock(&p->lock);

	it = md_malloc(size, fname, line);
	if (!it) {
		triton_log_error("mempool: out of memory\n");
		return NULL;
	}
	it->owner = p;
	it->magic = p->magic;

	__sync_fetch_and_add(&triton_stat.mempool_allocated, size);

	return it->ptr;
}


__export void mempool_free(void *ptr)
{
	struct _item_t *it = container_of(ptr, typeof(*it), ptr);
	uint32_t size = sizeof(*it) + it->owner->size;

	if (it->magic != it->owner->magic) {
		triton_log_error("mempool: memory corruption detected");
		abort();
	}
	spin_lock(&it->owner->lock);
	list_add_tail(&it->entry,&it->owner->items);
	spin_unlock(&it->owner->lock);

	__sync_fetch_and_add(&triton_stat.mempool_available, size);
}

void sigclean(int num)
{
	struct _mempool_t *p;
	struct _item_t *it;
	uint32_t size;

	triton_log_error("mempool: clean\n");

	spin_lock(&pools_lock);
	list_for_each_entry(p, &pools, entry) {
		size = sizeof(*it) + p->size;
		spin_lock(&p->lock);
		while (!list_empty(&p->items)) {
			it = list_entry(p->items.next, typeof(*it), entry);
			list_del(&it->entry);
			_free(it);
			__sync_fetch_and_sub(&triton_stat.mempool_allocated, size);
			__sync_fetch_and_sub(&triton_stat.mempool_available, size);
		}
		spin_unlock(&p->lock);
	}
	spin_unlock(&pools_lock);
}

static void __init init(void)
{
	signal(35, sigclean);
}