Mon, 21 Jul 2014 13:18:32 +0200
added some string tests
103
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
69
diff
changeset
|
1 | /* |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
69
diff
changeset
|
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
69
diff
changeset
|
3 | * |
177
11ad03783baf
go ahead, nothing to see here
Mike Becker <universe@uap-core.de>
parents:
158
diff
changeset
|
4 | * Copyright 2014 Olaf Wintermann. All rights reserved. |
103
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
69
diff
changeset
|
5 | * |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
69
diff
changeset
|
6 | * Redistribution and use in source and binary forms, with or without |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
69
diff
changeset
|
7 | * modification, are permitted provided that the following conditions are met: |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
69
diff
changeset
|
8 | * |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
69
diff
changeset
|
9 | * 1. Redistributions of source code must retain the above copyright |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
69
diff
changeset
|
10 | * notice, this list of conditions and the following disclaimer. |
13 | 11 | * |
103
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
69
diff
changeset
|
12 | * 2. Redistributions in binary form must reproduce the above copyright |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
69
diff
changeset
|
13 | * notice, this list of conditions and the following disclaimer in the |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
69
diff
changeset
|
14 | * documentation and/or other materials provided with the distribution. |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
69
diff
changeset
|
15 | * |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
69
diff
changeset
|
16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
69
diff
changeset
|
17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
69
diff
changeset
|
18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
69
diff
changeset
|
19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
69
diff
changeset
|
20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
69
diff
changeset
|
21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
69
diff
changeset
|
22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
69
diff
changeset
|
23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
69
diff
changeset
|
24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
69
diff
changeset
|
25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
08018864fb91
added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents:
69
diff
changeset
|
26 | * POSSIBILITY OF SUCH DAMAGE. |
13 | 27 | */ |
28 | ||
135
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
29 | /** |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
30 | * @file mempool.h |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
31 | * |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
32 | * Memory pool implementation. |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
33 | * |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
34 | * @author Mike Becker |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
35 | * @author Olaf Wintermann |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
36 | */ |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
37 | |
120 | 38 | #ifndef UCX_MEMPOOL_H |
39 | #define UCX_MEMPOOL_H | |
13 | 40 | |
69
fb59270b1de3
made the code work with VC++ compiler (use make CONF=windows)
Mike Becker <universe@uap-core.de>
parents:
50
diff
changeset
|
41 | #include "ucx.h" |
38
35f67a8ef875
added missing stddef includes
Mike Becker <universe@uap-core.de>
parents:
16
diff
changeset
|
42 | #include <stddef.h> |
50
ff194559eb41
moved allocator to seperate file
Mike Becker <universe@uap-core.de>
parents:
48
diff
changeset
|
43 | #include "allocator.h" |
38
35f67a8ef875
added missing stddef includes
Mike Becker <universe@uap-core.de>
parents:
16
diff
changeset
|
44 | |
13 | 45 | #ifdef __cplusplus |
46 | extern "C" { | |
47 | #endif | |
48 | ||
135
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
49 | /** |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
50 | * A function pointer to a destructor function. |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
51 | * @see ucx_mempool_setdestr() |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
52 | * @see ucx_mempool_regdestr() |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
53 | */ |
13 | 54 | typedef void(*ucx_destructor)(void*); |
55 | ||
135
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
56 | /** |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
57 | * UCX mempool structure. |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
58 | */ |
13 | 59 | typedef struct { |
158
81d580042da1
Added allocator to mempool struct + fixed suncc.mk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
146
diff
changeset
|
60 | /** UcxAllocator based on this pool */ |
81d580042da1
Added allocator to mempool struct + fixed suncc.mk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
146
diff
changeset
|
61 | UcxAllocator *allocator; |
81d580042da1
Added allocator to mempool struct + fixed suncc.mk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
146
diff
changeset
|
62 | |
135
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
63 | /** List of pointers to pooled memory. */ |
158
81d580042da1
Added allocator to mempool struct + fixed suncc.mk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
146
diff
changeset
|
64 | void **data; |
146
aa376dba1ba8
fixed documentation for netbeans parser + added sstrprefix() and sstrsuffix()
Mike Becker <universe@uap-core.de>
parents:
141
diff
changeset
|
65 | |
135
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
66 | /** Count of pooled memory items. */ |
158
81d580042da1
Added allocator to mempool struct + fixed suncc.mk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
146
diff
changeset
|
67 | size_t ndata; |
146
aa376dba1ba8
fixed documentation for netbeans parser + added sstrprefix() and sstrsuffix()
Mike Becker <universe@uap-core.de>
parents:
141
diff
changeset
|
68 | |
135
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
69 | /** Memory pool size. */ |
158
81d580042da1
Added allocator to mempool struct + fixed suncc.mk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
146
diff
changeset
|
70 | size_t size; |
13 | 71 | } UcxMempool; |
72 | ||
135
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
73 | /** Shorthand for a new default memory pool with a capacity of 16 elements. */ |
13 | 74 | #define ucx_mempool_new_default() ucx_mempool_new(16) |
135
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
75 | |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
76 | |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
77 | /** |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
78 | * Creates a memory pool with the specified initial size. |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
79 | * |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
80 | * As the created memory pool automatically grows in size by 16 elements, when |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
81 | * trying to allocate memory on a full pool, it is recommended that you use |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
82 | * a multiple of 16 for the initial size. |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
83 | * |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
84 | * @param n initial pool size (should be a multiple of 16) |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
85 | * @return a pointer to the new memory pool |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
86 | */ |
13 | 87 | UcxMempool *ucx_mempool_new(size_t n); |
135
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
88 | |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
89 | /** |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
90 | * Resizes a memory pool. |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
91 | * |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
92 | * @param pool the pool to resize |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
93 | * @param newcap the new capacity |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
94 | * @return <code>EXIT_SUCCESS</code> on success or |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
95 | * <code>EXIT_FAILURE</code> on failure |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
96 | */ |
15
2dc4c688c262
fixed mpool compiler warnings
Mike Becker <universe@uap-core.de>
parents:
13
diff
changeset
|
97 | int ucx_mempool_chcap(UcxMempool *pool, size_t newcap); |
13 | 98 | |
135
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
99 | /** |
141
c466e2a6cbd0
added mempool clamp + some minor fixes
Mike Becker <universe@uap-core.de>
parents:
135
diff
changeset
|
100 | * Changes the pool size to the next smallest multiple of 16. |
c466e2a6cbd0
added mempool clamp + some minor fixes
Mike Becker <universe@uap-core.de>
parents:
135
diff
changeset
|
101 | * |
c466e2a6cbd0
added mempool clamp + some minor fixes
Mike Becker <universe@uap-core.de>
parents:
135
diff
changeset
|
102 | * You may use this macro, to reduce the pool size after freeing |
c466e2a6cbd0
added mempool clamp + some minor fixes
Mike Becker <universe@uap-core.de>
parents:
135
diff
changeset
|
103 | * many pooled memory items. |
c466e2a6cbd0
added mempool clamp + some minor fixes
Mike Becker <universe@uap-core.de>
parents:
135
diff
changeset
|
104 | * |
c466e2a6cbd0
added mempool clamp + some minor fixes
Mike Becker <universe@uap-core.de>
parents:
135
diff
changeset
|
105 | * @param pool the pool to clamp |
c466e2a6cbd0
added mempool clamp + some minor fixes
Mike Becker <universe@uap-core.de>
parents:
135
diff
changeset
|
106 | * @return <code>EXIT_SUCCESS</code> on success or |
c466e2a6cbd0
added mempool clamp + some minor fixes
Mike Becker <universe@uap-core.de>
parents:
135
diff
changeset
|
107 | * <code>EXIT_FAILURE</code> on failure |
c466e2a6cbd0
added mempool clamp + some minor fixes
Mike Becker <universe@uap-core.de>
parents:
135
diff
changeset
|
108 | */ |
c466e2a6cbd0
added mempool clamp + some minor fixes
Mike Becker <universe@uap-core.de>
parents:
135
diff
changeset
|
109 | #define ucx_mempool_clamp(pool) ucx_mempool_chcap(pool, \ |
c466e2a6cbd0
added mempool clamp + some minor fixes
Mike Becker <universe@uap-core.de>
parents:
135
diff
changeset
|
110 | (pool->ndata & ~0xF)+0x10) |
c466e2a6cbd0
added mempool clamp + some minor fixes
Mike Becker <universe@uap-core.de>
parents:
135
diff
changeset
|
111 | |
c466e2a6cbd0
added mempool clamp + some minor fixes
Mike Becker <universe@uap-core.de>
parents:
135
diff
changeset
|
112 | /** |
135
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
113 | * Allocates pooled memory. |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
114 | * |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
115 | * @param pool the memory pool |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
116 | * @param n amount of memory to allocate |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
117 | * @return a pointer to the allocated memory |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
118 | * @see ucx_allocator_malloc() |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
119 | */ |
13 | 120 | void *ucx_mempool_malloc(UcxMempool *pool, size_t n); |
135
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
121 | /** |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
122 | * Allocates a pooled memory array. |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
123 | * |
181
1e9012ad8215
fixed stream copy bug + fixed doc issues in mempool
Mike Becker <universe@uap-core.de>
parents:
177
diff
changeset
|
124 | * The content of the allocated memory is set to zero. |
135
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
125 | * |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
126 | * @param pool the memory pool |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
127 | * @param nelem amount of elements to allocate |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
128 | * @param elsize amount of memory per element |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
129 | * @return a pointer to the allocated memory |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
130 | * @see ucx_allocator_calloc() |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
131 | */ |
13 | 132 | void *ucx_mempool_calloc(UcxMempool *pool, size_t nelem, size_t elsize); |
146
aa376dba1ba8
fixed documentation for netbeans parser + added sstrprefix() and sstrsuffix()
Mike Becker <universe@uap-core.de>
parents:
141
diff
changeset
|
133 | |
135
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
134 | /** |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
135 | * Reallocates pooled memory. |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
136 | * |
141
c466e2a6cbd0
added mempool clamp + some minor fixes
Mike Becker <universe@uap-core.de>
parents:
135
diff
changeset
|
137 | * If the memory to be reallocated is not contained by the specified pool, this |
c466e2a6cbd0
added mempool clamp + some minor fixes
Mike Becker <universe@uap-core.de>
parents:
135
diff
changeset
|
138 | * function will possibly fail. In case the memory had to be moved to another |
c466e2a6cbd0
added mempool clamp + some minor fixes
Mike Becker <universe@uap-core.de>
parents:
135
diff
changeset
|
139 | * location, this function will print out a message to <code>stderr</code> |
c466e2a6cbd0
added mempool clamp + some minor fixes
Mike Becker <universe@uap-core.de>
parents:
135
diff
changeset
|
140 | * and exit the program with error code <code>EXIT_FAILURE</code>. |
c466e2a6cbd0
added mempool clamp + some minor fixes
Mike Becker <universe@uap-core.de>
parents:
135
diff
changeset
|
141 | * |
135
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
142 | * @param pool the memory pool |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
143 | * @param ptr a pointer to the memory that shall be reallocated |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
144 | * @param n the new size of the memory |
181
1e9012ad8215
fixed stream copy bug + fixed doc issues in mempool
Mike Becker <universe@uap-core.de>
parents:
177
diff
changeset
|
145 | * @return a pointer to the new location of the memory |
135
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
146 | * @see ucx_allocator_realloc() |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
147 | */ |
13 | 148 | void *ucx_mempool_realloc(UcxMempool *pool, void *ptr, size_t n); |
146
aa376dba1ba8
fixed documentation for netbeans parser + added sstrprefix() and sstrsuffix()
Mike Becker <universe@uap-core.de>
parents:
141
diff
changeset
|
149 | |
135
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
150 | /** |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
151 | * Frees pooled memory. |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
152 | * |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
153 | * Before freeing the memory, the specified destructor function (if any) |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
154 | * is called. |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
155 | * |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
156 | * If you specify memory, that is not pooled by the specified memory pool, this |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
157 | * is considered as a severe error and an error message is written to |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
158 | * <code>stderr</code> and the application is shut down with code |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
159 | * <code>EXIT_FAILURE</code>. |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
160 | * |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
161 | * @param pool the memory pool |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
162 | * @param ptr a pointer to the memory that shall be freed |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
163 | * @see ucx_mempool_set_destr() |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
164 | */ |
113
8693d7874773
added mempool allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
103
diff
changeset
|
165 | void ucx_mempool_free(UcxMempool *pool, void *ptr); |
13 | 166 | |
135
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
167 | /** |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
168 | * Destroys a memory pool. |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
169 | * |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
170 | * For each element the destructor function (if any) is called and the element |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
171 | * is freed. |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
172 | * |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
173 | * Each of the registered destructor function that has no corresponding element |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
174 | * within the pool (namely those registered by ucx_mempool_reg_destr) is |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
175 | * called interleaving with the element destruction, but with guarantee to the |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
176 | * order in which they were registered (FIFO order). |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
177 | * |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
178 | * |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
179 | * @param pool the mempool to destroy |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
180 | */ |
113
8693d7874773
added mempool allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
103
diff
changeset
|
181 | void ucx_mempool_destroy(UcxMempool *pool); |
13 | 182 | |
135
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
183 | /** |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
184 | * Sets a destructor function for the specified memory. |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
185 | * |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
186 | * The destructor is automatically called when the memory is freed or the |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
187 | * pool is destroyed. |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
188 | * |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
189 | * The only requirement for the specified memory is, that it <b>MUST</b> be |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
190 | * pooled memory by an UcxMempool or an element-compatible mempool. The pointer |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
191 | * to the destructor function is saved in a reserved area before the actual |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
192 | * memory. |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
193 | * |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
194 | * @param ptr pooled memory |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
195 | * @param func a pointer to the destructor function |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
196 | * @see ucx_mempool_free() |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
197 | * @see ucx_mempool_destroy() |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
198 | */ |
13 | 199 | void ucx_mempool_set_destr(void *ptr, ucx_destructor func); |
135
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
200 | |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
201 | /** |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
202 | * Registers a destructor function for the specified (non-pooled) memory. |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
203 | * |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
204 | * This is useful, if you have memory that has not been allocated by a mempool, |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
205 | * but shall be managed by a mempool. |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
206 | * |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
207 | * This function creates an entry in the specified mempool and the memory will |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
208 | * therefore (logically) convert to pooled memory. |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
209 | * |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
210 | * @param pool the memory pool |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
211 | * @param ptr data the destructor is registered for |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
212 | * @param destr a pointer to the destructor function |
a0aa1c15f46b
documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents:
120
diff
changeset
|
213 | */ |
13 | 214 | void ucx_mempool_reg_destr(UcxMempool *pool, void *ptr, ucx_destructor destr); |
215 | ||
216 | #ifdef __cplusplus | |
217 | } | |
218 | #endif | |
219 | ||
120 | 220 | #endif /* UCX_MEMPOOL_H */ |
13 | 221 |