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