chiark / gitweb /
76fdba781b7e875ff76dc11dc019dc69622472a2
[elogind.git] / src / basic / mempool.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3   Copyright 2014 Michal Schmidt
4 ***/
5
6 #include <stdint.h>
7 #include <stdlib.h>
8
9 #include "macro.h"
10 #include "mempool.h"
11 #include "util.h"
12
13 struct pool {
14         struct pool *next;
15         size_t n_tiles;
16         size_t n_used;
17 };
18
19 void* mempool_alloc_tile(struct mempool *mp) {
20         size_t i;
21
22         /* When a tile is released we add it to the list and simply
23          * place the next pointer at its offset 0. */
24
25         assert(mp->tile_size >= sizeof(void*));
26         assert(mp->at_least > 0);
27
28         if (mp->freelist) {
29                 void *r;
30
31                 r = mp->freelist;
32                 mp->freelist = * (void**) mp->freelist;
33                 return r;
34         }
35
36         if (_unlikely_(!mp->first_pool) ||
37             _unlikely_(mp->first_pool->n_used >= mp->first_pool->n_tiles)) {
38                 size_t size, n;
39                 struct pool *p;
40
41                 n = mp->first_pool ? mp->first_pool->n_tiles : 0;
42                 n = MAX(mp->at_least, n * 2);
43                 size = PAGE_ALIGN(ALIGN(sizeof(struct pool)) + n*mp->tile_size);
44                 n = (size - ALIGN(sizeof(struct pool))) / mp->tile_size;
45
46                 p = malloc(size);
47                 if (!p)
48                         return NULL;
49
50                 p->next = mp->first_pool;
51                 p->n_tiles = n;
52                 p->n_used = 0;
53
54                 mp->first_pool = p;
55         }
56
57         i = mp->first_pool->n_used++;
58
59         return ((uint8_t*) mp->first_pool) + ALIGN(sizeof(struct pool)) + i*mp->tile_size;
60 }
61
62 void* mempool_alloc0_tile(struct mempool *mp) {
63         void *p;
64
65         p = mempool_alloc_tile(mp);
66         if (p)
67                 memzero(p, mp->tile_size);
68         return p;
69 }
70
71 void mempool_free_tile(struct mempool *mp, void *p) {
72         * (void**) p = mp->freelist;
73         mp->freelist = p;
74 }
75
76 #if VALGRIND
77
78 void mempool_drop(struct mempool *mp) {
79         struct pool *p = mp->first_pool;
80         while (p) {
81                 struct pool *n;
82                 n = p->next;
83                 free(p);
84                 p = n;
85         }
86 }
87
88 #endif