xref: /unit/src/nxt_mp.c (revision 68:661110aad990)
1 
2 /*
3  * Copyright (C) Igor Sysoev
4  * Copyright (C) NGINX, Inc.
5  */
6 
7 #include <nxt_main.h>
8 
9 
10 /*
11  * A memory pool allocates memory in clusters of specified size and aligned
12  * to page_alignment.  A cluster is divided on pages of specified size.  Page
13  * size must be a power of 2.  A page can be used entirely or can be divided
14  * on chunks of equal size.  Chunk size must be a power of 2.  Non-freeable
15  * memory is also allocated from pages.  A cluster can contains a mix of pages
16  * with different chunk sizes and non-freeable pages.  Cluster size must be
17  * a multiple of page size and may be not a power of 2.  Allocations greater
18  * than page are allocated outside clusters.  Start addresses and sizes of
19  * the clusters and large allocations are stored in rbtree blocks to find
20  * them on free operations.  The rbtree nodes are sorted by start addresses.
21  * The rbtree is also used to destroy memory pool.
22  */
23 
24 
25 typedef struct {
26     /*
27      * Used to link
28      *  *) pages with free chunks in pool chunk pages lists,
29      *  *) pages with free space for non-freeable allocations,
30      *  *) free pages in clusters.
31      */
32     nxt_queue_link_t     link;
33 
34     union {
35         /* Chunk bitmap.  There can be no more than 32 chunks in a page. */
36         uint32_t         map;
37 
38         /* Size of taken non-freeable space. */
39         uint32_t         taken;
40     } u;
41 
42     /*
43      * Size of chunks or page shifted by pool->chunk_size_shift.  Zero means
44      * that page is free, 0xFF means page with non-freeable allocations.
45      */
46     uint8_t              size;
47 
48     /* Number of free chunks of a chunked page. */
49     uint8_t              chunks;
50 
51     /*
52      * Number of allocation fails due to free space insufficiency
53      * in non-freeable page.
54      */
55     uint8_t              fails;
56 
57     /*
58      * Page number in page cluster.
59      * There can be no more than 256 pages in a cluster.
60      */
61     uint8_t              number;
62 } nxt_mp_page_t;
63 
64 
65 /*
66  * Some malloc implementations (e.g. jemalloc) allocates large enough
67  * blocks (e.g. greater than 4K) with 4K alignment.  So if a block
68  * descriptor will be allocated together with the block it will take
69  * excessive 4K memory.  So it is better to allocate the block descriptor
70  * apart.
71  */
72 
73 typedef enum {
74     /* Block of cluster.  The block is allocated apart of the cluster. */
75     NXT_MP_CLUSTER_BLOCK = 0,
76     /*
77      * Block of large allocation.
78      * The block is allocated apart of the allocation.
79      */
80     NXT_MP_DISCRETE_BLOCK,
81     /*
82      * Block of large allocation.
83      * The block is allocated just after of the allocation.
84      */
85     NXT_MP_EMBEDDED_BLOCK,
86 } nxt_mp_block_type_t;
87 
88 
89 typedef struct {
90     NXT_RBTREE_NODE      (node);
91     nxt_mp_block_type_t  type:8;
92 
93     /* Block size must be less than 4G. */
94     uint32_t             size;
95 
96     u_char               *start;
97     nxt_mp_page_t        pages[];
98 } nxt_mp_block_t;
99 
100 
101 struct nxt_mp_s {
102     /* rbtree of nxt_mp_block_t. */
103     nxt_rbtree_t         blocks;
104 
105     uint8_t              chunk_size_shift;
106     uint8_t              page_size_shift;
107     uint32_t             page_size;
108     uint32_t             page_alignment;
109     uint32_t             cluster_size;
110     uint32_t             retain;
111 
112     /* Lists of nxt_mp_page_t. */
113     nxt_queue_t          free_pages;
114     nxt_queue_t          nget_pages;
115     nxt_queue_t          get_pages;
116     nxt_queue_t          chunk_pages[0];
117 };
118 
119 
120 #define nxt_mp_chunk_get_free(map)                                            \
121     (__builtin_ffs(map) - 1)
122 
123 
124 #define nxt_mp_chunk_is_free(map, chunk)                                      \
125     ((map & (1 << chunk)) != 0)
126 
127 
128 #define nxt_mp_chunk_set_busy(map, chunk)                                     \
129     map &= ~(1 << chunk)
130 
131 
132 #define nxt_mp_chunk_set_free(map, chunk)                                     \
133     map |= (1 << chunk)
134 
135 
136 #define nxt_mp_free_junk(p, size)                                             \
137     memset((p), 0x5A, size)
138 
139 
140 #if !(NXT_DEBUG_MEMORY)
141 static void *nxt_mp_alloc_small(nxt_mp_t *mp, size_t size);
142 static void *nxt_mp_get_small(nxt_mp_t *mp, nxt_queue_t *pages, size_t size);
143 static nxt_mp_page_t *nxt_mp_alloc_page(nxt_mp_t *mp);
144 static nxt_mp_block_t *nxt_mp_alloc_cluster(nxt_mp_t *mp);
145 #endif
146 static void *nxt_mp_alloc_large(nxt_mp_t *mp, size_t alignment, size_t size);
147 static intptr_t nxt_mp_rbtree_compare(nxt_rbtree_node_t *node1,
148     nxt_rbtree_node_t *node2);
149 static nxt_mp_block_t *nxt_mp_find_block(nxt_rbtree_t *tree, u_char *p);
150 static const char *nxt_mp_chunk_free(nxt_mp_t *mp, nxt_mp_block_t *cluster,
151     u_char *p);
152 
153 
154 nxt_mp_t *
155 nxt_mp_create(size_t cluster_size, size_t page_alignment, size_t page_size,
156     size_t min_chunk_size)
157 {
158     nxt_mp_t     *mp;
159     nxt_uint_t   pages, chunk_size;
160     nxt_queue_t  *chunk_pages;
161 
162     pages = 0;
163     chunk_size = page_size;
164 
165     do {
166         pages++;
167         chunk_size /= 2;
168     } while (chunk_size > min_chunk_size);
169 
170     mp = nxt_zalloc(sizeof(nxt_mp_t) + pages * sizeof(nxt_queue_t));
171 
172     if (nxt_fast_path(mp != NULL)) {
173         mp->retain = 1;
174         mp->page_size = page_size;
175         mp->page_alignment = nxt_max(page_alignment, NXT_MAX_ALIGNMENT);
176         mp->cluster_size = cluster_size;
177 
178         chunk_pages = mp->chunk_pages;
179 
180         do {
181             nxt_queue_init(chunk_pages);
182             chunk_pages++;
183             chunk_size *= 2;
184         } while (chunk_size < page_size);
185 
186         mp->chunk_size_shift = nxt_lg2(min_chunk_size);
187         mp->page_size_shift = nxt_lg2(page_size);
188 
189         nxt_rbtree_init(&mp->blocks, nxt_mp_rbtree_compare);
190 
191         nxt_queue_init(&mp->free_pages);
192         nxt_queue_init(&mp->nget_pages);
193         nxt_queue_init(&mp->get_pages);
194     }
195 
196     return mp;
197 }
198 
199 
200 void
201 nxt_mp_destroy(nxt_mp_t *mp)
202 {
203     void               *p;
204     nxt_mp_block_t     *block;
205     nxt_rbtree_node_t  *node, *next;
206 
207     nxt_debug_alloc("mp destroy");
208 
209     next = nxt_rbtree_root(&mp->blocks);
210 
211     while (next != nxt_rbtree_sentinel(&mp->blocks)) {
212 
213         node = nxt_rbtree_destroy_next(&mp->blocks, &next);
214         block = (nxt_mp_block_t *) node;
215 
216         p = block->start;
217 
218         if (block->type != NXT_MP_EMBEDDED_BLOCK) {
219             nxt_free(block);
220         }
221 
222         nxt_free(p);
223     }
224 
225     nxt_free(mp);
226 }
227 
228 
229 nxt_bool_t
230 nxt_mp_test_sizes(size_t cluster_size, size_t page_alignment, size_t page_size,
231     size_t min_chunk_size)
232 {
233     nxt_bool_t  valid;
234 
235     /* Alignment and sizes must be a power of 2. */
236 
237     valid = nxt_expect(1, (nxt_is_power_of_two(page_alignment)
238                            && nxt_is_power_of_two(page_size)
239                            && nxt_is_power_of_two(min_chunk_size)));
240     if (!valid) {
241         return 0;
242     }
243 
244     page_alignment = nxt_max(page_alignment, NXT_MAX_ALIGNMENT);
245 
246     valid = nxt_expect(1, (page_size >= 64
247                            && page_size >= page_alignment
248                            && page_size >= min_chunk_size
249                            && min_chunk_size * 32 >= page_size
250                            && cluster_size >= page_size
251                            && cluster_size / page_size <= 256
252                            && cluster_size % page_size == 0));
253     if (!valid) {
254         return 0;
255     }
256 
257     return 1;
258 }
259 
260 
261 nxt_bool_t
262 nxt_mp_is_empty(nxt_mp_t *mp)
263 {
264     return (nxt_rbtree_is_empty(&mp->blocks)
265             && nxt_queue_is_empty(&mp->free_pages));
266 }
267 
268 
269 void *
270 nxt_mp_alloc(nxt_mp_t *mp, size_t size)
271 {
272     nxt_debug_alloc("mp alloc: %uz", size);
273 
274 #if !(NXT_DEBUG_MEMORY)
275 
276     if (size <= mp->page_size) {
277         return nxt_mp_alloc_small(mp, size);
278     }
279 
280 #endif
281 
282     return nxt_mp_alloc_large(mp, NXT_MAX_ALIGNMENT, size);
283 }
284 
285 
286 void *
287 nxt_mp_zalloc(nxt_mp_t *mp, size_t size)
288 {
289     void  *p;
290 
291     p = nxt_mp_alloc(mp, size);
292 
293     if (nxt_fast_path(p != NULL)) {
294         memset(p, 0, size);
295     }
296 
297     return p;
298 }
299 
300 
301 void *
302 nxt_mp_align(nxt_mp_t *mp, size_t alignment, size_t size)
303 {
304     nxt_debug_alloc("mp align: @%uz:%uz", alignment, size);
305 
306     /* Alignment must be a power of 2. */
307 
308     if (nxt_fast_path(nxt_is_power_of_two(alignment))) {
309 
310 #if !(NXT_DEBUG_MEMORY)
311 
312         if (size <= mp->page_size && alignment <= mp->page_alignment) {
313             size = nxt_max(size, alignment);
314 
315             if (size <= mp->page_size) {
316                 return nxt_mp_alloc_small(mp, size);
317             }
318         }
319 
320 #endif
321 
322         return nxt_mp_alloc_large(mp, alignment, size);
323     }
324 
325     return NULL;
326 }
327 
328 
329 void *
330 nxt_mp_zalign(nxt_mp_t *mp, size_t alignment, size_t size)
331 {
332     void  *p;
333 
334     p = nxt_mp_align(mp, alignment, size);
335 
336     if (nxt_fast_path(p != NULL)) {
337         memset(p, 0, size);
338     }
339 
340     return p;
341 }
342 
343 
344 nxt_inline nxt_uint_t
345 nxt_mp_chunk_pages_index(nxt_mp_t *mp, size_t size)
346 {
347     nxt_int_t  n, index;
348 
349     index = 0;
350 
351     if (size > 1) {
352         n = nxt_lg2(size - 1) + 1 - mp->chunk_size_shift;
353 
354         if (n > 0) {
355             index = n;
356         }
357     }
358 
359     return index;
360 }
361 
362 
363 #if !(NXT_DEBUG_MEMORY)
364 
365 nxt_inline u_char *
366 nxt_mp_page_addr(nxt_mp_t *mp, nxt_mp_page_t *page)
367 {
368     size_t          page_offset;
369     nxt_mp_block_t  *block;
370 
371     page_offset = page->number * sizeof(nxt_mp_page_t)
372                   + offsetof(nxt_mp_block_t, pages);
373 
374     block = (nxt_mp_block_t *) ((u_char *) page - page_offset);
375 
376     return block->start + (page->number << mp->page_size_shift);
377 }
378 
379 
380 static void *
381 nxt_mp_alloc_small(nxt_mp_t *mp, size_t size)
382 {
383     u_char            *p;
384     nxt_uint_t        n, index;
385     nxt_queue_t       *chunk_pages;
386     nxt_mp_page_t     *page;
387     nxt_queue_link_t  *link;
388 
389     p = NULL;
390 
391     if (size <= mp->page_size / 2) {
392 
393         index = nxt_mp_chunk_pages_index(mp, size);
394         chunk_pages = &mp->chunk_pages[index];
395 
396         if (nxt_fast_path(!nxt_queue_is_empty(chunk_pages))) {
397 
398             link = nxt_queue_first(chunk_pages);
399             page = nxt_queue_link_data(link, nxt_mp_page_t, link);
400 
401             p = nxt_mp_page_addr(mp, page);
402 
403             n = nxt_mp_chunk_get_free(page->u.map);
404             nxt_mp_chunk_set_busy(page->u.map, n);
405 
406             p += ((n << index) << mp->chunk_size_shift);
407 
408             page->chunks--;
409 
410             if (page->chunks == 0) {
411                 /*
412                  * Remove full page from the pool chunk pages list
413                  * of pages with free chunks.
414                  */
415                 nxt_queue_remove(&page->link);
416             }
417 
418         } else {
419             page = nxt_mp_alloc_page(mp);
420 
421             if (nxt_fast_path(page != NULL)) {
422                 page->size = (1 << index);
423 
424                 n = mp->page_size_shift - (index + mp->chunk_size_shift);
425                 page->chunks = (1 << n) - 1;
426 
427                 nxt_queue_insert_head(chunk_pages, &page->link);
428 
429                 /* Mark the first chunk as busy. */
430                 page->u.map = 0xFFFFFFFE;
431 
432                 p = nxt_mp_page_addr(mp, page);
433             }
434         }
435 
436     } else {
437         page = nxt_mp_alloc_page(mp);
438 
439         if (nxt_fast_path(page != NULL)) {
440             page->size = mp->page_size >> mp->chunk_size_shift;
441 
442             p = nxt_mp_page_addr(mp, page);
443         }
444     }
445 
446     nxt_debug_alloc("mp chunk:%uz alloc: %p",
447                     page->size << mp->chunk_size_shift, p);
448 
449     return p;
450 }
451 
452 
453 static void *
454 nxt_mp_get_small(nxt_mp_t *mp, nxt_queue_t *pages, size_t size)
455 {
456     u_char            *p;
457     uint32_t          available;
458     nxt_mp_page_t     *page;
459     nxt_queue_link_t  *link, *next;
460 
461     for (link = nxt_queue_first(pages);
462          link != nxt_queue_tail(pages);
463          link = next)
464     {
465         next = nxt_queue_next(link);
466         page = nxt_queue_link_data(link, nxt_mp_page_t, link);
467 
468         available = mp->page_size - page->u.taken;
469 
470         if (size <= available) {
471             goto found;
472         }
473 
474         if (available == 0 || page->fails++ > 100) {
475             nxt_queue_remove(link);
476         }
477     }
478 
479     page = nxt_mp_alloc_page(mp);
480 
481     if (nxt_slow_path(page == NULL)) {
482         return page;
483     }
484 
485     nxt_queue_insert_head(pages, &page->link);
486 
487     page->size = 0xFF;
488 
489 found:
490 
491     p = nxt_mp_page_addr(mp, page);
492 
493     p += page->u.taken;
494     page->u.taken += size;
495 
496     nxt_debug_alloc("mp get: %p", p);
497 
498     return p;
499 }
500 
501 
502 static nxt_mp_page_t *
503 nxt_mp_alloc_page(nxt_mp_t *mp)
504 {
505     nxt_mp_page_t     *page;
506     nxt_mp_block_t    *cluster;
507     nxt_queue_link_t  *link;
508 
509     if (nxt_queue_is_empty(&mp->free_pages)) {
510         cluster = nxt_mp_alloc_cluster(mp);
511         if (nxt_slow_path(cluster == NULL)) {
512             return NULL;
513         }
514     }
515 
516     link = nxt_queue_first(&mp->free_pages);
517     nxt_queue_remove(link);
518 
519     page = nxt_queue_link_data(link, nxt_mp_page_t, link);
520 
521     return page;
522 }
523 
524 
525 static nxt_mp_block_t *
526 nxt_mp_alloc_cluster(nxt_mp_t *mp)
527 {
528     nxt_uint_t      n;
529     nxt_mp_block_t  *cluster;
530 
531     n = mp->cluster_size >> mp->page_size_shift;
532 
533     cluster = nxt_zalloc(sizeof(nxt_mp_block_t) + n * sizeof(nxt_mp_page_t));
534 
535     if (nxt_slow_path(cluster == NULL)) {
536         return NULL;
537     }
538 
539     /* NXT_MP_CLUSTER_BLOCK type is zero. */
540 
541     cluster->size = mp->cluster_size;
542 
543     cluster->start = nxt_memalign(mp->page_alignment, mp->cluster_size);
544     if (nxt_slow_path(cluster->start == NULL)) {
545         nxt_free(cluster);
546         return NULL;
547     }
548 
549     n--;
550     cluster->pages[n].number = n;
551     nxt_queue_insert_head(&mp->free_pages, &cluster->pages[n].link);
552 
553     while (n != 0) {
554         n--;
555         cluster->pages[n].number = n;
556         nxt_queue_insert_before(&cluster->pages[n + 1].link,
557                                 &cluster->pages[n].link);
558     }
559 
560     nxt_rbtree_insert(&mp->blocks, &cluster->node);
561 
562     return cluster;
563 }
564 
565 #endif
566 
567 
568 static void *
569 nxt_mp_alloc_large(nxt_mp_t *mp, size_t alignment, size_t size)
570 {
571     u_char          *p;
572     size_t          aligned_size;
573     uint8_t         type;
574     nxt_mp_block_t  *block;
575 
576     /* Allocation must be less than 4G. */
577     if (nxt_slow_path(size >= 0xFFFFFFFF)) {
578         return NULL;
579     }
580 
581     if (nxt_is_power_of_two(size)) {
582         block = nxt_malloc(sizeof(nxt_mp_block_t));
583         if (nxt_slow_path(block == NULL)) {
584             return NULL;
585         }
586 
587         p = nxt_memalign(alignment, size);
588         if (nxt_slow_path(p == NULL)) {
589             nxt_free(block);
590             return NULL;
591         }
592 
593         type = NXT_MP_DISCRETE_BLOCK;
594 
595     } else {
596         aligned_size = nxt_align_size(size, sizeof(uintptr_t));
597 
598         p = nxt_memalign(alignment, aligned_size + sizeof(nxt_mp_block_t));
599         if (nxt_slow_path(p == NULL)) {
600             return NULL;
601         }
602 
603         block = (nxt_mp_block_t *) (p + aligned_size);
604         type = NXT_MP_EMBEDDED_BLOCK;
605     }
606 
607     block->type = type;
608     block->size = size;
609     block->start = p;
610 
611     nxt_rbtree_insert(&mp->blocks, &block->node);
612 
613     return p;
614 }
615 
616 
617 static intptr_t
618 nxt_mp_rbtree_compare(nxt_rbtree_node_t *node1, nxt_rbtree_node_t *node2)
619 {
620     nxt_mp_block_t  *block1, *block2;
621 
622     block1 = (nxt_mp_block_t *) node1;
623     block2 = (nxt_mp_block_t *) node2;
624 
625     return (uintptr_t) block1->start - (uintptr_t) block2->start;
626 }
627 
628 
629 void
630 nxt_mp_free(nxt_mp_t *mp, void *p)
631 {
632     const char      *err;
633     nxt_thread_t    *thread;
634     nxt_mp_block_t  *block;
635 
636     nxt_debug_alloc("mp free %p", p);
637 
638     block = nxt_mp_find_block(&mp->blocks, p);
639 
640     if (nxt_fast_path(block != NULL)) {
641 
642         if (block->type == NXT_MP_CLUSTER_BLOCK) {
643             err = nxt_mp_chunk_free(mp, block, p);
644 
645             if (nxt_fast_path(err == NULL)) {
646                 return;
647             }
648 
649         } else if (nxt_fast_path(p == block->start)) {
650             nxt_rbtree_delete(&mp->blocks, &block->node);
651 
652             if (block->type == NXT_MP_DISCRETE_BLOCK) {
653                 nxt_free(block);
654             }
655 
656             nxt_free(p);
657 
658             return;
659 
660         } else {
661             err = "freed pointer points to middle of block: %p";
662         }
663 
664     } else {
665         err = "freed pointer is out of pool: %p";
666     }
667 
668     thread = nxt_thread();
669 
670     nxt_log(thread->task, NXT_LOG_CRIT, err, p);
671 }
672 
673 
674 static nxt_mp_block_t *
675 nxt_mp_find_block(nxt_rbtree_t *tree, u_char *p)
676 {
677     nxt_mp_block_t     *block;
678     nxt_rbtree_node_t  *node, *sentinel;
679 
680     node = nxt_rbtree_root(tree);
681     sentinel = nxt_rbtree_sentinel(tree);
682 
683     while (node != sentinel) {
684 
685         block = (nxt_mp_block_t *) node;
686 
687         if (p < block->start) {
688             node = node->left;
689 
690         } else if (p >= block->start + block->size) {
691             node = node->right;
692 
693         } else {
694             return block;
695         }
696     }
697 
698     return NULL;
699 }
700 
701 
702 static const char *
703 nxt_mp_chunk_free(nxt_mp_t *mp, nxt_mp_block_t *cluster, u_char *p)
704 {
705     u_char         *start;
706     uintptr_t      offset;
707     nxt_uint_t     n, size, chunk;
708     nxt_queue_t    *chunk_pages;
709     nxt_mp_page_t  *page;
710 
711     n = (p - cluster->start) >> mp->page_size_shift;
712     start = cluster->start + (n << mp->page_size_shift);
713 
714     page = &cluster->pages[n];
715 
716     if (nxt_slow_path(page->size == 0)) {
717         return "freed pointer points to already free page: %p";
718     }
719 
720     if (nxt_slow_path(page->size == 0xFF)) {
721         return "freed pointer points to non-freeble page: %p";
722     }
723 
724     size = page->size << mp->chunk_size_shift;
725 
726     if (size != mp->page_size) {
727 
728         offset = (uintptr_t) (p - start) & (mp->page_size - 1);
729         chunk = offset / size;
730 
731         if (nxt_slow_path(offset != chunk * size)) {
732             return "freed pointer points to wrong chunk: %p";
733         }
734 
735         if (nxt_slow_path(nxt_mp_chunk_is_free(page->u.map, chunk))) {
736             return "freed pointer points to already free chunk: %p";
737         }
738 
739         nxt_mp_chunk_set_free(page->u.map, chunk);
740 
741         if (page->u.map != 0xFFFFFFFF) {
742             page->chunks++;
743 
744             if (page->chunks == 1) {
745                 /*
746                  * Add the page to the head of pool chunk pages list
747                  * of pages with free chunks.
748                  */
749                 n = nxt_mp_chunk_pages_index(mp, size);
750                 chunk_pages = &mp->chunk_pages[n];
751 
752                 nxt_queue_insert_head(chunk_pages, &page->link);
753             }
754 
755             nxt_mp_free_junk(p, size);
756 
757             return NULL;
758 
759         } else {
760             /*
761              * All chunks are free, remove the page from pool
762              * chunk pages list of pages with free chunks.
763              */
764             nxt_queue_remove(&page->link);
765         }
766 
767     } else if (nxt_slow_path(p != start)) {
768         return "invalid pointer to chunk: %p";
769     }
770 
771     /* Add the free page to the pool's free pages tree. */
772 
773     page->size = 0;
774     nxt_queue_insert_head(&mp->free_pages, &page->link);
775 
776     nxt_mp_free_junk(p, size);
777 
778     /* Test if all pages in the cluster are free. */
779 
780     n = mp->cluster_size >> mp->page_size_shift;
781     page = cluster->pages;
782 
783     do {
784          if (page->size != 0) {
785              return NULL;
786          }
787 
788          page++;
789          n--;
790     } while (n != 0);
791 
792     /* Free cluster. */
793 
794     n = mp->cluster_size >> mp->page_size_shift;
795     page = cluster->pages;
796 
797     do {
798          nxt_queue_remove(&page->link);
799          page++;
800          n--;
801     } while (n != 0);
802 
803     nxt_rbtree_delete(&mp->blocks, &cluster->node);
804 
805     p = cluster->start;
806 
807     nxt_free(cluster);
808     nxt_free(p);
809 
810     return NULL;
811 }
812 
813 
814 void *
815 nxt_mp_retain(nxt_mp_t *mp, size_t size)
816 {
817     void  *p;
818 
819     p = nxt_mp_alloc(mp, size);
820 
821     if (nxt_fast_path(p != NULL)) {
822         mp->retain++;
823         nxt_debug_alloc("mp retain: %uD", mp->retain);
824     }
825 
826     return p;
827 }
828 
829 
830 void
831 nxt_mp_release(nxt_mp_t *mp, void *p)
832 {
833     nxt_mp_free(mp, p);
834 
835     mp->retain--;
836 
837     nxt_debug_alloc("mp release: %uD", mp->retain);
838 
839     if (mp->retain == 0) {
840         nxt_mp_destroy(mp);
841     }
842 }
843 
844 
845 void *
846 nxt_mp_nget(nxt_mp_t *mp, size_t size)
847 {
848     nxt_debug_alloc("mp nget: %uz", size);
849 
850 #if !(NXT_DEBUG_MEMORY)
851 
852     if (size <= mp->page_size) {
853         return nxt_mp_get_small(mp, &mp->nget_pages, size);
854     }
855 
856 #endif
857 
858     return nxt_mp_alloc_large(mp, NXT_MAX_ALIGNMENT, size);
859 }
860 
861 
862 void *
863 nxt_mp_get(nxt_mp_t *mp, size_t size)
864 {
865     nxt_debug_alloc("mp get: %uz", size);
866 
867 #if !(NXT_DEBUG_MEMORY)
868 
869     if (size <= mp->page_size) {
870         size = nxt_max(size, NXT_MAX_ALIGNMENT);
871         return nxt_mp_get_small(mp, &mp->get_pages, size);
872     }
873 
874 #endif
875 
876     return nxt_mp_alloc_large(mp, NXT_MAX_ALIGNMENT, size);
877 }
878 
879 
880 void *
881 nxt_mp_zget(nxt_mp_t *mp, size_t size)
882 {
883     void  *p;
884 
885     p = nxt_mp_get(mp, size);
886 
887     if (nxt_fast_path(p != NULL)) {
888         memset(p, 0, size);
889     }
890 
891     return p;
892 }
893