Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
do not trim in split_quoted, that's expensive, and the caller can do it if his input...
[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 *mmemalign(void *md, size_t alignment, size_t size)
13 {
14   void *result;
15   unsigned long int adj;
16   struct alignlist *l;
17   struct mdesc *mdp;
18
19   if ((result = mmalloc(md, size + alignment - 1)) != NULL) {
20     adj = RESIDUAL(result, alignment);
21     if (adj != 0) {
22       mdp = MD_TO_MDP(md);
23       for (l = mdp->aligned_blocks; l != NULL; l = l->next) {
24         if (l->aligned == NULL) {
25           /* This slot is free.  Use it.  */
26           break;
27         }
28       }
29       if (l == NULL) {
30         l = (struct alignlist *) mmalloc(md, sizeof(struct alignlist));
31         if (l == NULL) {
32           mfree(md, result);
33           return (NULL);
34         }
35         l->next = mdp->aligned_blocks;
36         mdp->aligned_blocks = l;
37       }
38       l->exact = result;
39       result = l->aligned = (char *) result + alignment - adj;
40     }
41   }
42   return (result);
43 }