1/* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2// vim: ft=cpp:expandtab:ts=8:sw=4:softtabstop=4:
3#ident "$Id$"
4/*======
5This file is part of PerconaFT.
6
7
8Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved.
9
10 PerconaFT is free software: you can redistribute it and/or modify
11 it under the terms of the GNU General Public License, version 2,
12 as published by the Free Software Foundation.
13
14 PerconaFT is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with PerconaFT. If not, see <http://www.gnu.org/licenses/>.
21
22----------------------------------------
23
24 PerconaFT is free software: you can redistribute it and/or modify
25 it under the terms of the GNU Affero General Public License, version 3,
26 as published by the Free Software Foundation.
27
28 PerconaFT is distributed in the hope that it will be useful,
29 but WITHOUT ANY WARRANTY; without even the implied warranty of
30 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 GNU Affero General Public License for more details.
32
33 You should have received a copy of the GNU Affero General Public License
34 along with PerconaFT. If not, see <http://www.gnu.org/licenses/>.
35======= */
36
37#ident "Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved."
38
39#pragma once
40
41/* a memory pool is a contiguous region of memory that supports single
42 allocations from the pool. these allocated regions are never recycled.
43 when the memory pool no longer has free space, the allocated chunks
44 must be relocated by the application to a new memory pool. */
45
46#include <stddef.h>
47
48struct mempool;
49
50 // TODO 4050 Hide mempool struct internals from callers
51
52struct mempool {
53 void *base; /* the base address of the memory */
54 size_t free_offset; /* the offset of the memory pool free space */
55 size_t size; /* the size of the memory */
56 size_t frag_size; /* the size of the fragmented memory */
57};
58
59/* This is a constructor to be used when the memory for the mempool struct has been
60 * allocated by the caller, but no memory has yet been allocatd for the data.
61 */
62void toku_mempool_zero(struct mempool *mp);
63
64/* initialize the memory pool with the base address and size of a
65 contiguous chunk of memory */
66void toku_mempool_init(struct mempool *mp, void *base, size_t free_offset, size_t size);
67
68/* allocate memory and construct mempool
69 */
70void toku_mempool_construct(struct mempool *mp, size_t data_size);
71
72/* treat mempool as if it has just been created; ignore any frag and start allocating from beginning again.
73 */
74void toku_mempool_reset(struct mempool *mp);
75
76/* reallocate memory for construct mempool
77 */
78void toku_mempool_realloc_larger(struct mempool *mp, size_t data_size);
79
80/* destroy the memory pool */
81void toku_mempool_destroy(struct mempool *mp);
82
83/* get the base address of the memory pool */
84void *toku_mempool_get_base(const struct mempool *mp);
85
86/* get the a pointer that is offset bytes in front of base of the memory pool */
87void *toku_mempool_get_pointer_from_base_and_offset(const struct mempool *mp, size_t offset);
88
89/* get the offset from base of a pointer */
90size_t toku_mempool_get_offset_from_pointer_and_base(const struct mempool *mp, const void* p);
91
92/* get the a pointer of the first free byte (if any) */
93void* toku_mempool_get_next_free_ptr(const struct mempool *mp);
94
95/* get the limit of valid offsets. (anything later was not allocated) */
96size_t toku_mempool_get_offset_limit(const struct mempool *mp);
97
98/* get the size of the memory pool */
99size_t toku_mempool_get_size(const struct mempool *mp);
100
101/* get the amount of fragmented (wasted) space in the memory pool */
102size_t toku_mempool_get_frag_size(const struct mempool *mp);
103
104/* get the amount of space that is holding useful data */
105size_t toku_mempool_get_used_size(const struct mempool *mp);
106
107/* get the amount of space that is available for new data */
108size_t toku_mempool_get_free_size(const struct mempool *mp);
109
110/* get the amount of space that has been allocated for use (wasted or not) */
111size_t toku_mempool_get_allocated_size(const struct mempool *mp);
112
113/* allocate a chunk of memory from the memory pool */
114void *toku_mempool_malloc(struct mempool *mp, size_t size);
115
116/* free a previously allocated chunk of memory. the free only updates
117 a count of the amount of free space in the memory pool. the memory
118 pool does not keep track of the locations of the free chunks */
119void toku_mempool_mfree(struct mempool *mp, void *vp, size_t size);
120
121/* verify that a memory range is contained within a mempool */
122static inline int toku_mempool_inrange(struct mempool *mp, void *vp, size_t size) {
123 return (mp->base <= vp) && ((char *)vp + size <= (char *)mp->base + mp->size);
124}
125
126/* get memory footprint */
127size_t toku_mempool_footprint(struct mempool *mp);
128
129void toku_mempool_clone(const struct mempool* orig_mp, struct mempool* new_mp);
130