ucx/mempool.h

Mon, 04 Feb 2019 14:46:11 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Mon, 04 Feb 2019 14:46:11 +0100
changeset 156
62f1a55535e7
parent 152
62921b370c60
permissions
-rw-r--r--

new build system

0
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1 /*
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
3 *
152
62921b370c60 fixes use after free when a GtkTreeView was destroyed
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 124
diff changeset
4 * Copyright 2016 Olaf Wintermann. All rights reserved.
0
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
5 *
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
8 *
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
11 *
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
15 *
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
27 */
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
28
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
29 /**
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
30 * @file mempool.h
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
31 *
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
32 * Memory pool implementation.
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
33 *
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
34 * @author Mike Becker
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
35 * @author Olaf Wintermann
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
36 */
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
37
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
38 #ifndef UCX_MEMPOOL_H
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
39 #define UCX_MEMPOOL_H
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
40
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
41 #include "ucx.h"
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
42 #include <stddef.h>
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
43 #include "allocator.h"
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
44
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
45 #ifdef __cplusplus
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
46 extern "C" {
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
47 #endif
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
48
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
49 /**
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
50 * UCX mempool structure.
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
51 */
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
52 typedef struct {
2
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
53 /** UcxAllocator based on this pool */
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
54 UcxAllocator *allocator;
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
55
0
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
56 /** List of pointers to pooled memory. */
2
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
57 void **data;
0
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
58
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
59 /** Count of pooled memory items. */
2
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
60 size_t ndata;
0
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
61
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
62 /** Memory pool size. */
2
eeb50c534497 added support for replaceable documents
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
63 size_t size;
0
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
64 } UcxMempool;
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
65
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
66 /** Shorthand for a new default memory pool with a capacity of 16 elements. */
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
67 #define ucx_mempool_new_default() ucx_mempool_new(16)
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
68
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
69
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
70 /**
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
71 * Creates a memory pool with the specified initial size.
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
72 *
152
62921b370c60 fixes use after free when a GtkTreeView was destroyed
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 124
diff changeset
73 * As the created memory pool automatically grows in size by factor two when
0
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
74 * trying to allocate memory on a full pool, it is recommended that you use
152
62921b370c60 fixes use after free when a GtkTreeView was destroyed
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 124
diff changeset
75 * a power of two for the initial size.
0
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
76 *
152
62921b370c60 fixes use after free when a GtkTreeView was destroyed
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 124
diff changeset
77 * @param n initial pool size (should be a power of two, e.g. 16)
0
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
78 * @return a pointer to the new memory pool
152
62921b370c60 fixes use after free when a GtkTreeView was destroyed
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 124
diff changeset
79 * @see ucx_mempool_new_default()
0
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
80 */
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
81 UcxMempool *ucx_mempool_new(size_t n);
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
82
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
83 /**
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
84 * Resizes a memory pool.
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
85 *
152
62921b370c60 fixes use after free when a GtkTreeView was destroyed
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 124
diff changeset
86 * This function will fail if the new capacity is not sufficient for the
62921b370c60 fixes use after free when a GtkTreeView was destroyed
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 124
diff changeset
87 * present data.
62921b370c60 fixes use after free when a GtkTreeView was destroyed
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 124
diff changeset
88 *
0
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
89 * @param pool the pool to resize
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
90 * @param newcap the new capacity
152
62921b370c60 fixes use after free when a GtkTreeView was destroyed
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 124
diff changeset
91 * @return zero on success or non-zero on failure
0
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
92 */
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
93 int ucx_mempool_chcap(UcxMempool *pool, size_t newcap);
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
94
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
95 /**
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
96 * Allocates pooled memory.
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
97 *
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
98 * @param pool the memory pool
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
99 * @param n amount of memory to allocate
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
100 * @return a pointer to the allocated memory
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
101 * @see ucx_allocator_malloc()
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
102 */
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
103 void *ucx_mempool_malloc(UcxMempool *pool, size_t n);
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
104 /**
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
105 * Allocates a pooled memory array.
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
106 *
124
80609f9675f1 added support for icons for the table widget (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
107 * The content of the allocated memory is set to zero.
0
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
108 *
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
109 * @param pool the memory pool
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
110 * @param nelem amount of elements to allocate
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
111 * @param elsize amount of memory per element
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
112 * @return a pointer to the allocated memory
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
113 * @see ucx_allocator_calloc()
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
114 */
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
115 void *ucx_mempool_calloc(UcxMempool *pool, size_t nelem, size_t elsize);
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
116
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
117 /**
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
118 * Reallocates pooled memory.
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
119 *
152
62921b370c60 fixes use after free when a GtkTreeView was destroyed
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 124
diff changeset
120 * If the memory to be reallocated is not contained by the specified pool, the
62921b370c60 fixes use after free when a GtkTreeView was destroyed
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 124
diff changeset
121 * behavior is undefined.
0
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
122 *
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
123 * @param pool the memory pool
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
124 * @param ptr a pointer to the memory that shall be reallocated
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
125 * @param n the new size of the memory
124
80609f9675f1 added support for icons for the table widget (GTK)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
126 * @return a pointer to the new location of the memory
0
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
127 * @see ucx_allocator_realloc()
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
128 */
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
129 void *ucx_mempool_realloc(UcxMempool *pool, void *ptr, size_t n);
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
130
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
131 /**
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
132 * Frees pooled memory.
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
133 *
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
134 * Before freeing the memory, the specified destructor function (if any)
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
135 * is called.
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
136 *
152
62921b370c60 fixes use after free when a GtkTreeView was destroyed
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 124
diff changeset
137 * If you specify memory, that is not pooled by the specified memory pool, the
62921b370c60 fixes use after free when a GtkTreeView was destroyed
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 124
diff changeset
138 * program will terminate with a call to <code>abort()</code>.
0
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
139 *
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
140 * @param pool the memory pool
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
141 * @param ptr a pointer to the memory that shall be freed
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
142 * @see ucx_mempool_set_destr()
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
143 */
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
144 void ucx_mempool_free(UcxMempool *pool, void *ptr);
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
145
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
146 /**
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
147 * Destroys a memory pool.
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
148 *
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
149 * For each element the destructor function (if any) is called and the element
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
150 * is freed.
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
151 *
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
152 * Each of the registered destructor function that has no corresponding element
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
153 * within the pool (namely those registered by ucx_mempool_reg_destr) is
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
154 * called interleaving with the element destruction, but with guarantee to the
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
155 * order in which they were registered (FIFO order).
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
156 *
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
157 *
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
158 * @param pool the mempool to destroy
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
159 */
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
160 void ucx_mempool_destroy(UcxMempool *pool);
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
161
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
162 /**
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
163 * Sets a destructor function for the specified memory.
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
164 *
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
165 * The destructor is automatically called when the memory is freed or the
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
166 * pool is destroyed.
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
167 *
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
168 * The only requirement for the specified memory is, that it <b>MUST</b> be
152
62921b370c60 fixes use after free when a GtkTreeView was destroyed
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 124
diff changeset
169 * pooled memory by a UcxMempool or an element-compatible mempool. The pointer
0
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
170 * to the destructor function is saved in a reserved area before the actual
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
171 * memory.
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
172 *
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
173 * @param ptr pooled memory
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
174 * @param func a pointer to the destructor function
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
175 * @see ucx_mempool_free()
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
176 * @see ucx_mempool_destroy()
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
177 */
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
178 void ucx_mempool_set_destr(void *ptr, ucx_destructor func);
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
179
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
180 /**
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
181 * Registers a destructor function for the specified (non-pooled) memory.
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
182 *
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
183 * This is useful, if you have memory that has not been allocated by a mempool,
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
184 * but shall be managed by a mempool.
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
185 *
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
186 * This function creates an entry in the specified mempool and the memory will
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
187 * therefore (logically) convert to pooled memory.
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
188 *
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
189 * @param pool the memory pool
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
190 * @param ptr data the destructor is registered for
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
191 * @param destr a pointer to the destructor function
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
192 */
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
193 void ucx_mempool_reg_destr(UcxMempool *pool, void *ptr, ucx_destructor destr);
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
194
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
195 #ifdef __cplusplus
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
196 }
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
197 #endif
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
198
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
199 #endif /* UCX_MEMPOOL_H */
1f419bd32da1 added files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
200

mercurial