leaf-mempool.h
1 /*==============================================================================
2 
3  In short, mpool is distributed under so called "BSD license",
4 
5  Copyright (c) 2009-2010 Tatsuhiko Kubo <cubicdaiya@gmail.com>
6  All rights reserved.
7 
8  Redistribution and use in source and binary forms, with or without modification,
9  are permitted provided that the following conditions are met:
10 
11  * Redistributions of source code must retain the above copyright notice,
12  this list of conditions and the following disclaimer.
13 
14  * Redistributions in binary form must reproduce the above copyright notice,
15  this list of conditions and the following disclaimer in the documentation
16  and/or other materials provided with the distribution.
17 
18  * Neither the name of the authors nor the names of its contributors
19  may be used to endorse or promote products derived from this software
20  without specific prior written permission.
21 
22  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
28  TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 
34  written by C99 style
35  ==============================================================================*/
36 
37 #ifndef LEAF_MPOOL_H_INCLUDED
38 #define LEAF_MPOOL_H_INCLUDED
39 
40 #ifdef __cplusplus
41 extern "C" {
42 #endif
43 
44  //==============================================================================
45 
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <stdbool.h>
50 
51  //==============================================================================
52 
53 #define MPOOL_ALIGN_SIZE (8)
54 
55  typedef struct LEAF LEAF;
56 
57  typedef enum LEAFErrorType
58  {
59  LEAFMempoolOverrun = 0,
60  LEAFMempoolFragmentation,
61  LEAFInvalidFree,
62  LEAFErrorNil
63  } LEAFErrorType;
64 
72  // node of free list
73  typedef struct mpool_node_t {
74  char *pool; // memory pool field
75  struct mpool_node_t *next; // next node pointer
76  struct mpool_node_t *prev; // prev node pointer
77  size_t size;
78  } mpool_node_t;
79 
80  typedef struct _tMempool _tMempool;
81  typedef _tMempool* tMempool;
82  struct _tMempool
83  {
84  tMempool mempool;
85  LEAF* leaf;
86  char* mpool; // start of the mpool
87  size_t usize; // used size of the pool
88  size_t msize; // max size of the pool
89  mpool_node_t* head; // first node of memory pool free list
90  };
91 
93 
99  void tMempool_init (tMempool* const pool, char* memory, size_t size, LEAF* const leaf);
100 
101 
103 
106  void tMempool_free (tMempool* const pool);
107 
108 
110 
116  void tMempool_initToPool (tMempool* const mp, char* memory, size_t size, tMempool* const mem);
117 
121  //==============================================================================
122 
123  // typedef struct mpool_t {
124  // char* mpool; // start of the mpool
125  // size_t usize; // used size of the pool
126  // size_t msize; // max size of the pool
127  // mpool_node_t* head; // first node of memory pool free list
128  // } mpool_t;
129 
130  void mpool_create (char* memory, size_t size, _tMempool* pool);
131 
132  char* mpool_alloc(size_t size, _tMempool* pool);
133  char* mpool_calloc(size_t asize, _tMempool* pool);
134 
135  void mpool_free(char* ptr, _tMempool* pool);
136 
137  size_t mpool_get_size(_tMempool* pool);
138  size_t mpool_get_used(_tMempool* pool);
139 
140  void leaf_pool_init(LEAF* const leaf, char* memory, size_t size);
141 
142  char* leaf_alloc(LEAF* const leaf, size_t size);
143  char* leaf_calloc(LEAF* const leaf, size_t size);
144 
145  void leaf_free(LEAF* const leaf, char* ptr);
146 
147  size_t leaf_pool_get_size(LEAF* const leaf);
148  size_t leaf_pool_get_used(LEAF* const leaf);
149 
150  char* leaf_pool_get_pool(LEAF* const leaf);
151 
152 #ifdef __cplusplus
153 }
154 #endif
155 
156 #endif // LEAF_MPOOL_H
157 
158 //==============================================================================
159 
160 
161 
tMempool_initToPool
void tMempool_initToPool(tMempool *const mp, char *memory, size_t size, tMempool *const mem)
Initialize a tMempool for a given memory location and size to a specified mempool.
Definition: leaf-mempool.c:422
tMempool_init
void tMempool_init(tMempool *const pool, char *memory, size_t size, LEAF *const leaf)
Initialize a tMempool for a given memory location and size to the default mempool of a LEAF instance.
Definition: leaf-mempool.c:410
LEAF
Struct for an instance of LEAF.
Definition: leaf-global.h:31
tMempool_free
void tMempool_free(tMempool *const pool)
Free a tMempool from its mempool.
Definition: leaf-mempool.c:415