1mtmalloc(3MALLOC) Memory Allocation Library Functions mtmalloc(3MALLOC)
2
3
4
6 mtmalloc, mallocctl - MT hot memory allocator
7
9 #include <mtmalloc.h>
10 cc -o a.out -lthread -lmtmalloc
11
12 void *malloc(size_t size);
13
14
15 void free(void *ptr);
16
17
18 void *memalign(size_t alignment, size_t size);
19
20
21 void *realloc(void *ptr, size_t size);
22
23
24 void *valloc(size_t size);
25
26
27 void mallocctl(int cmd, long value);
28
29
31 The malloc() and free() functions provide a simple general-purpose mem‐
32 ory allocation package that is suitable for use in high performance
33 multithreaded applications. The suggested use of this library is in
34 multithreaded applications; it can be used for single threaded appli‐
35 cations, but there is no advantage in doing so. This library cannot be
36 dynamically loaded with dlopen(3C) during runtime because there must be
37 only one manager of the process heap.
38
39
40 The malloc() function returns a pointer to a block of at least size
41 bytes suitably aligned for any use.
42
43
44 The argument to free() is a pointer to a block previously allocated by
45 malloc() or realloc(). After free() is performed this space is avail‐
46 able for further allocation. If ptr is a null pointer, no action
47 occurs. The free() function does not set errno.
48
49
50 Undefined results will occur if the space assigned by malloc() is over‐
51 run or if a random number is handed to free(). A freed pointer that is
52 passed to free() will send a SIGABRT signal to the calling process.
53 This behavior is controlled by mallocctl().
54
55
56 The memalign() function allocates size bytes on a specified alignment
57 boundary and returns a pointer to the allocated block. The value of the
58 returned address is guaranteed to be an even multiple of alignment.
59 Note that the value of alignment must be a power of two, and must be
60 greater than or equal to the size of a word.
61
62
63 The realloc() function changes the size of the block pointed to by ptr
64 to size bytes and returns a pointer to the (possibly moved) block. The
65 contents will be unchanged up to the lesser of the new and old sizes.
66 If the new size of the block requires movement of the block, the space
67 for the previous instantiation of the block is freed. If the new size
68 is larger, the contents of the newly allocated portion of the block are
69 unspecified. If ptr is NULL, realloc() behaves like malloc() for the
70 specified size. If size is 0 and ptr is not a null pointer, the space
71 pointed to is freed.
72
73
74 The valloc() function has the same effect as malloc(), except that the
75 allocated memory will be aligned to a multiple of the value returned by
76 sysconf(_SC_PAGESIZE).
77
78
79 After possible pointer coercion, each allocation routine returns a
80 pointer to a space that is suitably aligned for storage of any type of
81 object.
82
83
84 The malloc(), realloc(), memalign(), and valloc() functions will fail
85 if there is not enough available memory.
86
87
88 The mallocctl() function controls the behavior of the malloc library.
89 The options fall into two general classes, debugging options and per‐
90 formance options.
91
92 MTDOUBLEFREE Allows double free of a pointer. Setting value to
93 1 means yes and 0 means no. The default behavior of
94 double free results in a core dump.
95
96
97 MTDEBUGPATTERN Writes misaligned data into the buffer after free().
98 When the buffer is reallocated, the contents are ver‐
99 ified to ensure that there was no access to the buf‐
100 fer after the free. If the buffer has been dirtied, a
101 SIGABRT signal is delivered to the process. Setting
102 value to 1 means yes and 0 means no. The default
103 behavior is to not write misaligned data. The pat‐
104 tern used is 0xdeadbeef. Use of this option results
105 in a performance penalty.
106
107
108 MTINITBUFFER Writes misaligned data into the newly allocated buf‐
109 fer. This option is useful for detecting some
110 accesses before initialization. Setting value to 1
111 means yes and 0 means no. The default behavior is to
112 not write misaligned data to the newly allocated buf‐
113 fer. The pattern used is 0xbaddcafe. Use of this
114 option results in a performance penalty.
115
116
117 MTCHUNKSIZE This option changes the size of allocated memory when
118 a pool has exhausted all available memory in the buf‐
119 fer. Increasing this value allocates more memory for
120 the application. A substantial performance gain can
121 occur because the library makes fewer calls to the
122 OS for more memory. Acceptable number values are
123 between 9 and 256. The default value is 9. This
124 value is multiplied by 8192.
125
126
128 If there is no available memory, malloc(), realloc(), memalign(), and
129 valloc() return a null pointer. When realloc() is called with size > 0
130 and returns NULL, the block pointed to by ptr is left intact. If size,
131 nelem, or elsize is 0, either a null pointer or a unique pointer that
132 can be passed to free() is returned.
133
134
135 If malloc() or realloc() returns unsuccessfully, errno will be set to
136 indicate the error.
137
139 The malloc() and realloc() functions will fail if:
140
141 ENOMEM The physical limits of the system are exceeded by size bytes
142 of memory which cannot be allocated.
143
144
145 EAGAIN There is not enough memory available to allocate size bytes
146 of memory; but the application could try again later.
147
148
150 Comparative features of the various allocation libraries can be found
151 in the umem_alloc(3MALLOC) manual page.
152
154 See attributes(5) for descriptions of the following attributes:
155
156
157
158
159 ┌─────────────────────────────┬─────────────────────────────┐
160 │ ATTRIBUTE TYPE │ ATTRIBUTE VALUE │
161 ├─────────────────────────────┼─────────────────────────────┤
162 │MT-Level │Safe │
163 └─────────────────────────────┴─────────────────────────────┘
164
166 brk(2), getrlimit(2), bsdmalloc(3MALLOC), dlopen(3C), malloc(3C), mal‐
167 loc(3MALLOC), mapmalloc(3MALLOC), signal.h(3HEAD), umem_alloc(3MALLOC),
168 watchmalloc(3MALLOC), attributes(5)
169
171 Undefined results will occur if the size requested for a block of mem‐
172 ory exceeds the maximum size of a process's heap. This information may
173 be obtained using getrlimit().
174
175
176
177SunOS 5.11 21 Mar 2005 mtmalloc(3MALLOC)