1 
2 /*
3  * Copyright (C) Igor Sysoev
4  * Copyright (C) NGINX, Inc.
5  */
6 
7 #ifndef _NXT_BUF_POOL_H_INCLUDED_
8 #define _NXT_BUF_POOL_H_INCLUDED_
9 
10 
11 /*
12  * nxt_buf_pool_t is intended to allocate up to the "max" number
13  * memory, memory/file, or mmap/file buffers.  A size of the buffers
14  * is set in the "size" field.  The size however can be overridden in
15  * nxt_buf_pool_XXX_alloc() by the "size" argument if the argument is
16  * not zero and lesser than or equal to the "size" field multiplied
17  * by 1.25.  The "flags" field is passed as the nxt_mem_buf() flags.
18  */
19 
20 typedef struct {
21     nxt_buf_t       *current;
22     nxt_buf_t       *free;
23     nxt_mp_t        *mem_pool;
24 
25     uint16_t        num;
26     uint16_t        max;
27 
28     uint32_t        size;
29 
30     uint8_t         flags;     /* 2 bits */
31     uint8_t         destroy;   /* 1 bit */
32     uint8_t         mmap;      /* 1 bit */
33 } nxt_buf_pool_t;
34 
35 
36 NXT_EXPORT nxt_int_t nxt_buf_pool_mem_alloc(nxt_buf_pool_t *bp, size_t size);
37 NXT_EXPORT nxt_int_t nxt_buf_pool_file_alloc(nxt_buf_pool_t *bp, size_t size);
38 NXT_EXPORT nxt_int_t nxt_buf_pool_mmap_alloc(nxt_buf_pool_t *bp, size_t size);
39 NXT_EXPORT void nxt_buf_pool_free(nxt_buf_pool_t *bp, nxt_buf_t *b);
40 NXT_EXPORT void nxt_buf_pool_destroy(nxt_buf_pool_t *bp);
41 
42 
43 /* There is ready free buffer. */
44 
45 #define                                                                       \
46 nxt_buf_pool_ready(bp)                                                        \
47     ((bp)->free != NULL                                                       \
48      || ((bp)->current != NULL                                                \
49          && (bp)->current->mem.free < (bp)->current->mem.end))
50 
51 
52 /* A free buffer is allowed to be allocated. */
53 
54 #define                                                                       \
55 nxt_buf_pool_obtainable(bp)                                                   \
56     ((bp)->num < (bp)->max)
57 
58 
59 /* There is ready free buffer or it is allowed to be allocated. */
60 
61 #define                                                                       \
62 nxt_buf_pool_available(bp)                                                    \
63     (nxt_buf_pool_obtainable(bp) || nxt_buf_pool_ready(bp))
64 
65 
66 /* Reserve allocation of "n" free buffers as they were allocated. */
67 
68 #define                                                                       \
69 nxt_buf_pool_reserve(bp, n)                                                   \
70     (bp)->num += (n)
71 
72 
73 /* Release a reservation. */
74 
75 #define                                                                       \
76 nxt_buf_pool_release(bp, n)                                                   \
77     (bp)->num -= (n)
78 
79 
80 #endif /* _NXT_BUF_POOL_H_INCLUDED_ */
81