xref: /unit/src/nxt_buf_pool.h (revision 2084:7d479274f334)
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 nxt_buf_pool_ready(bp)                                                \
46     ((bp)->free != NULL                                                       \
47      || ((bp)->current != NULL                                                \
48          && (bp)->current->mem.free < (bp)->current->mem.end))
49 
50 
51 /* A free buffer is allowed to be allocated. */
52 
53 #define nxt_buf_pool_obtainable(bp)                                           \
54     ((bp)->num < (bp)->max)
55 
56 
57 /* There is ready free buffer or it is allowed to be allocated. */
58 
59 #define nxt_buf_pool_available(bp)                                            \
60     (nxt_buf_pool_obtainable(bp) || nxt_buf_pool_ready(bp))
61 
62 
63 /* Reserve allocation of "n" free buffers as they were allocated. */
64 
65 #define nxt_buf_pool_reserve(bp, n)                                           \
66     (bp)->num += (n)
67 
68 
69 /* Release a reservation. */
70 
71 #define nxt_buf_pool_release(bp, n)                                           \
72     (bp)->num -= (n)
73 
74 
75 #endif /* _NXT_BUF_POOL_H_INCLUDED_ */
76