aboutsummaryrefslogtreecommitdiff
path: root/src/lib/memorypool.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/memorypool.h')
-rw-r--r--src/lib/memorypool.h130
1 files changed, 130 insertions, 0 deletions
diff --git a/src/lib/memorypool.h b/src/lib/memorypool.h
new file mode 100644
index 00000000..36136af8
--- /dev/null
+++ b/src/lib/memorypool.h
@@ -0,0 +1,130 @@
1/*
2 This file is part of libmicrohttpd
3 Copyright (C) 2007, 2009 Daniel Pittman and Christian Grothoff
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18*/
19
20/**
21 * @file memorypool.h
22 * @brief memory pool; mostly used for efficient (de)allocation
23 * for each connection and bounding memory use for each
24 * request
25 * @author Christian Grothoff
26 */
27
28#ifndef MEMORYPOOL_H
29#define MEMORYPOOL_H
30
31#include "internal.h"
32
33/**
34 * Opaque handle for a memory pool.
35 * Pools are not reentrant and must not be used
36 * by multiple threads.
37 */
38struct MemoryPool;
39
40
41/**
42 * Create a memory pool.
43 *
44 * @param max maximum size of the pool
45 * @return NULL on error
46 */
47struct MemoryPool *
48MHD_pool_create (size_t max);
49
50
51/**
52 * Destroy a memory pool.
53 *
54 * @param pool memory pool to destroy
55 */
56void
57MHD_pool_destroy (struct MemoryPool *pool);
58
59
60/**
61 * Allocate size bytes from the pool.
62 *
63 * @param pool memory pool to use for the operation
64 * @param size number of bytes to allocate
65 * @param from_end allocate from end of pool (set to #MHD_YES);
66 * use this for small, persistent allocations that
67 * will never be reallocated
68 * @return NULL if the pool cannot support size more
69 * bytes
70 */
71void *
72MHD_pool_allocate (struct MemoryPool *pool,
73 size_t size,
74 int from_end);
75
76
77/**
78 * Reallocate a block of memory obtained from the pool.
79 * This is particularly efficient when growing or
80 * shrinking the block that was last (re)allocated.
81 * If the given block is not the most recently
82 * (re)allocated block, the memory of the previous
83 * allocation may be leaked until the pool is
84 * destroyed (and copying the data maybe required).
85 *
86 * @param pool memory pool to use for the operation
87 * @param old the existing block
88 * @param old_size the size of the existing block
89 * @param new_size the new size of the block
90 * @return new address of the block, or
91 * NULL if the pool cannot support new_size
92 * bytes (old continues to be valid for old_size)
93 */
94void *
95MHD_pool_reallocate (struct MemoryPool *pool,
96 void *old,
97 size_t old_size,
98 size_t new_size);
99
100
101/**
102 * Check how much memory is left in the @a pool
103 *
104 * @param pool pool to check
105 * @return number of bytes still available in @a pool
106 */
107size_t
108MHD_pool_get_free (struct MemoryPool *pool);
109
110
111/**
112 * Clear all entries from the memory pool except
113 * for @a keep of the given @a copy_bytes. The pointer
114 * returned should be a buffer of @a new_size where
115 * the first @a copy_bytes are from @a keep.
116 *
117 * @param pool memory pool to use for the operation
118 * @param keep pointer to the entry to keep (maybe NULL)
119 * @param copy_bytes how many bytes need to be kept at this address
120 * @param new_size how many bytes should the allocation we return have?
121 * (should be larger or equal to @a copy_bytes)
122 * @return addr new address of @a keep (if it had to change)
123 */
124void *
125MHD_pool_reset (struct MemoryPool *pool,
126 void *keep,
127 size_t copy_bytes,
128 size_t new_size);
129
130#endif