xmalloc.c revision 1.12 1 1.12 christos /* $NetBSD: xmalloc.c,v 1.12 2013/01/24 17:57:29 christos Exp $ */
2 1.1 cgd
3 1.1 cgd /*
4 1.1 cgd * Copyright 1996 John D. Polstra.
5 1.1 cgd * Copyright 1996 Matt Thomas <matt (at) 3am-software.com>
6 1.1 cgd * All rights reserved.
7 1.1 cgd *
8 1.1 cgd * Redistribution and use in source and binary forms, with or without
9 1.1 cgd * modification, are permitted provided that the following conditions
10 1.1 cgd * are met:
11 1.1 cgd * 1. Redistributions of source code must retain the above copyright
12 1.1 cgd * notice, this list of conditions and the following disclaimer.
13 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 cgd * notice, this list of conditions and the following disclaimer in the
15 1.1 cgd * documentation and/or other materials provided with the distribution.
16 1.1 cgd * 3. All advertising materials mentioning features or use of this software
17 1.1 cgd * must display the following acknowledgement:
18 1.1 cgd * This product includes software developed by John Polstra.
19 1.1 cgd * 4. The name of the author may not be used to endorse or promote products
20 1.1 cgd * derived from this software without specific prior written permission.
21 1.1 cgd *
22 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 1.1 cgd * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 1.1 cgd * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 1.1 cgd * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 1.1 cgd * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 1.1 cgd * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 1.1 cgd * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 1.1 cgd * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 1.1 cgd * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 1.1 cgd * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 1.1 cgd */
33 1.1 cgd
34 1.7 ad /*
35 1.7 ad * Copyright (c) 1983 Regents of the University of California.
36 1.7 ad * All rights reserved.
37 1.7 ad *
38 1.7 ad * Redistribution and use in source and binary forms, with or without
39 1.7 ad * modification, are permitted provided that the following conditions
40 1.7 ad * are met:
41 1.7 ad * 1. Redistributions of source code must retain the above copyright
42 1.7 ad * notice, this list of conditions and the following disclaimer.
43 1.7 ad * 2. Redistributions in binary form must reproduce the above copyright
44 1.7 ad * notice, this list of conditions and the following disclaimer in the
45 1.7 ad * documentation and/or other materials provided with the distribution.
46 1.7 ad * 3. Neither the name of the University nor the names of its contributors
47 1.7 ad * may be used to endorse or promote products derived from this software
48 1.7 ad * without specific prior written permission.
49 1.7 ad *
50 1.7 ad * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51 1.7 ad * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 1.7 ad * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53 1.7 ad * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54 1.7 ad * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 1.7 ad * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 1.7 ad * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 1.7 ad * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 1.7 ad * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59 1.7 ad * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 1.7 ad * SUCH DAMAGE.
61 1.7 ad */
62 1.7 ad
63 1.7 ad #if defined(LIBC_SCCS) && !defined(lint)
64 1.7 ad /*static char *sccsid = "from: @(#)malloc.c 5.11 (Berkeley) 2/23/91";*/
65 1.7 ad #endif /* LIBC_SCCS and not lint */
66 1.7 ad
67 1.7 ad /*
68 1.7 ad * malloc.c (Caltech) 2/21/82
69 1.7 ad * Chris Kingsley, kingsley@cit-20.
70 1.7 ad *
71 1.7 ad * This is a very fast storage allocator. It allocates blocks of a small
72 1.7 ad * number of different sizes, and keeps free lists of each size. Blocks that
73 1.7 ad * don't exactly fit are passed up to the next larger size. In this
74 1.7 ad * implementation, the available sizes are 2^n-4 (or 2^n-10) bytes long.
75 1.7 ad * This is designed for use in a virtual memory environment.
76 1.7 ad */
77 1.7 ad
78 1.2 christos #include <sys/cdefs.h>
79 1.5 skrll #ifndef lint
80 1.12 christos __RCSID("$NetBSD: xmalloc.c,v 1.12 2013/01/24 17:57:29 christos Exp $");
81 1.5 skrll #endif /* not lint */
82 1.5 skrll
83 1.1 cgd #include <stdlib.h>
84 1.1 cgd #include <string.h>
85 1.7 ad #include <unistd.h>
86 1.7 ad #include <errno.h>
87 1.7 ad
88 1.7 ad #include <sys/types.h>
89 1.7 ad #include <sys/param.h>
90 1.7 ad #include <sys/mman.h>
91 1.7 ad #include <sys/stat.h>
92 1.7 ad
93 1.7 ad #include "rtld.h"
94 1.7 ad
95 1.7 ad /*
96 1.7 ad * Pre-allocate mmap'ed pages
97 1.7 ad */
98 1.7 ad #define NPOOLPAGES (32*1024/pagesz)
99 1.9 christos static char *pagepool_start, *pagepool_end;
100 1.7 ad static int morepages(int);
101 1.9 christos #define PAGEPOOL_SIZE (size_t)(pagepool_end - pagepool_start)
102 1.7 ad
103 1.7 ad /*
104 1.7 ad * The overhead on a block is at least 4 bytes. When free, this space
105 1.7 ad * contains a pointer to the next free block, and the bottom two bits must
106 1.7 ad * be zero. When in use, the first byte is set to MAGIC, and the second
107 1.7 ad * byte is the size index. The remaining bytes are for alignment.
108 1.7 ad * If range checking is enabled then a second word holds the size of the
109 1.7 ad * requested block, less 1, rounded up to a multiple of sizeof(RMAGIC).
110 1.7 ad * The order of elements is critical: ov_magic must overlay the low order
111 1.7 ad * bits of ov_next, and ov_magic can not be a valid ov_next bit pattern.
112 1.7 ad */
113 1.7 ad union overhead {
114 1.7 ad union overhead *ov_next; /* when free */
115 1.7 ad struct {
116 1.7 ad u_char ovu_magic; /* magic number */
117 1.7 ad u_char ovu_index; /* bucket # */
118 1.7 ad #ifdef RCHECK
119 1.7 ad u_short ovu_rmagic; /* range magic number */
120 1.7 ad u_int ovu_size; /* actual block size */
121 1.7 ad #endif
122 1.7 ad } ovu;
123 1.7 ad #define ov_magic ovu.ovu_magic
124 1.7 ad #define ov_index ovu.ovu_index
125 1.7 ad #define ov_rmagic ovu.ovu_rmagic
126 1.7 ad #define ov_size ovu.ovu_size
127 1.7 ad };
128 1.7 ad
129 1.9 christos static void morecore(size_t);
130 1.7 ad static void *imalloc(size_t);
131 1.7 ad
132 1.7 ad #define MAGIC 0xef /* magic # on accounting info */
133 1.7 ad #define RMAGIC 0x5555 /* magic # on range info */
134 1.7 ad
135 1.7 ad #ifdef RCHECK
136 1.7 ad #define RSLOP (sizeof (u_short))
137 1.7 ad #else
138 1.7 ad #define RSLOP 0
139 1.7 ad #endif
140 1.7 ad
141 1.7 ad /*
142 1.7 ad * nextf[i] is the pointer to the next free block of size 2^(i+3). The
143 1.7 ad * smallest allocatable block is 8 bytes. The overhead information
144 1.7 ad * precedes the data area returned to the user.
145 1.7 ad */
146 1.7 ad #define NBUCKETS 30
147 1.7 ad static union overhead *nextf[NBUCKETS];
148 1.7 ad
149 1.9 christos static size_t pagesz; /* page size */
150 1.9 christos static size_t pagebucket; /* page size bucket */
151 1.11 christos static size_t pageshift; /* page size shift */
152 1.7 ad
153 1.7 ad #ifdef MSTATS
154 1.7 ad /*
155 1.7 ad * nmalloc[i] is the difference between the number of mallocs and frees
156 1.7 ad * for a given block size.
157 1.7 ad */
158 1.7 ad static u_int nmalloc[NBUCKETS];
159 1.7 ad #endif
160 1.7 ad
161 1.7 ad #if defined(MALLOC_DEBUG) || defined(RCHECK)
162 1.7 ad #define ASSERT(p) if (!(p)) botch("p")
163 1.7 ad static void
164 1.10 joerg botch(const char *s)
165 1.7 ad {
166 1.7 ad xwarnx("\r\nassertion botched: %s\r\n", s);
167 1.7 ad abort();
168 1.7 ad }
169 1.7 ad #else
170 1.7 ad #define ASSERT(p)
171 1.7 ad #endif
172 1.7 ad
173 1.7 ad #define TRACE() xprintf("TRACE %s:%d\n", __FILE__, __LINE__)
174 1.7 ad
175 1.7 ad static void *
176 1.7 ad imalloc(size_t nbytes)
177 1.7 ad {
178 1.9 christos union overhead *op;
179 1.9 christos size_t bucket;
180 1.9 christos size_t n, m;
181 1.9 christos unsigned amt;
182 1.7 ad
183 1.7 ad /*
184 1.7 ad * First time malloc is called, setup page size and
185 1.7 ad * align break pointer so all data will be page aligned.
186 1.7 ad */
187 1.7 ad if (pagesz == 0) {
188 1.7 ad pagesz = n = _rtld_pagesz;
189 1.7 ad if (morepages(NPOOLPAGES) == 0)
190 1.7 ad return NULL;
191 1.7 ad op = (union overhead *)(pagepool_start);
192 1.9 christos m = sizeof (*op) - (((char *)op - (char *)NULL) & (n - 1));
193 1.9 christos if (n < m)
194 1.9 christos n += pagesz - m;
195 1.9 christos else
196 1.9 christos n -= m;
197 1.7 ad if (n) {
198 1.7 ad pagepool_start += n;
199 1.7 ad }
200 1.7 ad bucket = 0;
201 1.7 ad amt = sizeof(union overhead);
202 1.7 ad while (pagesz > amt) {
203 1.7 ad amt <<= 1;
204 1.7 ad bucket++;
205 1.7 ad }
206 1.7 ad pagebucket = bucket;
207 1.11 christos pageshift = ffs(pagesz) - 1;
208 1.7 ad }
209 1.7 ad /*
210 1.7 ad * Convert amount of memory requested into closest block size
211 1.7 ad * stored in hash buckets which satisfies request.
212 1.7 ad * Account for space used per block for accounting.
213 1.7 ad */
214 1.7 ad if (nbytes <= (n = pagesz - sizeof (*op) - RSLOP)) {
215 1.7 ad if (sizeof(union overhead) & (sizeof(union overhead) - 1)) {
216 1.7 ad amt = sizeof(union overhead) * 2;
217 1.7 ad bucket = 1;
218 1.7 ad } else {
219 1.7 ad amt = sizeof(union overhead); /* size of first bucket */
220 1.7 ad bucket = 0;
221 1.7 ad }
222 1.7 ad n = -(sizeof (*op) + RSLOP);
223 1.7 ad } else {
224 1.7 ad amt = pagesz;
225 1.7 ad bucket = pagebucket;
226 1.7 ad }
227 1.7 ad while (nbytes > amt + n) {
228 1.7 ad amt <<= 1;
229 1.7 ad if (amt == 0)
230 1.7 ad return (NULL);
231 1.7 ad bucket++;
232 1.7 ad }
233 1.7 ad /*
234 1.7 ad * If nothing in hash bucket right now,
235 1.7 ad * request more memory from the system.
236 1.7 ad */
237 1.7 ad if ((op = nextf[bucket]) == NULL) {
238 1.7 ad morecore(bucket);
239 1.7 ad if ((op = nextf[bucket]) == NULL)
240 1.7 ad return (NULL);
241 1.7 ad }
242 1.7 ad /* remove from linked list */
243 1.7 ad nextf[bucket] = op->ov_next;
244 1.7 ad op->ov_magic = MAGIC;
245 1.7 ad op->ov_index = bucket;
246 1.7 ad #ifdef MSTATS
247 1.7 ad nmalloc[bucket]++;
248 1.7 ad #endif
249 1.7 ad #ifdef RCHECK
250 1.7 ad /*
251 1.7 ad * Record allocated size of block and
252 1.7 ad * bound space with magic numbers.
253 1.7 ad */
254 1.7 ad op->ov_size = (nbytes + RSLOP - 1) & ~(RSLOP - 1);
255 1.7 ad op->ov_rmagic = RMAGIC;
256 1.7 ad *(u_short *)((caddr_t)(op + 1) + op->ov_size) = RMAGIC;
257 1.7 ad #endif
258 1.7 ad return ((char *)(op + 1));
259 1.7 ad }
260 1.7 ad
261 1.7 ad /*
262 1.7 ad * Allocate more memory to the indicated bucket.
263 1.7 ad */
264 1.7 ad static void
265 1.9 christos morecore(size_t bucket)
266 1.7 ad {
267 1.9 christos union overhead *op;
268 1.9 christos size_t sz; /* size of desired block */
269 1.9 christos size_t amt; /* amount to allocate */
270 1.9 christos size_t nblks; /* how many blocks we get */
271 1.7 ad
272 1.7 ad /*
273 1.7 ad * sbrk_size <= 0 only for big, FLUFFY, requests (about
274 1.7 ad * 2^30 bytes on a VAX, I think) or for a negative arg.
275 1.7 ad */
276 1.7 ad sz = 1 << (bucket + 3);
277 1.7 ad #ifdef MALLOC_DEBUG
278 1.7 ad ASSERT(sz > 0);
279 1.7 ad #endif
280 1.7 ad if (sz < pagesz) {
281 1.7 ad amt = pagesz;
282 1.11 christos nblks = amt >> (bucket + 3);
283 1.7 ad } else {
284 1.7 ad amt = sz + pagesz;
285 1.7 ad nblks = 1;
286 1.7 ad }
287 1.9 christos if (amt > PAGEPOOL_SIZE)
288 1.11 christos if (morepages((amt >> pageshift) + NPOOLPAGES) == 0)
289 1.7 ad return;
290 1.7 ad op = (union overhead *)pagepool_start;
291 1.7 ad pagepool_start += amt;
292 1.7 ad
293 1.7 ad /*
294 1.7 ad * Add new memory allocated to that on
295 1.7 ad * free list for this hash bucket.
296 1.7 ad */
297 1.7 ad nextf[bucket] = op;
298 1.7 ad while (--nblks > 0) {
299 1.7 ad op->ov_next = (union overhead *)((caddr_t)op + sz);
300 1.7 ad op = (union overhead *)((caddr_t)op + sz);
301 1.7 ad }
302 1.7 ad }
303 1.7 ad
304 1.7 ad void
305 1.10 joerg xfree(void *cp)
306 1.7 ad {
307 1.9 christos int size;
308 1.9 christos union overhead *op;
309 1.7 ad
310 1.7 ad if (cp == NULL)
311 1.7 ad return;
312 1.7 ad op = (union overhead *)((caddr_t)cp - sizeof (union overhead));
313 1.7 ad #ifdef MALLOC_DEBUG
314 1.7 ad ASSERT(op->ov_magic == MAGIC); /* make sure it was in use */
315 1.7 ad #else
316 1.7 ad if (op->ov_magic != MAGIC)
317 1.7 ad return; /* sanity */
318 1.7 ad #endif
319 1.7 ad #ifdef RCHECK
320 1.7 ad ASSERT(op->ov_rmagic == RMAGIC);
321 1.7 ad ASSERT(*(u_short *)((caddr_t)(op + 1) + op->ov_size) == RMAGIC);
322 1.7 ad #endif
323 1.7 ad size = op->ov_index;
324 1.7 ad ASSERT(size < NBUCKETS);
325 1.7 ad op->ov_next = nextf[size]; /* also clobbers ov_magic */
326 1.7 ad nextf[size] = op;
327 1.7 ad #ifdef MSTATS
328 1.7 ad nmalloc[size]--;
329 1.7 ad #endif
330 1.7 ad }
331 1.7 ad
332 1.7 ad static void *
333 1.7 ad irealloc(void *cp, size_t nbytes)
334 1.7 ad {
335 1.9 christos size_t onb;
336 1.9 christos size_t i;
337 1.7 ad union overhead *op;
338 1.7 ad char *res;
339 1.7 ad
340 1.7 ad if (cp == NULL)
341 1.7 ad return (imalloc(nbytes));
342 1.7 ad op = (union overhead *)((caddr_t)cp - sizeof (union overhead));
343 1.7 ad if (op->ov_magic != MAGIC) {
344 1.7 ad static const char *err_str =
345 1.7 ad "memory corruption or double free in realloc\n";
346 1.8 ad extern char *__progname;
347 1.7 ad write(STDERR_FILENO, __progname, strlen(__progname));
348 1.7 ad write(STDERR_FILENO, err_str, strlen(err_str));
349 1.7 ad abort();
350 1.7 ad }
351 1.7 ad
352 1.7 ad i = op->ov_index;
353 1.7 ad onb = 1 << (i + 3);
354 1.7 ad if (onb < pagesz)
355 1.7 ad onb -= sizeof (*op) + RSLOP;
356 1.7 ad else
357 1.7 ad onb += pagesz - sizeof (*op) - RSLOP;
358 1.7 ad /* avoid the copy if same size block */
359 1.7 ad if (i) {
360 1.7 ad i = 1 << (i + 2);
361 1.7 ad if (i < pagesz)
362 1.7 ad i -= sizeof (*op) + RSLOP;
363 1.7 ad else
364 1.7 ad i += pagesz - sizeof (*op) - RSLOP;
365 1.7 ad }
366 1.7 ad if (nbytes <= onb && nbytes > i) {
367 1.7 ad #ifdef RCHECK
368 1.7 ad op->ov_size = (nbytes + RSLOP - 1) & ~(RSLOP - 1);
369 1.7 ad *(u_short *)((caddr_t)(op + 1) + op->ov_size) = RMAGIC;
370 1.7 ad #endif
371 1.7 ad return(cp);
372 1.12 christos }
373 1.7 ad if ((res = imalloc(nbytes)) == NULL)
374 1.7 ad return (NULL);
375 1.12 christos if (cp != res) { /* common optimization if "compacting" */
376 1.7 ad memcpy(res, cp, (nbytes < onb) ? nbytes : onb);
377 1.12 christos xfree(cp);
378 1.12 christos }
379 1.7 ad return (res);
380 1.7 ad }
381 1.7 ad
382 1.7 ad #ifdef MSTATS
383 1.7 ad /*
384 1.7 ad * mstats - print out statistics about malloc
385 1.7 ad *
386 1.7 ad * Prints two lines of numbers, one showing the length of the free list
387 1.7 ad * for each size category, the second showing the number of mallocs -
388 1.7 ad * frees for each size category.
389 1.7 ad */
390 1.10 joerg void
391 1.7 ad mstats(char *s)
392 1.7 ad {
393 1.9 christos int i, j;
394 1.9 christos union overhead *p;
395 1.7 ad int totfree = 0,
396 1.7 ad totused = 0;
397 1.7 ad
398 1.7 ad xprintf("Memory allocation statistics %s\nfree:\t", s);
399 1.7 ad for (i = 0; i < NBUCKETS; i++) {
400 1.7 ad for (j = 0, p = nextf[i]; p; p = p->ov_next, j++)
401 1.7 ad ;
402 1.7 ad xprintf(" %d", j);
403 1.7 ad totfree += j * (1 << (i + 3));
404 1.7 ad }
405 1.7 ad xprintf("\nused:\t");
406 1.7 ad for (i = 0; i < NBUCKETS; i++) {
407 1.7 ad xprintf(" %d", nmalloc[i]);
408 1.7 ad totused += nmalloc[i] * (1 << (i + 3));
409 1.7 ad }
410 1.7 ad xprintf("\n\tTotal in use: %d, total free: %d\n",
411 1.7 ad totused, totfree);
412 1.7 ad }
413 1.7 ad #endif
414 1.7 ad
415 1.7 ad
416 1.7 ad static int
417 1.7 ad morepages(int n)
418 1.7 ad {
419 1.7 ad int fd = -1;
420 1.7 ad int offset;
421 1.7 ad
422 1.7 ad #ifdef NEED_DEV_ZERO
423 1.7 ad fd = open("/dev/zero", O_RDWR, 0);
424 1.7 ad if (fd == -1)
425 1.7 ad xerr(1, "/dev/zero");
426 1.7 ad #endif
427 1.7 ad
428 1.9 christos if (PAGEPOOL_SIZE > pagesz) {
429 1.7 ad caddr_t addr = (caddr_t)
430 1.7 ad (((long)pagepool_start + pagesz - 1) & ~(pagesz - 1));
431 1.7 ad if (munmap(addr, pagepool_end - addr) != 0)
432 1.7 ad xwarn("morepages: munmap %p", addr);
433 1.7 ad }
434 1.7 ad
435 1.7 ad offset = (long)pagepool_start - ((long)pagepool_start & ~(pagesz - 1));
436 1.7 ad
437 1.7 ad if ((pagepool_start = mmap(0, n * pagesz,
438 1.7 ad PROT_READ|PROT_WRITE,
439 1.7 ad MAP_ANON|MAP_PRIVATE, fd, 0)) == (caddr_t)-1) {
440 1.7 ad xprintf("Cannot map anonymous memory");
441 1.7 ad return 0;
442 1.7 ad }
443 1.7 ad pagepool_end = pagepool_start + n * pagesz;
444 1.7 ad pagepool_start += offset;
445 1.7 ad
446 1.7 ad #ifdef NEED_DEV_ZERO
447 1.7 ad close(fd);
448 1.7 ad #endif
449 1.7 ad return n;
450 1.7 ad }
451 1.1 cgd
452 1.1 cgd void *
453 1.4 skrll xcalloc(size_t size)
454 1.1 cgd {
455 1.3 simonb
456 1.2 christos return memset(xmalloc(size), 0, size);
457 1.1 cgd }
458 1.1 cgd
459 1.1 cgd void *
460 1.4 skrll xmalloc(size_t size)
461 1.1 cgd {
462 1.7 ad void *p = imalloc(size);
463 1.3 simonb
464 1.2 christos if (p == NULL)
465 1.2 christos xerr(1, "%s", xstrerror(errno));
466 1.2 christos return p;
467 1.1 cgd }
468 1.1 cgd
469 1.6 christos void *
470 1.6 christos xrealloc(void *p, size_t size)
471 1.6 christos {
472 1.7 ad p = irealloc(p, size);
473 1.6 christos
474 1.6 christos if (p == NULL)
475 1.6 christos xerr(1, "%s", xstrerror(errno));
476 1.6 christos return p;
477 1.6 christos }
478 1.6 christos
479 1.7 ad char *
480 1.7 ad xstrdup(const char *str)
481 1.1 cgd {
482 1.7 ad size_t len;
483 1.7 ad char *copy;
484 1.3 simonb
485 1.7 ad len = strlen(str) + 1;
486 1.7 ad copy = xmalloc(len);
487 1.7 ad memcpy(copy, str, len);
488 1.7 ad return (copy);
489 1.1 cgd }
490