Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
further little clarifications in mmalloc comments
[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(xbt_mheap_t mdp, size_t alignment, size_t size)
13 {
14   void *result;
15   unsigned long int adj;
16   struct alignlist *l;
17
18   if ((result = mmalloc(mdp, size + alignment - 1)) != NULL) {
19     adj = RESIDUAL(result, alignment);
20     if (adj != 0) {
21       for (l = mdp->aligned_blocks; l != NULL; l = l->next) {
22         if (l->aligned == NULL) {
23           /* This slot is free.  Use it.  */
24           break;
25         }
26       }
27       if (l == NULL) { /* No empty entry. Create & link a new one */
28         l = (struct alignlist *) mmalloc(mdp, sizeof(struct alignlist));
29         if (l == NULL) {  /* malloc error */
30           mfree(mdp, result);
31           return (NULL);
32         }
33         l->next = mdp->aligned_blocks;
34         mdp->aligned_blocks = l;
35       }
36       l->exact = result;
37       result = l->aligned = (char *) result + alignment - adj;
38     }
39   }
40   return (result);
41 }
42
43 /* Cache the pagesize for the current host machine.  Note that if the host
44    does not readily provide a getpagesize() function, we need to emulate it
45    elsewhere, not clutter up this file with lots of kluges to try to figure
46    it out. */
47 static size_t cache_pagesize;
48
49 void *mvalloc(xbt_mheap_t mdp, size_t size)
50 {
51   if (cache_pagesize == 0)
52     cache_pagesize = getpagesize();
53
54   return mmemalign(mdp, cache_pagesize, size);
55 }
56