Alexandre Lision | 8af73cb | 2013-12-10 14:11:20 -0500 | [diff] [blame] | 1 | /* $Id$ */ |
| 2 | /* |
| 3 | * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com) |
| 4 | * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 | */ |
| 20 | #ifndef __POOL_STACK_H__ |
| 21 | #define __POOL_STACK_H__ |
| 22 | |
| 23 | #include <pj/pool.h> |
| 24 | |
| 25 | /** |
| 26 | * @defgroup PJ_POOL_BUFFER Stack/Buffer Based Memory Pool Allocator |
| 27 | * @ingroup PJ_POOL_GROUP |
| 28 | * @brief Stack/buffer based pool. |
| 29 | * |
| 30 | * This section describes an implementation of memory pool which uses |
| 31 | * memory allocated from the stack. Application creates this pool |
| 32 | * by specifying a buffer (which can be allocated from static memory or |
| 33 | * stack variable), and then use normal pool API to access/use the pool. |
| 34 | * |
| 35 | * If the buffer specified during pool creation is a buffer located in the |
| 36 | * stack, the pool will be invalidated (or implicitly destroyed) when the |
| 37 | * execution leaves the enclosing block containing the buffer. Note |
| 38 | * that application must make sure that any objects allocated from this |
| 39 | * pool (such as mutexes) have been destroyed before the pool gets |
| 40 | * invalidated. |
| 41 | * |
| 42 | * Sample usage: |
| 43 | * |
| 44 | * \code |
| 45 | #include <pjlib.h> |
| 46 | |
| 47 | static void test() |
| 48 | { |
| 49 | char buffer[500]; |
| 50 | pj_pool_t *pool; |
| 51 | void *p; |
| 52 | |
| 53 | pool = pj_pool_create_on_buf("thepool", buffer, sizeof(buffer)); |
| 54 | |
| 55 | // Use the pool as usual |
| 56 | p = pj_pool_alloc(pool, ...); |
| 57 | ... |
| 58 | |
| 59 | // No need to release the pool |
| 60 | } |
| 61 | |
| 62 | int main() |
| 63 | { |
| 64 | pj_init(); |
| 65 | test(); |
| 66 | return 0; |
| 67 | } |
| 68 | |
| 69 | \endcode |
| 70 | * |
| 71 | * @{ |
| 72 | */ |
| 73 | |
| 74 | PJ_BEGIN_DECL |
| 75 | |
| 76 | /** |
| 77 | * Create the pool using the specified buffer as the pool's memory. |
| 78 | * Subsequent allocations made from the pool will use the memory from |
| 79 | * this buffer. |
| 80 | * |
| 81 | * If the buffer specified in the parameter is a buffer located in the |
| 82 | * stack, the pool will be invalid (or implicitly destroyed) when the |
| 83 | * execution leaves the enclosing block containing the buffer. Note |
| 84 | * that application must make sure that any objects allocated from this |
| 85 | * pool (such as mutexes) have been destroyed before the pool gets |
| 86 | * invalidated. |
| 87 | * |
| 88 | * @param name Optional pool name. |
| 89 | * @param buf Buffer to be used by the pool. |
| 90 | * @param size The size of the buffer. |
| 91 | * |
| 92 | * @return The memory pool instance. |
| 93 | */ |
| 94 | PJ_DECL(pj_pool_t*) pj_pool_create_on_buf(const char *name, |
| 95 | void *buf, |
| 96 | pj_size_t size); |
| 97 | |
| 98 | PJ_END_DECL |
| 99 | |
| 100 | /** |
| 101 | * @} |
| 102 | */ |
| 103 | |
| 104 | #endif /* __POOL_STACK_H__ */ |
| 105 | |