xref: /unit/src/nxt_buf.h (revision 4)
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_H_INCLUDED_
80Sigor@sysoev.ru #define _NXT_BUF_H_INCLUDED_
90Sigor@sysoev.ru 
100Sigor@sysoev.ru 
110Sigor@sysoev.ru /*
120Sigor@sysoev.ru  * There are four types of buffers.  They are different sizes, so they
130Sigor@sysoev.ru  * should be allocated by appropriate nxt_buf_XXX_alloc() function.
140Sigor@sysoev.ru  *
150Sigor@sysoev.ru  * 1) Memory-only buffers, their size is less than nxt_buf_t size, it
160Sigor@sysoev.ru  *    is equal to offsetof(nxt_buf_t, file_pos), that is it is nxt_buf_t
170Sigor@sysoev.ru  *    without file and mmap part.  The buffers are frequently used, so
180Sigor@sysoev.ru  *    the reduction allows to save 20-32 bytes depending on platform.
190Sigor@sysoev.ru  *
200Sigor@sysoev.ru  * 2) Memory/file buffers, on Unix their size is exactly nxt_buf_t size,
210Sigor@sysoev.ru  *    since nxt_mem_map_file_ctx_t() is empty macro.  On Windows the size
220Sigor@sysoev.ru  *    equals offsetof(nxt_buf_t, mmap), that is it is nxt_buf_t without
230Sigor@sysoev.ru  *    memory map context part.  The buffers can contain both memory and
240Sigor@sysoev.ru  *    file pointers at once, or only memory or file pointers.
250Sigor@sysoev.ru  *
260Sigor@sysoev.ru  * 3) Memory mapped buffers are similar to the memory/file buffers.  Their
270Sigor@sysoev.ru  *    size is exactly nxt_buf_t size.  The buffers can contain both memory
280Sigor@sysoev.ru  *    and file pointers at once, or only memory or file pointers.  If a
290Sigor@sysoev.ru  *    buffer is not currently mapped in memory, its mapping size is stored
300Sigor@sysoev.ru  *    in the mem.end field and available via nxt_buf_mem_size() macro.
310Sigor@sysoev.ru  *
320Sigor@sysoev.ru  * 4) Sync buffers, their size is the same size as memory-only buffers
330Sigor@sysoev.ru  *    size.  A sync buffer can be smaller but for memory pool cache
340Sigor@sysoev.ru  *    purpose it is better to allocate it as frequently used memory-only
350Sigor@sysoev.ru  *    buffer.  The buffers are used to synchronize pipeline processing
360Sigor@sysoev.ru  *    completion, because data buffers in the pipeline can be completed
370Sigor@sysoev.ru  *    and freed before their final output will even be passed to a peer.
380Sigor@sysoev.ru  *    For this purpose a sync buffer is allocated with the stop flag which
390Sigor@sysoev.ru  *    stops buffer chain completion processing on the sync buffer in
400Sigor@sysoev.ru  *    nxt_sendbuf_update() and nxt_sendbuf_completion().
410Sigor@sysoev.ru  *    Clearing the stop flag allows to continue completion processing.
420Sigor@sysoev.ru  *
430Sigor@sysoev.ru  *    The last flag means the end of the output and must be set only
440Sigor@sysoev.ru  *    in a sync buffer.  The last flag is not permitted in memory and
450Sigor@sysoev.ru  *    file buffers since it requires special handling while conversion
460Sigor@sysoev.ru  *    one buffer to another.
470Sigor@sysoev.ru  *
480Sigor@sysoev.ru  *    The nxt_buf_used_size() macro treats a sync buffer as a memory-only
490Sigor@sysoev.ru  *    buffer which has NULL pointers, thus the buffer content size is zero.
500Sigor@sysoev.ru  *    If allocated size of sync buffer would be lesser than memory-only
510Sigor@sysoev.ru  *    buffer, then the special memory flag would be required because
520Sigor@sysoev.ru  *    currently presence of memory part is indicated by non-NULL pointer
530Sigor@sysoev.ru  *    to a content in memory.
540Sigor@sysoev.ru  *
550Sigor@sysoev.ru  *    All types of buffers can have the flush flag that means the buffer
560Sigor@sysoev.ru  *    should be sent as much as possible.
570Sigor@sysoev.ru  */
580Sigor@sysoev.ru 
590Sigor@sysoev.ru typedef struct {
600Sigor@sysoev.ru     u_char                  *pos;
610Sigor@sysoev.ru     u_char                  *free;
620Sigor@sysoev.ru     u_char                  *start;
630Sigor@sysoev.ru     u_char                  *end;
640Sigor@sysoev.ru } nxt_buf_mem_t;
650Sigor@sysoev.ru 
660Sigor@sysoev.ru 
670Sigor@sysoev.ru struct nxt_buf_s {
680Sigor@sysoev.ru     void                    *data;
690Sigor@sysoev.ru     nxt_work_handler_t      completion_handler;
700Sigor@sysoev.ru     nxt_buf_t               *parent;
710Sigor@sysoev.ru 
720Sigor@sysoev.ru     /*
730Sigor@sysoev.ru      * The next link, flags, and nxt_buf_mem_t should
740Sigor@sysoev.ru      * reside together to improve cache locality.
750Sigor@sysoev.ru      */
760Sigor@sysoev.ru     nxt_buf_t               *next;
770Sigor@sysoev.ru 
780Sigor@sysoev.ru     uint32_t                retain;
790Sigor@sysoev.ru     /*
800Sigor@sysoev.ru      * Used by nxt_mem_cache_free() to return buffer
810Sigor@sysoev.ru      * in appropriate memory pool cache.
820Sigor@sysoev.ru      */
830Sigor@sysoev.ru     uint8_t                 size;
840Sigor@sysoev.ru 
850Sigor@sysoev.ru     uint8_t                 is_file;     /* 1 bit */
860Sigor@sysoev.ru 
870Sigor@sysoev.ru     uint16_t                is_mmap:1;
880Sigor@sysoev.ru 
890Sigor@sysoev.ru     uint16_t                is_sync:1;
900Sigor@sysoev.ru     uint16_t                is_nobuf:1;
910Sigor@sysoev.ru     uint16_t                is_flush:1;
920Sigor@sysoev.ru     uint16_t                is_last:1;
930Sigor@sysoev.ru 
940Sigor@sysoev.ru     nxt_buf_mem_t           mem;
950Sigor@sysoev.ru 
960Sigor@sysoev.ru     /* The file and mmap parts are not allocated by nxt_buf_mem_alloc(). */
970Sigor@sysoev.ru     nxt_file_t              *file;
980Sigor@sysoev.ru     nxt_off_t               file_pos;
990Sigor@sysoev.ru     nxt_off_t               file_end;
1000Sigor@sysoev.ru 
1010Sigor@sysoev.ru     /* The mmap part is not allocated by nxt_buf_file_alloc(). */
1020Sigor@sysoev.ru     nxt_mem_map_file_ctx_t  (mmap)
1030Sigor@sysoev.ru };
1040Sigor@sysoev.ru 
1050Sigor@sysoev.ru 
1060Sigor@sysoev.ru #define NXT_BUF_MEM_SIZE    offsetof(nxt_buf_t, file)
1070Sigor@sysoev.ru #define NXT_BUF_SYNC_SIZE   NXT_BUF_MEM_SIZE
1080Sigor@sysoev.ru #define NXT_BUF_MMAP_SIZE   sizeof(nxt_buf_t)
1090Sigor@sysoev.ru #define NXT_BUF_FILE_SIZE   sizeof(nxt_buf_t)
1100Sigor@sysoev.ru 
1110Sigor@sysoev.ru 
1120Sigor@sysoev.ru #define NXT_BUF_SYNC_NOBUF  1
1130Sigor@sysoev.ru #define NXT_BUF_SYNC_FLUSH  2
1140Sigor@sysoev.ru #define NXT_BUF_SYNC_LAST   4
1150Sigor@sysoev.ru 
1160Sigor@sysoev.ru 
1170Sigor@sysoev.ru #define                                                                       \
1180Sigor@sysoev.ru nxt_buf_is_mem(b)                                                             \
1190Sigor@sysoev.ru     ((b)->mem.pos != NULL)
1200Sigor@sysoev.ru 
1210Sigor@sysoev.ru 
1220Sigor@sysoev.ru #define                                                                       \
1230Sigor@sysoev.ru nxt_buf_is_file(b)                                                            \
1240Sigor@sysoev.ru     ((b)->is_file)
1250Sigor@sysoev.ru 
1260Sigor@sysoev.ru #define                                                                       \
1270Sigor@sysoev.ru nxt_buf_set_file(b)                                                           \
1280Sigor@sysoev.ru     (b)->is_file = 1
1290Sigor@sysoev.ru 
1300Sigor@sysoev.ru #define                                                                       \
1310Sigor@sysoev.ru nxt_buf_clear_file(b)                                                         \
1320Sigor@sysoev.ru     (b)->is_file = 0
1330Sigor@sysoev.ru 
1340Sigor@sysoev.ru 
1350Sigor@sysoev.ru #define                                                                       \
1360Sigor@sysoev.ru nxt_buf_is_mmap(b)                                                            \
1370Sigor@sysoev.ru     ((b)->is_mmap)
1380Sigor@sysoev.ru 
1390Sigor@sysoev.ru #define                                                                       \
1400Sigor@sysoev.ru nxt_buf_set_mmap(b)                                                           \
1410Sigor@sysoev.ru     (b)->is_mmap = 1
1420Sigor@sysoev.ru 
1430Sigor@sysoev.ru #define                                                                       \
1440Sigor@sysoev.ru nxt_buf_clear_mmap(b)                                                         \
1450Sigor@sysoev.ru     (b)->is_mmap = 0
1460Sigor@sysoev.ru 
1470Sigor@sysoev.ru 
1480Sigor@sysoev.ru #define                                                                       \
1490Sigor@sysoev.ru nxt_buf_is_sync(b)                                                            \
1500Sigor@sysoev.ru     ((b)->is_sync)
1510Sigor@sysoev.ru 
1520Sigor@sysoev.ru #define                                                                       \
1530Sigor@sysoev.ru nxt_buf_set_sync(b)                                                           \
1540Sigor@sysoev.ru     (b)->is_sync = 1
1550Sigor@sysoev.ru 
1560Sigor@sysoev.ru #define                                                                       \
1570Sigor@sysoev.ru nxt_buf_clear_sync(b)                                                         \
1580Sigor@sysoev.ru     (b)->is_sync = 0
1590Sigor@sysoev.ru 
1600Sigor@sysoev.ru 
1610Sigor@sysoev.ru #define                                                                       \
1620Sigor@sysoev.ru nxt_buf_is_nobuf(b)                                                           \
1630Sigor@sysoev.ru     ((b)->is_nobuf)
1640Sigor@sysoev.ru 
1650Sigor@sysoev.ru #define                                                                       \
1660Sigor@sysoev.ru nxt_buf_set_nobuf(b)                                                          \
1670Sigor@sysoev.ru     (b)->is_nobuf = 1
1680Sigor@sysoev.ru 
1690Sigor@sysoev.ru #define                                                                       \
1700Sigor@sysoev.ru nxt_buf_clear_nobuf(b)                                                        \
1710Sigor@sysoev.ru     (b)->is_nobuf = 0
1720Sigor@sysoev.ru 
1730Sigor@sysoev.ru 
1740Sigor@sysoev.ru #define                                                                       \
1750Sigor@sysoev.ru nxt_buf_is_flush(b)                                                           \
1760Sigor@sysoev.ru     ((b)->is_flush)
1770Sigor@sysoev.ru 
1780Sigor@sysoev.ru #define                                                                       \
1790Sigor@sysoev.ru nxt_buf_set_flush(b)                                                          \
1800Sigor@sysoev.ru     (b)->is_flush = 1
1810Sigor@sysoev.ru 
1820Sigor@sysoev.ru #define                                                                       \
1830Sigor@sysoev.ru nxt_buf_clear_flush(b)                                                        \
1840Sigor@sysoev.ru     (b)->is_flush = 0
1850Sigor@sysoev.ru 
1860Sigor@sysoev.ru 
1870Sigor@sysoev.ru #define                                                                       \
1880Sigor@sysoev.ru nxt_buf_is_last(b)                                                            \
1890Sigor@sysoev.ru     ((b)->is_last)
1900Sigor@sysoev.ru 
1910Sigor@sysoev.ru #define                                                                       \
1920Sigor@sysoev.ru nxt_buf_set_last(b)                                                           \
1930Sigor@sysoev.ru     (b)->is_last = 1
1940Sigor@sysoev.ru 
1950Sigor@sysoev.ru #define                                                                       \
1960Sigor@sysoev.ru nxt_buf_clear_last(b)                                                         \
1970Sigor@sysoev.ru    (b)->is_last = 0
1980Sigor@sysoev.ru 
1990Sigor@sysoev.ru 
2000Sigor@sysoev.ru #define                                                                       \
2010Sigor@sysoev.ru nxt_buf_mem_set_size(bm, size)                                                \
2020Sigor@sysoev.ru     do {                                                                      \
2030Sigor@sysoev.ru         (bm)->start = 0;                                                      \
2040Sigor@sysoev.ru         (bm)->end = (void *) size;                                            \
2050Sigor@sysoev.ru     } while (0)
2060Sigor@sysoev.ru 
2070Sigor@sysoev.ru 
2080Sigor@sysoev.ru #define                                                                       \
2090Sigor@sysoev.ru nxt_buf_mem_size(bm)                                                          \
2100Sigor@sysoev.ru     ((bm)->end - (bm)->start)
2110Sigor@sysoev.ru 
2120Sigor@sysoev.ru 
2130Sigor@sysoev.ru #define                                                                       \
2140Sigor@sysoev.ru nxt_buf_mem_used_size(bm)                                                     \
2150Sigor@sysoev.ru     ((bm)->free - (bm)->pos)
2160Sigor@sysoev.ru 
2170Sigor@sysoev.ru 
2180Sigor@sysoev.ru #define                                                                       \
2190Sigor@sysoev.ru nxt_buf_mem_free_size(bm)                                                     \
2200Sigor@sysoev.ru     ((bm)->end - (bm)->free)
2210Sigor@sysoev.ru 
2220Sigor@sysoev.ru 
2230Sigor@sysoev.ru #define                                                                       \
2240Sigor@sysoev.ru nxt_buf_used_size(b)                                                          \
2250Sigor@sysoev.ru     (nxt_buf_is_file(b) ? (b)->file_end - (b)->file_pos:                      \
2260Sigor@sysoev.ru                           nxt_buf_mem_used_size(&(b)->mem))
2270Sigor@sysoev.ru 
2280Sigor@sysoev.ru 
229*4Sigor@sysoev.ru NXT_EXPORT void nxt_buf_mem_init(nxt_buf_t *b, void *start, size_t size);
2300Sigor@sysoev.ru NXT_EXPORT nxt_buf_t *nxt_buf_mem_alloc(nxt_mem_pool_t *mp, size_t size,
2310Sigor@sysoev.ru     nxt_uint_t flags);
2320Sigor@sysoev.ru NXT_EXPORT nxt_buf_t *nxt_buf_file_alloc(nxt_mem_pool_t *mp, size_t size,
2330Sigor@sysoev.ru     nxt_uint_t flags);
2340Sigor@sysoev.ru NXT_EXPORT nxt_buf_t *nxt_buf_mmap_alloc(nxt_mem_pool_t *mp, size_t size);
2350Sigor@sysoev.ru NXT_EXPORT nxt_buf_t *nxt_buf_sync_alloc(nxt_mem_pool_t *mp, nxt_uint_t flags);
2360Sigor@sysoev.ru 
2370Sigor@sysoev.ru 
2380Sigor@sysoev.ru #define                                                                       \
2390Sigor@sysoev.ru nxt_buf_free(mp, b)                                                           \
2400Sigor@sysoev.ru     nxt_mem_cache_free0((mp), (b), (b)->size)
2410Sigor@sysoev.ru 
2420Sigor@sysoev.ru 
2430Sigor@sysoev.ru NXT_EXPORT void nxt_buf_chain_add(nxt_buf_t **head, nxt_buf_t *in);
2440Sigor@sysoev.ru NXT_EXPORT size_t nxt_buf_chain_length(nxt_buf_t *b);
2450Sigor@sysoev.ru 
2460Sigor@sysoev.ru 
2470Sigor@sysoev.ru #endif /* _NXT_BUF_H_INCLIDED_ */
248