]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/mempool.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2010-2014 Lennart Poettering
6 Copyright 2014 Michal Schmidt
22 void* mempool_alloc_tile(struct mempool
*mp
) {
25 /* When a tile is released we add it to the list and simply
26 * place the next pointer at its offset 0. */
28 assert(mp
->tile_size
>= sizeof(void*));
29 assert(mp
->at_least
> 0);
35 mp
->freelist
= * (void**) mp
->freelist
;
39 if (_unlikely_(!mp
->first_pool
) ||
40 _unlikely_(mp
->first_pool
->n_used
>= mp
->first_pool
->n_tiles
)) {
44 n
= mp
->first_pool
? mp
->first_pool
->n_tiles
: 0;
45 n
= MAX(mp
->at_least
, n
* 2);
46 size
= PAGE_ALIGN(ALIGN(sizeof(struct pool
)) + n
*mp
->tile_size
);
47 n
= (size
- ALIGN(sizeof(struct pool
))) / mp
->tile_size
;
53 p
->next
= mp
->first_pool
;
60 i
= mp
->first_pool
->n_used
++;
62 return ((uint8_t*) mp
->first_pool
) + ALIGN(sizeof(struct pool
)) + i
*mp
->tile_size
;
65 void* mempool_alloc0_tile(struct mempool
*mp
) {
68 p
= mempool_alloc_tile(mp
);
70 memzero(p
, mp
->tile_size
);
74 void mempool_free_tile(struct mempool
*mp
, void *p
) {
75 * (void**) p
= mp
->freelist
;
81 void mempool_drop(struct mempool
*mp
) {
82 struct pool
*p
= mp
->first_pool
;