• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #pragma once
2 #ifndef IWPOOL_H
3 #define IWPOOL_H
4 
5 /**************************************************************************************************
6  * Memory pool implementation.
7  *
8  * IOWOW library
9  *
10  * MIT License
11  *
12  * Copyright (c) 2012-2020 Softmotions Ltd <info@softmotions.com>
13  *
14  * Permission is hereby granted, free of charge, to any person obtaining a copy
15  * of this software and associated documentation files (the "Software"), to deal
16  * in the Software without restriction, including without limitation the rights
17  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18  *  copies of the Software, and to permit persons to whom the Software is
19  * furnished to do so, subject to the following conditions:
20  *
21  * The above copyright notice and this permission notice shall be included in all
22  * copies or substantial portions of the Software.
23  *
24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30  * SOFTWARE.
31  *************************************************************************************************/
32 
33 #include "basedefs.h"
34 IW_EXTERN_C_START
35 
36 #ifndef IWPOOL_POOL_SIZ
37 #define IWPOOL_POOL_SIZ   (8 * 1024)
38 #endif
39 
40 struct _IWPOOL;
41 typedef struct _IWPOOL IWPOOL;
42 
43 /**
44  * @brief Creates memory pool and preallocate initial buffer of size `siz` bytes.
45  * In the case if `siz` is zero then size of initial memory buffer will be `IWPOOL_POOL_SIZ` bytes.
46  *
47  * @param siz Initial memory buffer size. Can be zero.
48  * @return Pointer to the new pool or `zero` if allocation is failed.
49  */
50 IW_EXPORT IWPOOL *iwpool_create(size_t siz);
51 
52 /**
53  * @brief Create empty pool with no preallocated buffer.
54  * @return Pointer to the new pool or `zero` if allocation is failed.
55  */
56 IW_EXPORT IWPOOL *iwpool_create_empty(void);
57 
58 /**
59  * @brief Allocates buffer of specified size.
60  *
61  * @param siz  Size of buffer.
62  * @param pool Pointer to memory pool.
63  * @return Pointer to buffer or `zero` if allocation is failed.
64  */
65 IW_EXPORT void *iwpool_alloc(size_t siz, IWPOOL *pool);
66 
67 /**
68  * @brief Allocates zero initialized memory buffer
69  *        and initializes allocated buffer with zeroes.
70  *
71  * @param siz Size of buffer.
72  * @param pool Pointer to memory pool.
73  * @return Pointer to buffer or `zero` if allocation is failed.
74  */
75 IW_EXPORT void *iwpool_calloc(size_t siz, IWPOOL *pool);
76 
77 /**
78  * @brief Copy a given `str` of size `len` into memory pool.
79  *
80  * @param pool Pointer to memory pool.
81  * @param str Pointer to buffer to copy.
82  * @param len Size of buffer in bytes.
83  * @param rcp Pointer to status code holder.
84  * @return Pointer to copied buffer or `zero` if operation failed.
85  */
86 IW_EXPORT char *iwpool_strndup(IWPOOL *pool, const char *str, size_t len, iwrc *rcp);
87 
88 /**
89  * @brief Copy a given zero terminated char buffer into memory pool.
90  *
91  * @param pool Pointer to memory pool.
92  * @param str Zero terminated char buffer.
93  * @param rcp Pointer to status code holder.
94  * @return Pointer to copied buffer or `zero` if operation failed.
95  */
96 IW_EXPORT char *iwpool_strdup(IWPOOL *pool, const char *str, iwrc *rcp);
97 
98 /**
99  * @brief Do `fprintf` into string allocated in this memory pool.
100  *
101  * @param pool Pointer to memory pool.
102  * @param format `fprintf` format specification.
103  * @param ...
104  * @return Pointer to resulted string of `zero` if operation is failed.
105  */
106 IW_EXPORT char *iwpool_printf(IWPOOL *pool, const char *format, ...);
107 
108 IW_EXPORT char **iwpool_split_string(IWPOOL *pool, const char *haystack,
109                                      const char *split_chars, bool ignore_whitespace);
110 
111 IW_EXPORT char **iwpool_printf_split(IWPOOL *pool,
112                                      const char *split_chars, bool ignore_whitespace,
113                                      const char *format, ...);
114 
115 /**
116  * @brief Destroys a given memory pool and frees its resources.
117  *
118  * @param pool
119  * @return IW_EXPORT
120  */
121 IW_EXPORT void iwpool_destroy(IWPOOL *pool);
122 
123 /**
124  * @brief Dispose function for `IWPOOL` stored as user data.
125  *
126  * @param pool Memory pool to be destroyed.
127  */
128 IW_EXPORT void iwpool_free_fn(void *pool);
129 
130 /**
131  * @brief Sets arbitrary user data associated with this pool.
132  *        User data will be freed on pool destroy or new user data set.
133  *
134  * @param pool Pointer to memory pool.
135  * @param data User data. Can be zero.
136  * @param free_fn User data dispose function. Can be zero.
137  */
138 IW_EXPORT void iwpool_user_data_set(IWPOOL *pool, void *data, void (*free_fn)(void *));
139 
140 /**
141  * @brief Returns pointer to user data associated with this pool. Or zero.
142  */
143 IW_EXPORT void *iwpool_user_data_get(IWPOOL *pool);
144 
145 /**
146  * @brief Reset user data free function for current user data stored in pool.
147  *
148  * @param pool Pointer to memory pool.
149  * @return Pointer to current user data stored or zero,
150  */
151 IW_EXPORT void *iwpool_user_data_detach(IWPOOL *pool);
152 
153 /**
154  * @brief Returns number of bytes allocated for this memory pool.
155  *
156  * @param pool Pointer to memory pool.
157  */
158 IW_EXPORT size_t iwpool_allocated_size(IWPOOL *pool);
159 
160 /**
161  * @brief Returns number of bytes actually used for allocated buffers.
162  *
163  * @param pool Pointer to mmemory pool.
164  */
165 IW_EXPORT size_t iwpool_used_size(IWPOOL *pool);
166 
167 IW_EXTERN_C_END
168 #endif
169