ucx/mempool.h

Wed, 14 Aug 2013 15:54:22 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Wed, 14 Aug 2013 15:54:22 +0200
changeset 144
b6dcc9d112eb
parent 141
c466e2a6cbd0
child 146
aa376dba1ba8
permissions
-rw-r--r--

fixed printf functions

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 *
08018864fb91 added license and copyright notice to all files
Mike Becker <universe@uap-core.de>
parents: 69
diff changeset
4 * Copyright 2013 Olaf Wintermann. All rights reserved.
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
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
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
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
27 */
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
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
8170f658f017 some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 113
diff changeset
38 #ifndef UCX_MEMPOOL_H
8170f658f017 some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 113
diff changeset
39 #define UCX_MEMPOOL_H
13
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
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
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
45 #ifdef __cplusplus
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
46 extern "C" {
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
47 #endif
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
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
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
54 typedef void(*ucx_destructor)(void*);
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
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
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
59 typedef struct {
135
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
60 /** List of pointers to pooled memory. */
13
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
61 void **data;
135
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
62 /** Count of pooled memory items. */
13
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
63 size_t ndata;
135
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
64 /** Memory pool size. */
13
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
65 size_t size;
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
66 } UcxMempool;
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
67
135
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
68 /** Shorthand for a new default memory pool with a capacity of 16 elements. */
13
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
69 #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
70
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
71
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
72 /**
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
73 * Creates a memory pool with the specified initial size.
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
74 *
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
75 * 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
76 * 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
77 * a multiple of 16 for the initial size.
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
78 *
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
79 * @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
80 * @return a pointer to the new memory pool
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
81 */
13
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
82 UcxMempool *ucx_mempool_new(size_t n);
135
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 /**
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
85 * Resizes a memory pool.
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
86 *
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
87 * @param pool the pool to resize
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
88 * @param newcap the new capacity
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
89 * @return <code>EXIT_SUCCESS</code> on success or
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
90 * <code>EXIT_FAILURE</code> on failure
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
91 */
15
2dc4c688c262 fixed mpool compiler warnings
Mike Becker <universe@uap-core.de>
parents: 13
diff changeset
92 int ucx_mempool_chcap(UcxMempool *pool, size_t newcap);
13
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
93
135
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
94 /**
141
c466e2a6cbd0 added mempool clamp + some minor fixes
Mike Becker <universe@uap-core.de>
parents: 135
diff changeset
95 * 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
96 *
c466e2a6cbd0 added mempool clamp + some minor fixes
Mike Becker <universe@uap-core.de>
parents: 135
diff changeset
97 * 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
98 * many pooled memory items.
c466e2a6cbd0 added mempool clamp + some minor fixes
Mike Becker <universe@uap-core.de>
parents: 135
diff changeset
99 *
c466e2a6cbd0 added mempool clamp + some minor fixes
Mike Becker <universe@uap-core.de>
parents: 135
diff changeset
100 * @param pool the pool to clamp
c466e2a6cbd0 added mempool clamp + some minor fixes
Mike Becker <universe@uap-core.de>
parents: 135
diff changeset
101 * @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
102 * <code>EXIT_FAILURE</code> on failure
c466e2a6cbd0 added mempool clamp + some minor fixes
Mike Becker <universe@uap-core.de>
parents: 135
diff changeset
103 */
c466e2a6cbd0 added mempool clamp + some minor fixes
Mike Becker <universe@uap-core.de>
parents: 135
diff changeset
104 #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
105 (pool->ndata & ~0xF)+0x10)
c466e2a6cbd0 added mempool clamp + some minor fixes
Mike Becker <universe@uap-core.de>
parents: 135
diff changeset
106
c466e2a6cbd0 added mempool clamp + some minor fixes
Mike Becker <universe@uap-core.de>
parents: 135
diff changeset
107 /**
135
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
108 * Allocates pooled memory.
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
109 *
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
110 * @param pool the memory pool
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
111 * @param n amount of memory to allocate
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
112 * @return a pointer to the allocated memory
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
113 * @see ucx_allocator_malloc()
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
114 */
13
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
115 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
116 /**
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
117 * Allocates a pooled memory array.
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
118 *
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
119 * The contents of the allocated memory is set to zero.
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
120 *
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
121 * @param pool the memory pool
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
122 * @param nelem amount of elements to allocate
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
123 * @param elsize amount of memory per element
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
124 * @return a pointer to the allocated memory
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
125 * @see ucx_allocator_calloc()
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
126 */
13
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
127 void *ucx_mempool_calloc(UcxMempool *pool, size_t nelem, size_t elsize);
135
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
128 /**
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
129 * Reallocates pooled memory.
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
130 *
141
c466e2a6cbd0 added mempool clamp + some minor fixes
Mike Becker <universe@uap-core.de>
parents: 135
diff changeset
131 * 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
132 * 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
133 * 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
134 * 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
135 *
135
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
136 * @param pool the memory pool
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
137 * @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
138 * @param n the new size of the memory
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
139 * @return a pointer to the the location of the memory
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
140 * @see ucx_allocator_realloc()
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
141 */
13
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
142 void *ucx_mempool_realloc(UcxMempool *pool, void *ptr, size_t n);
135
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
143 /**
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
144 * Frees pooled memory.
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
145 *
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
146 * 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
147 * is called.
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
148 *
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
149 * 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
150 * 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
151 * <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
152 * <code>EXIT_FAILURE</code>.
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
153 *
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
154 * @param pool the memory pool
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
155 * @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
156 * @see ucx_mempool_set_destr()
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
157 */
113
8693d7874773 added mempool allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
158 void ucx_mempool_free(UcxMempool *pool, void *ptr);
13
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
159
135
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 * Destroys a memory pool.
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
162 *
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
163 * 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
164 * is freed.
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
165 *
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
166 * 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
167 * 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
168 * 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
169 * order in which they were registered (FIFO order).
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
170 *
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
171 *
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
172 * @param pool the mempool to destroy
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
173 */
113
8693d7874773 added mempool allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
174 void ucx_mempool_destroy(UcxMempool *pool);
13
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
175
135
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
176 /**
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
177 * Sets a destructor function for the specified memory.
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 * 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
180 * pool is destroyed.
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
181 *
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
182 * 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
183 * 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
184 * 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
185 * memory.
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
186 *
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
187 * @param ptr pooled memory
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
188 * @param func a pointer to the destructor function
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
189 * @see ucx_mempool_free()
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
190 * @see ucx_mempool_destroy()
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
191 */
13
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
192 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
193
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
194 /**
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
195 * 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
196 *
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
197 * 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
198 * but shall be managed by a mempool.
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
199 *
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
200 * 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
201 * therefore (logically) convert to pooled memory.
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
202 *
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
203 * @param pool the memory pool
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
204 * @param ptr data the destructor is registered for
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
205 * @param destr a pointer to the destructor function
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
206 */
13
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
207 void ucx_mempool_reg_destr(UcxMempool *pool, void *ptr, ucx_destructor destr);
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
208
135
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 * Creates an UcxAllocator based on an UcxMempool.
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
211 *
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
212 * @param pool the mempool to create the UcxAllocator for
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
213 * @return a new UcxAllocator based on the specified pool
a0aa1c15f46b documented mempool + some fixes
Mike Becker <universe@uap-core.de>
parents: 120
diff changeset
214 */
113
8693d7874773 added mempool allocator
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 103
diff changeset
215 UcxAllocator* ucx_mempool_allocator(UcxMempool *pool);
13
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
216
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
217 #ifdef __cplusplus
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
218 }
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
219 #endif
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
220
120
8170f658f017 some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 113
diff changeset
221 #endif /* UCX_MEMPOOL_H */
13
98ac89e3aa37 Added mempool
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
222

mercurial