summaryrefslogtreecommitdiffhomepage
path: root/src/nxt_mp.h
blob: 71a48de67647d66a90068f99e3b2c881dbfb7d7b (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

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

#ifndef _NXT_MP_H_INCLUDED_
#define _NXT_MP_H_INCLUDED_


/*
 * Memory pool keeps track of all allocations so they can be freed at once
 * on pool destruction.  A memory pool is not thread safe, so only one thread
 * must work with the pool.  If an allocation should be passed to another
 * thread, it should be allocated with nxt_mp_retain() and then should be
 * freed with nxt_mp_release().  These functions updates pool retention
 * counter.  Memory pools decrease number of malloc() and free() calls and
 * thus reduces thread contention on locks in malloc library.  Memory pools
 * allow to make both freeable and non-freeable allocations.  The freeable
 * memory is allocated in fixed size chunks to decrease memory fragmentaiton
 * on reallocations.  The non-freeable memory is intended to allocate
 * structures and other items which should be available until memory pool
 * destruction.  Due to allocation strategy described in nxt_mp.c memory pools
 * may also improve data cache locality.
 */

typedef struct nxt_mp_s  nxt_mp_t;


/*
 * nxt_mp_create() creates a memory pool and sets the pool's retention
 * counter to 1.
 */
NXT_EXPORT nxt_mp_t *nxt_mp_create(size_t cluster_size, size_t page_alignment,
    size_t page_size, size_t min_chunk_size)
    NXT_MALLOC_LIKE;

/*
 * nxt_mp_destroy() destroys memory pool in spite of the pool's retention
 * counter.
 */
NXT_EXPORT void nxt_mp_destroy(nxt_mp_t *mp);

/* nxt_mp_test_sizes() tests validity of memory pool parameters. */
NXT_EXPORT nxt_bool_t nxt_mp_test_sizes(size_t cluster_size,
    size_t page_alignment, size_t page_size, size_t min_chunk_size);

/* nxt_mp_is_empty() tests that pool is empty. */
NXT_EXPORT nxt_bool_t nxt_mp_is_empty(nxt_mp_t *mp);


/*
 * nxt_mp_alloc() returns aligned freeable memory.
 * The alignment is sutiable to allocate structures.
 */
NXT_EXPORT void *nxt_mp_alloc(nxt_mp_t *mp, size_t size)
    NXT_MALLOC_LIKE;


/*
 * nxt_mp_zalloc() returns zeroed aligned freeable memory.
 * The alignment is sutiable to allocate structures.
 */
NXT_EXPORT void *nxt_mp_zalloc(nxt_mp_t *mp, size_t size)
    NXT_MALLOC_LIKE;

/* nxt_mp_align() returns aligned freeable memory. */
NXT_EXPORT void *nxt_mp_align(nxt_mp_t *mp, size_t alignment, size_t size)
    NXT_MALLOC_LIKE;

/* nxt_mp_zalign() returns zeroed aligned freeable memory. */
NXT_EXPORT void *nxt_mp_zalign(nxt_mp_t *mp, size_t alignment, size_t size)
    NXT_MALLOC_LIKE;

/* nxt_mp_free() frees freeable memory. */
NXT_EXPORT void nxt_mp_free(nxt_mp_t *mp, void *p);


/*
 * nxt_mp_retain() returns aligned freeable memory and increases memory
 * pool retention counter.
 */
NXT_EXPORT void *nxt_mp_retain(nxt_mp_t *mp, size_t size)
    NXT_MALLOC_LIKE;

/*
 * nxt_mp_release() returns freeable memory and decreases memory pool
 * retention counter.  If the counter becomes zero the pool is destroyed.
 */
NXT_EXPORT uint32_t nxt_mp_release(nxt_mp_t *mp, void *p);


/* nxt_mp_nget() returns non-aligned non-freeable memory. */
NXT_EXPORT void *nxt_mp_nget(nxt_mp_t *mp, size_t size)
    NXT_MALLOC_LIKE;

/*
 * nxt_mp_get() returns aligned non-freeable memory.
 * The alignment is sutiable to allocate structures.
 */
NXT_EXPORT void *nxt_mp_get(nxt_mp_t *mp, size_t size)
    NXT_MALLOC_LIKE;

/*
 * nxt_mp_zget() returns zeroed aligned non-freeable memory.
 * The alignment is sutiable to allocate structures.
 */
NXT_EXPORT void *nxt_mp_zget(nxt_mp_t *mp, size_t size)
    NXT_MALLOC_LIKE;


NXT_EXPORT nxt_int_t nxt_mp_cleanup(nxt_mp_t *mp, nxt_work_handler_t handler,
    nxt_task_t *task, void *obj, void *data);


NXT_EXPORT void nxt_mp_thread_adopt(nxt_mp_t *mp);

#endif /* _NXT_MP_H_INCLUDED_ */