xref: /illumos-gate/usr/src/man/man3c/memory.3c (revision 2c65701281156d8db8fa6f6f9c5faa6ca021c621)
1.\"
2.\" Copyright (c) 2014, Joyent, Inc.
3.\" Copyright (c) 2009, Sun Microsystems, Inc.  All Rights Reserved.
4.\" Copyright 1989 AT&T
5.\" Copyright 2021 Oxide Computer Company
6.\"
7.\" The contents of this file are subject to the terms of the
8.\" Common Development and Distribution License (the "License").
9.\" You may not use this file except in compliance with the License.
10.\"
11.\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
12.\" or http://www.opensolaris.org/os/licensing.
13.\" See the License for the specific language governing permissions
14.\" and limitations under the License.
15.\"
16.\" When distributing Covered Code, include this CDDL HEADER in each
17.\" file and include the License file at usr/src/OPENSOLARIS.LICENSE.
18.\" If applicable, add the following below this CDDL HEADER, with the
19.\" fields enclosed by brackets "[]" replaced with your own identifying
20.\" information: Portions Copyright [yyyy] [name of copyright owner]
21.\"
22.Dd January 25, 2022
23.Dt MEMORY 3C
24.Os
25.Sh NAME
26.Nm memory ,
27.Nm memccpy ,
28.Nm memchr ,
29.Nm memcmp ,
30.Nm memcpy ,
31.Nm memmem ,
32.Nm memmove ,
33.Nm memrchr ,
34.Nm memset
35.Nd memory operations
36.Sh SYNOPSIS
37.In string.h
38.Ft "void *"
39.Fo memccpy
40.Fa "void *restrict s1"
41.Fa "const void *restrict s2"
42.Fa "int c"
43.Fa "size_t n"
44.Fc
45.Ft "void *"
46.Fo memchr
47.Fa "void *s"
48.Fa "int c"
49.Fa "size_t n"
50.Fc
51.Ft int
52.Fo memcmp
53.Fa "const void *s1"
54.Fa "const void *s2"
55.Fa "size_t n"
56.Fc
57.Ft "void *"
58.Fo memcpy
59.Fa "void *restrict s1"
60.Fa "const void *restrict s2"
61.Fa "size_t n"
62.Fc
63.Ft "void *"
64.Fo memmem
65.Fa "const void *l"
66.Fa "size_t l_len"
67.Fa "const void *s"
68.Fa "size_t s_len"
69.Fc
70.Ft "void *"
71.Fo memmove
72.Fa "void *s1"
73.Fa "const void *s2"
74.Fa "size_t n"
75.Fc
76.Ft "void *"
77.Fo memrchr
78.Fa "void *s"
79.Fa "int c"
80.Fa "size_t n"
81.Fc
82.Ft "void *"
83.Fo memset
84.Fa "void *s"
85.Fa "int c"
86.Fa "size_t n"
87.Fc
88.Sh DESCRIPTION
89These functions operate as efficiently as possible on memory areas (arrays of
90bytes bounded by a count, not terminated by a null character).
91They do not check for the overflow of any receiving memory area.
92.Pp
93The
94.Fn memccpy
95function copies bytes from memory area
96.Fa s2
97into
98.Fa s1 ,
99stopping after the first occurrence of
100.Fa c
101.Po
102converted to an
103.Vt unsigned char
104.Pc
105has been copied, or after
106.Fa n
107bytes have been copied, whichever comes first.
108It returns a pointer to the byte after the copy of
109.Fa c
110in
111.Fa s1 ,
112or a
113.Dv NULL
114pointer if
115.Fa c
116was not found in the first
117.Fa n
118bytes of
119.Fa s2 .
120.Pp
121The
122.Fn memchr
123function returns a pointer to the first occurrence of
124.Fa c
125.Po
126converted to an
127.Vt unsigned char
128.Pc
129in the first
130.Fa n
131bytes
132.Po
133each interpreted as an
134.Vt unsigned char
135.Pc
136of memory area
137.Fa s ,
138or a
139.Dv NULL
140pointer if
141.Fa c
142does not occur.
143.Pp
144The
145.Fn memrchr
146function behaves similarly to the
147.Fn memchr
148function, except that the memory area is searched in reverse from the
149last byte.
150.Pp
151The
152.Fn memcmp
153function compares its arguments, looking at the first
154.Fa n
155bytes
156.Po
157each interpreted as an
158.Vt unsigned char
159.Pc ,
160and returns an integer less than, equal to, or greater than 0, according as
161.Fa s1
162is less than, equal to, or greater than
163.Fa s2
164when taken to be unsigned characters.
165.Pp
166The
167.Fn memcpy
168function copies
169.Fa n
170bytes from memory area
171.Fa s2
172to
173.Fa s1
174It returns
175.Fa s1 .
176If copying takes place between objects that overlap, the behavior is undefined.
177In such cases, use
178.Fn memmove
179instead.
180.Pp
181The
182.Fn memmem
183function searches for the
184.Fa s_len
185long byte pattern
186.Fa s
187in the memory region starting at
188.Fa l
189for
190.Fa l_len
191bytes.
192If a match is found, a pointer to the starting location in
193.Fa l
194is returned.
195If no match is found,
196.Fa l_len
197is zero,
198.Fa s_len
199is zero, or
200.Fa l_len
201is less than
202.Fa s_len
203then a
204.Dv NULL
205pointer is return.
206.Pp
207The
208.Fn memmove
209function copies
210.Fa n
211bytes from memory area
212.Fa s2
213to memory area
214.Fa s1 .
215Copying between objects that overlap will take place correctly.
216It returns
217.Fa s1 .
218.Pp
219The
220.Fn memset
221function sets the first
222.Fa n
223bytes in memory area
224.Fa s
225to the value of
226.Fa c
227.Po
228converted to an
229.Vt unsigned char
230.Pc .
231It returns
232.Fa s .
233.Sh USAGE
234Using
235.Fn memcpy
236might be faster than using
237.Fn memmove
238if the
239application knows that the objects being copied do not overlap.
240.Sh INTERFACE STABILITY
241.Sy Committed
242.Sh MT-LEVEL
243.Sy MT-Safe
244.Sh SEE ALSO
245.Xr string 3C ,
246.Xr attributes 7 ,
247.Xr standards 7
248.Sh NOTES
249Overlap between objects being copied can arise even when their
250.Pq virtual
251address ranges appear to be disjoint; for example, as a result of
252memory-mapping overlapping portions of the same underlying file, or of
253attaching the same shared memory segment more than once.
254