]>
Commit | Line | Data |
---|---|---|
b3dcf58e MS |
1 | /*** |
2 | This file is part of systemd. | |
3 | ||
4 | Copyright 2010-2014 Lennart Poettering | |
5 | Copyright 2014 Michal Schmidt | |
6 | ||
7 | systemd is free software; you can redistribute it and/or modify it | |
8 | under the terms of the GNU Lesser General Public License as published by | |
9 | the Free Software Foundation; either version 2.1 of the License, or | |
10 | (at your option) any later version. | |
11 | ||
12 | systemd is distributed in the hope that it will be useful, but | |
13 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 | Lesser General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU Lesser General Public License | |
18 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
19 | ***/ | |
20 | ||
11c3a366 TA |
21 | #include <stdint.h> |
22 | #include <stdlib.h> | |
23 | ||
b3dcf58e | 24 | #include "macro.h" |
cf0fbc49 | 25 | #include "mempool.h" |
b3dcf58e MS |
26 | #include "util.h" |
27 | ||
28 | struct pool { | |
29 | struct pool *next; | |
30 | unsigned n_tiles; | |
31 | unsigned n_used; | |
32 | }; | |
33 | ||
34 | void* mempool_alloc_tile(struct mempool *mp) { | |
35 | unsigned i; | |
36 | ||
37 | /* When a tile is released we add it to the list and simply | |
38 | * place the next pointer at its offset 0. */ | |
39 | ||
40 | assert(mp->tile_size >= sizeof(void*)); | |
41 | assert(mp->at_least > 0); | |
42 | ||
43 | if (mp->freelist) { | |
44 | void *r; | |
45 | ||
46 | r = mp->freelist; | |
47 | mp->freelist = * (void**) mp->freelist; | |
48 | return r; | |
49 | } | |
50 | ||
51 | if (_unlikely_(!mp->first_pool) || | |
52 | _unlikely_(mp->first_pool->n_used >= mp->first_pool->n_tiles)) { | |
53 | unsigned n; | |
54 | size_t size; | |
55 | struct pool *p; | |
56 | ||
57 | n = mp->first_pool ? mp->first_pool->n_tiles : 0; | |
58 | n = MAX(mp->at_least, n * 2); | |
59 | size = PAGE_ALIGN(ALIGN(sizeof(struct pool)) + n*mp->tile_size); | |
60 | n = (size - ALIGN(sizeof(struct pool))) / mp->tile_size; | |
61 | ||
62 | p = malloc(size); | |
63 | if (!p) | |
64 | return NULL; | |
65 | ||
66 | p->next = mp->first_pool; | |
67 | p->n_tiles = n; | |
68 | p->n_used = 0; | |
69 | ||
70 | mp->first_pool = p; | |
71 | } | |
72 | ||
73 | i = mp->first_pool->n_used++; | |
74 | ||
75 | return ((uint8_t*) mp->first_pool) + ALIGN(sizeof(struct pool)) + i*mp->tile_size; | |
76 | } | |
77 | ||
52fc5ce3 MS |
78 | void* mempool_alloc0_tile(struct mempool *mp) { |
79 | void *p; | |
80 | ||
81 | p = mempool_alloc_tile(mp); | |
82 | if (p) | |
83 | memzero(p, mp->tile_size); | |
84 | return p; | |
85 | } | |
86 | ||
b3dcf58e MS |
87 | void mempool_free_tile(struct mempool *mp, void *p) { |
88 | * (void**) p = mp->freelist; | |
89 | mp->freelist = p; | |
90 | } | |
91 | ||
92 | #ifdef VALGRIND | |
93 | ||
94 | void mempool_drop(struct mempool *mp) { | |
95 | struct pool *p = mp->first_pool; | |
96 | while (p) { | |
97 | struct pool *n; | |
98 | n = p->next; | |
99 | free(p); | |
100 | p = n; | |
101 | } | |
102 | } | |
103 | ||
104 | #endif |