Sync usage with man page.
[netbsd-mini2440.git] / gnu / lib / libmalloc / memalign.c
blobf5ad17cb23a2dc0465651978f90ea73f33db3d06
1 /* Copyright (C) 1991, 1992 Free Software Foundation, Inc.
3 This library is free software; you can redistribute it and/or
4 modify it under the terms of the GNU Library General Public License as
5 published by the Free Software Foundation; either version 2 of the
6 License, or (at your option) any later version.
8 This library is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 Library General Public License for more details.
13 You should have received a copy of the GNU Library General Public
14 License along with this library; see the file COPYING.LIB. If
15 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
16 Cambridge, MA 02139, USA. */
18 #ifndef _MALLOC_INTERNAL
19 #define _MALLOC_INTERNAL
20 #include <malloc.h>
21 #endif
23 __ptr_t
24 memalign (alignment, size)
25 size_t alignment;
26 size_t size;
28 __ptr_t result;
29 unsigned long int adj;
31 size = ((size + alignment - 1) / alignment) * alignment;
33 result = malloc (size);
34 if (result == NULL)
35 return NULL;
36 adj = (unsigned long int) ((unsigned long int) ((char *) result -
37 (char *) NULL)) % alignment;
38 if (adj != 0)
40 struct alignlist *l;
41 for (l = _aligned_blocks; l != NULL; l = l->next)
42 if (l->aligned == NULL)
43 /* This slot is free. Use it. */
44 break;
45 if (l == NULL)
47 l = (struct alignlist *) malloc (sizeof (struct alignlist));
48 if (l == NULL)
50 free (result);
51 return NULL;
54 l->exact = result;
55 result = l->aligned = (char *) result + alignment - adj;
56 l->next = _aligned_blocks;
57 _aligned_blocks = l;
60 return result;