Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Unify the copyright headers of mmalloc with the rest of the library
[simgrid.git] / src / xbt / mmalloc / mmemalign.c
1 /* Copyright (C) 1991, 1992 Free Software Foundation, Inc.
2    This file was then part of the GNU C Library. */
3
4 /* Copyright (c) 2010. The SimGrid Team.
5  * All rights reserved.                                                     */
6
7 /* This program is free software; you can redistribute it and/or modify it
8  * under the terms of the license (GNU LGPL) which comes with this package. */
9
10 #include "mmprivate.h"
11
12 void *
13 mmemalign (void *md, size_t alignment, size_t size)
14 {
15   void* result;
16   unsigned long int adj;
17   struct alignlist *l;
18   struct mdesc *mdp;
19
20   if ((result = mmalloc (md, size + alignment - 1)) != NULL)
21     {
22       adj = RESIDUAL (result, alignment);
23       if (adj != 0)
24         {
25           mdp = MD_TO_MDP (md);
26           for (l = mdp -> aligned_blocks; l != NULL; l = l -> next)
27             {
28               if (l -> aligned == NULL)
29                 {
30                   /* This slot is free.  Use it.  */
31                   break;
32                 }
33             }
34           if (l == NULL)
35             {
36               l = (struct alignlist *) mmalloc (md, sizeof (struct alignlist));
37               if (l == NULL)
38                 {
39                   mfree (md, result);
40                   return (NULL);
41                 }
42               l -> next = mdp -> aligned_blocks;
43               mdp -> aligned_blocks = l;
44             }
45           l -> exact = result;
46           result = l -> aligned = (char*) result + alignment - adj;
47         }
48     }
49   return (result);
50 }