xref: /unit/src/nxt_buf_pool.h (revision 2084)
10Sigor@sysoev.ru 
20Sigor@sysoev.ru /*
30Sigor@sysoev.ru  * Copyright (C) Igor Sysoev
40Sigor@sysoev.ru  * Copyright (C) NGINX, Inc.
50Sigor@sysoev.ru  */
60Sigor@sysoev.ru 
70Sigor@sysoev.ru #ifndef _NXT_BUF_POOL_H_INCLUDED_
80Sigor@sysoev.ru #define _NXT_BUF_POOL_H_INCLUDED_
90Sigor@sysoev.ru 
100Sigor@sysoev.ru 
110Sigor@sysoev.ru /*
120Sigor@sysoev.ru  * nxt_buf_pool_t is intended to allocate up to the "max" number
130Sigor@sysoev.ru  * memory, memory/file, or mmap/file buffers.  A size of the buffers
140Sigor@sysoev.ru  * is set in the "size" field.  The size however can be overridden in
150Sigor@sysoev.ru  * nxt_buf_pool_XXX_alloc() by the "size" argument if the argument is
160Sigor@sysoev.ru  * not zero and lesser than or equal to the "size" field multiplied
170Sigor@sysoev.ru  * by 1.25.  The "flags" field is passed as the nxt_mem_buf() flags.
180Sigor@sysoev.ru  */
190Sigor@sysoev.ru 
200Sigor@sysoev.ru typedef struct {
210Sigor@sysoev.ru     nxt_buf_t       *current;
220Sigor@sysoev.ru     nxt_buf_t       *free;
2365Sigor@sysoev.ru     nxt_mp_t        *mem_pool;
240Sigor@sysoev.ru 
250Sigor@sysoev.ru     uint16_t        num;
260Sigor@sysoev.ru     uint16_t        max;
270Sigor@sysoev.ru 
280Sigor@sysoev.ru     uint32_t        size;
290Sigor@sysoev.ru 
300Sigor@sysoev.ru     uint8_t         flags;     /* 2 bits */
310Sigor@sysoev.ru     uint8_t         destroy;   /* 1 bit */
320Sigor@sysoev.ru     uint8_t         mmap;      /* 1 bit */
330Sigor@sysoev.ru } nxt_buf_pool_t;
340Sigor@sysoev.ru 
350Sigor@sysoev.ru 
360Sigor@sysoev.ru NXT_EXPORT nxt_int_t nxt_buf_pool_mem_alloc(nxt_buf_pool_t *bp, size_t size);
370Sigor@sysoev.ru NXT_EXPORT nxt_int_t nxt_buf_pool_file_alloc(nxt_buf_pool_t *bp, size_t size);
380Sigor@sysoev.ru NXT_EXPORT nxt_int_t nxt_buf_pool_mmap_alloc(nxt_buf_pool_t *bp, size_t size);
390Sigor@sysoev.ru NXT_EXPORT void nxt_buf_pool_free(nxt_buf_pool_t *bp, nxt_buf_t *b);
400Sigor@sysoev.ru NXT_EXPORT void nxt_buf_pool_destroy(nxt_buf_pool_t *bp);
410Sigor@sysoev.ru 
420Sigor@sysoev.ru 
430Sigor@sysoev.ru /* There is ready free buffer. */
440Sigor@sysoev.ru 
45*2084Salx.manpages@gmail.com #define nxt_buf_pool_ready(bp)                                                \
460Sigor@sysoev.ru     ((bp)->free != NULL                                                       \
470Sigor@sysoev.ru      || ((bp)->current != NULL                                                \
480Sigor@sysoev.ru          && (bp)->current->mem.free < (bp)->current->mem.end))
490Sigor@sysoev.ru 
500Sigor@sysoev.ru 
510Sigor@sysoev.ru /* A free buffer is allowed to be allocated. */
520Sigor@sysoev.ru 
53*2084Salx.manpages@gmail.com #define nxt_buf_pool_obtainable(bp)                                           \
540Sigor@sysoev.ru     ((bp)->num < (bp)->max)
550Sigor@sysoev.ru 
560Sigor@sysoev.ru 
570Sigor@sysoev.ru /* There is ready free buffer or it is allowed to be allocated. */
580Sigor@sysoev.ru 
59*2084Salx.manpages@gmail.com #define nxt_buf_pool_available(bp)                                            \
600Sigor@sysoev.ru     (nxt_buf_pool_obtainable(bp) || nxt_buf_pool_ready(bp))
610Sigor@sysoev.ru 
620Sigor@sysoev.ru 
630Sigor@sysoev.ru /* Reserve allocation of "n" free buffers as they were allocated. */
640Sigor@sysoev.ru 
65*2084Salx.manpages@gmail.com #define nxt_buf_pool_reserve(bp, n)                                           \
660Sigor@sysoev.ru     (bp)->num += (n)
670Sigor@sysoev.ru 
680Sigor@sysoev.ru 
690Sigor@sysoev.ru /* Release a reservation. */
700Sigor@sysoev.ru 
71*2084Salx.manpages@gmail.com #define nxt_buf_pool_release(bp, n)                                           \
720Sigor@sysoev.ru     (bp)->num -= (n)
730Sigor@sysoev.ru 
740Sigor@sysoev.ru 
750Sigor@sysoev.ru #endif /* _NXT_BUF_POOL_H_INCLUDED_ */
76