]> git.ozlabs.org Git - ccan/blob - block_pool.h
4db25a8dad9aac24e3b1a7aadc63e7f6786eeac4
[ccan] / block_pool.h
1 /*
2         Copyright (c) 2009  Joseph A. Adams
3         All rights reserved.
4         
5         Redistribution and use in source and binary forms, with or without
6         modification, are permitted provided that the following conditions
7         are met:
8         1. Redistributions of source code must retain the above copyright
9            notice, this list of conditions and the following disclaimer.
10         2. Redistributions in binary form must reproduce the above copyright
11            notice, this list of conditions and the following disclaimer in the
12            documentation and/or other materials provided with the distribution.
13         3. The name of the author may not be used to endorse or promote products
14            derived from this software without specific prior written permission.
15         
16         THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17         IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18         OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19         IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20         INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21         NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22         DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23         THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24         (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25         THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #ifndef CCAN_BLOCK_POOL
29 #define CCAN_BLOCK_POOL
30
31 #include <ccan/talloc/talloc.h>
32 #include <string.h>
33
34 struct block_pool;
35
36 /* Construct a new block pool.
37    ctx is a talloc context (or NULL if you don't know what talloc is ;) ) */
38 struct block_pool *block_pool_new(void *ctx);
39
40 /* Same as block_pool_alloc, but allows you to manually specify alignment.
41    For instance, strings need not be aligned, so set align=1 for them.
42    align must be a power of two. */
43 void *block_pool_alloc_align(struct block_pool *bp, size_t size, size_t align);
44
45 /* Allocate a block of a given size.  The returned pointer will remain valid
46    for the life of the block_pool.  The block cannot be resized or
47    freed individually. */
48 static inline void *block_pool_alloc(struct block_pool *bp, size_t size) {
49         size_t align = size & -size; //greatest power of two by which size is divisible
50         if (align > 16)
51                 align = 16;
52         return block_pool_alloc_align(bp, size, align);
53 }
54
55 static inline void block_pool_free(struct block_pool *bp) {
56         talloc_free(bp);
57 }
58
59
60 char *block_pool_strdup(struct block_pool *bp, const char *str);
61
62 static inline void *block_pool_memdup(struct block_pool *bp, const void *src, size_t size) {
63         void *ret = block_pool_alloc(bp, size);
64         memcpy(ret, src, size);
65         return ret;
66 }
67
68 #endif