-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpool.h
104 lines (94 loc) · 2.71 KB
/
pool.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/***************************************************************************
begin........: June 2012
copyright....: Sebastian Fedrau
email........: [email protected]
***************************************************************************/
/***************************************************************************
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License v3 as published by
the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License v3 for more details.
***************************************************************************/
/**
* \file pool.h
* \brief Allocate memory blocks of same sizes.
* \author Sebastian Fedrau <[email protected]>
*/
#ifndef POOL_H
#define POOL_H
#include <stdint.h>
#include <stddef.h>
/**
*\struct Pool
*\brief Allocate groups of equal-sized chunks of memory.
*/
typedef struct _Pool
{
/*! Function to allocate memory. */
void *(*alloc)(struct _Pool *pool);
/*! Function to free memory. */
void (*free)(struct _Pool *pool, void *ptr);
} Pool;
/**
*\struct MemoryPool
*\brief This memory pool allocates blocks of memory and grows automatically.
*/
typedef struct
{
/*! Padding.*/
Pool padding;
/**
*\struct _MemoryBlock
*\brief Blocks of memory are stored in a singly-linked list.
*
*\var block
*\brief First memory block.
*/
struct _MemoryBlock
{
/*! The allocated memory block. */
int8_t *items;
/*! Offset of the next available address. */
size_t offset;
/*! Pointer to next available memory block or NULL. */
struct _MemoryBlock *next;
} *block;
/**
*\struct _MemoryPtrBlock
*\brief Freed pointers are stored in blocks holding addresses.
*
*\var free_block
*\brief First pointer block.
*/
struct _MemoryPtrBlock
{
/*! Array containing freed pointers. */
void **items;
/*! Offset to find next available position in items array. */
size_t offset;
/*! Pointer to next memory or NULL. */
struct _MemoryPtrBlock *next;
} *free_block;
/*! Number of items a memory memory can hold. */
size_t block_size;
/*! Size of an allocated item. */
size_t item_size;
} MemoryPool;
/**
*\param item_size size of allocated items
*\param block_size number of elements a memory can hold
*\return a new pool
*
* Creates a new MemoryPool.
*/
MemoryPool *memory_pool_new(size_t item_size, size_t block_size);
/**
*\param pool MemoryPool to destroy
*
* Destroys the given MemoryPool.
*/
void memory_pool_destroy(MemoryPool *pool);
#endif