xref: /unit/src/nxt_buf.h (revision 65)
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;
7042Smax.romanov@nginx.com     void                    *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;
8842Smax.romanov@nginx.com     uint16_t                is_port_mmap:1;
890Sigor@sysoev.ru 
900Sigor@sysoev.ru     uint16_t                is_sync:1;
910Sigor@sysoev.ru     uint16_t                is_nobuf:1;
920Sigor@sysoev.ru     uint16_t                is_flush:1;
930Sigor@sysoev.ru     uint16_t                is_last:1;
9442Smax.romanov@nginx.com     uint16_t                is_port_mmap_sent:1;
950Sigor@sysoev.ru 
960Sigor@sysoev.ru     nxt_buf_mem_t           mem;
970Sigor@sysoev.ru 
980Sigor@sysoev.ru     /* The file and mmap parts are not allocated by nxt_buf_mem_alloc(). */
990Sigor@sysoev.ru     nxt_file_t              *file;
1000Sigor@sysoev.ru     nxt_off_t               file_pos;
1010Sigor@sysoev.ru     nxt_off_t               file_end;
1020Sigor@sysoev.ru 
1030Sigor@sysoev.ru     /* The mmap part is not allocated by nxt_buf_file_alloc(). */
1040Sigor@sysoev.ru     nxt_mem_map_file_ctx_t  (mmap)
1050Sigor@sysoev.ru };
1060Sigor@sysoev.ru 
1070Sigor@sysoev.ru 
10842Smax.romanov@nginx.com #define NXT_BUF_MEM_SIZE        offsetof(nxt_buf_t, file)
10942Smax.romanov@nginx.com #define NXT_BUF_SYNC_SIZE       NXT_BUF_MEM_SIZE
11042Smax.romanov@nginx.com #define NXT_BUF_FILE_SIZE       sizeof(nxt_buf_t)
11142Smax.romanov@nginx.com #define NXT_BUF_MMAP_SIZE       NXT_BUF_FILE_SIZE
11242Smax.romanov@nginx.com #define NXT_BUF_PORT_MMAP_SIZE  NXT_BUF_MEM_SIZE
1130Sigor@sysoev.ru 
1140Sigor@sysoev.ru 
1150Sigor@sysoev.ru #define NXT_BUF_SYNC_NOBUF  1
1160Sigor@sysoev.ru #define NXT_BUF_SYNC_FLUSH  2
1170Sigor@sysoev.ru #define NXT_BUF_SYNC_LAST   4
1180Sigor@sysoev.ru 
1190Sigor@sysoev.ru 
1200Sigor@sysoev.ru #define                                                                       \
1210Sigor@sysoev.ru nxt_buf_is_mem(b)                                                             \
1220Sigor@sysoev.ru     ((b)->mem.pos != NULL)
1230Sigor@sysoev.ru 
1240Sigor@sysoev.ru 
1250Sigor@sysoev.ru #define                                                                       \
1260Sigor@sysoev.ru nxt_buf_is_file(b)                                                            \
1270Sigor@sysoev.ru     ((b)->is_file)
1280Sigor@sysoev.ru 
1290Sigor@sysoev.ru #define                                                                       \
1300Sigor@sysoev.ru nxt_buf_set_file(b)                                                           \
1310Sigor@sysoev.ru     (b)->is_file = 1
1320Sigor@sysoev.ru 
1330Sigor@sysoev.ru #define                                                                       \
1340Sigor@sysoev.ru nxt_buf_clear_file(b)                                                         \
1350Sigor@sysoev.ru     (b)->is_file = 0
1360Sigor@sysoev.ru 
1370Sigor@sysoev.ru 
1380Sigor@sysoev.ru #define                                                                       \
1390Sigor@sysoev.ru nxt_buf_is_mmap(b)                                                            \
1400Sigor@sysoev.ru     ((b)->is_mmap)
1410Sigor@sysoev.ru 
1420Sigor@sysoev.ru #define                                                                       \
1430Sigor@sysoev.ru nxt_buf_set_mmap(b)                                                           \
1440Sigor@sysoev.ru     (b)->is_mmap = 1
1450Sigor@sysoev.ru 
1460Sigor@sysoev.ru #define                                                                       \
1470Sigor@sysoev.ru nxt_buf_clear_mmap(b)                                                         \
1480Sigor@sysoev.ru     (b)->is_mmap = 0
1490Sigor@sysoev.ru 
1500Sigor@sysoev.ru 
1510Sigor@sysoev.ru #define                                                                       \
15242Smax.romanov@nginx.com nxt_buf_is_port_mmap(b)                                                       \
15342Smax.romanov@nginx.com     ((b)->is_port_mmap)
15442Smax.romanov@nginx.com 
15542Smax.romanov@nginx.com #define                                                                       \
15642Smax.romanov@nginx.com nxt_buf_set_port_mmap(b)                                                      \
15742Smax.romanov@nginx.com     (b)->is_port_mmap = 1
15842Smax.romanov@nginx.com 
15942Smax.romanov@nginx.com #define                                                                       \
16042Smax.romanov@nginx.com nxt_buf_clear_port_mmap(b)                                                    \
16142Smax.romanov@nginx.com     (b)->is_port_mmap = 0
16242Smax.romanov@nginx.com 
16342Smax.romanov@nginx.com 
16442Smax.romanov@nginx.com #define                                                                       \
1650Sigor@sysoev.ru nxt_buf_is_sync(b)                                                            \
1660Sigor@sysoev.ru     ((b)->is_sync)
1670Sigor@sysoev.ru 
1680Sigor@sysoev.ru #define                                                                       \
1690Sigor@sysoev.ru nxt_buf_set_sync(b)                                                           \
1700Sigor@sysoev.ru     (b)->is_sync = 1
1710Sigor@sysoev.ru 
1720Sigor@sysoev.ru #define                                                                       \
1730Sigor@sysoev.ru nxt_buf_clear_sync(b)                                                         \
1740Sigor@sysoev.ru     (b)->is_sync = 0
1750Sigor@sysoev.ru 
1760Sigor@sysoev.ru 
1770Sigor@sysoev.ru #define                                                                       \
1780Sigor@sysoev.ru nxt_buf_is_nobuf(b)                                                           \
1790Sigor@sysoev.ru     ((b)->is_nobuf)
1800Sigor@sysoev.ru 
1810Sigor@sysoev.ru #define                                                                       \
1820Sigor@sysoev.ru nxt_buf_set_nobuf(b)                                                          \
1830Sigor@sysoev.ru     (b)->is_nobuf = 1
1840Sigor@sysoev.ru 
1850Sigor@sysoev.ru #define                                                                       \
1860Sigor@sysoev.ru nxt_buf_clear_nobuf(b)                                                        \
1870Sigor@sysoev.ru     (b)->is_nobuf = 0
1880Sigor@sysoev.ru 
1890Sigor@sysoev.ru 
1900Sigor@sysoev.ru #define                                                                       \
1910Sigor@sysoev.ru nxt_buf_is_flush(b)                                                           \
1920Sigor@sysoev.ru     ((b)->is_flush)
1930Sigor@sysoev.ru 
1940Sigor@sysoev.ru #define                                                                       \
1950Sigor@sysoev.ru nxt_buf_set_flush(b)                                                          \
1960Sigor@sysoev.ru     (b)->is_flush = 1
1970Sigor@sysoev.ru 
1980Sigor@sysoev.ru #define                                                                       \
1990Sigor@sysoev.ru nxt_buf_clear_flush(b)                                                        \
2000Sigor@sysoev.ru     (b)->is_flush = 0
2010Sigor@sysoev.ru 
2020Sigor@sysoev.ru 
2030Sigor@sysoev.ru #define                                                                       \
2040Sigor@sysoev.ru nxt_buf_is_last(b)                                                            \
2050Sigor@sysoev.ru     ((b)->is_last)
2060Sigor@sysoev.ru 
2070Sigor@sysoev.ru #define                                                                       \
2080Sigor@sysoev.ru nxt_buf_set_last(b)                                                           \
2090Sigor@sysoev.ru     (b)->is_last = 1
2100Sigor@sysoev.ru 
2110Sigor@sysoev.ru #define                                                                       \
2120Sigor@sysoev.ru nxt_buf_clear_last(b)                                                         \
2130Sigor@sysoev.ru    (b)->is_last = 0
2140Sigor@sysoev.ru 
2150Sigor@sysoev.ru 
2160Sigor@sysoev.ru #define                                                                       \
2170Sigor@sysoev.ru nxt_buf_mem_set_size(bm, size)                                                \
2180Sigor@sysoev.ru     do {                                                                      \
2190Sigor@sysoev.ru         (bm)->start = 0;                                                      \
2200Sigor@sysoev.ru         (bm)->end = (void *) size;                                            \
2210Sigor@sysoev.ru     } while (0)
2220Sigor@sysoev.ru 
2230Sigor@sysoev.ru 
2240Sigor@sysoev.ru #define                                                                       \
2250Sigor@sysoev.ru nxt_buf_mem_size(bm)                                                          \
2260Sigor@sysoev.ru     ((bm)->end - (bm)->start)
2270Sigor@sysoev.ru 
2280Sigor@sysoev.ru 
2290Sigor@sysoev.ru #define                                                                       \
2300Sigor@sysoev.ru nxt_buf_mem_used_size(bm)                                                     \
2310Sigor@sysoev.ru     ((bm)->free - (bm)->pos)
2320Sigor@sysoev.ru 
2330Sigor@sysoev.ru 
2340Sigor@sysoev.ru #define                                                                       \
2350Sigor@sysoev.ru nxt_buf_mem_free_size(bm)                                                     \
2360Sigor@sysoev.ru     ((bm)->end - (bm)->free)
2370Sigor@sysoev.ru 
2380Sigor@sysoev.ru 
2390Sigor@sysoev.ru #define                                                                       \
2400Sigor@sysoev.ru nxt_buf_used_size(b)                                                          \
2410Sigor@sysoev.ru     (nxt_buf_is_file(b) ? (b)->file_end - (b)->file_pos:                      \
2420Sigor@sysoev.ru                           nxt_buf_mem_used_size(&(b)->mem))
2430Sigor@sysoev.ru 
2440Sigor@sysoev.ru 
2454Sigor@sysoev.ru NXT_EXPORT void nxt_buf_mem_init(nxt_buf_t *b, void *start, size_t size);
246*65Sigor@sysoev.ru NXT_EXPORT nxt_buf_t *nxt_buf_mem_alloc(nxt_mp_t *mp, size_t size,
2470Sigor@sysoev.ru     nxt_uint_t flags);
248*65Sigor@sysoev.ru NXT_EXPORT nxt_buf_t *nxt_buf_file_alloc(nxt_mp_t *mp, size_t size,
2490Sigor@sysoev.ru     nxt_uint_t flags);
250*65Sigor@sysoev.ru NXT_EXPORT nxt_buf_t *nxt_buf_mmap_alloc(nxt_mp_t *mp, size_t size);
251*65Sigor@sysoev.ru NXT_EXPORT nxt_buf_t *nxt_buf_sync_alloc(nxt_mp_t *mp, nxt_uint_t flags);
2520Sigor@sysoev.ru 
2530Sigor@sysoev.ru 
2540Sigor@sysoev.ru #define                                                                       \
2550Sigor@sysoev.ru nxt_buf_free(mp, b)                                                           \
256*65Sigor@sysoev.ru     nxt_mp_free((mp), (b))
2570Sigor@sysoev.ru 
2580Sigor@sysoev.ru 
2590Sigor@sysoev.ru NXT_EXPORT void nxt_buf_chain_add(nxt_buf_t **head, nxt_buf_t *in);
2600Sigor@sysoev.ru NXT_EXPORT size_t nxt_buf_chain_length(nxt_buf_t *b);
2610Sigor@sysoev.ru 
2620Sigor@sysoev.ru 
2630Sigor@sysoev.ru #endif /* _NXT_BUF_H_INCLIDED_ */
264