summaryrefslogtreecommitdiffhomepage
path: root/src/nxt_mem_pool.c
blob: 7817bac74fcb7b2b6de5c22adc122d30511a1ba6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644

/*
 * Copyright (C) Igor Sysoev
 * Copyright (C) NGINX, Inc.
 */

#include <nxt_main.h>


/*
 * The pool allocator provides cheap allocation of small objects.
 * The objects are allocated from larger preallocated chunks.
 *
 *   aligned and non-aligned allocations,
 *   cache of reusable objects, lvlhsh-specific cache
 *   eliminating align padding
 *   data locality
 *   freeing on pool destruction
 *   freeing large allocations
 */


static void *nxt_mem_pool_align(nxt_mem_pool_t *mp, size_t alignment,
    size_t size);
static void *nxt_mem_pool_ext(nxt_mem_pool_t *mp, size_t size);
static nxt_mem_pool_chunk_t *nxt_mem_pool_next_chunk(nxt_mem_pool_t *mp,
    nxt_mem_pool_chunk_t *chunk);
static nxt_mem_pool_chunk_t *nxt_mem_pool_chunk(nxt_mem_pool_t *mp);
static void *nxt_mem_lvlhsh_alloc_chunk(nxt_mem_pool_cache_t *cache,
    size_t size, nxt_uint_t nalloc);


#if (NXT_DEBUG)

static nxt_bool_t
nxt_mem_pool_thread_is_invalid(nxt_mem_pool_t *mp)
{
    nxt_tid_t     tid;
    nxt_thread_t  *thr;

    thr = nxt_thread();
    tid = nxt_thread_tid(thr);

    if (nxt_slow_path(mp->tid != tid)) {

        if (mp->pid == nxt_pid) {
            nxt_log_alert(thr->log, "mem_pool locked by thread %PT", mp->tid);
            nxt_abort();
            return 1;
        }

        mp->pid = nxt_pid;
        mp->tid = tid;
    }

    return 0;
}


/* SunC does not support C99 variadic macro with empty __VA_ARGS__. */

#define                                                                       \
nxt_mem_pool_thread_assert(mp)                                                \
    if (nxt_mem_pool_thread_is_invalid(mp))                                   \
        return


#define                                                                       \
nxt_mem_pool_thread_assert_return(mp, ret)                                    \
    if (nxt_mem_pool_thread_is_invalid(mp))                                   \
        return ret


#else /* !(NXT_DEBUG) */

#define                                                                       \
nxt_mem_pool_thread_assert(mp)

#define                                                                       \
nxt_mem_pool_thread_assert_return(mp, ret)

#endif


nxt_mem_pool_t *
nxt_mem_pool_create(size_t size)
{
    u_char          *p;
    size_t          min_ext_size;
    nxt_mem_pool_t  *mp;

    mp = nxt_malloc(size);

    if (nxt_fast_path(mp != NULL)) {

        mp->chunk_size = (uint32_t) size;

        min_ext_size = size - sizeof(nxt_mem_pool_t) + 1;
        mp->min_ext_size = (uint32_t) nxt_min(min_ext_size,
                                              NXT_MEM_POOL_MIN_EXT_SIZE);

        nxt_malloc_usable_size(mp, size);

        p = (u_char *) mp;

        mp->chunk.free = p + sizeof(nxt_mem_pool_t);
        mp->chunk.end = p + size;
        mp->chunk.next = NULL;
        mp->chunk.fails = 0;

        mp->current = &mp->chunk;
        mp->ext = NULL;
        mp->cleanup = NULL;
        mp->cache = NULL;

        nxt_thread_log_debug("mem pool chunk size:%uz avail:%uz",
                             size, mp->chunk.end - mp->chunk.free);

        nxt_mem_pool_debug_lock(mp, nxt_thread_tid(NULL));
    }

    return mp;
}


void
nxt_mem_pool_destroy(nxt_mem_pool_t *mp)
{
    nxt_task_t              *task;
    nxt_mem_pool_ext_t      *ext;
    nxt_mem_pool_chunk_t    *chunk, *next;
    nxt_mem_pool_cleanup_t  *mpcl;

    task = NULL;

    nxt_mem_pool_thread_assert(mp);

    for (mpcl = mp->cleanup; mpcl != NULL; mpcl = mpcl->next) {
        if (mpcl->handler != NULL) {
            nxt_thread_log_debug("mem pool cleanup: %p", mpcl);
            mpcl->handler(task, mpcl->data);
        }
    }

    for (ext = mp->ext; ext != NULL; ext = ext->next) {
        if (ext->data != NULL) {
            nxt_free(ext->data);
        }
    }

    chunk = &mp->chunk;

    do {
        nxt_thread_log_debug("mem pool chunk fails:%uD unused:%uz",
                             chunk->fails, chunk->end - chunk->free);
        next = chunk->next;
        nxt_free(chunk);
        chunk = next;

    } while (chunk != NULL);
}


void *
nxt_mem_align(nxt_mem_pool_t *mp, size_t alignment, size_t size)
{
    nxt_mem_pool_thread_assert_return(mp, NULL);

    if (nxt_fast_path(size < mp->min_ext_size)) {
        return nxt_mem_pool_align(mp, alignment, size);
    }

    return nxt_mem_pool_ext(mp, size);
}


void *
nxt_mem_zalign(nxt_mem_pool_t *mp, size_t alignment, size_t size)
{
    void  *p;

    p = nxt_mem_align(mp, alignment, size);

    if (nxt_fast_path(p != NULL)) {
        nxt_memzero(p, size);
    }

    return p;
}


/*
 * Zero-filled aligned allocation, suitable for struct
 * allocation without long double and SIMD values.
 */

void *
nxt_mem_zalloc(nxt_mem_pool_t *mp, size_t size)
{
    void  *p;

    p = nxt_mem_alloc(mp, size);

    if (nxt_fast_path(p != NULL)) {
        nxt_memzero(p, size);
    }

    return p;
}


void *
nxt_mem_buf(nxt_mem_pool_t *mp, size_t *sizep, nxt_uint_t flags)
{
    u_char  *p;
    size_t  size;

    nxt_mem_pool_thread_assert_return(mp, NULL);

    size = *sizep;

    if (nxt_fast_path(size >= mp->min_ext_size)) {

        nxt_malloc_cutback(flags & NXT_MEM_BUF_CUTBACK, size);

        /* Windows only: try to minimize number of allocated pages. */
        p = nxt_mem_pool_ext(mp, size);
        if (p != NULL) {

            if (flags & NXT_MEM_BUF_USABLE) {
                nxt_malloc_usable_size(p, size);
            }

            *sizep = size;
        }

        return p;
    }

    return nxt_mem_pool_align(mp, NXT_ALIGNMENT, size);
}


/* Non-aligned allocation, suitable for string allocation. */

void *
nxt_mem_nalloc(nxt_mem_pool_t *mp, size_t size)
{
    u_char                *p;
    nxt_mem_pool_chunk_t  *chunk;

    nxt_mem_pool_thread_assert_return(mp, NULL);

    if (nxt_slow_path(size >= mp->min_ext_size)) {
        return nxt_mem_pool_ext(mp, size);
    }

    chunk = mp->current;

    for ( ;; ) {
        p = chunk->end - size;

        if (nxt_fast_path(p >= chunk->free)) {
            chunk->end = p;
            return p;
        }

        chunk = nxt_mem_pool_next_chunk(mp, chunk);

        if (nxt_slow_path(chunk == NULL)) {
            return NULL;
        }
    }
}


/* An attempt to deallocate a large allocation outside pool. */

nxt_int_t
nxt_mem_free(nxt_mem_pool_t *mp, void *p)
{
    nxt_mem_pool_ext_t  *ext;

    nxt_mem_pool_thread_assert_return(mp, NXT_DECLINED);

    for (ext = mp->ext; ext != NULL; ext = ext->next) {

        if (p == ext->data) {
            nxt_free(ext->data);
            ext->data = NULL;

            return NXT_OK;
        }
    }

    return NXT_DECLINED;
}


static void *
nxt_mem_pool_ext(nxt_mem_pool_t *mp, size_t size)
{
    void                *p;
    nxt_mem_pool_ext_t  *ext;

    ext = nxt_mem_pool_align(mp, sizeof(void *), sizeof(nxt_mem_pool_ext_t));

    if (nxt_fast_path(ext != NULL)) {
        p = nxt_malloc(size);

        if (nxt_fast_path(p != NULL)) {
            ext->data = p;
            ext->next = mp->ext;
            mp->ext = ext;

            return p;
        }
    }

    return NULL;
}


static void *
nxt_mem_pool_align(nxt_mem_pool_t *mp, size_t alignment, size_t size)
{
    u_char                *p, *f;
    nxt_mem_pool_chunk_t  *chunk;

    chunk = mp->current;

    for ( ;; ) {

        p = nxt_align_ptr(chunk->free, alignment);
        f = p + size;

        if (nxt_fast_path(f <= chunk->end)) {
            chunk->free = f;
            return p;
        }

        chunk = nxt_mem_pool_next_chunk(mp, chunk);

        if (nxt_slow_path(chunk == NULL)) {
            return NULL;
        }
    }
}


static nxt_mem_pool_chunk_t *
nxt_mem_pool_next_chunk(nxt_mem_pool_t *mp, nxt_mem_pool_chunk_t *chunk)
{
    nxt_bool_t  full;

    full = (chunk->free == chunk->end || chunk->fails++ > 10);

    chunk = chunk->next;

    if (chunk == NULL) {
        chunk = nxt_mem_pool_chunk(mp);

        if (nxt_slow_path(chunk == NULL)) {
            return NULL;
        }
    }

    if (full) {
        mp->current = chunk;
    }

    return chunk;
}


static nxt_mem_pool_chunk_t *
nxt_mem_pool_chunk(nxt_mem_pool_t *mp)
{
    u_char                *p;
    size_t                size;
    nxt_mem_pool_chunk_t  *ch, *chunk;

    size = mp->chunk_size;

    chunk = nxt_malloc(size);

    if (nxt_fast_path(chunk != NULL)) {

        nxt_malloc_usable_size(chunk, size);

        p = (u_char *) chunk;

        chunk->free = p + sizeof(nxt_mem_pool_chunk_t);
        chunk->end = p + size;
        chunk->next = NULL;
        chunk->fails = 0;

        for (ch = mp->current; ch->next; ch = ch->next) { /* void */ }

        ch->next = chunk;
    }

    return chunk;
}


nxt_mem_pool_cleanup_t *
nxt_mem_pool_cleanup(nxt_mem_pool_t *mp, size_t size)
{
    nxt_mem_pool_cleanup_t  *mpcl;

    nxt_mem_pool_thread_assert_return(mp, NULL);

    mpcl = nxt_mem_pool_align(mp, sizeof(void *),
                              sizeof(nxt_mem_pool_cleanup_t));
    if (nxt_fast_path(mpcl != NULL)) {

        mpcl->handler = NULL;
        mpcl->data = NULL;

        if (size != 0) {
            mpcl->data = nxt_mem_alloc(mp, size);
            if (nxt_slow_path(mpcl->data == NULL)) {
                return NULL;
            }
        }

        mpcl->next = mp->cleanup;
        mp->cleanup = mpcl;

        nxt_thread_log_debug("mem pool cleanup add: %p", mpcl);
    }

    return mpcl;
}


/* Allocation of reusable object with specified size. */

void *
nxt_mem_cache_alloc0(nxt_mem_pool_t *mp, size_t size)
{
    void                  **pp;
    nxt_mem_pool_cache_t  *cache;

    nxt_mem_pool_thread_assert_return(mp, NULL);

    for (cache = mp->cache; cache != NULL; cache = cache->next) {

        if (cache->size == size && cache->nalloc == 0) {

            if (cache->free != NULL) {
                pp = cache->free;
                cache->free = *pp;
                return pp;
            }

            break;
        }
    }

    return nxt_mem_alloc(mp, size);
}


void *
nxt_mem_cache_zalloc0(nxt_mem_pool_t *mp, size_t size)
{
    void  *p;

    p = nxt_mem_cache_alloc0(mp, size);

    if (nxt_fast_path(p != NULL)) {
        nxt_memzero(p, size);
    }

    return p;
}


/* Deallocation of reusable object with specified size. */

void
nxt_mem_cache_free0(nxt_mem_pool_t *mp, void *p, size_t size)
{
    void                  **pp;
    nxt_mem_pool_cache_t  *cache, **pcache;

    nxt_mem_pool_thread_assert(mp);

    pp = p;

    pcache = &mp->cache;
    for (cache = mp->cache; cache != NULL; cache = cache->next) {

        if (cache->size == size && cache->nalloc == 0) {
            *pp = cache->free;
            cache->free = p;
            return;
        }

        pcache = &cache->next;
    }

    /* Non-lvlhash caches are created only on return. */

    cache = nxt_mem_pool_align(mp, sizeof(void *),
                               sizeof(nxt_mem_pool_cache_t));
    if (nxt_fast_path(cache != NULL)) {
        *pp = NULL;
        cache->size = (uint32_t) size;
        cache->nalloc = 0;
        cache->free = p;
        cache->next = NULL;
        *pcache = cache;
    }
}


/*
 * lvlhsh requires allocations aligned to a size of the allocations.
 * This is not issue for slab-like allocators, but glibc allocator may
 * waste memory on such aligned allocations.  So nxt_mem_lvlhsh_alloc()
 * allocates memory in chunks specified by the "nalloc" parameter
 * except the first allocation.  The first lvlhsh allocation is a bucket
 * allocation and it is enough for small hashes and for early stage
 * of a hash.  By default lvlhsh uses 128-bytes buckets and levels.
 * This allows to search up to 10 entries in one memory access and
 * up to 160 entries in two memory accesses on 64-bit platform.
 * And on 32-bit platform up to 15 entries and up to 480 entries
 * respectively.
 *
 * After the bucket will be filled up with 10 64-bit entries or 15
 * 32-bit entries, lvlhsh will expand it to a level and content
 * of the first bucket will spread to the level's new buckets.
 * Number of the new buckets may be up to 11 on 64-bit or 16 on 32-bit
 * platforms.  It's better to allocate them together to eliminate
 * wasting memory and CPU time.
 *
 * The "nalloc" should be 16 if bucket size is 128 bytes.
 */


/* Allocation of lvlhsh level or bucket with specified size. */

void *
nxt_mem_lvlhsh_alloc(void *ctx, size_t size, nxt_uint_t nalloc)
{
    void                  *p, **pp;
    nxt_mem_pool_t        *mp;
    nxt_mem_pool_cache_t  *cache;

    mp = ctx;

    nxt_mem_pool_thread_assert_return(mp, NULL);

    for (cache = mp->cache; cache != NULL; cache = cache->next) {

        if (cache->size == size && cache->nalloc != 0) {

            if (cache->free != NULL) {
                pp = cache->free;
                cache->free = *pp;
                return pp;
            }

            return nxt_mem_lvlhsh_alloc_chunk(cache, size, nalloc);
        }
    }

    cache = nxt_mem_pool_align(mp, sizeof(void *),
                               sizeof(nxt_mem_pool_cache_t));
    if (nxt_fast_path(cache != NULL)) {

        p = nxt_memalign(size, size);

        if (nxt_fast_path(p != NULL)) {
            cache->size = (uint32_t) size;
            cache->nalloc = nalloc;
            cache->free = NULL;
            cache->next = mp->cache;
            mp->cache = cache;
            return p;
        }
    }

    return NULL;
}


static void *
nxt_mem_lvlhsh_alloc_chunk(nxt_mem_pool_cache_t *cache, size_t size,
    nxt_uint_t nalloc)
{
    char    *m, *p, *end;
    void    **pp;
    size_t  n;

    n = (nalloc == 0) ? 1 : nalloc;
    n *= size;

    m = nxt_memalign(size, n);

    if (nxt_fast_path(m != NULL)) {

        pp = &cache->free;
        end = m + n;

        for (p = m + size; p < end; p = p + size) {
            *pp = p;
            pp = (void **) p;
        }

        *pp = NULL;
    }

    return m;
}


/* Deallocation of lvlhsh level or bucket with specified size. */

void
nxt_mem_lvlhsh_free(void *ctx, void *p, size_t size)
{
    void                  **pp;
    nxt_mem_pool_t        *mp;
    nxt_mem_pool_cache_t  *cache;

    mp = ctx;

    nxt_mem_pool_thread_assert(mp);

    pp = p;

    for (cache = mp->cache; cache != NULL; cache = cache->next) {

        if (cache->size == size && cache->nalloc != 0) {
            *pp = cache->free;
            cache->free = p;
            return;
        }
    }
}